From c04a235f484f3c1b780f6e08aade2b33966be77b Mon Sep 17 00:00:00 2001 From: Kelly Song Date: Mon, 3 Jun 2024 17:51:51 -0700 Subject: [PATCH] Add unit tests for new methods --- .../AadIssuerValidatorTests.cs | 46 +++++++++++++++++++ .../AadSigningKeyIssuerValidatorTests.cs | 15 ++++++ 2 files changed, 61 insertions(+) create mode 100644 test/Microsoft.IdentityModel.Validators.Tests/AadIssuerValidatorTests.cs diff --git a/test/Microsoft.IdentityModel.Validators.Tests/AadIssuerValidatorTests.cs b/test/Microsoft.IdentityModel.Validators.Tests/AadIssuerValidatorTests.cs new file mode 100644 index 0000000000..4acf55a0fa --- /dev/null +++ b/test/Microsoft.IdentityModel.Validators.Tests/AadIssuerValidatorTests.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +using System; +using Xunit; + +namespace Microsoft.IdentityModel.Validators.Tests +{ + public class AadIssuerValidatorTests + { + [Fact] + public static void IssuersWithTemplatesAreEqualTests_EqualIssuers() + { + // arrange + var issuer1Template = "{tenantId}"; + var issuer1 = ValidatorConstants.AadInstance + issuer1Template; + var issuer2Template = ValidatorConstants.TenantIdAsGuid; + var issuer2 = ValidatorConstants.AadInstance + issuer2Template; + int templateStartIndex = issuer1.IndexOf(issuer1Template); + + // act + var result = AadIssuerValidator.IssuersWithTemplatesAreEqual( + issuer1.AsSpan(), issuer1Template.AsSpan(), templateStartIndex, issuer2.AsSpan(), issuer2Template.AsSpan()); + + // assert + Assert.True(result); + } + + [Fact] + public static void IssuersWithTemplatesAreEqualTests_UnequalIssuers() + { + // arrange + var issuer1Template = "{tenantId}"; + var issuer1 = ValidatorConstants.AadInstancePPE + issuer1Template; + var issuer2Template = ValidatorConstants.TenantIdAsGuid; + var issuer2 = ValidatorConstants.AadInstance + issuer2Template; + int templateStartIndex = issuer1.IndexOf(issuer1Template); + + // act + var result = AadIssuerValidator.IssuersWithTemplatesAreEqual( + issuer1.AsSpan(), issuer1Template.AsSpan(), templateStartIndex, issuer2.AsSpan(), issuer2Template.AsSpan()); + + // assert + Assert.False(result); + } + } +} diff --git a/test/Microsoft.IdentityModel.Validators.Tests/AadSigningKeyIssuerValidatorTests.cs b/test/Microsoft.IdentityModel.Validators.Tests/AadSigningKeyIssuerValidatorTests.cs index e768a2372f..b979f7c49e 100644 --- a/test/Microsoft.IdentityModel.Validators.Tests/AadSigningKeyIssuerValidatorTests.cs +++ b/test/Microsoft.IdentityModel.Validators.Tests/AadSigningKeyIssuerValidatorTests.cs @@ -325,6 +325,21 @@ public static TheoryData ValidateIssuerSigningKey } } + [Fact] + public static void CreateIssuer_ReturnsExpectedIssuer() + { + // arrange + var issuerTemplate = "{tenantId}"; + var issuer = ValidatorConstants.AadInstance + issuerTemplate; + int templateStartIndex = issuer.IndexOf(issuerTemplate); + + // act + var result = AadTokenValidationParametersExtension.CreateIssuer(issuer, issuerTemplate, ValidatorConstants.TenantIdAsGuid, templateStartIndex); + + // assert + Assert.Equal(ValidatorConstants.AadInstance + ValidatorConstants.TenantIdAsGuid, result); + } + private static OpenIdConnectConfiguration GetConfigurationMock() { var config = new OpenIdConnectConfiguration();