|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Security.Claims; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.AspNetCore.Components.Server.Circuits; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace Microsoft.AspNetCore.Components.Server.Tests.Circuits |
| 11 | +{ |
| 12 | + public class ServerAuthenticationStateProviderTest |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public async Task CannotProvideAuthenticationStateBeforeInitialization() |
| 16 | + { |
| 17 | + await Assert.ThrowsAsync<InvalidOperationException>(() => |
| 18 | + new ServerAuthenticationStateProvider() |
| 19 | + .GetAuthenticationStateAsync()); |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public async Task SuppliesAuthenticationStateWithFixedUser() |
| 24 | + { |
| 25 | + // Arrange |
| 26 | + var user = new ClaimsPrincipal(); |
| 27 | + var provider = new ServerAuthenticationStateProvider(); |
| 28 | + |
| 29 | + // Act 1 |
| 30 | + var expectedAuthenticationState1 = new AuthenticationState(user); |
| 31 | + provider.SetAuthenticationState(Task.FromResult(expectedAuthenticationState1)); |
| 32 | + |
| 33 | + // Assert 1 |
| 34 | + var actualAuthenticationState1 = await provider.GetAuthenticationStateAsync(); |
| 35 | + Assert.NotNull(actualAuthenticationState1); |
| 36 | + Assert.Same(expectedAuthenticationState1, actualAuthenticationState1); |
| 37 | + |
| 38 | + // Act 2: Show we can update it further |
| 39 | + var expectedAuthenticationState2 = new AuthenticationState(user); |
| 40 | + provider.SetAuthenticationState(Task.FromResult(expectedAuthenticationState2)); |
| 41 | + |
| 42 | + // Assert 2 |
| 43 | + var actualAuthenticationState2 = await provider.GetAuthenticationStateAsync(); |
| 44 | + Assert.NotNull(actualAuthenticationState2); |
| 45 | + Assert.NotSame(actualAuthenticationState1, actualAuthenticationState2); |
| 46 | + Assert.Same(expectedAuthenticationState2, actualAuthenticationState2); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments