// 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.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection.Fallback; using Microsoft.Framework.OptionsModel; using System.Collections.Generic; using Xunit; using System; using System.Threading; using System.Threading.Tasks; using System.Linq; using Microsoft.Framework.Logging; namespace Microsoft.AspNet.Identity.Test { public class IdentityBuilderTest { [Fact] public void CanOverrideUserStore() { var services = new ServiceCollection(); services.AddIdentity().AddUserStore(); var thingy = services.BuildServiceProvider().GetRequiredService>() as MyUberThingy; Assert.NotNull(thingy); } [Fact] public void CanOverrideRoleStore() { var services = new ServiceCollection(); services.AddIdentity().AddRoleStore(); var thingy = services.BuildServiceProvider().GetRequiredService>() as MyUberThingy; Assert.NotNull(thingy); } [Fact] public void CanOverrideRoleValidator() { var services = new ServiceCollection(); services.AddIdentity().AddRoleValidator(); var thingy = services.BuildServiceProvider().GetRequiredService>() as MyUberThingy; Assert.NotNull(thingy); } [Fact] public void CanOverrideUserValidator() { var services = new ServiceCollection(); services.AddIdentity().AddUserValidator(); var thingy = services.BuildServiceProvider().GetRequiredService>() as MyUberThingy; Assert.NotNull(thingy); } [Fact] public void CanOverridePasswordValidator() { var services = new ServiceCollection(); services.AddIdentity().AddPasswordValidator(); var thingy = services.BuildServiceProvider().GetRequiredService>() as MyUberThingy; Assert.NotNull(thingy); } [Fact] public void CanOverrideUserManager() { var services = new ServiceCollection(); services.AddIdentity() .AddUserStore() .AddUserManager(); var myUserManager = services.BuildServiceProvider().GetRequiredService(typeof(UserManager)) as MyUserManager; Assert.NotNull(myUserManager); } [Fact] public void CanOverrideRoleManager() { var services = new ServiceCollection(); services.AddIdentity() .AddRoleStore() .AddRoleManager(); var myRoleManager = services.BuildServiceProvider().GetRequiredService>() as MyRoleManager; Assert.NotNull(myRoleManager); } [Fact] public void EnsureDefaultServices() { var services = new ServiceCollection(); services.AddIdentity(); var provider = services.BuildServiceProvider(); var userValidator = provider.GetRequiredService>() as UserValidator; Assert.NotNull(userValidator); var pwdValidator = provider.GetRequiredService>() as PasswordValidator; Assert.NotNull(pwdValidator); var hasher = provider.GetRequiredService>() as PasswordHasher; Assert.NotNull(hasher); } [Fact] public void EnsureDefaultTokenProviders() { var services = new ServiceCollection(); services.AddIdentity().AddDefaultTokenProviders(); var provider = services.BuildServiceProvider(); var tokenProviders = provider.GetRequiredService>>(); Assert.Equal(3, tokenProviders.Count()); } private class MyUberThingy : IUserValidator, IPasswordValidator, IRoleValidator, IUserStore, IRoleStore { public Task CreateAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task CreateAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task DeleteAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task DeleteAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public void Dispose() { throw new NotImplementedException(); } public Task FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetNormalizedRoleNameAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetNormalizedUserNameAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetRoleIdAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetRoleNameAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetUserIdAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task GetUserNameAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task SetNormalizedRoleNameAsync(IdentityRole role, string normalizedName, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task SetNormalizedUserNameAsync(IdentityUser user, string normalizedName, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task SetRoleNameAsync(IdentityRole role, string roleName, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task SetUserNameAsync(IdentityUser user, string userName, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task UpdateAsync(IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task UpdateAsync(IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task ValidateAsync(RoleManager manager, IdentityRole role, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task ValidateAsync(UserManager manager, IdentityUser user, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } public Task ValidateAsync(UserManager manager, IdentityUser user, string password, CancellationToken cancellationToken = default(CancellationToken)) { throw new NotImplementedException(); } Task IRoleStore.FindByIdAsync(string roleId, CancellationToken cancellationToken) { throw new NotImplementedException(); } Task IRoleStore.FindByNameAsync(string roleName, CancellationToken cancellationToken) { throw new NotImplementedException(); } } private class MyUserManager : UserManager { public MyUserManager(IUserStore store) : base(store) { } } private class MyRoleManager : RoleManager { public MyRoleManager(IRoleStore store, IEnumerable> roleValidators) : base(store) { } } } }