public async Task WhenUsernameUpdated_SendsMessage()
        {
            var uniqueData = UNIQUE_PREFIX + "UsernameUpd_SM";

            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepository();
            var userAreaCode      = UserAreaWithoutPasswordSignIn.Code;
            var roleId            = await app.TestData.Roles().AddAsync(uniqueData, userAreaCode);

            var addCommand = new AddUserCommand()
            {
                Username     = uniqueData,
                RoleId       = roleId,
                UserAreaCode = userAreaCode
            };

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(addCommand);

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            var updateCommand = new UpdateCurrentUserCommand()
            {
                Username  = addCommand.Username + "_X",
                FirstName = addCommand.FirstName,
                LastName  = addCommand.LastName
            };

            await contentRepository
            .Users()
            .Current()
            .UpdateAsync(updateCommand);

            using (new AssertionScope())
            {
                app.Mocks
                .CountMessagesPublished <UserUpdatedMessage>(m => m.UserId == addCommand.OutputUserId && m.UserAreaCode == addCommand.UserAreaCode)
                .Should().Be(1);

                app.Mocks
                .CountMessagesPublished <UserEmailUpdatedMessage>(m => m.UserId == addCommand.OutputUserId && m.UserAreaCode == addCommand.UserAreaCode)
                .Should().Be(0);

                app.Mocks
                .CountMessagesPublished <UserUsernameUpdatedMessage>(m => m.UserId == addCommand.OutputUserId && m.UserAreaCode == addCommand.UserAreaCode)
                .Should().Be(1);

                app.Mocks
                .CountMessagesPublished <UserSecurityStampUpdatedMessage>(m => m.UserId == addCommand.OutputUserId && m.UserAreaCode == addCommand.UserAreaCode)
                .Should().Be(1);
            }
        }
        public async Task CanUpdateName()
        {
            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepository();
            var userArea          = app.SeededEntities.TestUserArea1;

            var addCommand = new AddUserCommand()
            {
                Email        = "reginald.dwight" + EMAIL_DOMAIN,
                Password     = PASSWORD,
                FirstName    = "Reginald",
                LastName     = "Dwight",
                DisplayName  = "Reggie",
                RoleCode     = userArea.RoleA.RoleCode,
                UserAreaCode = userArea.UserAreaCode
            };

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(addCommand);

            var originalUserState = await GetUserByIdAsync(app, userId);

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            var updateCommand = new UpdateCurrentUserCommand()
            {
                Email       = addCommand.Email,
                FirstName   = "Elton",
                LastName    = "John",
                DisplayName = "Rocketman"
            };

            await contentRepository
            .Users()
            .Current()
            .UpdateAsync(updateCommand);

            var user = await GetUserByIdAsync(app, userId);

            using (new AssertionScope())
            {
                user.Should().NotBeNull();
                user.FirstName.Should().Be(updateCommand.FirstName);
                user.LastName.Should().Be(updateCommand.LastName);
                user.DisplayName.Should().Be(updateCommand.DisplayName);
                user.SecurityStamp.Should().Be(originalUserState.SecurityStamp);
                user.Email.Should().Be(addCommand.Email.ToLowerInvariant());
            }
        }
        public async Task CanUpdateUsername()
        {
            var uniqueData = UNIQUE_PREFIX + nameof(CanUpdateUsername);

            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepository();
            var userAreaCode      = UserAreaWithoutEmailAsUsername.Code;
            var roleId            = await app.TestData.Roles().AddAsync(uniqueData, userAreaCode);

            var addCommand = new AddUserCommand()
            {
                Email        = uniqueData + EMAIL_DOMAIN,
                Username     = "******",
                Password     = PASSWORD,
                RoleId       = roleId,
                UserAreaCode = userAreaCode
            };

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(addCommand);

            var originalUserState = await GetUserByIdAsync(app, userId);

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            var updateCommand = new UpdateCurrentUserCommand()
            {
                Email    = addCommand.Email,
                Username = "******"
            };

            await contentRepository
            .Users()
            .Current()
            .UpdateAsync(updateCommand);

            var user = await GetUserByIdAsync(app, userId);

            using (new AssertionScope())
            {
                user.Should().NotBeNull();
                user.Username.Should().Be(updateCommand.Username);
                user.UniqueUsername.Should().Be(updateCommand.Username.ToLowerInvariant());
                user.SecurityStamp.Should().NotBeNull().And.NotBe(originalUserState.SecurityStamp);
            }
        }
        public async Task WhenUsernameAsDisplayName_DisplayNameUpdated()
        {
            var uniqueData = UNIQUE_PREFIX + "UnAsDN_DNUpd";

            using var app = _appFactory.Create(c => c.Configure <UsersSettings>(c => c.Username.UseAsDisplayName = true));
            var contentRepository = app.Services.GetContentRepository();
            var userAreaCode      = UserAreaWithoutEmailAsUsername.Code;
            var roleId            = await app.TestData.Roles().AddAsync(uniqueData, userAreaCode);

            var addCommand = new AddUserCommand()
            {
                Email        = "*****@*****.**",
                Username     = "******",
                Password     = PASSWORD,
                RoleId       = roleId,
                UserAreaCode = userAreaCode
            };

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(addCommand);

            var originalUserState = await GetUserByIdAsync(app, userId);

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            var updateCommand = new UpdateCurrentUserCommand()
            {
                Email       = addCommand.Email,
                Username    = "******",
                DisplayName = "This data will be ignored"
            };

            await contentRepository
            .Users()
            .Current()
            .UpdateAsync(updateCommand);

            var user = await GetUserByIdAsync(app, userId);

            using (new AssertionScope())
            {
                user.Should().NotBeNull();
                user.DisplayName.Should().Be(updateCommand.Username);
            }
        }
        public async Task WhenEmailNotUnique_Throws()
        {
            var uniqueData = UNIQUE_PREFIX + nameof(WhenEmailNotUnique_Throws);

            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepository();
            var userArea          = app.SeededEntities.TestUserArea1;

            await contentRepository
            .WithElevatedPermissions()
            .Users()
            .AddAsync(new AddUserCommand()
            {
                Email        = uniqueData + EMAIL_DOMAIN,
                Password     = PASSWORD,
                RoleCode     = userArea.RoleA.RoleCode,
                UserAreaCode = userArea.UserAreaCode
            });

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(new AddUserCommand()
            {
                Email        = uniqueData + "2" + EMAIL_DOMAIN,
                Password     = PASSWORD,
                RoleCode     = userArea.RoleA.RoleCode,
                UserAreaCode = userArea.UserAreaCode
            });

            var updateCommand = new UpdateCurrentUserCommand()
            {
                Email = uniqueData + EMAIL_DOMAIN,
            };

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            await contentRepository
            .Awaiting(r => r.Users().Current().UpdateAsync(updateCommand))
            .Should()
            .ThrowAsync <ValidationErrorException>()
            .WithMemberNames(nameof(updateCommand.Email))
            .WithMessage("*already registered*");
        }
Esempio n. 6
0
        public async Task <ActionResult> UpdateCurrentUserAsync([FromBody] UpdateCurrentUserCommand command)
        {
            if (ModelState.IsValid)
            {
                command.CurrentUser = await userManager.FindByEmailAsync(User.Identity.Name);

                await pipelineService.HandleCommandAsync(command);

                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Esempio n. 7
0
        public async Task <IActionResult> UpdateUser([FromBody] UpdateCurrentUserCommand command)
        {
            var user = await Mediator.Send(command);

            return(Ok(user));
        }
        public async Task CanUpdateEmail()
        {
            using var app = _appFactory.Create();
            var contentRepository = app.Services.GetContentRepository();
            var dbContext         = app.Services.GetRequiredService <CofoundryDbContext>();
            var userArea          = app.SeededEntities.TestUserArea1;

            var addCommand = new AddUserCommand()
            {
                Email        = $"prince" + EMAIL_DOMAIN,
                Password     = PASSWORD,
                RoleCode     = userArea.RoleA.RoleCode,
                UserAreaCode = userArea.UserAreaCode
            };

            var userId = await contentRepository
                         .WithElevatedPermissions()
                         .Users()
                         .AddAsync(addCommand);

            var originalUserState = await GetUserByIdAsync(app, userId);

            await contentRepository
            .Users()
            .Authentication()
            .SignInAuthenticatedUserAsync(new SignInAuthenticatedUserCommand()
            {
                UserId = userId
            });

            var newEmailDomain = $"{UNIQUE_PREFIX}2.example.com";
            var newEmailLocal  = "TAFKAP@";
            var updateCommand  = new UpdateCurrentUserCommand()
            {
                Email = newEmailLocal + newEmailDomain,
            };

            await contentRepository
            .Users()
            .Current()
            .UpdateAsync(updateCommand);

            var user = await dbContext
                       .Users
                       .AsNoTracking()
                       .Include(u => u.EmailDomain)
                       .FilterById(userId)
                       .SingleOrDefaultAsync();

            var normalizedDomain   = newEmailDomain.ToLowerInvariant();
            var newNormalizedEmail = newEmailLocal + normalizedDomain;
            var newEmailLower      = newNormalizedEmail.ToLowerInvariant();

            using (new AssertionScope())
            {
                user.Should().NotBeNull();
                user.FirstName.Should().Be(updateCommand.FirstName);
                user.LastName.Should().Be(updateCommand.LastName);
                user.Email.Should().Be(newNormalizedEmail);
                user.UniqueEmail.Should().Be(newEmailLower);
                user.Username.Should().Be(newNormalizedEmail);
                user.UniqueUsername.Should().Be(newEmailLower);
                user.EmailDomain.Name.Should().Be(normalizedDomain);
                user.SecurityStamp.Should().NotBeNull().And.NotBe(originalUserState.SecurityStamp);
            }
        }
 public Task UpdateAsync(UpdateCurrentUserCommand command)
 {
     return(ExtendableContentRepository.ExecuteCommandAsync(command));
 }
Esempio n. 10
0
        public async Task HandleUpadteCurrentUserAsync(UpdateCurrentUserCommand userCommand)
        {
            userCommand.CurrentUser = mapper.Map <User>(userCommand.UpdateUserInfo);

            await userManager.UpdateAsync(userCommand.CurrentUser);
        }