// 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.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Hosting; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.OptionsModel; using Moq; namespace Microsoft.AspNet.Identity.Test { public static class MockHelpers { //public static UserManager CreateManager(IUserStore store) where TUser : class //{ // var services = new ServiceCollection(); // services.Add(OptionsServices.GetDefaultServices()); // services.Add(HostingServices.GetDefaultServices()); // services.AddDefaultIdentity().AddUserStore(store); // services.ConfigureIdentity(options => // { // options.Password.RequireDigit = false; // options.Password.RequireLowercase = false; // options.Password.RequireNonLetterOrDigit = false; // options.Password.RequireUppercase = false; // options.User.UserNameValidationRegex = null; // }); // return services.BuildServiceProvider().GetService>(); //} public static Mock> MockUserManager() where TUser : class { var store = new Mock>(); var options = new OptionsManager(null); return new Mock>( store.Object, options, new PasswordHasher(new PasswordHasherOptionsAccessor()), new UserValidator(), new PasswordValidator(), new UpperInvariantUserNameNormalizer(), new List>(), new List()); } public static Mock> MockRoleManager() where TRole : class { var store = new Mock>(); return new Mock>(store.Object, new RoleValidator()); } public static UserManager TestUserManager() where TUser : class { return TestUserManager(new Mock>().Object); } public static UserManager TestUserManager(IUserStore store) where TUser : class { var options = new OptionsManager(null); var validator = new Mock>(); var userManager = new UserManager(store, options, new PasswordHasher(new PasswordHasherOptionsAccessor()), validator.Object, new PasswordValidator(), new UpperInvariantUserNameNormalizer(), null, null); validator.Setup(v => v.ValidateAsync(userManager, It.IsAny(), CancellationToken.None)) .Returns(Task.FromResult(IdentityResult.Success)).Verifiable(); return userManager; } } }