Skip to content

Commit b2c03e1

Browse files
authored
add a few more tests (#2128)
1 parent 8021a03 commit b2c03e1

9 files changed

+235
-7
lines changed

src/Microsoft.Identity.Web/DownstreamWebApiSupport/MicrosoftIdentityAuthenticationBaseMessageHandler.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,11 @@ protected MicrosoftIdentityAuthenticationMessageHandlerOptions GetOptionsForRequ
6363
return options;
6464
}
6565

66-
private static void CreateProofOfPossessionConfiguration(MicrosoftIdentityAuthenticationMessageHandlerOptions options, Uri apiUri, HttpMethod method)
66+
internal static void CreateProofOfPossessionConfiguration(MicrosoftIdentityAuthenticationMessageHandlerOptions options, Uri apiUri, HttpMethod method)
6767
{
6868
if (options.IsProofOfPossessionRequest)
6969
{
70-
if (options.TokenAcquisitionOptions == null)
71-
{
72-
options.TokenAcquisitionOptions = new TokenAcquisitionOptions();
73-
}
70+
options.TokenAcquisitionOptions ??= new TokenAcquisitionOptions();
7471

7572
options.TokenAcquisitionOptions.PoPConfiguration = new PoPAuthenticationConfiguration(apiUri)
7673
{

tests/Microsoft.Identity.Web.Test/Base64UrlHelpersTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,28 @@ public void DecodeToBytes_InvalidBase64UrlStringLength_ThrowsException()
9292
var exception = Assert.Throws<ArgumentException>(decodeAction);
9393
Assert.Equal(IDWebErrorMessage.InvalidBase64UrlString + " (Parameter 'str')", exception.Message);
9494
}
95+
96+
[Fact]
97+
public void EncodeString_TakesStringArgument_ReturnsEncodedString()
98+
{
99+
// Arrange
100+
var input = "Hello, world!";
101+
102+
// Act
103+
string? result = Base64UrlHelpers.EncodeString(input);
104+
105+
// Assert
106+
Assert.NotEmpty(result!);
107+
}
108+
109+
[Fact]
110+
public void DecodeBytes_TakesNullArgument_ReturnsNull()
111+
{
112+
// Arrange & Act
113+
var result = Base64UrlHelpers.DecodeBytes(null);
114+
115+
// Assert
116+
Assert.Null(result);
117+
}
95118
}
96119
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Xunit;
5+
6+
namespace Microsoft.Identity.Web.Test
7+
{
8+
public class CertificateLessOptionsTests
9+
{
10+
[Fact]
11+
public void IsEnabled_DefaultValue_IsFalse()
12+
{
13+
// Arrange
14+
var options = new CertificatelessOptions();
15+
16+
// Act/Assert
17+
Assert.False(options.IsEnabled);
18+
}
19+
20+
[Fact]
21+
public void IsEnabled_SetValue_GetsValue()
22+
{
23+
// Arrange
24+
var options = new CertificatelessOptions
25+
{
26+
IsEnabled = true
27+
};
28+
29+
// Act/Assert
30+
Assert.True(options.IsEnabled);
31+
}
32+
33+
[Fact]
34+
public void ManagedIdentityClientId_DefaultValue_IsNull()
35+
{
36+
// Arrange
37+
var options = new CertificatelessOptions();
38+
39+
// Act/Assert
40+
Assert.Null(options.ManagedIdentityClientId);
41+
}
42+
43+
[Fact]
44+
public void ManagedIdentityClientId_SetValue_GetsValue()
45+
{
46+
// Arrange
47+
var options = new CertificatelessOptions
48+
{
49+
ManagedIdentityClientId = "client_id"
50+
};
51+
52+
// Act/Assert
53+
Assert.Equal("client_id", options.ManagedIdentityClientId);
54+
}
55+
}
56+
}

tests/Microsoft.Identity.Web.Test/ClientAssertionTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,20 @@ public async Task TestClientAssertion()
4040
assertion = await clientAssertionDescription.GetSignedAssertion(CancellationToken.None).ConfigureAwait(false);
4141
Assert.Equal("2", assertion);
4242
}
43+
44+
[Fact]
45+
public void Constructor_ValidInput_SetsProperties()
46+
{
47+
// Arrange
48+
var signedAssertion = "assertion";
49+
var expiry = DateTimeOffset.Now.AddDays(1);
50+
51+
// Act
52+
var assertion = new ClientAssertion(signedAssertion, expiry);
53+
54+
// Assert
55+
Assert.Equal(signedAssertion, assertion.SignedAssertion);
56+
Assert.Equal(expiry, assertion.Expiry);
57+
}
4358
}
4459
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Net.Http;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Identity.Web.TokenCacheProviders.InMemory;
8+
using Xunit;
9+
10+
namespace Microsoft.Identity.Web.Test
11+
{
12+
public class DefaultMicrosoftIdentityAuthDelegatingHandlerFactoryTests
13+
{
14+
private IServiceProvider InitializeServiceCollection()
15+
{
16+
var services = new ServiceCollection();
17+
services.AddTokenAcquisition();
18+
services.AddInMemoryTokenCaches();
19+
services.AddHttpClient();
20+
return services.BuildServiceProvider();
21+
}
22+
23+
[Fact]
24+
public void CreateAppHandler_Should_Return_MicrosoftIdentityAppAuthenticationMessageHandler()
25+
{
26+
// Arrange
27+
var factory = new DefaultMicrosoftIdentityAuthenticationDelegatingHandlerFactory(InitializeServiceCollection());
28+
string serviceName = "test-service";
29+
30+
// Act
31+
DelegatingHandler handler = factory.CreateAppHandler(serviceName);
32+
33+
// Assert
34+
Assert.IsType<MicrosoftIdentityAppAuthenticationMessageHandler>(handler);
35+
}
36+
37+
[Fact]
38+
public void CreateUserHandler_Should_Return_MicrosoftIdentityUserAuthenticationMessageHandler()
39+
{
40+
// Arrange
41+
var factory = new DefaultMicrosoftIdentityAuthenticationDelegatingHandlerFactory(InitializeServiceCollection());
42+
string serviceName = "test-service";
43+
44+
// Act
45+
DelegatingHandler handler = factory.CreateUserHandler(serviceName);
46+
47+
// Assert
48+
Assert.IsType<MicrosoftIdentityUserAuthenticationMessageHandler>(handler);
49+
}
50+
}
51+
}

tests/Microsoft.Identity.Web.Test/MergedOptionsTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6-
using Microsoft.Graph.SecurityNamespace;
76
using Microsoft.Identity.Abstractions;
87
using Xunit;
98

tests/Microsoft.Identity.Web.Test/Microsoft.Identity.Web.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
3333
<!-- Special need for Microsoft.Extensions.Hosting.Host which is only available from 3.1-->
34-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1"/>
34+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1" />
3535
</ItemGroup>
3636

3737
<ItemGroup Condition="'$(TargetFramework)' == 'net472' Or '$(TargetFramework)' == 'net462'">

tests/Microsoft.Identity.Web.Test/MicrosoftIdentityAuthenticationMessageHandlerTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,73 @@ public async Task MicrosoftIdentityAuthenticationMessageHandler_Replaces_Authori
173173
Assert.Equal($"Bearer {_authenticationResult.AccessToken}", _mockedMessageHandler.Requests[0].Headers.GetValues(Constants.Authorization).ElementAt(0));
174174
}
175175

176+
[Fact]
177+
public void CreateProofOfPossessionConfiguration_WithProofOfPossessionRequest_SetsTokenAcquisitionOptions()
178+
{
179+
// Arrange
180+
var options = new MicrosoftIdentityAuthenticationMessageHandlerOptions
181+
{
182+
IsProofOfPossessionRequest = true
183+
};
184+
var httpPath = new Uri("https://api.example.com");
185+
var method = HttpMethod.Post;
186+
187+
// Act
188+
MicrosoftIdentityAuthenticationBaseMessageHandler.CreateProofOfPossessionConfiguration(options, httpPath, method);
189+
190+
// Assert
191+
Assert.NotNull(options.TokenAcquisitionOptions);
192+
Assert.NotNull(options.TokenAcquisitionOptions.PoPConfiguration);
193+
Assert.Equal(httpPath.AbsolutePath, options.TokenAcquisitionOptions.PoPConfiguration.HttpPath);
194+
Assert.Equal(method, options.TokenAcquisitionOptions.PoPConfiguration.HttpMethod);
195+
}
196+
197+
[Fact]
198+
public void CreateProofOfPossessionConfiguration_WithoutProofOfPossessionRequest_DoesNotSetTokenAcquisitionOptions()
199+
{
200+
// Arrange
201+
var options = new MicrosoftIdentityAuthenticationMessageHandlerOptions
202+
{
203+
IsProofOfPossessionRequest = false
204+
};
205+
var httpPath = new Uri("https://api.example.com");
206+
var method = HttpMethod.Post;
207+
208+
// Act
209+
MicrosoftIdentityAuthenticationBaseMessageHandler.CreateProofOfPossessionConfiguration(options, httpPath, method);
210+
211+
// Assert
212+
Assert.NotNull(options.TokenAcquisitionOptions);
213+
Assert.Null(options.TokenAcquisitionOptions.PoPConfiguration);
214+
}
215+
216+
[Fact]
217+
public void Clone_ClonesOptionsSuccessfully()
218+
{
219+
// Arrange
220+
var options = new MicrosoftIdentityAuthenticationMessageHandlerOptions
221+
{
222+
Scopes = TestConstants.Scopes,
223+
Tenant = TestConstants.TenantIdAsGuid,
224+
UserFlow = TestConstants.B2CSignUpSignInUserFlow,
225+
IsProofOfPossessionRequest = true,
226+
TokenAcquisitionOptions = new TokenAcquisitionOptions { Tenant = TestConstants.B2CTenant },
227+
AuthenticationScheme = "Bearer",
228+
};
229+
230+
// Act
231+
var clonedOptions = options.Clone();
232+
233+
// Assert
234+
Assert.NotSame(clonedOptions, options);
235+
Assert.Equal(options.Scopes, clonedOptions.Scopes);
236+
Assert.Equal(options.Tenant, clonedOptions.Tenant);
237+
Assert.Equal(options.UserFlow, clonedOptions.UserFlow);
238+
Assert.Equal(options.IsProofOfPossessionRequest, clonedOptions.IsProofOfPossessionRequest);
239+
Assert.Equal(options.TokenAcquisitionOptions.Tenant, clonedOptions.TokenAcquisitionOptions.Tenant);
240+
Assert.Equal(options.AuthenticationScheme, clonedOptions.AuthenticationScheme);
241+
}
242+
176243
private class MockHttpMessageHandler : HttpMessageHandler
177244
{
178245
private readonly HttpStatusCode _statusCode;

tests/Microsoft.Identity.Web.Test/TokenAcquisitionAuthorityTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,25 @@ public void VerifyCorrectBooleansAsync(
211211
Assert.False(mergedOptions.SendX5C);
212212
}
213213
}
214+
215+
[Fact]
216+
public void TestParseAuthorityIfNecessary()
217+
{
218+
// Arrange
219+
MergedOptions mergedOptions = new()
220+
{
221+
Authority = TestConstants.AuthorityWithTenantSpecified,
222+
TenantId = TestConstants.TenantIdAsGuid,
223+
Instance = TestConstants.AadInstance
224+
};
225+
226+
// Act
227+
MergedOptions.ParseAuthorityIfNecessary(mergedOptions);
228+
229+
// Assert
230+
Assert.Equal(TestConstants.AuthorityWithTenantSpecified, mergedOptions.Authority);
231+
Assert.Equal(TestConstants.AadInstance, mergedOptions.Instance);
232+
Assert.Equal(TestConstants.TenantIdAsGuid, mergedOptions.TenantId);
233+
}
214234
}
215235
}

0 commit comments

Comments
 (0)