From 9940cb3cb2c9b1ab7310e62a9a9ad943edbad204 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 23 Jun 2014 15:31:45 -0700 Subject: [PATCH] Reenable unit test in its proper home --- .../IdentityBuilderExtensions.cs | 3 +- .../HttpSignInTest.cs | 54 +------------- .../HttpSignInTest.cs | 71 +++++++++++++++++++ ...rosoft.AspNet.Identity.InMemory.Test.kproj | 1 + .../project.json | 3 + 5 files changed, 79 insertions(+), 53 deletions(-) create mode 100644 test/Microsoft.AspNet.Identity.InMemory.Test/HttpSignInTest.cs diff --git a/src/Microsoft.AspNet.Identity.Authentication/IdentityBuilderExtensions.cs b/src/Microsoft.AspNet.Identity.Authentication/IdentityBuilderExtensions.cs index 2f56d6ccac..7edcbdcbd2 100644 --- a/src/Microsoft.AspNet.Identity.Authentication/IdentityBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Identity.Authentication/IdentityBuilderExtensions.cs @@ -8,8 +8,9 @@ namespace Microsoft.Framework.DependencyInjection { public static class IdentityBuilderExtensions { - public static IdentityBuilder AddHttpSignIn(this IdentityBuilder builder) + public static IdentityBuilder AddHttpSignIn(this IdentityBuilder builder) where TUser : class + where TRole : class { // todo: review should this be scoped? builder.Services.AddTransient(); diff --git a/test/Microsoft.AspNet.Identity.Authentication.Test/HttpSignInTest.cs b/test/Microsoft.AspNet.Identity.Authentication.Test/HttpSignInTest.cs index 1082c4ad19..3c850dbc1a 100644 --- a/test/Microsoft.AspNet.Identity.Authentication.Test/HttpSignInTest.cs +++ b/test/Microsoft.AspNet.Identity.Authentication.Test/HttpSignInTest.cs @@ -1,20 +1,16 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; -using System.Threading; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Security; using Microsoft.AspNet.Identity.Test; -using Microsoft.AspNet.Security.Cookies; using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.OptionsModel; using Moq; +using System; using System.Security.Claims; +using System.Threading; using System.Threading.Tasks; using Xunit; -using Microsoft.AspNet.Builder; namespace Microsoft.AspNet.Identity.Authentication.Test { @@ -23,52 +19,6 @@ namespace Microsoft.AspNet.Identity.Authentication.Test public class HttpSignInTest { #if NET45 - //[Theory] - //[InlineData(true)] - //[InlineData(false)] - //public async Task VerifyAccountControllerSignIn(bool isPersistent) - //{ - // IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); - // app.UseCookieAuthentication(new CookieAuthenticationOptions - // { - // AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie - // }); - - // var context = new Mock(); - // var response = new Mock(); - // context.Setup(c => c.Response).Returns(response.Object).Verifiable(); - // response.Setup(r => r.SignIn(It.IsAny(), It.Is(v => v.IsPersistent == isPersistent))).Verifiable(); - // var contextAccessor = new Mock>(); - // contextAccessor.Setup(a => a.Value).Returns(context.Object); - // app.UseServices(services => - // { - // services.Add(OptionsServices.GetDefaultServices()); - // services.AddInstance(contextAccessor.Object); - // services.AddIdentity(s => - // { - // s.AddInMemory(); - // }).AddHttpSignIn(); - // }); - - // // Act - // var user = new ApplicationUser - // { - // UserName = "Yolo" - // }; - // const string password = "Yol0Sw@g!"; - // var userManager = app.ApplicationServices.GetService>(); - // var signInManager = app.ApplicationServices.GetService>(); - - // IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); - // var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false); - - // // Assert - // Assert.Equal(SignInStatus.Success, result); - // context.VerifyAll(); - // response.VerifyAll(); - // contextAccessor.VerifyAll(); - //} - //[Theory] //[InlineData(true)] //[InlineData(false)] diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/HttpSignInTest.cs b/test/Microsoft.AspNet.Identity.InMemory.Test/HttpSignInTest.cs new file mode 100644 index 0000000000..cd5571c1be --- /dev/null +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/HttpSignInTest.cs @@ -0,0 +1,71 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Http; +using Microsoft.AspNet.Http.Security; +using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Identity.Test; +using Microsoft.AspNet.Security.Cookies; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; +using Moq; +using System.Security.Claims; +using System.Threading.Tasks; +using Xunit; + +namespace Microsoft.AspNet.Identity.InMemory.Test +{ + public class ApplicationUser : IdentityUser { } + + public class HttpSignInTest + { +#if NET45 + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task VerifyAccountControllerSignIn(bool isPersistent) + { + IBuilder app = new Builder.Builder(new ServiceCollection().BuildServiceProvider()); + app.UseCookieAuthentication(new CookieAuthenticationOptions + { + AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie + }); + + var context = new Mock(); + var response = new Mock(); + context.Setup(c => c.Response).Returns(response.Object).Verifiable(); + response.Setup(r => r.SignIn(It.IsAny(), It.Is(v => v.IsPersistent == isPersistent))).Verifiable(); + var contextAccessor = new Mock>(); + contextAccessor.Setup(a => a.Value).Returns(context.Object); + app.UseServices(services => + { + services.AddInstance(contextAccessor.Object); + services.AddIdentity(s => + { + s.AddInMemory(); + }).AddHttpSignIn(); + }); + + // Act + var user = new ApplicationUser + { + UserName = "Yolo" + }; + const string password = "Yol0Sw@g!"; + var userManager = app.ApplicationServices.GetService>(); + var signInManager = app.ApplicationServices.GetService>(); + + IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); + var result = await signInManager.PasswordSignInAsync(user.UserName, password, isPersistent, false); + + // Assert + Assert.Equal(SignInStatus.Success, result); + context.VerifyAll(); + response.VerifyAll(); + contextAccessor.VerifyAll(); + } +#endif + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/Microsoft.AspNet.Identity.InMemory.Test.kproj b/test/Microsoft.AspNet.Identity.InMemory.Test/Microsoft.AspNet.Identity.InMemory.Test.kproj index e73267a5a0..1e5d80d1e5 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/Microsoft.AspNet.Identity.InMemory.Test.kproj +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/Microsoft.AspNet.Identity.InMemory.Test.kproj @@ -21,6 +21,7 @@ + diff --git a/test/Microsoft.AspNet.Identity.InMemory.Test/project.json b/test/Microsoft.AspNet.Identity.InMemory.Test/project.json index 3f668b4d65..a6919e1efc 100644 --- a/test/Microsoft.AspNet.Identity.InMemory.Test/project.json +++ b/test/Microsoft.AspNet.Identity.InMemory.Test/project.json @@ -2,8 +2,11 @@ "dependencies": { "Microsoft.AspNet.Http" : "1.0.0-*", "Microsoft.AspNet.Identity" : "", + "Microsoft.AspNet.Identity.Authentication" : "", "Microsoft.AspNet.PipelineCore" : "1.0.0-*", "Microsoft.AspNet.RequestContainer" : "1.0.0-*", + "Microsoft.AspNet.Security" : "1.0.0-*", + "Microsoft.AspNet.Security.Cookies" : "1.0.0-*", "Microsoft.AspNet.Testing" : "1.0.0-*", "Microsoft.Framework.ConfigurationModel": "1.0.0-*", "Microsoft.Framework.DependencyInjection" : "1.0.0-*",