Пример #1
0
        public async Task Should_Remove_From_Role()
        {
            LoginAsHostAdmin();

            //Arrange
            var adminUser = await GetUserByUserNameOrNullAsync(User.AdminUserName);
            await UsingDbContextAsync(async context =>
            {
                var roleCount = await context.UserRoles.CountAsync(ur => ur.UserId == adminUser.Id);
                roleCount.ShouldBeGreaterThan(0); //There should be 1 role at least
            });

            //Act
            await UserAppService.CreateOrUpdateUser(
                new CreateOrUpdateUserInput
                {
                    User = new UserEditDto //Not changing user properties
                    {
                        Id = adminUser.Id,
                        EmailAddress = adminUser.EmailAddress,
                        Name = adminUser.Name,
                        Surname = adminUser.Surname,
                        UserName = adminUser.UserName,
                        Password = null
                    },
                    AssignedRoleNames = new string[0] //Just deleting all roles
                });

            //Assert
            await UsingDbContextAsync(async context =>
            {
                var roleCount = await context.UserRoles.CountAsync(ur => ur.UserId == adminUser.Id);
                roleCount.ShouldBe(0);
            });
        }
        public async Task Should_Not_Update_User_With_Duplicate_Username_Or_EmailAddress()
        {
            //Arrange

            CreateTestUsers();
            var jnashUser = await GetUserByUserNameOrNullAsync("jnash");

            //Act

            //Try to update with existing username
            var exception = await Assert.ThrowsAsync <UserFriendlyException>(async() =>
                                                                             await UserAppService.CreateOrUpdateUser(
                                                                                 new CreateOrUpdateUserInput
            {
                User = new UserEditDto
                {
                    Id           = jnashUser.Id,
                    EmailAddress = "*****@*****.**",
                    Name         = "John",
                    Surname      = "Nash",
                    UserName     = "******",            //Changed user name to an existing user
                    Password     = "******"
                },
                AssignedRoleNames = new string[0]
            }));

            exception.Message.ShouldContain("adams_d");

            //Try to update with existing email address
            exception = await Assert.ThrowsAsync <UserFriendlyException>(async() =>
                                                                         await UserAppService.CreateOrUpdateUser(
                                                                             new CreateOrUpdateUserInput
            {
                User = new UserEditDto
                {
                    Id           = jnashUser.Id,
                    EmailAddress = "*****@*****.**",                //Changed email to an existing user
                    Name         = "John",
                    Surname      = "Nash",
                    UserName     = "******",
                    Password     = "******"
                },
                AssignedRoleNames = new string[0]
            }));

            exception.Message.ShouldContain("*****@*****.**");
        }
        private async Task CreateUserAndTestAsync(string userName, string name, string surname, string emailAddress, int?tenantId, params string[] roleNames)
        {
            //Arrange
            AbpSession.TenantId = tenantId;

            //Act
            await UserAppService.CreateOrUpdateUser(
                new CreateOrUpdateUserInput
            {
                User = new UserEditDto
                {
                    EmailAddress = emailAddress,
                    Name         = name,
                    Surname      = surname,
                    UserName     = userName,
                    Password     = "******"
                },
                AssignedRoleNames = roleNames
            });

            //Assert
            await UsingDbContext(async context =>
            {
                //Get created user
                var createdUser = await context.Users.FirstOrDefaultAsync(u => u.UserName == userName);
                createdUser.ShouldNotBe(null);

                //Check some properties
                createdUser.EmailAddress.ShouldBe(emailAddress);
                createdUser.TenantId.ShouldBe(tenantId);

                //Check roles
                if (roleNames.IsNullOrEmpty())
                {
                    createdUser.Roles.Count.ShouldBe(0);
                }
                else
                {
                    createdUser.Roles.Count.ShouldBe(roleNames.Length);
                    foreach (var roleName in roleNames)
                    {
                        var roleId = (await GetRoleAsync(roleName)).Id;
                        createdUser.Roles.Any(ur => ur.RoleId == roleId && ur.TenantId == tenantId).ShouldBe(true);
                    }
                }
            });
        }
        public async Task Update_User_Basic_Tests()
        {
            //Arrange
            var managerRole = CreateRole("Manager");
            var adminUser   = await GetUserByUserNameOrNullAsync(User.AdminUserName);

            //Act
            await UserAppService.CreateOrUpdateUser(
                new CreateOrUpdateUserInput
            {
                User = new UserEditDto
                {
                    Id           = adminUser.Id,
                    EmailAddress = "*****@*****.**",
                    Name         = "System1",
                    Surname      = "Admin2",
                    Password     = "******",
                    UserName     = adminUser.UserName
                },
                AssignedRoleNames = new[] { "Manager" }
            });

            //Assert
            await UsingDbContextAsync(async context =>
            {
                //Get created user
                var updatedAdminUser = await GetUserByUserNameOrNullAsync(adminUser.UserName, includeRoles: true);
                updatedAdminUser.ShouldNotBe(null);
                updatedAdminUser.Id.ShouldBe(adminUser.Id);

                //Check some properties
                updatedAdminUser.EmailAddress.ShouldBe("*****@*****.**");
                updatedAdminUser.TenantId.ShouldBe(AbpSession.TenantId);

                LocalIocManager
                .Resolve <IPasswordHasher <User> >()
                .VerifyHashedPassword(updatedAdminUser, updatedAdminUser.Password, "123qwE*")
                .ShouldBe(PasswordVerificationResult.Success);

                //Check roles
                updatedAdminUser.Roles.Count.ShouldBe(1);
                updatedAdminUser.Roles.Any(ur => ur.RoleId == managerRole.Id).ShouldBe(true);
            });
        }
        public async Task Should_Not_Create_User_With_Duplicate_Username_Or_EmailAddress()
        {
            //Arrange
            CreateTestUsers();

            //Act
            await Assert.ThrowsAsync <UserFriendlyException>(
                async() =>
                await UserAppService.CreateOrUpdateUser(
                    new CreateOrUpdateUserInput
            {
                User = new UserEditDto
                {
                    EmailAddress = "*****@*****.**",
                    Name         = "John",
                    Surname      = "Nash",
                    UserName     = "******",                //Same username is added before (in CreateTestUsers)
                    Password     = "******"
                },
                AssignedRoleNames = new string[0]
            }));
        }