// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Diagnostics; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder.Internal; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.AspNet.Identity.Test; using Microsoft.Framework.DependencyInjection; using Microsoft.Dnx.Runtime.Infrastructure; using Moq; using Xunit; namespace Microsoft.AspNet.Identity.InMemory.Test { public class HttpSignInTest { [Theory] [InlineData(true)] [InlineData(false)] public async Task VerifyAccountControllerSignIn(bool isPersistent) { var app = new ApplicationBuilder(CallContextServiceLocator.Locator.ServiceProvider); app.UseCookieAuthentication(); var context = new Mock(); var auth = new Mock(); context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable(); auth.Setup(a => a.SignInAsync(IdentityOptions.ApplicationCookieAuthenticationScheme, It.IsAny(), It.IsAny())).Returns(Task.FromResult(0)).Verifiable(); // REVIEW: is persistant mocking broken //It.Is(v => v.IsPersistent == isPersistent))).Returns(Task.FromResult(0)).Verifiable(); var contextAccessor = new Mock(); contextAccessor.Setup(a => a.HttpContext).Returns(context.Object); var services = new ServiceCollection(); services.AddLogging(); services.AddInstance(contextAccessor.Object); services.AddIdentity(); services.AddSingleton, InMemoryUserStore>(); services.AddSingleton, InMemoryRoleStore>(); app.ApplicationServices = services.BuildServiceProvider(); // Act var user = new TestUser { UserName = "Yolo" }; const string password = "Yol0Sw@g!"; var userManager = app.ApplicationServices.GetRequiredService>(); var signInManager = app.ApplicationServices.GetRequiredService>(); IdentityResultAssert.IsSuccess(await userManager.CreateAsync(user, password)); var result = await signInManager.PasswordSignInAsync(user, password, isPersistent, false); // Assert Assert.True(result.Succeeded); context.VerifyAll(); auth.VerifyAll(); contextAccessor.VerifyAll(); } } }