Reenable unit test in its proper home

This commit is contained in:
Hao Kung 2014-06-23 15:31:45 -07:00
parent a45d4fd338
commit 9940cb3cb2
5 changed files with 79 additions and 53 deletions

View File

@ -8,8 +8,9 @@ namespace Microsoft.Framework.DependencyInjection
{
public static class IdentityBuilderExtensions
{
public static IdentityBuilder<TUser, IdentityRole> AddHttpSignIn<TUser>(this IdentityBuilder<TUser, IdentityRole> builder)
public static IdentityBuilder<TUser, TRole> AddHttpSignIn<TUser, TRole>(this IdentityBuilder<TUser, TRole> builder)
where TUser : class
where TRole : class
{
// todo: review should this be scoped?
builder.Services.AddTransient<IAuthenticationManager, HttpAuthenticationManager>();

View File

@ -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<HttpContext>();
// var response = new Mock<HttpResponse>();
// context.Setup(c => c.Response).Returns(response.Object).Verifiable();
// response.Setup(r => r.SignIn(It.IsAny<ClaimsIdentity>(), It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
// var contextAccessor = new Mock<IContextAccessor<HttpContext>>();
// contextAccessor.Setup(a => a.Value).Returns(context.Object);
// app.UseServices(services =>
// {
// services.Add(OptionsServices.GetDefaultServices());
// services.AddInstance(contextAccessor.Object);
// services.AddIdentity<ApplicationUser, IdentityRole>(s =>
// {
// s.AddInMemory();
// }).AddHttpSignIn<ApplicationUser>();
// });
// // Act
// var user = new ApplicationUser
// {
// UserName = "Yolo"
// };
// const string password = "Yol0Sw@g!";
// var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();
// var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>();
// 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)]

View File

@ -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<HttpContext>();
var response = new Mock<HttpResponse>();
context.Setup(c => c.Response).Returns(response.Object).Verifiable();
response.Setup(r => r.SignIn(It.IsAny<ClaimsIdentity>(), It.Is<AuthenticationProperties>(v => v.IsPersistent == isPersistent))).Verifiable();
var contextAccessor = new Mock<IContextAccessor<HttpContext>>();
contextAccessor.Setup(a => a.Value).Returns(context.Object);
app.UseServices(services =>
{
services.AddInstance(contextAccessor.Object);
services.AddIdentity<ApplicationUser, IdentityRole>(s =>
{
s.AddInMemory();
}).AddHttpSignIn();
});
// Act
var user = new ApplicationUser
{
UserName = "Yolo"
};
const string password = "Yol0Sw@g!";
var userManager = app.ApplicationServices.GetService<UserManager<ApplicationUser>>();
var signInManager = app.ApplicationServices.GetService<SignInManager<ApplicationUser>>();
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
}
}

View File

@ -21,6 +21,7 @@
<Content Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="HttpSignInTest.cs" />
<Compile Include="IdentityBuilderExtensions.cs" />
<Compile Include="InMemoryRoleStore.cs" />
<Compile Include="InMemoryStoreTest.cs" />

View File

@ -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-*",