public async Task should_be_able_to_signin_new_user() { // arrange var username = Guid.NewGuid().ToString("N").Substring(4, 8); var password = "******"; _theApp.CreateUser(username, password); // Act var request = _theApp.Server.CreateRequest("/signin") .WithFormContent(new Dictionary <string, string>() { { "UserName", username }, { "Password", password }, { "__RequestVerificationToken", _antiForgeryTokens.VerificationToken } }) .WithCookie(_antiForgeryTokens.Cookie); var response = await request.PostAsync(); // assert response.StatusCode.ShouldEqual(HttpStatusCode.Redirect); var cookieHeaders = response.Headers.GetValues("Set-Cookie").ToList(); cookieHeaders.ShouldContain(cookie => cookie.Contains(".AspNetCore.Identity.Application")); }
public async Task should_signin_user_and_redirect_when_signin_with_valid_user() { // Arrange ClaimsPrincipal signedInClaimsPrincipal = null; var authService = new Mock <IAuthenticationService>(); authService.Setup(auth => auth.SignInAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <ClaimsPrincipal>(), It.IsAny <AuthenticationProperties>())) .Returns(Task.CompletedTask) .Callback((HttpContext ctx, string scheme, ClaimsPrincipal claimsPrincipal, AuthenticationProperties props) => { signedInClaimsPrincipal = claimsPrincipal; }) .Verifiable(); ReplacableServiceProvider.Replace(services => { services.AddSingleton(authService.Object); }); var accountCtrl = _myApp.CreateController <AccountController>(); var userRepo = _myApp.GetService <IRepository <User> >(); const string password = "******"; _myApp.CreateUser("jim", password, "Jim Green"); // Act var userModel = new SigninUserViewModel { UserName = "******", Password = password }; var sigininResult = await accountCtrl.DoSignin(userModel, null); // Assert Assert.True(accountCtrl.ModelState.IsValid); sigininResult.IsType <RedirectResult>(); authService.Verify(); Assert.Equal("jim", signedInClaimsPrincipal.ToDiscussionUser(userRepo).UserName); }