public UserWithTokenModel Login([FromBody] LoginModel model)
        {
            var result      = _accountQuery.Authenticate(model.Email, model.Password);
            var resultModel = _mapper.Map <UserWithTokenModel>(result);

            return(resultModel);
        }
Exemplo n.º 2
0
        public void AuthenticateShouldReturnUserWithToken()
        {
            // arrange
            var password = _random.Next().ToString();
            var user     = new UserEntity
            {
                Email    = "*****@*****.**",
                Password = password.WithBCrypt(),
                Roles    = new List <UserRoleEntity>
                {
                    new UserRoleEntity {
                        Role = new RoleEntity {
                            RoleName = _random.Next().ToString()
                        }
                    },
                    new UserRoleEntity {
                        Role = new RoleEntity {
                            RoleName = _random.Next().ToString()
                        }
                    }
                }
            };

            _userList.Add(user);
            var expiryTokenDate = DateTime.UtcNow.ToLocalTime() + TokenAuthOption.ExpiresSpan;
            var token           = _random.Next().ToString();

            _tokenBuilderMock.Setup(tb => tb.Build(
                                        user.Email,
                                        It.Is <string[]>(roles => roles.SequenceEqual(user.Roles.Select(x => x.Role.RoleName).ToArray())),
                                        It.Is <DateTime>(d => d - expiryTokenDate < TimeSpan.FromSeconds(1))))
            .Returns(token);

            // act
            var result = _accountQueryProcessor.Authenticate(user.Email, password);

            // assert
            result.User.Should().Be(user);
            result.Token.Should().Be(token);
            result.ExpiresAt.Should().BeCloseTo(expiryTokenDate, 1000);
        }