Skip to content

Commit ef6b94f

Browse files
Refactor SuperOffice tests
Refactor the tests to be less repetitive.
1 parent 2c024de commit ef6b94f

File tree

1 file changed

+8
-22
lines changed

1 file changed

+8
-22
lines changed

test/AspNet.Security.OAuth.Providers.Tests/SuperOffice/SuperOfficeTests.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66

77
using System;
88
using System.IdentityModel.Tokens.Jwt;
9-
using System.Security.Claims;
109
using System.Threading.Tasks;
11-
using AspNet.Security.OAuth.SuperOffice;
1210
using Microsoft.AspNetCore.Authentication;
13-
using Microsoft.AspNetCore.Http;
1411
using Microsoft.Extensions.DependencyInjection;
1512
using Microsoft.IdentityModel.Tokens;
13+
using Shouldly;
1614
using Xunit;
1715
using Xunit.Abstractions;
1816

@@ -29,9 +27,14 @@ public SuperOfficeTests(ITestOutputHelper outputHelper)
2927

3028
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
3129
{
30+
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
31+
3232
builder.AddSuperOffice(options =>
3333
{
3434
ConfigureDefaults(builder, options);
35+
36+
options.ClientId = "gg454918d75b1b53101065c16ee51123";
37+
options.TokenValidationParameters.ValidAudience = options.ClientId;
3538
});
3639
}
3740

@@ -52,13 +55,7 @@ public async Task Can_Sign_In_Using_SuperOffice(string claimType, string claimVa
5255
// Arrange
5356
static void ConfigureServices(IServiceCollection services)
5457
{
55-
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
56-
services.AddSingleton<JwtSecurityTokenHandler, MockJwtSecurityTokenHandler>();
57-
services.PostConfigureAll<SuperOfficeAuthenticationOptions>((options) =>
58-
{
59-
options.ClientId = "gg454918d75b1b53101065c16ee51123";
60-
options.TokenValidationParameters.ValidAudience = options.ClientId;
61-
});
58+
services.AddSingleton<JwtSecurityTokenHandler, FrozenJwtSecurityTokenHandler>();
6259
}
6360

6461
using var server = CreateTestServer(ConfigureServices);
@@ -79,12 +76,9 @@ public async Task Can_Sign_In_Using_SuperOffice_With_FunctionRights(string claim
7976
// Arrange
8077
static void ConfigureServices(IServiceCollection services)
8178
{
82-
Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
83-
services.AddSingleton<JwtSecurityTokenHandler, MockJwtSecurityTokenHandler>();
79+
services.AddSingleton<JwtSecurityTokenHandler, FrozenJwtSecurityTokenHandler>();
8480
services.PostConfigureAll<SuperOfficeAuthenticationOptions>((options) =>
8581
{
86-
options.ClientId = "gg454918d75b1b53101065c16ee51123";
87-
options.TokenValidationParameters.ValidAudience = options.ClientId;
8882
options.IncludeFunctionalRightsAsClaims = true;
8983
});
9084
}
@@ -97,13 +91,5 @@ static void ConfigureServices(IServiceCollection services)
9791
// Assert
9892
AssertClaim(claims, claimType, claimValue);
9993
}
100-
101-
private sealed class MockJwtSecurityTokenHandler : JwtSecurityTokenHandler
102-
{
103-
protected override void ValidateLifetime(DateTime? notBefore, DateTime? expires, JwtSecurityToken jwtToken, TokenValidationParameters validationParameters)
104-
{
105-
// Do not validate the lifetime as the test token has expired
106-
}
107-
}
10894
}
10995
}

0 commit comments

Comments
 (0)