|
| 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 | +} |
0 commit comments