-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Security.Claims; | ||
using Duende.AccessTokenManagement.OpenIdConnect; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NSubstitute; | ||
using Shouldly; | ||
|
||
namespace Duende.Bff.Blazor.UnitTests; | ||
|
||
public class ServerSideTokenStoreTests | ||
{ | ||
private ClaimsPrincipal CreatePrincipal(string sub, string sid) | ||
{ | ||
return new ClaimsPrincipal(new ClaimsIdentity([ | ||
new Claim("sub", sub), | ||
new Claim("sid", sid) | ||
], "pwd", "name", "role")); | ||
} | ||
|
||
[Fact] | ||
public async Task Can_add_retrieve_and_remove_tokens() | ||
{ | ||
var user = CreatePrincipal("sub", "sid"); | ||
var props = new AuthenticationProperties(); | ||
var expectedToken = new UserToken() | ||
{ | ||
AccessToken = "expected-access-token" | ||
}; | ||
|
||
// Create shared dependencies | ||
var sessionStore = new InMemoryUserSessionStore(); | ||
var dataProtection = new EphemeralDataProtectionProvider(); | ||
|
||
// Use the ticket store to save the user's initial session | ||
// Note that we don't yet have tokens in the session | ||
var sessionService = new ServerSideTicketStore(sessionStore, dataProtection, Substitute.For<ILogger<ServerSideTicketStore>>()); | ||
sessionService.StoreAsync(new AuthenticationTicket( | ||
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / macOS-latest
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / macOS-latest
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / Analyze (csharp)
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / Analyze (csharp)
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / ubuntu-latest
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / ubuntu-latest
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / windows-latest
Check warning on line 40 in test/Duende.Bff.Blazor.UnitTests/ServerSideTokenStoreTests.cs GitHub Actions / windows-latest
|
||
user, | ||
props, | ||
"test" | ||
)); | ||
|
||
var tokensInProps = MockStoreTokensInAuthProps(); | ||
var sut = new ServerSideTokenStore( | ||
tokensInProps, | ||
sessionStore, | ||
dataProtection, | ||
Substitute.For<ILogger<ServerSideTokenStore>>()); | ||
|
||
await sut.StoreTokenAsync(user, expectedToken); | ||
var actualToken = await sut.GetTokenAsync(user); | ||
|
||
actualToken.ShouldNotBe(null); | ||
actualToken.AccessToken.ShouldBe(expectedToken.AccessToken); | ||
|
||
await sut.ClearTokenAsync(user); | ||
|
||
var resultAfterClearing = await sut.GetTokenAsync(user); | ||
resultAfterClearing.AccessToken.ShouldBeNull(); | ||
} | ||
|
||
private static StoreTokensInAuthenticationProperties MockStoreTokensInAuthProps() | ||
{ | ||
var tokenManagementOptionsMonitor = Substitute.For<IOptionsMonitor<UserTokenManagementOptions>>(); | ||
var tokenManagementOptions = new UserTokenManagementOptions { UseChallengeSchemeScopedTokens = false }; | ||
tokenManagementOptionsMonitor.CurrentValue.Returns(tokenManagementOptions); | ||
|
||
var cookieOptionsMonitor = Substitute.For<IOptionsMonitor<CookieAuthenticationOptions>>(); | ||
var cookieAuthenticationOptions = new CookieAuthenticationOptions(); | ||
cookieOptionsMonitor.CurrentValue.Returns(cookieAuthenticationOptions); | ||
|
||
var schemeProvider = Substitute.For<IAuthenticationSchemeProvider>(); | ||
schemeProvider.GetDefaultSignInSchemeAsync().Returns(new AuthenticationScheme("TestScheme", null, typeof(IAuthenticationHandler))); | ||
|
||
return new StoreTokensInAuthenticationProperties( | ||
tokenManagementOptionsMonitor, | ||
cookieOptionsMonitor, | ||
schemeProvider, | ||
Substitute.For<ILogger<StoreTokensInAuthenticationProperties>>()); | ||
} | ||
} |