// 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.Threading; using System.Threading.Tasks; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.OptionsModel; using Moq; using System; namespace Microsoft.AspNet.Identity.Test { public static class MockHelpers { public static UserManager CreateManager(Func> storeFunc) where TUser : class { var services = new ServiceCollection(); services.Add(OptionsServices.GetDefaultServices()); services.AddIdentity(b => b.AddUserStore(storeFunc)); services.SetupOptions(options => { options.Password.RequireDigit = false; options.Password.RequireLowercase = false; options.Password.RequireNonLetterOrDigit = false; options.Password.RequireUppercase = false; options.User.AllowOnlyAlphanumericNames = false; }); return services.BuildServiceProvider().GetService>(); } public static Mock> MockUserManager() where TUser : class { var store = new Mock>(); var options = new OptionsAccessor(null); return new Mock>(new ServiceCollection().BuildServiceProvider(), store.Object, options); } public static UserManager TestUserManager() where TUser : class { return TestUserManager(new Mock>().Object); } public static UserManager TestUserManager(IUserStore store) where TUser : class { var options = new OptionsAccessor(null); var validator = new Mock>(); var userManager = new UserManager(new ServiceCollection().BuildServiceProvider(), store, options); validator.Setup(v => v.ValidateAsync(userManager, It.IsAny(), CancellationToken.None)).Returns(Task.FromResult(IdentityResult.Success)).Verifiable(); userManager.UserValidator = validator.Object; return userManager; } } }