public async Task Handle_Calls_CreateBearerTokenAsync() { // Arrange GetAuthQuery request = new GetAuthQuery() { ClientId = "any_client_id", GrantType = "any_grant_type", Email = "any_email", Password = "******" }; string anyAccessToken = "any_access_token"; int anyExpiresIn = 8 * 3600; AutoMocker mocker = new AutoMocker(MockBehavior.Strict); mocker.GetMock <IAuthService>() .Setup(service => service.CreateBearerTokenAsync(request.Email, It.IsAny <CancellationToken>())) .ReturnsAsync((anyAccessToken, anyExpiresIn)) .Verifiable(); GetAuthQueryInteractor sut = mocker.CreateInstance <GetAuthQueryInteractor>(); // Act await sut.Handle(request, default); // Assign mocker.GetMock <IAuthService>() .Verify(service => service.CreateBearerTokenAsync(request.Email, It.IsAny <CancellationToken>()), Times.Once, failMessage: "CreateBearerTokenAsync was not called once"); }
public void Invalid_Validate_ThorwsAuthErrorException(string grantType, string clientId, string email, string password) { // Assign GetAuthQuery invalidQuery = new GetAuthQuery() { ClientId = clientId, GrantType = grantType, Email = email, Password = password, }; User user = new User() { Email = invalidQuery.Email, Password = invalidQuery.Password }; AutoMocker mocker = new AutoMocker(MockBehavior.Strict); mocker.GetMock <IUserRepository>() .Setup(repo => repo.GetByEmailAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(user); mocker.GetMock <IHashingService>() .Setup(service => service.HashAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .ReturnsAsync(string.Empty); GetAuthQueryValidator validator = mocker.CreateInstance <GetAuthQueryValidator>(); // Act Action validation = () => validator.Validate(invalidQuery); // Assert validation.Should().Throw <AuthErrorException>(); }
public async Task <object> GetToken([FromBody] GetAuthQuery query) { var result = await _lubyTasksHandler.ExecuteAsync(query); var token = Authentication.GetToken(result.Data.FirstOrDefault()); return(result.GetTokenResult(token)); }
public async Task <TokenViewModel> Auth(AuthViewModel authViewModel) { var query = new GetAuthQuery(authViewModel.Login, authViewModel.Senha); var usuarioModel = await _mediator.SendQuery(query); if (usuarioModel != null) { var usuarioViewModel = _mapper.Map <ClienteViewModel>(usuarioModel); return(_tokenServiceApp.GenereteToken(usuarioViewModel)); } return(new TokenViewModel()); }
public async Task <IActionResult> GetAuthToken([FromQuery] GetAuthQuery query, CancellationToken cancellationToken) => new OkObjectResult(await Mediator.Send(query, cancellationToken));