Skip to content

Commit 41fef44

Browse files
committed
Merged with master; Migrated InstanceIdClient to the new HttpClient API
2 parents a87b529 + 78714da commit 41fef44

23 files changed

+1068
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ bin/
22
obj/
33
.vscode/
44
.vs/
5+
.idea/

CONTRIBUTING.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Run the following commands from the command line to get your local environmentse
100100
```bash
101101
$ git clone https://github.com/firebase/firebase-admin-dotnet.git
102102
$ cd firebase-admin-dotnet/FirebaseAdmin # Change into the FirebaseAdmin solution directory
103-
$ dotnet restore # Install dependencies
103+
$ dotnet restore # Install dependencies
104104
```
105105

106106
### Running Tests
@@ -119,6 +119,24 @@ To run the unit test suite:
119119
$ dotnet test FirebaseAdmin.Tests
120120
```
121121

122+
To invoke code coverage tools, run the unit tests as follows:
123+
124+
```bash
125+
$ dotnet test FirebaseAdmin.Tests /p:CollectCoverage=true
126+
```
127+
128+
The above command calculates and displays a code coverage summary. To produce a more detailed
129+
code coverage report, you can use a tool like
130+
[Report Generator](https://github.com/danielpalme/ReportGenerator):
131+
132+
```bash
133+
$ dotnet test FirebaseAdmin.Tests /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
134+
$ dotnet path/to/ReportGenerator.dll -reports:./FirebaseAdmin.Tests/coverage.opencover.xml -targetdir:./reports
135+
```
136+
137+
This generates a collection of HTML code coverage reports in a local subdirectory named
138+
`reports/`.
139+
122140
The integration test suite requires a service account JSON key file, and an API key for a Firebase
123141
project. Create a new project in the [Firebase console](https://console.firebase.google.com) if
124142
you do not already have one. Use a separate, dedicated project for integration tests since the
@@ -143,4 +161,3 @@ Here are some highlights of the directory structure and notable source files
143161
* `FirebaseAdmin.Tests/` - Unit tests directory.
144162
* `FirebaseAdmin.IntegrationTests/` - Integration tests directory.
145163
* `FirebaseAdmin.Snippets/` - Example code snippets for the SDK.
146-

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseMessagingTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.Text.RegularExpressions;
1718
using System.Threading.Tasks;
1819
using FirebaseAdmin.Messaging;
@@ -120,5 +121,29 @@ public async Task SendMulticast()
120121
Assert.NotNull(response.Responses[0].Exception);
121122
Assert.NotNull(response.Responses[1].Exception);
122123
}
124+
125+
[Fact]
126+
public async Task SubscribeToTopic()
127+
{
128+
var response = await FirebaseMessaging.DefaultInstance.SubscribeToTopicAsync("test-topic", new List<string> { "token1", "token2" });
129+
Assert.NotNull(response);
130+
Assert.Equal(2, response.FailureCount);
131+
Assert.Equal("invalid-argument", response.Errors[0].Reason);
132+
Assert.Equal(0, response.Errors[0].Index);
133+
Assert.Equal("invalid-argument", response.Errors[1].Reason);
134+
Assert.Equal(1, response.Errors[1].Index);
135+
}
136+
137+
[Fact]
138+
public async Task UnsubscribeFromTopic()
139+
{
140+
var response = await FirebaseMessaging.DefaultInstance.UnsubscribeFromTopicAsync("test-topic", new List<string> { "token1", "token2" });
141+
Assert.NotNull(response);
142+
Assert.Equal(2, response.FailureCount);
143+
Assert.Equal("invalid-argument", response.Errors[0].Reason);
144+
Assert.Equal(0, response.Errors[0].Index);
145+
Assert.Equal("invalid-argument", response.Errors[1].Reason);
146+
Assert.Equal(1, response.Errors[1].Index);
147+
}
123148
}
124149
}

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.0</TargetFramework>
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="coverlet.msbuild" Version="2.6.3" />
1314
<PackageReference Include="Google.Apis.Auth" Version="1.40.0" />
1415
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
1516
<PackageReference Include="xunit" Version="2.3.1" />
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using FirebaseAdmin.Messaging;
17+
using FirebaseAdmin.Messaging.Util;
18+
using Xunit;
19+
20+
namespace FirebaseAdmin.Tests.Messaging
21+
{
22+
public class FcmOptionsTest
23+
{
24+
[Fact]
25+
public void FcmOptionsCopyAndValidate()
26+
{
27+
var options = new FcmOptions() { AnalyticsLabel = "label" };
28+
var result = options.CopyAndValidate();
29+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
30+
}
31+
32+
[Fact]
33+
public void ApnsFcmOptionsCopyAndValidate()
34+
{
35+
var options = new ApnsFcmOptions() { AnalyticsLabel = "label" };
36+
var result = options.CopyAndValidate();
37+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
38+
}
39+
40+
[Fact]
41+
public void AndroidFcmOptionsCopyAndValidate()
42+
{
43+
var options = new AndroidFcmOptions() { AnalyticsLabel = "label" };
44+
var result = options.CopyAndValidate();
45+
Assert.Equal(options.AnalyticsLabel, result.AnalyticsLabel);
46+
}
47+
48+
[Fact]
49+
public void FcmOptionsCopyAndValidateNullLabel()
50+
{
51+
var options = new FcmOptions() { AnalyticsLabel = null };
52+
options.CopyAndValidate();
53+
}
54+
55+
[Fact]
56+
public void FcmOptionsCopyAndValidateEmptyLabel()
57+
{
58+
var options = new FcmOptions() { AnalyticsLabel = string.Empty };
59+
Assert.Throws<ArgumentException>(() => options.CopyAndValidate());
60+
}
61+
62+
[Fact]
63+
public void AnalyticsLabelTooLong()
64+
{
65+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel(new string('a', 51)));
66+
}
67+
68+
[Fact]
69+
public void AnalyticsLabelEmtpty()
70+
{
71+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel(string.Empty));
72+
}
73+
74+
[Fact]
75+
public void AnalyticsLabelInvalidCharacters()
76+
{
77+
Assert.Throws<ArgumentException>(() => AnalyticsLabelChecker.ValidateAnalyticsLabel("label(label)"));
78+
}
79+
}
80+
}

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/FirebaseMessagingTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
using System;
16+
using System.Collections.Generic;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using FirebaseAdmin.Tests;
@@ -118,6 +119,56 @@ await Assert.ThrowsAsync<OperationCanceledException>(
118119
new Message() { Topic = "test-topic" }, canceller.Token));
119120
}
120121

122+
[Fact]
123+
public async Task SubscribeWithClientFactory()
124+
{
125+
var handler = new MockMessageHandler()
126+
{
127+
Response = @"{""results"":[{}]}",
128+
};
129+
var factory = new MockHttpClientFactory(handler);
130+
131+
var app = FirebaseApp.Create(new AppOptions()
132+
{
133+
Credential = GoogleCredential.FromAccessToken("test-token"),
134+
HttpClientFactory = factory,
135+
ProjectId = "test-project",
136+
});
137+
FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
138+
Assert.NotNull(messaging);
139+
Assert.Same(messaging, FirebaseMessaging.GetMessaging(app));
140+
141+
var response = await messaging.SubscribeToTopicAsync("test-topic", new List<string> { "test-token" });
142+
Assert.Equal(0, response.FailureCount);
143+
Assert.Equal(1, response.SuccessCount);
144+
app.Delete();
145+
}
146+
147+
[Fact]
148+
public async Task UnsubscribeWithClientFactory()
149+
{
150+
var handler = new MockMessageHandler()
151+
{
152+
Response = @"{""results"":[{}]}",
153+
};
154+
var factory = new MockHttpClientFactory(handler);
155+
156+
var app = FirebaseApp.Create(new AppOptions()
157+
{
158+
Credential = GoogleCredential.FromAccessToken("test-token"),
159+
HttpClientFactory = factory,
160+
ProjectId = "test-project",
161+
});
162+
FirebaseMessaging messaging = FirebaseMessaging.GetMessaging(app);
163+
Assert.NotNull(messaging);
164+
Assert.Same(messaging, FirebaseMessaging.GetMessaging(app));
165+
166+
var response = await messaging.UnsubscribeFromTopicAsync("test-topic", new List<string> { "test-token" });
167+
Assert.Equal(0, response.FailureCount);
168+
Assert.Equal(1, response.SuccessCount);
169+
app.Delete();
170+
}
171+
121172
public void Dispose()
122173
{
123174
FirebaseApp.DeleteAll();

0 commit comments

Comments
 (0)