From 19d49b06a68491caffe962205bdbc177a8bcd25c Mon Sep 17 00:00:00 2001 From: Chris Ross Date: Fri, 25 Apr 2014 14:30:56 -0700 Subject: [PATCH] #53 - Add Auth unit tests. --- .../DefaultHttpContextTests.cs | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs index 4eb0b87d9e..6701e62863 100644 --- a/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs +++ b/test/Microsoft.AspNet.PipelineCore.Tests/DefaultHttpContextTests.cs @@ -1,8 +1,9 @@ using System; using System.Collections.Generic; -using System.Globalization; -using System.Security.Claims; +using System.IO; using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; using Microsoft.AspNet.Abstractions; using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.HttpFeature; @@ -39,5 +40,62 @@ namespace Microsoft.AspNet.PipelineCore.Tests Assert.Equal("SomeAuthType", context.User.Identity.AuthenticationType); Assert.True(context.User.Identity.IsAuthenticated); } + + [Fact] + public async Task AuthenticateWithNoAuthMiddlewareThrows() + { + var context = CreateContext(); + Assert.Throws(() => context.Authenticate("Foo")); + await Assert.ThrowsAsync(async () => await context.AuthenticateAsync("Foo")); + } + + [Fact] + public void ChallengeWithNoAuthMiddlewareMayThrow() + { + var context = CreateContext(); + context.Response.Challenge(); + Assert.Equal(401, context.Response.StatusCode); + + Assert.Throws(() => context.Response.Challenge("Foo")); + } + + [Fact] + public void SignInWithNoAuthMiddlewareThrows() + { + var context = CreateContext(); + Assert.Throws(() => context.Response.SignIn(new ClaimsIdentity("Foo"))); + } + + [Fact] + public void SignOutWithNoAuthMiddlewareMayThrow() + { + var context = CreateContext(); + context.Response.SignOut(); + + Assert.Throws(() => context.Response.SignOut("Foo")); + } + + private HttpContext CreateContext() + { + var context = new DefaultHttpContext(new FeatureCollection()); + context.SetFeature(new FakeHttpResponse()); + return context; + } + + private class FakeHttpResponse : IHttpResponseInformation + { + public int StatusCode { get; set; } + + public string ReasonPhrase { get; set; } + + public IDictionary Headers { get; set; } + + public Stream Body { get; set; } + + public void OnSendingHeaders(Action callback, object state) + { + throw new NotImplementedException(); + } + } } } \ No newline at end of file