Exemple #1
0
        public async Task <IActionResult> Register([FromBody] AuthorRegistrationRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new AuthFailedResponse
                {
                    Errors = ModelState.Values.SelectMany(x =>
                                                          x.Errors.Select(e => e.ErrorMessage))
                }));
            }

            var author = _mapper.Map <Author>(request);

            var authResponse = await _authService
                               .RegisterAsync(author)
                               .ConfigureAwait(false);

            if (!authResponse.Success)
            {
                return(BadRequest(new AuthFailedResponse
                {
                    Errors = authResponse.Errors
                }));
            }

            return(Ok(new AuthSuccessResponse
            {
                Token = authResponse.Token
            }));
        }
Exemple #2
0
        public async Task ShouldBeReturnedAuthenticationResultWithFailedIfEmailAlreadyExists()
        {
            var alreadyEmail = "*****@*****.**";
            var salt         = SecurePasswordHasher.CreateSalt(8);
            var newAuthor    = new AuthorBuilder()
                               .WithFirstName(_faker.Person.FirstName)
                               .WithLastName(_faker.Person.LastName)
                               .WithUsername(_faker.Person.UserName)
                               .WithEmail(alreadyEmail)
                               .WithPassword(SecurePasswordHasher.GenerateHash("joao123", salt))
                               .WithSalt(salt)
                               .Build();

            var authenticationResult = await _authenticationService.RegisterAsync(newAuthor);

            authenticationResult.Should().BeOfType <AuthenticationResult>();
            authenticationResult.Success.Should().BeFalse();
            authenticationResult.Token.Should().BeNull();
            authenticationResult.Errors.Should().SatisfyRespectively(
                err => err.Should().Be("Author with this email already exists"));
        }