public static async Task <AppDbContext> GetMemoryContext() { const string inMemoryConnectionString = "DataSource=:memory:"; var connection = new SqliteConnection(inMemoryConnectionString); connection.Open(); var options = new DbContextOptionsBuilder <AppDbContext>() .UseSqlite(connection) .Options; var authUser = new Mock <ICurrentUserInfo>(MockBehavior.Strict); authUser.Setup(r => r.UserId).Returns(DefaultApplicationUsers.GetSuperUser().Id); authUser.Setup(r => r.UserName).Returns(DefaultApplicationUsers.GetSuperUser().UserName); authUser.Setup(r => r.Roles).Returns(DefaultApplicationRoles.GetDefaultRoles().Select(x => x.Name).ToList()); var dbContext = new AppDbContext(options, authUser.Object); await dbContext.Database.EnsureDeletedAsync(); await dbContext.Database.EnsureCreatedAsync(); return(dbContext); }
private static async Task SeedDefaultUserRolesAsync(UserManager <ApplicationUser> userManager, RoleManager <ApplicationRole> roleManager) { var defaultRoles = DefaultApplicationRoles.GetDefaultRoles(); if (!await roleManager.Roles.AnyAsync()) { foreach (var defaultRole in defaultRoles) { await roleManager.CreateAsync(defaultRole); } } if (!await roleManager.RoleExistsAsync(DefaultApplicationRoles.SuperAdmin)) { await roleManager.CreateAsync(new ApplicationRole(DefaultApplicationRoles.SuperAdmin)); } var defaultUser = DefaultApplicationUsers.GetSuperUser(); var userByName = await userManager.FindByNameAsync(defaultUser.UserName); var userByEmail = await userManager.FindByEmailAsync(defaultUser.Email); if (userByName == null && userByEmail == null) { await userManager.CreateAsync(defaultUser, "SuperAdmin"); foreach (var defaultRole in defaultRoles) { await userManager.AddToRoleAsync(defaultUser, defaultRole.Name); } } var role = await roleManager.FindByNameAsync(DefaultApplicationRoles.SuperAdmin); var rolePermissions = await roleManager.GetClaimsAsync(role); var allPermissions = PermissionHelper.GetPermissionClaims(); foreach (var permission in allPermissions) { if (rolePermissions.Any(x => x.Value == permission.Value && x.Type == permission.Type) == false) { await roleManager.AddClaimAsync(role, permission); } } }
private Mock <IApplicationRoleManager> GetMockApplicationRoleManager() { var applicationRoleManager = new Mock <IApplicationRoleManager>(MockBehavior.Strict); applicationRoleManager.Setup(x => x.Roles()) .Returns(DefaultApplicationRoles.GetDefaultRoles().AsQueryable()); applicationRoleManager.Setup(x => x.GetRoleAsync(It.IsAny <string>())) .ReturnsAsync((ApplicationRole)null); applicationRoleManager.Setup(x => x.AddRoleAsync(It.IsAny <ApplicationRole>())) .ReturnsAsync(IdentityResponse.Success("Succeeded")); applicationRoleManager.Setup(x => x.FindByIdAsync(It.IsAny <string>())) .ReturnsAsync(new ApplicationRole(DefaultApplicationRoles.SuperAdmin)); applicationRoleManager.Setup(x => x.GetClaimsAsync(It.IsAny <List <string> >())) .ReturnsAsync(PermissionHelper.GetPermissionClaims()); applicationRoleManager.Setup(x => x.GetClaimsAsync(It.IsAny <ApplicationRole>())) .ReturnsAsync(PermissionHelper.GetPermissionClaims()); applicationRoleManager.Setup(x => x.RemoveClaimAsync(It.IsAny <ApplicationRole>(), It.IsAny <Claim>())) .ReturnsAsync(IdentityResponse.Success("Succeeded")); applicationRoleManager.Setup(x => x.AddClaimAsync(It.IsAny <ApplicationRole>(), It.IsAny <Claim>())) .ReturnsAsync(IdentityResponse.Success("Succeeded")); return(applicationRoleManager); }