Skip to content

Commit 85ee525

Browse files
committed
Move Utils and EnvironmentVariable class to Auth namespace
1 parent 19c386a commit 85ee525

File tree

8 files changed

+54
-96
lines changed

8 files changed

+54
-96
lines changed

FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseAuthTest.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using FirebaseAdmin.Auth.Jwt;
1717
using Google.Apis.Auth.OAuth2;
1818
using Xunit;
19+
using static FirebaseAdmin.Auth.Utils;
1920

2021
[assembly: CollectionBehavior(DisableTestParallelization = true)]
2122
namespace FirebaseAdmin.Auth.Tests
@@ -137,9 +138,24 @@ public void ServiceAccountId()
137138
Assert.IsType<FixedAccountIAMSigner>(tokenFactory.Signer);
138139
}
139140

140-
public void Dispose()
141+
public virtual void Dispose()
141142
{
142143
FirebaseApp.DeleteAll();
143144
}
144145
}
146+
147+
public class EmulatorFirebaseAuthTest : FirebaseAuthTest, IDisposable
148+
{
149+
private void SetFirebaseHostEnvironmentVariable(string value)
150+
=> Environment.SetEnvironmentVariable(EnvironmentVariable.FirebaseAuthEmulatorHostName, value);
151+
152+
public EmulatorFirebaseAuthTest()
153+
=> SetFirebaseHostEnvironmentVariable("localhost:9099");
154+
155+
public override void Dispose()
156+
{
157+
base.Dispose();
158+
SetFirebaseHostEnvironmentVariable(string.Empty);
159+
}
160+
}
145161
}

FirebaseAdmin/FirebaseAdmin.Tests/Auth/Users/FirebaseUserManagerTest.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
using FirebaseAdmin.Auth.Jwt.Tests;
2525
using FirebaseAdmin.Auth.Tests;
2626
using FirebaseAdmin.Tests;
27-
using FirebaseAdmin.Util;
2827
using Google.Apis.Json;
2928
using Google.Apis.Util;
3029
using Newtonsoft.Json.Linq;
3130
using Xunit;
31+
using static FirebaseAdmin.Auth.Utils;
3232

3333
namespace FirebaseAdmin.Auth.Users.Tests
3434
{
@@ -2194,7 +2194,7 @@ internal void AssertRequest(
21942194
string expectedSuffix, MockMessageHandler.IncomingRequest request)
21952195
{
21962196
var tenantInfo = this.TenantId != null ? $"/tenants/{this.TenantId}" : string.Empty;
2197-
var expectedUrl = $"{Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V1)}{tenantInfo}/{expectedSuffix}";
2197+
var expectedUrl = $"{Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V1)}{tenantInfo}/{expectedSuffix}";
21982198
Assert.Equal(expectedUrl, request.Url.ToString());
21992199
}
22002200

@@ -2226,14 +2226,13 @@ private IDictionary<string, object> GetUserResponseDictionary(string response =
22262226

22272227
public class EmulatorFirebaseUserManagerTest : FirebaseUserManagerTest, IDisposable
22282228
{
2229+
private void SetFirebaseHostEnvironmentVariable(string value)
2230+
=> Environment.SetEnvironmentVariable(EnvironmentVariable.FirebaseAuthEmulatorHostName, value);
2231+
22292232
public EmulatorFirebaseUserManagerTest()
2230-
{
2231-
EnvironmentVariable.FirebaseAuthEmulatorHost = "localhost:9099";
2232-
}
2233+
=> SetFirebaseHostEnvironmentVariable("localhost:9099");
22332234

22342235
public void Dispose()
2235-
{
2236-
EnvironmentVariable.FirebaseAuthEmulatorHost = string.Empty;
2237-
}
2236+
=> SetFirebaseHostEnvironmentVariable(string.Empty);
22382237
}
22392238
}

FirebaseAdmin/FirebaseAdmin.Tests/EnvironmentVariableTest.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.

FirebaseAdmin/FirebaseAdmin.Tests/Util/UtilTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public void ResolvesToCorrectVersion()
1515
var expectedV1Host = $"https://identitytoolkit.googleapis.com/v1/projects/{MockProjectId}";
1616
var expectedV2Host = $"https://identitytoolkit.googleapis.com/v2/projects/{MockProjectId}";
1717

18-
Assert.Equal(expectedV1Host, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V1));
19-
Assert.Equal(expectedV2Host, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
18+
Assert.Equal(expectedV1Host, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V1));
19+
Assert.Equal(expectedV2Host, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
2020
}
2121

2222
[Fact]
@@ -26,20 +26,20 @@ public void ResolvesToEmulatorHost()
2626

2727
var expectedHost = $"http://{CustomHost}/identitytoolkit.googleapis.com/v2/projects/{MockProjectId}";
2828

29-
Assert.Equal(expectedHost, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
29+
Assert.Equal(expectedHost, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
3030
}
3131

3232
[Fact]
3333
public void FailsOnNoProjectId()
3434
{
35-
Assert.Throws<ArgumentException>(() => Utils.ResolveIdToolkitHost(string.Empty, IdToolkitVersion.V2));
35+
Assert.Throws<ArgumentException>(() => Utils.GetIdToolkitHost(string.Empty, IdToolkitVersion.V2));
3636
}
3737

3838
[Fact]
3939
public void ResolvesToFirebaseHost()
4040
{
4141
var expectedHost = $"https://identitytoolkit.googleapis.com/v2/projects/{MockProjectId}";
42-
Assert.Equal(expectedHost, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
42+
Assert.Equal(expectedHost, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
4343
}
4444

4545
public void Dispose()

FirebaseAdmin/FirebaseAdmin/Auth/Multitenancy/TenantManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal TenantManager(Args args)
7272

7373
this.app = args.App;
7474

75-
this.baseUrl = Utils.ResolveIdToolkitHost(args.ProjectId, IdToolkitVersion.V2);
75+
this.baseUrl = Utils.GetIdToolkitHost(args.ProjectId, IdToolkitVersion.V2);
7676
this.httpClient = new ErrorHandlingHttpClient<FirebaseAuthException>(
7777
args.ToHttpClientArgs());
7878
}

FirebaseAdmin/FirebaseAdmin/Auth/Users/FirebaseUserManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ internal FirebaseUserManager(Args args)
7878
RetryOptions = args.RetryOptions,
7979
});
8080
this.clock = args.Clock ?? SystemClock.Default;
81-
var baseUrl = Utils.ResolveIdToolkitHost(args.ProjectId, IdToolkitVersion.V1);
81+
var baseUrl = Utils.GetIdToolkitHost(args.ProjectId, IdToolkitVersion.V1);
8282
if (this.TenantId != null)
8383
{
8484
this.baseUrl = $"{baseUrl}/tenants/{this.TenantId}";

FirebaseAdmin/FirebaseAdmin/Util/Utils.cs renamed to FirebaseAdmin/FirebaseAdmin/Auth/Utils.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2019, Google Inc. All rights reserved.
1+
// Copyright 2021, Google Inc. All rights reserved.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
1414

1515
using System;
1616

17-
namespace FirebaseAdmin.Util
17+
namespace FirebaseAdmin.Auth
1818
{
1919
internal enum IdToolkitVersion
2020
{
@@ -24,8 +24,21 @@ internal enum IdToolkitVersion
2424

2525
internal class Utils
2626
{
27+
internal static class EnvironmentVariable
28+
{
29+
internal const string FirebaseAuthEmulatorHostName = "FIREBASE_AUTH_EMULATOR_HOST";
30+
/// <summary>
31+
/// Gets environment variable for connecting to the Firebase Authentication Emulator.
32+
/// The variable is expected to be in the form &lt;host&gt;:&lt;port&gt;, e.g. localhost:9099.
33+
/// </summary>
34+
internal static string FirebaseAuthEmulatorHost
35+
{
36+
get => Environment.GetEnvironmentVariable(FirebaseAuthEmulatorHostName) ?? string.Empty;
37+
}
38+
}
39+
2740
/// <summary>
28-
/// Resolves to the correct identity toolkit api host.
41+
/// Gets the correct identity toolkit api host.
2942
/// It does this by checking if <see cref="EnvironmentVariable.FirebaseAuthEmulatorHost" /> exists
3043
/// and then prepends the url with that host if it does. Otherwise it returns the regular identity host.
3144
/// Example:
@@ -35,7 +48,7 @@ internal class Utils
3548
/// <param name="projectId">The project ID to connect to.</param>
3649
/// <param name="version">The version of the API to connect to.</param>
3750
/// <returns>Resolved identity toolkit host.</returns>
38-
internal static string ResolveIdToolkitHost(string projectId, IdToolkitVersion version = IdToolkitVersion.V2)
51+
internal static string GetIdToolkitHost(string projectId, IdToolkitVersion version = IdToolkitVersion.V2)
3952
{
4053
const string IdToolkitUrl = "https://identitytoolkit.googleapis.com/{0}/projects/{1}";
4154
const string IdToolkitEmulatorUrl = "http://{0}/identitytoolkit.googleapis.com/{1}/projects/{2}";
@@ -46,11 +59,13 @@ internal static string ResolveIdToolkitHost(string projectId, IdToolkitVersion v
4659
}
4760

4861
var emulatorHostEnvVar = EnvironmentVariable.FirebaseAuthEmulatorHost;
49-
var useEmulatorHost = !string.IsNullOrWhiteSpace(emulatorHostEnvVar);
5062
var versionAsString = version.ToString().ToLower();
51-
return useEmulatorHost
52-
? string.Format(IdToolkitEmulatorUrl, emulatorHostEnvVar, versionAsString, projectId)
53-
: string.Format(IdToolkitUrl, versionAsString, projectId);
63+
64+
if (!string.IsNullOrWhiteSpace(emulatorHostEnvVar))
65+
{
66+
return string.Format(IdToolkitEmulatorUrl, emulatorHostEnvVar, versionAsString, projectId);
67+
}
68+
return string.Format(IdToolkitUrl, versionAsString, projectId);
5469
}
5570
}
5671
}

FirebaseAdmin/FirebaseAdmin/Util/EnvironmentVariable.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)