public async Task AuthenticateAsync_ValidationException_401() { m_requestAuthenticator .Setup(ra => ra.AuthenticateAsync(It.IsAny <HttpRequestMessage>())) .Throws(new ExpiredTokenException()); await m_authenticationFilter .AuthenticateAsync(m_authenticationContext, new CancellationToken()) .SafeAsync(); Assert.IsNull(m_authenticationContext.Principal); Assert.AreEqual(typeof(AuthenticationFailureResult), m_authenticationContext.ErrorResult.GetType()); }
public async Task AuthenticateAsync_Traces_DidNothing() { // Arrange CancellationToken cancellationToken = CreateCancellationToken(); IAuthenticationFilter filter = CreateStubFilter(); TraceRecord record = null; ITraceWriter tracer = CreateTracer((r) => { record = r; }); IAuthenticationFilter product = CreateProductUnderTest(filter, tracer); const string expectedMessage = "The authentication filter did not encounter an error or set a principal."; using (HttpRequestMessage expectedRequest = CreateRequest()) { HttpAuthenticationContext authenticationContext = CreateAuthenticationContext(expectedRequest); // Act await product.AuthenticateAsync(authenticationContext, cancellationToken); // Assert Assert.NotNull(record); Assert.Same(expectedRequest, record.Request); Assert.Same(TraceCategories.FiltersCategory, record.Category); Assert.Equal(TraceLevel.Info, record.Level); Assert.Equal(TraceKind.End, record.Kind); Assert.Equal(filter.GetType().Name, record.Operator); Assert.Equal("AuthenticateAsync", record.Operation); Assert.Null(record.Exception); Assert.Equal(expectedMessage, record.Message); } }
/// <inheritdoc /> public Task AuthenticateAsync( HttpAuthenticationContext context, CancellationToken cancellationToken ) { return(_innerFilter.AuthenticateAsync(context, cancellationToken)); }
public void AuthenticateAsync_Traces() { // Arrange CancellationToken cancellationToken = CreateCancellationToken(); IAuthenticationFilter filter = CreateStubFilter(); TraceRecord record = null; ITraceWriter tracer = CreateTracer((r) => { record = r; }); IAuthenticationFilter product = CreateProductUnderTest(filter, tracer); using (HttpRequestMessage expectedRequest = CreateRequest()) { HttpAuthenticationContext authenticationContext = CreateAuthenticationContext(expectedRequest); // Act Task task = product.AuthenticateAsync(authenticationContext, cancellationToken); // Assert Assert.NotNull(task); task.Wait(); Assert.NotNull(record); Assert.Same(expectedRequest, record.Request); Assert.Same(TraceCategories.FiltersCategory, record.Category); Assert.Equal(TraceLevel.Info, record.Level); Assert.Equal(TraceKind.End, record.Kind); Assert.Equal(filter.GetType().Name, record.Operator); Assert.Equal("AuthenticateAsync", record.Operation); Assert.Null(record.Exception); Assert.Null(record.Message); } }
public async Task AuthenticateAsync_DelegatesToInnerFilter() { // Arrange HttpAuthenticationContext expectedAuthenticationContext = CreateAuthenticationContext(); CancellationToken expectedCancellationToken = CreateCancellationToken(); Mock <IAuthenticationFilter> mock = new Mock <IAuthenticationFilter>(); int calls = 0; mock.Setup( f => f.AuthenticateAsync( expectedAuthenticationContext, expectedCancellationToken ) ) .Callback( () => { calls++; } ) .Returns(() => Task.FromResult <object>(null)); IAuthenticationFilter filter = mock.Object; ITraceWriter tracer = CreateStubTracer(); IAuthenticationFilter product = CreateProductUnderTest(filter, tracer); // Act await product.AuthenticateAsync( expectedAuthenticationContext, expectedCancellationToken ); // Assert Assert.Equal(1, calls); }
public async Task AuthenticateAsync_SetsNoPrincipalOrError_WhenOwinAuthenticateReturnsNullIdentity() { // Arrange string authenticationType = "AuthenticationType"; IAuthenticationFilter filter = CreateProductUnderTest(authenticationType); IIdentity expectedIdentity = CreateDummyIdentity(); IAuthenticationManager authenticationManager = CreateAuthenticationManager( (ignore1) => Task.FromResult(new AuthenticateResult(null, new AuthenticationProperties(), new AuthenticationDescription()))); IOwinContext owinContext = CreateOwinContext(authenticationManager); IPrincipal expectedPrincipal = CreateDummyPrincipal(); HttpAuthenticationContext context; using (HttpRequestMessage request = CreateRequest(owinContext)) { context = CreateAuthenticationContext(request, expectedPrincipal); // Act await filter.AuthenticateAsync(context, CancellationToken.None); } // Assert Assert.Null(context.ErrorResult); Assert.Same(expectedPrincipal, context.Principal); }
public Task AuthenticateAsync_Throws_WhenContextIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); // Act & Assert return(Assert.ThrowsArgumentNullAsync( () => filter.AuthenticateAsync(null, CancellationToken.None), "context")); }
public void AuthenticateAsync_Throws_WhenContextIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); // Act & Assert Assert.ThrowsArgumentNull(() => { filter.AuthenticateAsync(null, CancellationToken.None).ThrowIfFaulted(); }, "context"); }
public async Task AuthenticateAsync_Traces_ErrorResult() { // Arrange IHttpActionResult result = new AuthenticationFailureResult(); CancellationToken cancellationToken = CreateCancellationToken(); Mock <IAuthenticationFilter> filterMock = new Mock <IAuthenticationFilter>(); filterMock .Setup( f => f.AuthenticateAsync( It.IsAny <HttpAuthenticationContext>(), It.IsAny <CancellationToken>() ) ) .Callback( (HttpAuthenticationContext context, CancellationToken token) => context.ErrorResult = result ) .Returns(Task.FromResult <object>(null)); TraceRecord record = null; ITraceWriter tracer = CreateTracer( (r) => { record = r; } ); IAuthenticationFilter product = CreateProductUnderTest(filterMock.Object, tracer); const string expectedMessage = "The authentication filter encountered an error. ErrorResult=" + "'System.Web.Http.Tracing.Tracers.AuthenticationFilterTracerTests+AuthenticationFailureResult'."; using (HttpRequestMessage expectedRequest = CreateRequest()) { HttpAuthenticationContext authenticationContext = CreateAuthenticationContext( expectedRequest ); // Act await product.AuthenticateAsync(authenticationContext, cancellationToken); // Assert Assert.NotNull(record); Assert.Same(expectedRequest, record.Request); Assert.Same(TraceCategories.FiltersCategory, record.Category); Assert.Equal(TraceLevel.Info, record.Level); Assert.Equal(TraceKind.End, record.Kind); Assert.Equal(filterMock.Object.GetType().Name, record.Operator); Assert.Equal("AuthenticateAsync", record.Operation); Assert.Null(record.Exception); Assert.Equal(expectedMessage, record.Message); } }
public Task AuthenticateAsync_Throws_WhenRequestIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); HttpAuthenticationContext context = CreateAuthenticationContext(); Assert.Null(context.Request); // Act & Assert return(Assert.ThrowsAsync <InvalidOperationException>( () => filter.AuthenticateAsync(context, CancellationToken.None), "HttpAuthenticationContext.Request must not be null.")); }
public async Task AuthenticateAsync_Traces(bool withIdentity, string expectedMessage) { // Arrange CancellationToken cancellationToken = CreateCancellationToken(); Mock <IAuthenticationFilter> filterMock = new Mock <IAuthenticationFilter>(); filterMock .Setup( f => f.AuthenticateAsync( It.IsAny <HttpAuthenticationContext>(), It.IsAny <CancellationToken>() ) ) .Callback( (HttpAuthenticationContext context, CancellationToken token) => context.Principal = CreateDummyPrincipal(withIdentity) ) .Returns(Task.FromResult <object>(null)); IAuthenticationFilter filter = filterMock.Object; TraceRecord record = null; ITraceWriter tracer = CreateTracer( (r) => { record = r; } ); IAuthenticationFilter product = CreateProductUnderTest(filter, tracer); using (HttpRequestMessage expectedRequest = CreateRequest()) { HttpAuthenticationContext authenticationContext = CreateAuthenticationContext( expectedRequest, isPrincipalSet: false ); // Act await product.AuthenticateAsync(authenticationContext, cancellationToken); // Assert Assert.NotNull(record); Assert.Same(expectedRequest, record.Request); Assert.Same(TraceCategories.FiltersCategory, record.Category); Assert.Equal(TraceLevel.Info, record.Level); Assert.Equal(TraceKind.End, record.Kind); Assert.Equal(filter.GetType().Name, record.Operator); Assert.Equal("AuthenticateAsync", record.Operation); Assert.Null(record.Exception); Assert.Equal(expectedMessage, record.Message); } }
public async Task AuthenticateAsync_ReturnsCanceledTask_WhenCancellationIsRequested() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); IOwinContext owinContext = CreateOwinContext(); using (HttpRequestMessage request = CreateRequest(owinContext)) { HttpAuthenticationContext context = CreateAuthenticationContext(request); CancellationToken cancellationToken = new CancellationToken(true); // Act & Assert await Assert.ThrowsAsync <OperationCanceledException>(() => filter.AuthenticateAsync(context, cancellationToken)); } }
public async Task AuthenticateAsync_Throws_WhenOwinContextIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); using (HttpRequestMessage request = CreateRequest()) { HttpAuthenticationContext context = CreateAuthenticationContext(request); // Act & Assert await Assert.ThrowsAsync <InvalidOperationException>( () => filter.AuthenticateAsync(context, CancellationToken.None), "No OWIN authentication manager is associated with the request."); } }
public async Task <HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { IHttpActionResult result = _innerResult; IPrincipal originalPrincipal = _principalService.GetCurrentPrincipal(_request); HttpAuthenticationContext authenticationContext = new HttpAuthenticationContext(_context, originalPrincipal); for (int i = 0; i < _filters.Length; i++) { IAuthenticationFilter filter = _filters[i]; await filter.AuthenticateAsync(authenticationContext, cancellationToken); IHttpActionResult error = authenticationContext.ErrorResult; // Short-circuit on the first authentication filter to provide an error result. if (error != null) { result = error; break; } } IPrincipal newPrincipal = authenticationContext.Principal; if (newPrincipal != originalPrincipal) { _principalService.SetCurrentPrincipal(newPrincipal, _request); } _controller.User = newPrincipal; // Run challenge on all filters (passing the result of each into the next). If a filter failed, the // challenges run on the failure result. If no filter failed, the challenges run on the original inner // result. HttpAuthenticationChallengeContext challengeContext = new HttpAuthenticationChallengeContext(_context, result); for (int i = 0; i < _filters.Length; i++) { IAuthenticationFilter filter = _filters[i]; await filter.ChallengeAsync(challengeContext, cancellationToken); } Contract.Assert(challengeContext.Result != null); result = challengeContext.Result; return(await result.ExecuteAsync(cancellationToken)); }
public void AuthenticateAsync_Throws_WhenRequestIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); HttpAuthenticationContext context = CreateAuthenticationContext(); Assert.Null(context.Request); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => { filter.AuthenticateAsync(context, CancellationToken.None).ThrowIfFaulted(); }); Assert.Equal("HttpAuthenticationContext.Request must not be null.", exception.Message); }
public async Task AuthenticateAsync_SetsClaimsPrincipal_WhenOwinAuthenticateReturnsIdentity() { // Arrange string authenticationType = "AuthenticationType"; IAuthenticationFilter filter = CreateProductUnderTest(authenticationType); IIdentity expectedIdentity = CreateDummyIdentity(); IAuthenticationManager authenticationManager = CreateAuthenticationManager( (a) => { AuthenticateResult result; if (a == authenticationType) { result = new AuthenticateResult( expectedIdentity, new AuthenticationProperties(), new AuthenticationDescription() ); } else { result = null; } return(Task.FromResult(result)); } ); IOwinContext owinContext = CreateOwinContext(authenticationManager); HttpAuthenticationContext context; using (HttpRequestMessage request = CreateRequest(owinContext)) { context = CreateAuthenticationContext(request); // Act await filter.AuthenticateAsync(context, CancellationToken.None); } // Assert Assert.Null(context.ErrorResult); IPrincipal principal = context.Principal; ClaimsPrincipal claimsPrincipal = Assert.IsType <ClaimsPrincipal>(principal); IIdentity identity = claimsPrincipal.Identity; Assert.Same(expectedIdentity, identity); }
public async Task AuthenticateAsync_Traces_WhenContextIsNull() { // Arrange CancellationToken cancellationToken = CreateCancellationToken(); IAuthenticationFilter filter = CreateStubFilter(); TraceRecord record = null; ITraceWriter tracer = CreateTracer((r) => { record = r; }); IAuthenticationFilter product = CreateProductUnderTest(filter, tracer); HttpAuthenticationContext authenticationContext = null; // Act await product.AuthenticateAsync(authenticationContext, cancellationToken); // Assert Assert.NotNull(record); Assert.Null(record.Request); }
public void AuthenticateAsync_Throws_WhenOwinContextIsNull() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); using (HttpRequestMessage request = CreateRequest()) { HttpAuthenticationContext context = CreateAuthenticationContext(request); // Act & Assert InvalidOperationException exception = Assert.Throws <InvalidOperationException>(() => { filter.AuthenticateAsync(context, CancellationToken.None).ThrowIfFaulted(); }); Assert.Equal("No OWIN authentication manager is associated with the request.", exception.Message); } }
public void AuthenticateAsync_DelegatesToInnerFilter() { // Arrange Task expectedTask = CreateTask(); HttpAuthenticationContext context = CreateAuthenticationContext(); CancellationToken cancellationToken = CreateCancellationToken(); Mock <IAuthenticationFilter> spyMock = new Mock <IAuthenticationFilter>(MockBehavior.Strict); spyMock.Setup(f => f.AuthenticateAsync(context, cancellationToken)).Returns(expectedTask); IAuthenticationFilter spy = spyMock.Object; IAuthenticationFilter product = CreateProductUnderTest(spy); // Act Task task = product.AuthenticateAsync(context, cancellationToken); // Assert Assert.Same(expectedTask, task); }
public void AuthenticateAsync_ReturnsCanceledTask_WhenCancellationIsRequested() { // Arrange IAuthenticationFilter filter = CreateProductUnderTest(); IOwinContext owinContext = CreateOwinContext(); Task task; using (HttpRequestMessage request = CreateRequest(owinContext)) { HttpAuthenticationContext context = CreateAuthenticationContext(request); CancellationToken cancellationToken = new CancellationToken(true); // Act task = filter.AuthenticateAsync(context, cancellationToken); // Assert task.WaitUntilCompleted(); } Assert.Equal(TaskStatus.Canceled, task.Status); }