public void ShouldReturnOriginalStateWhenModificationIsPoppedViaDisposableTwice() { var originalState = new AuthenticationContextState { IsAuthenticated = true, SubjectId = Guid.Parse("ecf06726-3f30-45ca-b68d-1f5bd7fa5261") }; var sut = new AuthenticationContext <AuthenticationContextState>(originalState); var disposable = sut.SetContext(state => { state.IsAuthenticated = false; }); var newState = sut.GetState(); newState.ShouldNotBeNull(); newState.ShouldNotBeSameAs(originalState); newState.IsAuthenticated.ShouldBe(false); newState.SubjectId.ShouldBe(originalState.SubjectId); disposable.Dispose(); disposable.Dispose(); var poppedState = sut.GetState(); poppedState.ShouldNotBeNull(); poppedState.ShouldBeSameAs(originalState); poppedState.IsAuthenticated.ShouldBe(true); poppedState.SubjectId.ShouldBe(originalState.SubjectId); }
public async Task ShouldNotModifyTheAuthenticationContextWhenHttpContextIdentityIsNotAuthenticated() { // Setup var nullLogger = NullLogger <AuthenticationContextMiddleware <AuthenticationContextState> > .Instance; var sut = new AuthenticationContextMiddleware <AuthenticationContextState>(nullLogger, (state, _) => { state.SubjectId = Guid.Parse("77268032-39a0-488c-81de-493452fdc29b"); }); var originalState = new AuthenticationContextState { IsAuthenticated = false, SubjectId = Guid.Parse("a09a42bc-aae0-4e70-a4a2-f880d4c9ce6c") }; var authenticationContext = new AuthenticationContext <AuthenticationContextState>(originalState); var serviceProviderMock = new Mock <IServiceProvider>(); serviceProviderMock.Setup(x => x.GetService(typeof(IAuthenticationContext <AuthenticationContextState>))) .Returns(authenticationContext); var httpContextMock = new Mock <HttpContext>(); httpContextMock.Setup(x => x.User).Returns(new ClaimsPrincipal()); httpContextMock.Setup(x => x.RequestServices).Returns(serviceProviderMock.Object); var requestDelegate = new RequestDelegate(_ => { var inProgressState = authenticationContext.GetState(); inProgressState.ShouldBeSameAs(originalState); inProgressState.ShouldNotBeNull(); inProgressState.IsAuthenticated.ShouldBeFalse(); inProgressState.SubjectId.ShouldBe(Guid.Parse("a09a42bc-aae0-4e70-a4a2-f880d4c9ce6c")); return(Task.FromResult(0)); }); // Action await sut.InvokeAsync(httpContextMock.Object, requestDelegate); var newState = authenticationContext.GetState(); // Assert & Validate serviceProviderMock.Verify(x => x.GetService(typeof(IAuthenticationContext <AuthenticationContextState>)), Times.Never); newState.ShouldBeSameAs(originalState); newState.ShouldNotBeNull(); newState.IsAuthenticated.ShouldBeFalse(); newState.SubjectId.ShouldBe(Guid.Parse("a09a42bc-aae0-4e70-a4a2-f880d4c9ce6c")); }
public void ShouldReturnTheOriginalStateWhenStateHasNotBeenModified() { var originalState = new AuthenticationContextState { IsAuthenticated = true, SubjectId = Guid.Parse("ecf06726-3f30-45ca-b68d-1f5bd7fa5261") }; var sut = new AuthenticationContext <AuthenticationContextState>(originalState); var newState = sut.GetState(); newState.ShouldNotBeNull(); newState.ShouldBeSameAs(originalState); }