Skip to content

Commit 7ea2974

Browse files
authored
chore: Added tenant-aware integ tests for IdP config management (#240)
* chore: Added tenant-aware integ tests for IdP config management * chore: Cleaned up the test fixtures
1 parent 34db851 commit 7ea2974

17 files changed

+569
-431
lines changed

FirebaseAdmin/FirebaseAdmin.IntegrationTests/Auth/AbstractFirebaseAuthTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,18 @@ private async Task<DeleteUsersResult> SlowDeleteUsersAsync(IReadOnlyList<string>
634634
return await this.Auth.DeleteUsersAsync(uids);
635635
}
636636
}
637+
638+
/// <summary>
639+
/// Additional Xunit style asserts that allow specifying an error message upon failure.
640+
/// </summary>
641+
internal static class AssertWithMessage
642+
{
643+
internal static void NotNull(object obj, string msg)
644+
{
645+
if (obj == null)
646+
{
647+
throw new Xunit.Sdk.XunitException("Assert.NotNull() Failure: " + msg);
648+
}
649+
}
650+
}
637651
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2020, 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.Collections.Generic;
16+
using System.Threading.Tasks;
17+
using FirebaseAdmin.Auth;
18+
using FirebaseAdmin.Auth.Providers;
19+
using Xunit;
20+
21+
namespace FirebaseAdmin.IntegrationTests.Auth
22+
{
23+
[TestCaseOrderer(
24+
"FirebaseAdmin.IntegrationTests.TestRankOrderer", "FirebaseAdmin.IntegrationTests")]
25+
public abstract class AbstractOidcProviderConfigTest<T>
26+
where T : AbstractFirebaseAuth
27+
{
28+
private readonly OidcProviderConfigFixture<T> fixture;
29+
private readonly T auth;
30+
private readonly string providerId;
31+
32+
public AbstractOidcProviderConfigTest(OidcProviderConfigFixture<T> fixture)
33+
{
34+
this.fixture = fixture;
35+
this.auth = fixture.Auth;
36+
this.providerId = fixture.ProviderId;
37+
}
38+
39+
[Fact]
40+
[TestRank(0)]
41+
public void CreateProviderConfig()
42+
{
43+
var config = this.fixture.ProviderConfig;
44+
45+
Assert.Equal(this.providerId, config.ProviderId);
46+
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
47+
Assert.True(config.Enabled);
48+
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
49+
Assert.Equal("https://oidc.com/issuer", config.Issuer);
50+
}
51+
52+
[Fact]
53+
[TestRank(10)]
54+
public async Task GetProviderConfig()
55+
{
56+
var config = await this.auth.GetOidcProviderConfigAsync(this.providerId);
57+
58+
Assert.Equal(this.providerId, config.ProviderId);
59+
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
60+
Assert.True(config.Enabled);
61+
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
62+
Assert.Equal("https://oidc.com/issuer", config.Issuer);
63+
}
64+
65+
[Fact]
66+
[TestRank(10)]
67+
public async Task ListProviderConfig()
68+
{
69+
OidcProviderConfig config = null;
70+
71+
var pagedEnumerable = this.auth.ListOidcProviderConfigsAsync(null);
72+
var enumerator = pagedEnumerable.GetEnumerator();
73+
while (await enumerator.MoveNext())
74+
{
75+
if (enumerator.Current.ProviderId == this.providerId)
76+
{
77+
config = enumerator.Current;
78+
break;
79+
}
80+
}
81+
82+
Assert.NotNull(config);
83+
Assert.Equal(this.providerId, config.ProviderId);
84+
Assert.Equal("OIDC_DISPLAY_NAME", config.DisplayName);
85+
Assert.True(config.Enabled);
86+
Assert.Equal("OIDC_CLIENT_ID", config.ClientId);
87+
Assert.Equal("https://oidc.com/issuer", config.Issuer);
88+
}
89+
90+
[Fact]
91+
[TestRank(20)]
92+
public async Task UpdateProviderConfig()
93+
{
94+
var args = new OidcProviderConfigArgs
95+
{
96+
ProviderId = this.providerId,
97+
DisplayName = "UPDATED_OIDC_DISPLAY_NAME",
98+
Enabled = false,
99+
ClientId = "UPDATED_OIDC_CLIENT_ID",
100+
Issuer = "https://oidc.com/updated-issuer",
101+
};
102+
103+
var config = await this.auth.UpdateProviderConfigAsync(args);
104+
105+
Assert.Equal(this.providerId, config.ProviderId);
106+
Assert.Equal("UPDATED_OIDC_DISPLAY_NAME", config.DisplayName);
107+
Assert.False(config.Enabled);
108+
Assert.Equal("UPDATED_OIDC_CLIENT_ID", config.ClientId);
109+
Assert.Equal("https://oidc.com/updated-issuer", config.Issuer);
110+
}
111+
112+
[Fact]
113+
[TestRank(30)]
114+
public async Task DeleteProviderConfig()
115+
{
116+
await this.auth.DeleteProviderConfigAsync(this.providerId);
117+
this.fixture.ProviderConfig = null;
118+
119+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
120+
() => this.auth.GetOidcProviderConfigAsync(this.providerId));
121+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
122+
Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
123+
}
124+
}
125+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2020, 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.Collections.Generic;
16+
using System.Threading.Tasks;
17+
using FirebaseAdmin.Auth;
18+
using FirebaseAdmin.Auth.Providers;
19+
using Xunit;
20+
21+
namespace FirebaseAdmin.IntegrationTests.Auth
22+
{
23+
[TestCaseOrderer(
24+
"FirebaseAdmin.IntegrationTests.TestRankOrderer", "FirebaseAdmin.IntegrationTests")]
25+
public abstract class AbstractSamlProviderConfigTest<T>
26+
where T : AbstractFirebaseAuth
27+
{
28+
private readonly SamlProviderConfigFixture<T> fixture;
29+
private readonly T auth;
30+
private readonly string providerId;
31+
32+
public AbstractSamlProviderConfigTest(SamlProviderConfigFixture<T> fixture)
33+
{
34+
this.fixture = fixture;
35+
this.auth = fixture.Auth;
36+
this.providerId = fixture.ProviderId;
37+
}
38+
39+
[Fact]
40+
[TestRank(0)]
41+
public void CreateProviderConfig()
42+
{
43+
var config = this.fixture.ProviderConfig;
44+
45+
Assert.Equal(this.providerId, config.ProviderId);
46+
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
47+
Assert.True(config.Enabled);
48+
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
49+
Assert.Equal("https://example.com/login", config.SsoUrl);
50+
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
51+
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
52+
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
53+
}
54+
55+
[Fact]
56+
[TestRank(10)]
57+
public async Task GetProviderConfig()
58+
{
59+
var config = await this.auth.GetSamlProviderConfigAsync(this.providerId);
60+
61+
Assert.Equal(this.providerId, config.ProviderId);
62+
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
63+
Assert.True(config.Enabled);
64+
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
65+
Assert.Equal("https://example.com/login", config.SsoUrl);
66+
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
67+
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
68+
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
69+
}
70+
71+
[Fact]
72+
[TestRank(10)]
73+
public async Task ListProviderConfig()
74+
{
75+
SamlProviderConfig config = null;
76+
77+
var pagedEnumerable = this.auth.ListSamlProviderConfigsAsync(null);
78+
var enumerator = pagedEnumerable.GetEnumerator();
79+
while (await enumerator.MoveNext())
80+
{
81+
if (enumerator.Current.ProviderId == this.providerId)
82+
{
83+
config = enumerator.Current;
84+
break;
85+
}
86+
}
87+
88+
Assert.NotNull(config);
89+
Assert.Equal(this.providerId, config.ProviderId);
90+
Assert.Equal("SAML_DISPLAY_NAME", config.DisplayName);
91+
Assert.True(config.Enabled);
92+
Assert.Equal("IDP_ENTITY_ID", config.IdpEntityId);
93+
Assert.Equal("https://example.com/login", config.SsoUrl);
94+
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[0]);
95+
Assert.Equal("RP_ENTITY_ID", config.RpEntityId);
96+
Assert.Equal("https://projectId.firebaseapp.com/__/auth/handler", config.CallbackUrl);
97+
}
98+
99+
[Fact]
100+
[TestRank(20)]
101+
public async Task UpdateProviderConfig()
102+
{
103+
var args = new SamlProviderConfigArgs()
104+
{
105+
ProviderId = this.providerId,
106+
DisplayName = "UPDATED_SAML_DISPLAY_NAME",
107+
Enabled = false,
108+
IdpEntityId = "UPDATED_IDP_ENTITY_ID",
109+
SsoUrl = "https://example.com/updated-login",
110+
X509Certificates = new List<string>
111+
{
112+
SamlProviderConfigFixture<T>.X509Certificates[1],
113+
},
114+
RpEntityId = "UPDATED_RP_ENTITY_ID",
115+
CallbackUrl = "https://projectId.firebaseapp.com/__/auth/updated-handler",
116+
};
117+
118+
var config = await this.auth.UpdateProviderConfigAsync(args);
119+
120+
Assert.Equal(this.providerId, config.ProviderId);
121+
Assert.Equal("UPDATED_SAML_DISPLAY_NAME", config.DisplayName);
122+
Assert.False(config.Enabled);
123+
Assert.Equal("UPDATED_IDP_ENTITY_ID", config.IdpEntityId);
124+
Assert.Equal("https://example.com/updated-login", config.SsoUrl);
125+
Assert.Single(config.X509Certificates, SamlProviderConfigFixture<T>.X509Certificates[1]);
126+
Assert.Equal("UPDATED_RP_ENTITY_ID", config.RpEntityId);
127+
Assert.Equal(
128+
"https://projectId.firebaseapp.com/__/auth/updated-handler", config.CallbackUrl);
129+
}
130+
131+
[Fact]
132+
[TestRank(30)]
133+
public async Task DeleteProviderConfig()
134+
{
135+
await this.auth.DeleteProviderConfigAsync(this.providerId);
136+
this.fixture.ProviderConfig = null;
137+
138+
var exception = await Assert.ThrowsAsync<FirebaseAuthException>(
139+
() => this.auth.GetSamlProviderConfigAsync(this.providerId));
140+
Assert.Equal(ErrorCode.NotFound, exception.ErrorCode);
141+
Assert.Equal(AuthErrorCode.ConfigurationNotFound, exception.AuthErrorCode);
142+
}
143+
}
144+
}

FirebaseAdmin/FirebaseAdmin.IntegrationTests/Auth/AuthIntegrationUtils.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using System;
1616
using System.Collections.Generic;
17+
using System.Linq;
1718
using System.Net.Http;
1819
using System.Text;
1920
using System.Threading.Tasks;
@@ -133,6 +134,15 @@ internal static async Task<string> SignInWithPasswordAsync(
133134
return response.IdToken;
134135
}
135136

137+
internal static string GetRandomIdentifier(int length = 10)
138+
{
139+
var random = new Random();
140+
const string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
141+
var suffix = new string(Enumerable.Repeat(chars, length)
142+
.Select(s => s[random.Next(s.Length)]).ToArray());
143+
return $"id-{suffix}";
144+
}
145+
136146
private static async Task<T> SendAndDeserialize<T>(HttpRequestMessage request)
137147
{
138148
using (var client = new HttpClient())

FirebaseAdmin/FirebaseAdmin.IntegrationTests/Auth/FirebaseAuthFixture.cs

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

0 commit comments

Comments
 (0)