Skip to content

Commit

Permalink
Move Utils and EnvironmentVariable class to Auth namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
floppydisken committed Mar 24, 2021
1 parent 19c386a commit 85ee525
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 96 deletions.
18 changes: 17 additions & 1 deletion FirebaseAdmin/FirebaseAdmin.Tests/Auth/FirebaseAuthTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using FirebaseAdmin.Auth.Jwt;
using Google.Apis.Auth.OAuth2;
using Xunit;
using static FirebaseAdmin.Auth.Utils;

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

public void Dispose()
public virtual void Dispose()
{
FirebaseApp.DeleteAll();
}
}

public class EmulatorFirebaseAuthTest : FirebaseAuthTest, IDisposable
{
private void SetFirebaseHostEnvironmentVariable(string value)
=> Environment.SetEnvironmentVariable(EnvironmentVariable.FirebaseAuthEmulatorHostName, value);

public EmulatorFirebaseAuthTest()
=> SetFirebaseHostEnvironmentVariable("localhost:9099");

public override void Dispose()
{
base.Dispose();
SetFirebaseHostEnvironmentVariable(string.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
using FirebaseAdmin.Auth.Jwt.Tests;
using FirebaseAdmin.Auth.Tests;
using FirebaseAdmin.Tests;
using FirebaseAdmin.Util;
using Google.Apis.Json;
using Google.Apis.Util;
using Newtonsoft.Json.Linq;
using Xunit;
using static FirebaseAdmin.Auth.Utils;

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

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

public class EmulatorFirebaseUserManagerTest : FirebaseUserManagerTest, IDisposable
{
private void SetFirebaseHostEnvironmentVariable(string value)
=> Environment.SetEnvironmentVariable(EnvironmentVariable.FirebaseAuthEmulatorHostName, value);

public EmulatorFirebaseUserManagerTest()
{
EnvironmentVariable.FirebaseAuthEmulatorHost = "localhost:9099";
}
=> SetFirebaseHostEnvironmentVariable("localhost:9099");

public void Dispose()
{
EnvironmentVariable.FirebaseAuthEmulatorHost = string.Empty;
}
=> SetFirebaseHostEnvironmentVariable(string.Empty);
}
}
37 changes: 0 additions & 37 deletions FirebaseAdmin/FirebaseAdmin.Tests/EnvironmentVariableTest.cs

This file was deleted.

10 changes: 5 additions & 5 deletions FirebaseAdmin/FirebaseAdmin.Tests/Util/UtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public void ResolvesToCorrectVersion()
var expectedV1Host = $"https://identitytoolkit.googleapis.com/v1/projects/{MockProjectId}";
var expectedV2Host = $"https://identitytoolkit.googleapis.com/v2/projects/{MockProjectId}";

Assert.Equal(expectedV1Host, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V1));
Assert.Equal(expectedV2Host, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
Assert.Equal(expectedV1Host, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V1));
Assert.Equal(expectedV2Host, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
}

[Fact]
Expand All @@ -26,20 +26,20 @@ public void ResolvesToEmulatorHost()

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

Assert.Equal(expectedHost, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
Assert.Equal(expectedHost, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
}

[Fact]
public void FailsOnNoProjectId()
{
Assert.Throws<ArgumentException>(() => Utils.ResolveIdToolkitHost(string.Empty, IdToolkitVersion.V2));
Assert.Throws<ArgumentException>(() => Utils.GetIdToolkitHost(string.Empty, IdToolkitVersion.V2));
}

[Fact]
public void ResolvesToFirebaseHost()
{
var expectedHost = $"https://identitytoolkit.googleapis.com/v2/projects/{MockProjectId}";
Assert.Equal(expectedHost, Utils.ResolveIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
Assert.Equal(expectedHost, Utils.GetIdToolkitHost(MockProjectId, IdToolkitVersion.V2));
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ internal TenantManager(Args args)

this.app = args.App;

this.baseUrl = Utils.ResolveIdToolkitHost(args.ProjectId, IdToolkitVersion.V2);
this.baseUrl = Utils.GetIdToolkitHost(args.ProjectId, IdToolkitVersion.V2);
this.httpClient = new ErrorHandlingHttpClient<FirebaseAuthException>(
args.ToHttpClientArgs());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ internal FirebaseUserManager(Args args)
RetryOptions = args.RetryOptions,
});
this.clock = args.Clock ?? SystemClock.Default;
var baseUrl = Utils.ResolveIdToolkitHost(args.ProjectId, IdToolkitVersion.V1);
var baseUrl = Utils.GetIdToolkitHost(args.ProjectId, IdToolkitVersion.V1);
if (this.TenantId != null)
{
this.baseUrl = $"{baseUrl}/tenants/{this.TenantId}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019, Google Inc. All rights reserved.
// Copyright 2021, Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@

using System;

namespace FirebaseAdmin.Util
namespace FirebaseAdmin.Auth
{
internal enum IdToolkitVersion
{
Expand All @@ -24,8 +24,21 @@ internal enum IdToolkitVersion

internal class Utils
{
internal static class EnvironmentVariable
{
internal const string FirebaseAuthEmulatorHostName = "FIREBASE_AUTH_EMULATOR_HOST";
/// <summary>
/// Gets environment variable for connecting to the Firebase Authentication Emulator.
/// The variable is expected to be in the form &lt;host&gt;:&lt;port&gt;, e.g. localhost:9099.
/// </summary>
internal static string FirebaseAuthEmulatorHost
{
get => Environment.GetEnvironmentVariable(FirebaseAuthEmulatorHostName) ?? string.Empty;
}
}

/// <summary>
/// Resolves to the correct identity toolkit api host.
/// Gets the correct identity toolkit api host.
/// It does this by checking if <see cref="EnvironmentVariable.FirebaseAuthEmulatorHost" /> exists
/// and then prepends the url with that host if it does. Otherwise it returns the regular identity host.
/// Example:
Expand All @@ -35,7 +48,7 @@ internal class Utils
/// <param name="projectId">The project ID to connect to.</param>
/// <param name="version">The version of the API to connect to.</param>
/// <returns>Resolved identity toolkit host.</returns>
internal static string ResolveIdToolkitHost(string projectId, IdToolkitVersion version = IdToolkitVersion.V2)
internal static string GetIdToolkitHost(string projectId, IdToolkitVersion version = IdToolkitVersion.V2)
{
const string IdToolkitUrl = "https://identitytoolkit.googleapis.com/{0}/projects/{1}";
const string IdToolkitEmulatorUrl = "http://{0}/identitytoolkit.googleapis.com/{1}/projects/{2}";
Expand All @@ -46,11 +59,13 @@ internal static string ResolveIdToolkitHost(string projectId, IdToolkitVersion v
}

var emulatorHostEnvVar = EnvironmentVariable.FirebaseAuthEmulatorHost;
var useEmulatorHost = !string.IsNullOrWhiteSpace(emulatorHostEnvVar);
var versionAsString = version.ToString().ToLower();
return useEmulatorHost
? string.Format(IdToolkitEmulatorUrl, emulatorHostEnvVar, versionAsString, projectId)
: string.Format(IdToolkitUrl, versionAsString, projectId);

if (!string.IsNullOrWhiteSpace(emulatorHostEnvVar))
{
return string.Format(IdToolkitEmulatorUrl, emulatorHostEnvVar, versionAsString, projectId);
}
return string.Format(IdToolkitUrl, versionAsString, projectId);
}
}
}
35 changes: 0 additions & 35 deletions FirebaseAdmin/FirebaseAdmin/Util/EnvironmentVariable.cs

This file was deleted.

0 comments on commit 85ee525

Please sign in to comment.