Esempio n. 1
0
        public void AddUserAuth_adds_auth_data_and_find_by_username_finds_it()
        {
            _userAuthenticationDataRepository.AddUserAuth(authData);

            var found = _userAuthenticationDataRepository.FindUserAuth(authData.UserName);

            found.Should().NotBeNull();
            found.Should().BeEquivalentTo(authData);
        }
Esempio n. 2
0
        protected override Task <RequestStatus> HandleCommand(SignInCommand command, CancellationToken cancellationToken)
        {
            var authData = _userAuthenticationDataRepository.FindUserAuth(command.Username);

            if (authData != null)
            {
                if (authData.Password.Equals(command.Password))
                {
                    var userIdentity = new UserIdentity()
                    {
                        UserId = authData.UserId, UserName = authData.UserName
                    };
                    var response = RequestStatus.CreateFromCommandContext(command.CommandContext, Status.COMPLETED, new Dictionary <string, object>()
                    {
                        { "UserIdentity", userIdentity }
                    });
                    return(Task.FromResult(response));
                }
                else
                {
                    throw new InvalidPasswordException("Invalid password");
                }
            }

            throw new UserNotFoundException($"Cannot find user {command.Username}");
        }
        protected override Task <RequestStatus> HandleCommand(SignUpCommand request, CancellationToken cancellationToken)
        {
            var existing = _userAuthenticationDataRepository.FindUserAuth(request.Username);

            if (existing != null)
            {
                throw new UsernameConflictException($"User {request.Username} already exists");
            }

            var user = new User();

            user.Register(request.Username);

            var response = RequestStatus.CreateFromCommandContext(request.CommandContext, Status.COMPLETED);
            var userAuth = new UserAuthenticationData()
            {
                Password = request.Password,
                UserId   = user.UserIdentity.UserId,
                UserName = user.UserIdentity.UserName,
                Email    = request.Email
            };

            _userAuthenticationDataRepository.AddUserAuth(userAuth);
            _userRepository.AddUser(user);
            _eventBusService.Publish(user.PendingEvents, response.CorrelationId, request);
            user.MarkPendingEventsAsHandled();

            return(Task.FromResult(response));
        }