Skip to content

Commit 47753b9

Browse files
authored
Additional FirebaseApp Tests (#2)
* Starter code for the .NET SDK * Added documentation * Added newlines at eof * Responding to code review comments * Added logging; More documentation and cleanup * Removed AppOptionsInternal * Updated unit test * Reading project ID from service account * Adding newline at eof * Upgraded to latest Google API client; Re-enabled service accountt test * newline at eof * Fixes based on code review comments
1 parent e3754ae commit 47753b9

File tree

6 files changed

+105
-14
lines changed

6 files changed

+105
-14
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Google.Apis.Auth" Version="1.34.0" />
10+
<PackageReference Include="Google.Apis.Auth" Version="1.35.1" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
1212
<PackageReference Include="xunit" Version="2.3.1" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Google.Apis.Auth" Version="1.34.0" />
10+
<PackageReference Include="Google.Apis.Auth" Version="1.35.1" />
1111
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
1212
<PackageReference Include="xunit" Version="2.3.1" />
1313
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAppTest.cs

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace FirebaseAdmin.Tests
2222
{
2323
public class FirebaseAppTest: IDisposable
2424
{
25+
private static readonly AppOptions TestOptions = new AppOptions()
26+
{
27+
Credential = GoogleCredential.FromAccessToken("token"),
28+
};
29+
2530
[Fact]
2631
public void GetNonExistingDefaultInstance()
2732
{
@@ -37,11 +42,11 @@ public void GetNonExistingInstance()
3742
[Fact]
3843
public void DefaultInstance()
3944
{
40-
var app = FirebaseApp.Create();
45+
var app = FirebaseApp.Create(TestOptions);
4146
Assert.Equal("[DEFAULT]", app.Name);
4247
Assert.NotNull(app.Options);
4348
Assert.Same(app, FirebaseApp.DefaultInstance);
44-
Assert.Throws<ArgumentException>(() => FirebaseApp.Create());
49+
Assert.Throws<ArgumentException>(() => FirebaseApp.Create(TestOptions));
4550
app.Delete();
4651
Assert.Null(FirebaseApp.DefaultInstance);
4752
}
@@ -50,19 +55,19 @@ public void DefaultInstance()
5055
public void CreateNamedInstance()
5156
{
5257
const string name = "MyApp";
53-
var app = FirebaseApp.Create(name);
58+
var app = FirebaseApp.Create(TestOptions, name);
5459
Assert.Equal(name, app.Name);
5560
Assert.NotNull(app.Options);
5661
Assert.Same(app, FirebaseApp.GetInstance(name));
57-
Assert.Throws<ArgumentException>(() => FirebaseApp.Create(name: name));
62+
Assert.Throws<ArgumentException>(() => FirebaseApp.Create(TestOptions, name));
5863
app.Delete();
59-
Assert.Null(FirebaseApp.GetInstance(name: name));
64+
Assert.Null(FirebaseApp.GetInstance(name));
6065
}
6166

6267
[Fact]
6368
public void CreateWithInvalidName()
6469
{
65-
Assert.Throws<ArgumentException>(() => FirebaseApp.Create((String) null));
70+
Assert.Throws<ArgumentException>(() => FirebaseApp.Create(name: null));
6671
Assert.Throws<ArgumentException>(() => FirebaseApp.Create(name: string.Empty));
6772
}
6873

@@ -114,32 +119,80 @@ public void ServiceAccountCredentialScoping()
114119
Assert.NotSame(options, copy);
115120
Assert.NotSame(credential, copy.Credential);
116121
Assert.IsType<ServiceAccountCredential>(copy.Credential.UnderlyingCredential);
117-
var credentialScopes = (copy.Credential.UnderlyingCredential as ServiceAccountCredential).Scopes;
122+
var credentialScopes = (copy.Credential.UnderlyingCredential
123+
as ServiceAccountCredential).Scopes;
118124
foreach (var scope in FirebaseApp.DefaultScopes)
119125
{
120126
Assert.Contains(scope, credentialScopes);
121127
}
122128
}
123129

130+
[Fact]
131+
public void ApplicationDefaultCredentials()
132+
{
133+
Environment.SetEnvironmentVariable(
134+
"GOOGLE_APPLICATION_CREDENTIALS", "./resources/service_account.json");
135+
try
136+
{
137+
var app = FirebaseApp.Create();
138+
Assert.NotNull(app.Options.Credential.ToServiceAccountCredential());
139+
}
140+
finally
141+
{
142+
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "");
143+
}
144+
}
145+
124146
[Fact]
125147
public void GetProjectIdFromOptions()
126148
{
127149
var options = new AppOptions()
128150
{
129151
Credential = GoogleCredential.FromAccessToken("token"),
130-
ProjectId = "test-project",
152+
ProjectId = "explicit-project",
153+
};
154+
var app = FirebaseApp.Create(options);
155+
Assert.Equal("explicit-project", app.GetProjectId());
156+
}
157+
158+
[Fact]
159+
public void GetProjectIdFromServiceAccount()
160+
{
161+
var options = new AppOptions()
162+
{
163+
Credential = GoogleCredential.FromFile("./resources/service_account.json"),
131164
};
132165
var app = FirebaseApp.Create(options);
133166
Assert.Equal("test-project", app.GetProjectId());
134167
}
135168

169+
[Fact]
170+
public void GetProjectIdFromEnvironment()
171+
{
172+
foreach (var name in new string[]{"GOOGLE_CLOUD_PROJECT", "GCLOUD_PROJECT"})
173+
{
174+
Environment.SetEnvironmentVariable(name, "env-project");
175+
try
176+
{
177+
var app = FirebaseApp.Create(TestOptions);
178+
Assert.Equal("env-project", app.GetProjectId());
179+
app.Delete();
180+
}
181+
finally
182+
{
183+
Environment.SetEnvironmentVariable(name, "");
184+
}
185+
}
186+
}
187+
136188
[Fact]
137189
public void GetOrInitService()
138190
{
139-
ServiceFactory<MockService> factory = () => {
191+
ServiceFactory<MockService> factory = () =>
192+
{
140193
return new MockService();
141194
};
142-
var app = FirebaseApp.Create();
195+
var app = FirebaseApp.Create(TestOptions);
143196
var service1 = app.GetOrInit("MockService", factory);
144197
var service2 = app.GetOrInit("MockService", factory);
145198
Assert.Same(service1, service2);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2018, 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 Google.Apis.Auth.OAuth2;
16+
17+
namespace FirebaseAdmin
18+
{
19+
/// <summary>
20+
/// A collection of extension methods for internal use.
21+
/// </summary>
22+
internal static class Extensions
23+
{
24+
public static ServiceAccountCredential ToServiceAccountCredential(this GoogleCredential credential)
25+
{
26+
if (credential.UnderlyingCredential is GoogleCredential)
27+
{
28+
return ((GoogleCredential) credential.UnderlyingCredential).ToServiceAccountCredential();
29+
}
30+
return credential.UnderlyingCredential as ServiceAccountCredential;
31+
}
32+
}
33+
}

FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Google.Apis.Auth" Version="1.34.0" />
11+
<PackageReference Include="Google.Apis.Auth" Version="1.35.1" />
1212
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
1313
</ItemGroup>
1414

FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,14 @@ internal string GetProjectId()
154154
{
155155
return Options.ProjectId;
156156
}
157+
var projectId = Options.Credential.ToServiceAccountCredential()?.ProjectId;
158+
if (!String.IsNullOrEmpty(projectId))
159+
{
160+
return projectId;
161+
}
157162
foreach (var variableName in new [] {"GOOGLE_CLOUD_PROJECT", "GCLOUD_PROJECT"})
158163
{
159-
string projectId = Environment.GetEnvironmentVariable(variableName);
164+
projectId = Environment.GetEnvironmentVariable(variableName);
160165
if (!String.IsNullOrEmpty(projectId))
161166
{
162167
return projectId;

0 commit comments

Comments
 (0)