public async Task ShouldThrowErrorWhenCaseErrorInPassword() { var command = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******", HostName = "localhost" }; await Assert.ThrowsAsync <Exception>(async() => await _commandHandler.Handle(command, CancellationToken.None)); }
public async Task ShouldGetModelForValidInformation() { var command = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******", HostName = "localhost" }; var adminUser = await _commandHandler.Handle(command, CancellationToken.None); Assert.Equal(command.Email, adminUser.Email, ignoreCase: true); }
public async Task <IActionResult> Authenticate([FromBody] AuthenticateAdminUserCommand command) { try { command.HostName = HonoHost; var model = await Mediator.Send(command); var(user, stringToken) = _adminUserService.GenerateToken(model); return(Ok(new { User = user, Token = stringToken })); } catch (Exception e) { return(BadRequest(e)); } }
public static HttpClient GetTokenAuthorizeHttpClient(CustomWebApplicationFactory <Startup> factory) { var client = factory.CreateClient(); var authenticateAdminUserCommand = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******" }; var content = JsonConvert.SerializeObject(authenticateAdminUserCommand); var httpResponse = client.PostAsync("/adminuser/authenticate", new StringContent(content, Encoding.UTF8, StringConstants.ApplicationJson)).Result; var token = JsonConvert.DeserializeObject <Dictionary <string, object> >(httpResponse.Content.ReadAsStringAsync().Result)["token"]; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.ToString()); return(client); }
public async Task CanLoginAdminUser() { // The endpoint or route of the controller action. var command = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******" }; var json = JsonConvert.SerializeObject(command); var httpResponse = await _client.PostAsync("/AdminUser/Authenticate", new StringContent(json, Encoding.UTF8, StringConstants.ApplicationJson)); // Must be successful. httpResponse.EnsureSuccessStatusCode(); Assert.True(httpResponse.IsSuccessStatusCode); Assert.Equal(HttpStatusCode.OK, httpResponse.StatusCode); }
public async Task ShouldIncreaseInvalidPasswordAttempsNumberWhenInvalidInformation() { var command = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******", HostName = "localhost" }; var dbUser = _context .AdminUsers .First(u => u.Email == command.Email); var before = dbUser.NumberOfInvalidPasswordAttemps; await Assert.ThrowsAsync <Exception>(async() => await _commandHandler.Handle(command, CancellationToken.None)); var current = dbUser.NumberOfInvalidPasswordAttemps; var actual = current - before; Assert.Equal(1, actual); }
public async Task CanRenewTokenAdminUser() { //First request to get Token. var command = new AuthenticateAdminUserCommand { Email = "*****@*****.**", Password = "******" }; var json = JsonConvert.SerializeObject(command); var authenticateResponse = await _client.PostAsync("/AdminUser/Authenticate", new StringContent(json, Encoding.UTF8, StringConstants.ApplicationJson)); var firstToken = JsonConvert.DeserializeObject <AuthenticateResponseModel>(await authenticateResponse.Content.ReadAsStringAsync()).Token; //Second request to renewToken var renewToken = JsonConvert.SerializeObject(firstToken); var renewTokenResponse = await _client.PostAsync("/AdminUser/RenewToken", new StringContent(renewToken, Encoding.UTF8, StringConstants.ApplicationJson)); // Must be successful. renewTokenResponse.EnsureSuccessStatusCode(); Assert.True(renewTokenResponse.IsSuccessStatusCode); Assert.Equal(HttpStatusCode.Created, renewTokenResponse.StatusCode); }