Skip to content

chore: Added tenant-aware integ tests for IdP config management #240

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 2 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,18 @@ private async Task<DeleteUsersResult> SlowDeleteUsersAsync(IReadOnlyList<string>
return await this.Auth.DeleteUsersAsync(uids);
}
}

/// <summary>
/// Additional Xunit style asserts that allow specifying an error message upon failure.
/// </summary>
internal static class AssertWithMessage
{
internal static void NotNull(object obj, string msg)
{
if (obj == null)
{
throw new Xunit.Sdk.XunitException("Assert.NotNull() Failure: " + msg);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2020, 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
using FirebaseAdmin.Auth.Providers;
using Xunit;

namespace FirebaseAdmin.IntegrationTests.Auth
{
[TestCaseOrderer(
"FirebaseAdmin.IntegrationTests.TestRankOrderer", "FirebaseAdmin.IntegrationTests")]
public abstract class AbstractOidcProviderConfigTest<T>
where T : AbstractFirebaseAuth
{
private readonly OidcProviderConfigFixture<T> fixture;
private readonly T auth;
private readonly string providerId;

public AbstractOidcProviderConfigTest(OidcProviderConfigFixture<T> fixture)
{
this.fixture = fixture;
this.auth = fixture.Auth;
this.providerId = fixture.ProviderId;
}

[Fact]
[TestRank(0)]
public void CreateProviderConfig()
{
var config = this.fixture.ProviderConfig;

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
Assert.Equal("https://oidc.com/issuer", config.Issuer);
}

[Fact]
[TestRank(10)]
public async Task GetProviderConfig()
{
var config = await this.auth.GetOidcProviderConfigAsync(this.providerId);

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
Assert.Equal("https://oidc.com/issuer", config.Issuer);
}

[Fact]
[TestRank(10)]
public async Task ListProviderConfig()
{
OidcProviderConfig config = null;

var pagedEnumerable = this.auth.ListOidcProviderConfigsAsync(null);
var enumerator = pagedEnumerable.GetEnumerator();
while (await enumerator.MoveNext())
{
if (enumerator.Current.ProviderId == this.providerId)
{
config = enumerator.Current;
break;
}
}

Assert.NotNull(config);
Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
Assert.Equal("https://oidc.com/issuer", config.Issuer);
}

[Fact]
[TestRank(20)]
public async Task UpdateProviderConfig()
{
var args = new OidcProviderConfigArgs
{
ProviderId = this.providerId,
DisplayName = "UPDATED_OIDC_DISPLAY_NAME",
Enabled = false,
ClientId = "UPDATED_OIDC_CLIENT_ID",
Issuer = "https://oidc.com/updated-issuer",
};

var config = await this.auth.UpdateProviderConfigAsync(args);

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("UPDATED_OIDC_DISPLAY_NAME", config.DisplayName);
Assert.False(config.Enabled);
Assert.Equal("UPDATED_OIDC_CLIENT_ID", config.ClientId);
Assert.Equal("https://oidc.com/updated-issuer", config.Issuer);
}

[Fact]
[TestRank(30)]
public async Task DeleteProviderConfig()
{
await this.auth.DeleteProviderConfigAsync(this.providerId);
this.fixture.ProviderConfig = null;

var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
() => this.auth.GetOidcProviderConfigAsync(this.providerId));
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright 2020, 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Threading.Tasks;
using FirebaseAdmin.Auth;
using FirebaseAdmin.Auth.Providers;
using Xunit;

namespace FirebaseAdmin.IntegrationTests.Auth
{
[TestCaseOrderer(
"FirebaseAdmin.IntegrationTests.TestRankOrderer", "FirebaseAdmin.IntegrationTests")]
public abstract class AbstractSamlProviderConfigTest<T>
where T : AbstractFirebaseAuth
{
private readonly SamlProviderConfigFixture<T> fixture;
private readonly T auth;
private readonly string providerId;

public AbstractSamlProviderConfigTest(SamlProviderConfigFixture<T> fixture)
{
this.fixture = fixture;
this.auth = fixture.Auth;
this.providerId = fixture.ProviderId;
}

[Fact]
[TestRank(0)]
public void CreateProviderConfig()
{
var config = this.fixture.ProviderConfig;

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
Assert.Equal("https://example.com/login", config.SsoUrl);
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
}

[Fact]
[TestRank(10)]
public async Task GetProviderConfig()
{
var config = await this.auth.GetSamlProviderConfigAsync(this.providerId);

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
Assert.Equal("https://example.com/login", config.SsoUrl);
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
}

[Fact]
[TestRank(10)]
public async Task ListProviderConfig()
{
SamlProviderConfig config = null;

var pagedEnumerable = this.auth.ListSamlProviderConfigsAsync(null);
var enumerator = pagedEnumerable.GetEnumerator();
while (await enumerator.MoveNext())
{
if (enumerator.Current.ProviderId == this.providerId)
{
config = enumerator.Current;
break;
}
}

Assert.NotNull(config);
Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
Assert.True(config.Enabled);
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
Assert.Equal("https://example.com/login", config.SsoUrl);
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
}

[Fact]
[TestRank(20)]
public async Task UpdateProviderConfig()
{
var args = new SamlProviderConfigArgs()
{
ProviderId = this.providerId,
DisplayName = "UPDATED_SAML_DISPLAY_NAME",
Enabled = false,
IdpEntityId = "UPDATED_IDP_ENTITY_ID",
SsoUrl = "https://example.com/updated-login",
X509Certificates = new List<string>
{
SamlProviderConfigFixture<T>.X509Certificates[1],
},
RpEntityId = "UPDATED_RP_ENTITY_ID",
CallbackUrl = "https://projectId.firebaseapp.com/__/auth/updated-handler",
};

var config = await this.auth.UpdateProviderConfigAsync(args);

Assert.Equal(this.providerId, config.ProviderId);
Assert.Equal("UPDATED_SAML_DISPLAY_NAME", config.DisplayName);
Assert.False(config.Enabled);
Assert.Equal("UPDATED_IDP_ENTITY_ID", config.IdpEntityId);
Assert.Equal("https://example.com/updated-login", config.SsoUrl);
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[1]);
Assert.Equal("UPDATED_RP_ENTITY_ID", config.RpEntityId);
Assert.Equal(
"https://projectId.firebaseapp.com/__/auth/updated-handler", config.CallbackUrl);
}

[Fact]
[TestRank(30)]
public async Task DeleteProviderConfig()
{
await this.auth.DeleteProviderConfigAsync(this.providerId);
this.fixture.ProviderConfig = null;

var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
() => this.auth.GetSamlProviderConfigAsync(this.providerId));
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -133,6 +134,15 @@ internal static async Task<string> SignInWithPasswordAsync(
return response.IdToken;
}

internal static string GetRandomIdentifier(int length = 10)
{
var random = new Random();
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
var suffix = new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
return $"id-{suffix}";
}

private static async Task<T> SendAndDeserialize<T>(HttpRequestMessage request)
{
using (var client = new HttpClient())
Expand Down

This file was deleted.

Loading