Esempio n. 1
0
        public async Task HandleAsync_Should_Complete_Account_Creation_With_Failure_When_UserCreationCompletedIntegrationEvent_Is_Received_And_Getting_Account_Throws_Any_Exception()
        {
            var          userCreationCompletedIntegrationEvent = new UserCreationCompletedIntegrationEvent(Guid.NewGuid(), DateTimeOffset.UtcNow, Guid.NewGuid());
            var          exception  = new Exception("Exception occured.");
            const string logMessage = "accountId={accountId}, message={message}, stackTrace={stackTrace}";

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>()))
            .ThrowsAsync(exception);
            _loggerMock.Setup(x => x.LogIntegrationEventError(It.IsAny <ServiceComponentEnumeration>(),
                                                              It.IsAny <IIntegrationEvent>(), It.IsAny <string>(), It.IsAny <object[]>()))
            .Verifiable();
            _integrationEventBusMock.Setup(x => x.PublishIntegrationEventAsync(It.IsAny <IIntegrationEvent>()))
            .Returns(Task.CompletedTask)
            .Verifiable();


            Func <Task> result = async() => await _userCreatedIntegrationEventHandler.HandleAsync(userCreationCompletedIntegrationEvent);

            await result.Should().NotThrowAsync <Exception>();

            _loggerMock.Verify(x => x.LogIntegrationEventError(
                                   It.Is <ServiceComponentEnumeration>(s => Equals(s, ServiceComponentEnumeration.RivaIdentity)),
                                   It.Is <IIntegrationEvent>(ie => ie == userCreationCompletedIntegrationEvent),
                                   It.Is <string>(m => m.Equals(logMessage)), It.IsAny <object[]>()), Times.Once);
            _integrationEventBusMock.Verify(x => x.PublishIntegrationEventAsync(It.Is <IIntegrationEvent>(ie =>
                                                                                                          IsPublishedIntegrationEventCorrect((AccountCreationCompletedIntegrationEventFailure)ie,
                                                                                                                                             userCreationCompletedIntegrationEvent.CorrelationId, userCreationCompletedIntegrationEvent.UserId,
                                                                                                                                             IntegrationEventErrorCodeEnumeration.UnexpectedError.DisplayName,
                                                                                                                                             IntegrationEventErrorMessage.UnexpectedError))));
        }
Esempio n. 2
0
        public async Task HandleAsync_Should_Complete_Account_Creation_With_Success_When_UserCreationCompletedIntegrationEvent_Is_Received_And_Account_Has_Not_Confirmation_Token()
        {
            var userCreationCompletedIntegrationEvent = new UserCreationCompletedIntegrationEvent(Guid.NewGuid(), DateTimeOffset.UtcNow, Guid.NewGuid());
            var account = Account.Builder()
                          .SetId(userCreationCompletedIntegrationEvent.UserId)
                          .SetEmail("*****@*****.**")
                          .SetConfirmed(false)
                          .SetPasswordHash("PasswordHash")
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                          .Build();
            var getAccountResult = GetResult <Account> .Ok(account);

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getAccountResult);
            _integrationEventBusMock.Setup(x => x.PublishIntegrationEventAsync(It.IsAny <IIntegrationEvent>()))
            .Returns(Task.CompletedTask).Verifiable();


            Func <Task> result = async() => await _userCreatedIntegrationEventHandler.HandleAsync(userCreationCompletedIntegrationEvent);

            await result.Should().NotThrowAsync <Exception>();

            _integrationEventBusMock.Verify(x => x.PublishIntegrationEventAsync(It.Is <IIntegrationEvent>(ie =>
                                                                                                          IsPublishedIntegrationEventCorrect((AccountCreationCompletedIntegrationEvent)ie, userCreationCompletedIntegrationEvent.CorrelationId,
                                                                                                                                             account.Id))));
        }
Esempio n. 3
0
        public async Task HandleAsync(AccountCreatedIntegrationEvent integrationEvent, CancellationToken cancellationToken = default)
        {
            try
            {
                var user = User.Builder()
                           .SetId(integrationEvent.AccountId)
                           .SetEmail(integrationEvent.Email)
                           .SetServiceActive(DefaultUserSettings.ServiceActive)
                           .SetAnnouncementPreferenceLimit(DefaultUserSettings.AnnouncementPreferenceLimit)
                           .SetAnnouncementSendingFrequency(DefaultUserSettings.AnnouncementSendingFrequency)
                           .SetPicture(integrationEvent.Picture)
                           .Build();

                user.AddCreatedEvent(integrationEvent.CorrelationId);

                await _communicationBus.DispatchDomainEventsAsync(user, cancellationToken);

                await _userRepository.AddAsync(user);

                var userCreationCompletedIntegrationEvent = new UserCreationCompletedIntegrationEvent(integrationEvent.CorrelationId, user.Id);
                await _integrationEventBus.PublishIntegrationEventAsync(userCreationCompletedIntegrationEvent);
            }
            catch (Exception e)
            {
                _logger.LogIntegrationEventError(ServiceComponentEnumeration.RivaUsers, integrationEvent,
                                                 "userId={userId}, message={message}, stackTrace={stackTrace}", integrationEvent.AccountId, e.Message, e.StackTrace);
                var userCreationCompletedIntegrationEventFailure = new UserCreationCompletedIntegrationEventFailure(
                    integrationEvent.CorrelationId, IntegrationEventErrorCodeEnumeration.UnexpectedError.DisplayName,
                    IntegrationEventErrorMessage.UnexpectedError, integrationEvent.AccountId);
                await _integrationEventBus.PublishIntegrationEventAsync(userCreationCompletedIntegrationEventFailure);
            }
        }
Esempio n. 4
0
        public async Task HandleAsync_Should_Complete_Account_Creation_With_Success_When_UserCreationCompletedIntegrationEvent_Is_Received_Account_Has_Confirmation_Token()
        {
            var userCreationCompletedIntegrationEvent = new UserCreationCompletedIntegrationEvent(Guid.NewGuid(), DateTimeOffset.UtcNow, Guid.NewGuid());
            var token = Token.Builder()
                        .SetIssued(DateTimeOffset.UtcNow.AddHours(-1))
                        .SetExpires(DateTimeOffset.UtcNow.AddDays(1))
                        .SetType(TokenTypeEnumeration.AccountConfirmation)
                        .SetValue("12345")
                        .Build();
            var account = Account.Builder()
                          .SetId(userCreationCompletedIntegrationEvent.UserId)
                          .SetEmail("*****@*****.**")
                          .SetConfirmed(false)
                          .SetPasswordHash("PasswordHash")
                          .SetSecurityStamp(Guid.NewGuid())
                          .SetCreated(DateTimeOffset.UtcNow)
                          .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                          .SetTokens(new[] { token })
                          .Build();
            var getAccountResult = GetResult <Account> .Ok(account);

            _accountGetterServiceMock.Setup(x => x.GetByIdAsync(It.IsAny <Guid>())).ReturnsAsync(getAccountResult);
            _integrationEventBusMock.Setup(x => x.PublishIntegrationEventAsync(It.IsAny <IIntegrationEvent>()))
            .Returns(Task.CompletedTask).Verifiable();
            _accountConfirmationRequestServiceMock
            .Setup(x => x.PublishAccountConfirmationRequestedIntegrationEventAsync(It.IsAny <string>(),
                                                                                   It.IsAny <string>(), It.IsAny <Guid>()))
            .Returns(Task.CompletedTask)
            .Verifiable();


            Func <Task> result = async() => await _userCreatedIntegrationEventHandler.HandleAsync(userCreationCompletedIntegrationEvent);

            await result.Should().NotThrowAsync <Exception>();

            _integrationEventBusMock.Verify(x => x.PublishIntegrationEventAsync(It.Is <IIntegrationEvent>(ie =>
                                                                                                          IsPublishedIntegrationEventCorrect((AccountCreationCompletedIntegrationEvent)ie, userCreationCompletedIntegrationEvent.CorrelationId,
                                                                                                                                             account.Id))));
            _accountConfirmationRequestServiceMock.Verify(x =>
                                                          x.PublishAccountConfirmationRequestedIntegrationEventAsync(It.Is <string>(e => e == account.Email),
                                                                                                                     It.Is <string>(t => t == token.Value),
                                                                                                                     It.Is <Guid>(c => c == userCreationCompletedIntegrationEvent.CorrelationId)));
        }