public async Task MarkUserAsAuthenticatedTest() { LocalStorageServiceMock localStorageService = new LocalStorageServiceMock(); ApiConnectorMock mock = new ApiConnectorMock(); CustomAuthenticationStateProvider authenticationStateProvider = new CustomAuthenticationStateProvider(localStorageService, mock, navigationManagerMock); bool wasStateChanged = false; authenticationStateProvider.AuthenticationStateChanged += async(result) => { wasStateChanged = true; AuthenticationState state = await result; Assert.IsNotNull(state.User); Assert.AreEqual(user.UserId.ToString(), state.User.Identity.Name); }; await authenticationStateProvider.MarkUserAsAuthenticated(user); Assert.IsTrue(wasStateChanged); Assert.AreSame(user, mock.CurrentUser); Assert.AreEqual(user.AccessToken, localStorageService.SetItems["accessToken"]); Assert.AreEqual(user.RefreshToken, localStorageService.SetItems["refreshToken"]); }
public async Task MarkUserAsLoggedOutTest() { LocalStorageServiceMock localStorageService = new LocalStorageServiceMock(); ApiConnectorMock mock = new ApiConnectorMock(); CustomAuthenticationStateProvider authenticationStateProvider = new CustomAuthenticationStateProvider(localStorageService, mock, navigationManagerMock); bool wasStateChanged = false; authenticationStateProvider.AuthenticationStateChanged += async(result) => { wasStateChanged = true; AuthenticationState state = await result; Assert.IsNull(state.User.Identity.Name); }; await authenticationStateProvider.MarkUserAsAuthenticated(user); await authenticationStateProvider.MarkUserAsLoggedOut(); Assert.IsTrue(wasStateChanged); Assert.IsNull(mock.CurrentUser); Assert.IsFalse(localStorageService.SetItems.ContainsKey("accessToken")); Assert.IsFalse(localStorageService.SetItems.ContainsKey("refreshToken")); }