Exemple #1
0
        public async Task AddUserAsync_EmailExists_ReturnsError()
        {
            using (var dbContext = TestHelpers.CreateDbContext())
                using (var userManager = TestHelpers.CreateUserManager(dbContext))
                    using (var roleManager = TestHelpers.CreateRoleManager(dbContext))
                    {
                        // Arrange
                        var userManagementService = new UserManagementService(dbContext, userManager, roleManager);

                        const string ROLE_NAME = "administrator";
                        const string NEW_EMAIL = "*****@*****.**";

                        var testRole = new IdentityRole(ROLE_NAME);
                        await roleManager.CreateAsync(testRole);

                        var testUsers = TestHelpers.GetTestUsers();

                        ApplicationUser user1 = testUsers[0];
                        await userManager.CreateAsync(user1, ACCEPTABLE_PASSWORD);

                        await userManager.AddToRoleAsync(user1, ROLE_NAME);

                        ApplicationUser user2 = testUsers[1];
                        user2.Email = NEW_EMAIL;

                        // Act
                        var identityResult = await userManagementService.AddUserAsync(user2, ACCEPTABLE_PASSWORD, ROLE_NAME);

                        // Assert
                        Assert.False(identityResult.Succeeded);
                    }
        }
Exemple #2
0
        public async Task AddUserAsync_EmailUnique_AddsUserToRole()
        {
            using (var dbContext = TestHelpers.CreateDbContext())
                using (var userManager = TestHelpers.CreateUserManager(dbContext))
                    using (var roleManager = TestHelpers.CreateRoleManager(dbContext))
                    {
                        // Arrange
                        var userManagementService = new UserManagementService(dbContext, userManager, roleManager);

                        const string ROLE_NAME = "administrator";
                        var          testRole  = new IdentityRole(ROLE_NAME);
                        await roleManager.CreateAsync(testRole);

                        var testUsers = TestHelpers.GetTestUsers();

                        ApplicationUser user = testUsers[0];

                        // Act
                        await userManagementService.AddUserAsync(user, ACCEPTABLE_PASSWORD, ROLE_NAME);

                        // Assert
                        Assert.True(await userManager.IsInRoleAsync(user, ROLE_NAME));
                    }
        }