// 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.Testing; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; using Microsoft.Data.Entity.Storage; using Microsoft.Data.Entity.InMemory; using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using System; using System.Linq; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; using Xunit; namespace Microsoft.AspNet.Identity.Entity.Test { public static class TestIdentityFactory { public static IdentityContext CreateContext() { var services = new ServiceCollection(); //#if NET45 // services.AddEntityFramework().AddSqlServer(); //#else services.AddEntityFramework().AddInMemoryStore(); //#endif var serviceProvider = services.BuildServiceProvider(); var db = new IdentityContext(serviceProvider); // TODO: Recreate DB, doesn't support String ID or Identity context yet if (!db.Database.Exists()) { db.Database.Create(); } // TODO: CreateAsync DB? return db; } public class TestSetup : IOptionsSetup { private readonly IdentityOptions _options; public TestSetup(IdentityOptions options) { _options = options; } public int Order { get { return 0; } } public void Setup(IdentityOptions options) { options.Copy(_options); } } public static UserManager CreateManager(DbContext context) { var services = new ServiceCollection(); services.AddTransient, UserValidator>(); services.AddTransient, PasswordValidator>(); services.AddInstance>(new InMemoryUserStore(context)); services.AddSingleton, UserManager>(); var options = new IdentityOptions { Password = new PasswordOptions { RequireDigit = false, RequireLowercase = false, RequireNonLetterOrDigit = false, RequireUppercase = false } }; var optionsAccessor = new OptionsAccessor(new[] { new TestSetup(options) }); //services.AddInstance>(new OptionsAccessor(new[] { new TestSetup(options) })); //return services.BuildServiceProvider().GetService>(); return new UserManager(services.BuildServiceProvider(), new InMemoryUserStore(context), optionsAccessor); } public static UserManager CreateManager() { return CreateManager(CreateContext()); } public static RoleManager CreateRoleManager(DbContext context) { var services = new ServiceCollection(); services.AddTransient, RoleValidator>(); services.AddInstance>(new EntityRoleStore(context)); // return services.BuildServiceProvider().GetService>(); return new RoleManager(services.BuildServiceProvider(), new EntityRoleStore(context)); } public static RoleManager CreateRoleManager() { return CreateRoleManager(CreateContext()); } } }