public async Task <Account> ProvideAccountForExternalLoginAsync(string email, Guid correlationId) { var getAccountResult = await _accountGetterService.GetByEmailAsync(email); Account account; if (!getAccountResult.Success) { account = await _accountCreatorService.CreateAsync(Guid.NewGuid(), email, string.Empty, correlationId); } else { account = getAccountResult.Value; if (account.Confirmed) { return(account); } account.Confirm(correlationId); await _communicationBus.DispatchDomainEventsAsync(account); await _accountRepository.UpdateAsync(account); } return(account); }
public async Task HandleAsync(CreateAccountCommand command, CancellationToken cancellationToken = default) { var emailIsNotTakenVerificationResult = await _accountVerificationService.VerifyEmailIsNotTakenAsync(command.Email); if (!emailIsNotTakenVerificationResult.Success) { throw new ConflictException(emailIsNotTakenVerificationResult.Errors); } await _accountCreatorService.CreateAsync(command.AccountId, command.Email, command.Password, command.CorrelationId); var accountCreatedIntegrationEvent = new AccountCreatedIntegrationEvent(command.CorrelationId, command.AccountId, command.Email, string.Empty); await _integrationEventBus.PublishIntegrationEventAsync(accountCreatedIntegrationEvent); }
public async Task CreateAsync_Should_Create_Account(string password, string correlationIdString) { var correlationId = new Guid(correlationIdString); var role = new Role(Guid.NewGuid(), Array.Empty <byte>(), DefaultRoleEnumeration.User.DisplayName); const string hashedPassword = "******"; var accountId = Guid.NewGuid(); const string email = "*****@*****.**"; if (!string.IsNullOrWhiteSpace(password)) { _passwordServiceMock.Setup(x => x.HashPassword(It.IsAny <string>())).Returns(hashedPassword); } _roleRepositoryMock.Setup(x => x.GetByNameAsync(It.IsAny <string>())).ReturnsAsync(role); _communicationBusMock.Setup(x => x.DispatchDomainEventsAsync(It.IsAny <Account>(), It.IsAny <CancellationToken>())) .Returns(Task.CompletedTask); _accountRepositoryMock.Setup(x => x.AddAsync(It.IsAny <Account>())).Returns(Task.CompletedTask); var result = await _accountCreatorService.CreateAsync(accountId, email, password, correlationId); result.Id.Should().Be(accountId); result.Email.Should().Be(email); result.Roles.Should().BeEquivalentTo(new List <Guid> { role.Id }); result.DomainEvents.Should().Contain(x => x.GetType() == typeof(AccountCreatedDomainEvent)); result.DomainEvents.Should().Contain(x => x.GetType() == typeof(AccountRoleAddedDomainEvent)); if (!string.IsNullOrWhiteSpace(password)) { result.PasswordHash.Should().Be(hashedPassword); result.Confirmed.Should().BeFalse(); result.LastLogin.Should().BeNull(); result.Tokens.Should().Contain(x => x.Type.Equals(TokenTypeEnumeration.AccountConfirmation)); result.DomainEvents.Should().Contain(x => x.GetType() == typeof(AccountTokenGeneratedDomainEvent)); } else { result.PasswordHash.Should().BeNullOrWhiteSpace(); result.Confirmed.Should().BeTrue(); result.LastLogin.Should().BeNull(); } }