-
Notifications
You must be signed in to change notification settings - Fork 144
Implementation of InstanceIdClient to allow subscribe/unsubscribe to topic #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
497f8c4
Compiles, ready for test
949a109
Before removal
bcdca8e
Integration tests working as expected
b668b38
Fixing some renames
b00db83
Improve docs, add missing dispose
4675f21
PR comment resolutions
a62c04f
Improve integation tests
186b96d
changes part 1
c69b430
Refactored and tests pass
b22734a
HTTP status code tests
1ccd27a
Fix broken test
3576351
Testing and refactoring
0724927
Test fixes
ea9e351
PR requetc changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
FirebaseAdmin/FirebaseAdmin.Tests/Messaging/InstanceIdClientTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using FirebaseAdmin.Messaging; | ||
using Google.Apis.Auth.OAuth2; | ||
using Google.Apis.Http; | ||
using Xunit; | ||
|
||
namespace FirebaseAdmin.Tests.Messaging | ||
{ | ||
public class InstanceIdClientTest | ||
{ | ||
private static readonly GoogleCredential MockCredential = | ||
GoogleCredential.FromAccessToken("test-token"); | ||
|
||
[Fact] | ||
public void NoCredential() | ||
{ | ||
var clientFactory = new HttpClientFactory(); | ||
Assert.Throws<ArgumentNullException>( | ||
() => new InstanceIdClient(clientFactory, null)); | ||
} | ||
|
||
[Fact] | ||
public void NoClientFactory() | ||
{ | ||
var clientFactory = new HttpClientFactory(); | ||
Assert.Throws<ArgumentNullException>( | ||
() => new InstanceIdClient(null, MockCredential)); | ||
} | ||
|
||
[Fact] | ||
public async Task SubscribeToTopicAsync() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
Response = @"{""results"":[{}]}", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var result = await client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" }); | ||
|
||
Assert.Equal(1, result.SuccessCount); | ||
} | ||
|
||
[Fact] | ||
public async Task UnsubscribeFromTopicAsync() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
Response = @"{""results"":[{}]}", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var result = await client.UnsubscribeFromTopicAsync("test-topic", new List<string> { "abc123" }); | ||
|
||
Assert.Equal(1, result.SuccessCount); | ||
} | ||
|
||
[Fact] | ||
public async Task BadRequest() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
StatusCode = HttpStatusCode.BadRequest, | ||
Response = "BadRequest", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( | ||
() => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); | ||
|
||
Assert.Equal(ErrorCode.InvalidArgument, exception.ErrorCode); | ||
Assert.Equal("Unexpected HTTP response with status: 400 (BadRequest)\nBadRequest", exception.Message); | ||
Assert.Null(exception.MessagingErrorCode); | ||
Assert.NotNull(exception.HttpResponse); | ||
Assert.Null(exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public async Task Unauthorized() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
StatusCode = HttpStatusCode.Unauthorized, | ||
Response = "Unauthorized", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( | ||
() => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); | ||
Leo-Mepham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Assert.Equal(ErrorCode.Unauthenticated, exception.ErrorCode); | ||
Assert.Equal("Unexpected HTTP response with status: 401 (Unauthorized)\nUnauthorized", exception.Message); | ||
Assert.Null(exception.MessagingErrorCode); | ||
Assert.NotNull(exception.HttpResponse); | ||
Assert.Null(exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public async Task Forbidden() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
StatusCode = HttpStatusCode.Forbidden, | ||
Response = "Forbidden", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( | ||
() => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); | ||
|
||
Assert.Equal(ErrorCode.PermissionDenied, exception.ErrorCode); | ||
Assert.Equal("Unexpected HTTP response with status: 403 (Forbidden)\nForbidden", exception.Message); | ||
Assert.Null(exception.MessagingErrorCode); | ||
Assert.NotNull(exception.HttpResponse); | ||
Assert.Null(exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public async Task NotFound() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
StatusCode = HttpStatusCode.NotFound, | ||
Response = "NotFound", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( | ||
() => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); | ||
|
||
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode); | ||
Assert.Equal("Unexpected HTTP response with status: 404 (NotFound)\nNotFound", exception.Message); | ||
Assert.Null(exception.MessagingErrorCode); | ||
Assert.NotNull(exception.HttpResponse); | ||
Assert.Null(exception.InnerException); | ||
} | ||
|
||
[Fact] | ||
public async Task ServiceUnavailable() | ||
{ | ||
var handler = new MockMessageHandler() | ||
{ | ||
StatusCode = HttpStatusCode.ServiceUnavailable, | ||
Response = "ServiceUnavailable", | ||
}; | ||
var factory = new MockHttpClientFactory(handler); | ||
|
||
var client = new InstanceIdClient(factory, MockCredential); | ||
|
||
var exception = await Assert.ThrowsAsync<FirebaseMessagingException>( | ||
() => client.SubscribeToTopicAsync("test-topic", new List<string> { "abc123" })); | ||
|
||
Assert.Equal(ErrorCode.Unavailable, exception.ErrorCode); | ||
Assert.Equal("Unexpected HTTP response with status: 503 (ServiceUnavailable)\nServiceUnavailable", exception.Message); | ||
Assert.Null(exception.MessagingErrorCode); | ||
Assert.NotNull(exception.HttpResponse); | ||
Assert.Null(exception.InnerException); | ||
} | ||
} | ||
Leo-Mepham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
83 changes: 83 additions & 0 deletions
83
FirebaseAdmin/FirebaseAdmin.Tests/Messaging/TopicManagementResponseTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using FirebaseAdmin.Messaging; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
|
||
namespace FirebaseAdmin.Tests.Messaging | ||
{ | ||
public class TopicManagementResponseTest | ||
{ | ||
[Fact] | ||
public void SuccessfulReponse() | ||
{ | ||
var json = @"{""results"": [{}, {}]}"; | ||
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json); | ||
var response = new TopicManagementResponse(instanceIdServiceResponse); | ||
|
||
Assert.Equal(0, response.FailureCount); | ||
Leo-Mepham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.Equal(2, response.SuccessCount); | ||
} | ||
|
||
[Fact] | ||
public void UnsuccessfulResponse() | ||
{ | ||
var json = @"{""results"": [{}, {""error"":""NOT_FOUND""}]}"; | ||
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json); | ||
var response = new TopicManagementResponse(instanceIdServiceResponse); | ||
|
||
Assert.Equal(1, response.FailureCount); | ||
Assert.Equal(1, response.SuccessCount); | ||
Assert.NotEmpty(response.Errors); | ||
Leo-Mepham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.Equal("registration-token-not-registered", response.Errors[0].Reason); | ||
Assert.Equal(1, response.Errors[0].Index); | ||
} | ||
|
||
[Fact] | ||
public void NullResponse() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
new TopicManagementResponse(null); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void EmptyResponse() | ||
{ | ||
Assert.Throws<ArgumentNullException>(() => | ||
{ | ||
var instanceIdServiceResponse = new InstanceIdServiceResponse(); | ||
Leo-Mepham marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new TopicManagementResponse(instanceIdServiceResponse); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void UnregisteredToken() | ||
{ | ||
var json = @"{""results"": [{}, {""error"":""NOT_FOUND""}]}"; | ||
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json); | ||
var response = new TopicManagementResponse(instanceIdServiceResponse); | ||
|
||
Assert.Single(response.Errors); | ||
Assert.Equal("registration-token-not-registered", response.Errors[0].Reason); | ||
Assert.Equal(1, response.Errors[0].Index); | ||
} | ||
|
||
[Fact] | ||
public void CountsSuccessAndErrors() | ||
{ | ||
var json = @"{""results"": [{""error"": ""NOT_FOUND""}, {}, {""error"": ""INVALID_ARGUMENT""}, {}, {}]}"; | ||
var instanceIdServiceResponse = JsonConvert.DeserializeObject<InstanceIdServiceResponse>(json); | ||
var response = new TopicManagementResponse(instanceIdServiceResponse); | ||
|
||
Assert.Equal(2, response.FailureCount); | ||
Assert.Equal(3, response.SuccessCount); | ||
Assert.Equal("registration-token-not-registered", response.Errors[0].Reason); | ||
Assert.NotEmpty(response.Errors); | ||
Assert.Equal(0, response.Errors[0].Index); | ||
Assert.Equal("invalid-argument", response.Errors[1].Reason); | ||
Assert.Equal(2, response.Errors[1].Index); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.