public async Task ExecuteShouldReturnSuccessAndClaims() { //Arrange Mock <UserManager <AppUser> > userManagerMoq = UserManagerMoq.Get(); AppUser user = new AppUser("das"); var AppUsers = new List <AppUser>() { user }.AsQueryable().BuildMock(); userManagerMoq.SetupGet(x => x.Users).Returns(AppUsers.Object); userManagerMoq.Setup(x => x.CheckPasswordAsync(It.IsAny <AppUser>(), It.IsAny <string>())) .ReturnsAsync(true); userManagerMoq.Setup(x => x.GetClaimsAsync(user)).ReturnsAsync(new List <Claim>() { new Claim(ClaimTypes.Name, "Name") }); AuthenticationModel registerModel = new AuthenticationModel { UserName = "******", Password = "******", }; //Act var result = await new LoginHandler(userManagerMoq.Object).Handle(registerModel, default); //Assert result.IsSuccess.Should().BeTrue(); result.Message.Should().BeNullOrEmpty(); userManagerMoq.Verify(x => x.CheckPasswordAsync(It.IsAny <AppUser>(), It.IsAny <string>()), Times.Once); userManagerMoq.Verify(x => x.GetClaimsAsync(user), Times.Once); }
public async Task ShouldThrowExceptionWhenUserHasNotEmailConfirmedAccountIsNotActivated() { //Arrange Mock <SignInManager <User> > signInManager = SignInManagerMoq.Get(); SchoolManagementContext context = new ContextBuilder().BuildClean(); signInManager.Setup(x => x.CheckPasswordSignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(SignInResult.Success); Model.Domain.User user = new Model.Domain.User() { EmailConfirmed = false }; signInManager.Setup(x => x.SignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <bool>(), It.IsAny <string>())).Returns(Task.FromResult(user)); Mock <UserManager <User> > userManger = UserManagerMoq.Get(); userManger.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user); signInManager.Object.UserManager = userManger.Object; Command cmd = new Command { Password = "******", Email = "*****@*****.**" }; //Act var result = await new Handler(signInManager.Object, context).Handle(cmd, CancellationToken.None); //Assert userManger.Verify(x => x.FindByEmailAsync(It.IsAny <string>()), Times.Once); signInManager.Verify(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once); signInManager.Verify(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>()), Times.Never); result.Status.Should().Be(DataResult.ResultStatus.Error); Assert.Equal("Konto jest nieaktywne.", result.Message); }
public async Task ExecuteShouldReturnFailureUserManagerFailure() { //Arrange Mock <UserManager <AppUser> > userManagerMoq = UserManagerMoq.Get(); var message = "Username is taken"; userManagerMoq.Setup(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>())).ReturnsAsync(IdentityResult.Failed(new[] { new IdentityError() { Description = message } })); RegisterModel registerModel = new RegisterModel { Email = "dsa", UserName = "******", Password = "******", RePassword = "******" }; //Act var result = await new RegisterHandler(userManagerMoq.Object).Handle(registerModel, default); //Assert result.IsSuccess.Should().BeFalse("User is taken"); result.Message.Should().NotBeNullOrEmpty().And.Be(message); userManagerMoq.Verify(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>()), Times.Once); }
public async Task ShouldReturnErrorWhenUserHasActivatedAccount() { //Arrange Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); User user = new User() { EmailConfirmed = true }; userManagerMock.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user); string tokenEncoded = HttpUtility.HtmlEncode("przykaldoweasdadsa@#+==32323232"); Command command = new Command { Email = "*****@*****.**", Token = tokenEncoded }; string tokenDecoded = HttpUtility.HtmlDecode(command.Token); userManagerMock.Setup(x => x.ConfirmEmailAsync(user, tokenDecoded)) .ReturnsAsync(IdentityResult.Success); //Act var result = await new Handler(userManagerMock.Object).Handle(command, CancellationToken.None); //Assert Assert.Equal(PolishReadableMessage.Account.UserHasActivatedAccountAlready, result.Message); result.Status.Should().Be(DataResult.ResultStatus.Error); userManagerMock.Verify(x => x.FindByEmailAsync(It.IsAny <String>()), Times.Once); userManagerMock.Verify(x => x.ConfirmEmailAsync(user, tokenDecoded), Times.Never); }
public async Task ExecuteShouldReturnFailurePasswordMismatch() { //Arrange Mock <UserManager <AppUser> > userManagerMoq = UserManagerMoq.Get(); AppUser user = new AppUser("das"); var AppUsers = new List <AppUser>() { user }.AsQueryable().BuildMock(); userManagerMoq.SetupGet(x => x.Users).Returns(AppUsers.Object); userManagerMoq.Setup(x => x.CheckPasswordAsync(It.IsAny <AppUser>(), It.IsAny <string>())) .ReturnsAsync(false); AuthenticationModel registerModel = new AuthenticationModel { UserName = "******", Password = "******", }; //Act var result = await new LoginHandler(userManagerMoq.Object).Handle(registerModel, default); //Assert result.IsSuccess.Should().BeFalse("User not exist"); result.Message.Should().NotBeNullOrEmpty().And.Be("Username or password is invalid"); userManagerMoq.Verify(x => x.CheckPasswordAsync(It.IsAny <AppUser>(), It.IsAny <string>()), Times.Once); userManagerMoq.Verify(x => x.GetClaimsAsync(user), Times.Never); }
public async Task ShouldThrowExceptionWhenPasswordIsInvalid() { //Arrange Mock <SignInManager <User> > signInManager = SignInManagerMoq.Get(); SchoolManagementContext context = new ContextBuilder().BuildClean(); signInManager.Setup(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(SignInResult.Failed); User user = new User() { }; signInManager.Setup(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>())).Returns(Task.FromResult(user)); Mock <UserManager <User> > userManger = UserManagerMoq.Get(); userManger.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user); signInManager.Object.UserManager = userManger.Object; Command cmd = new Command { Password = "******", Email = "*****@*****.**" }; //Act var result = await new Handler(signInManager.Object, context).Handle(cmd, CancellationToken.None); //Assert userManger.Verify(x => x.FindByEmailAsync(It.IsAny <string>()), Times.Once); signInManager.Verify(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once); signInManager.Verify(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>()), Times.Never); result.Status.Should().Be(DataResult.ResultStatus.Error); Assert.Equal("Błędny login lub hasło.", result.Message); }
public void RegisterWithValidCredentialsShouldReturnRedirectResult() { var ModelMock = Mock.Of <UserViewModel>(x => x.Username == "ValidUsername" && x.Password == "ValidPassword"); UserManagerMoq.Setup(x => x.CreateAsync(It.IsAny <AuthUser>(), It.IsAny <string>())) .ReturnsAsync(IdentityResult.Success); var Sut = new AuthenticationController(UserManagerMoq.Object, SignInManagerMoq.Object); var Result = Sut.Register(ModelMock); Assert.IsType <RedirectToActionResult>(Result.Result); }
public async Task WhenUserNotFoundShouldThrowException() { //Arrange Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); var cmd = new Command() { UserId = Guid.NewGuid().ToString() }; //Act AssignToRoleException exception = await Assert.ThrowsAsync <AssignToRoleException>(async() => await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None)); //Assert Assert.Equal(PolishReadableMessage.Assign.UserNotFound, exception.Message); userManagerMock.Verify(x => x.FindByIdAsync(cmd.UserId), Times.Once); userManagerMock.Verify(x => x.IsInRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Never); userManagerMock.Verify(x => x.AddToRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Never); }
public async Task ShouldThrowExceptionMissingPermission(string roleName) { Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipalBuilder().Build(); Command cmd = new Command { RoleName = roleName, UserId = Guid.NewGuid().ToString(), ClaimsPrincipal = claimsPrincipal }; UnassignRoleException unassignRoleException = await Assert.ThrowsAsync <UnassignRoleException>(async() => await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None)); Assert.Equal(PolishReadableMessage.Assign.DontHavePermissionForUnassignThisRole, unassignRoleException.Message); userManagerMock.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Never); userManagerMock.Verify(x => x.RemoveFromRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Never); }
public async Task ShouldThrowExceptionUserNotFound() { Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipalBuilder().AddPermissionClaim(Permissions.Anchors.Remove).Build(); userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny <string>())).ReturnsAsync((User)null); Command cmd = new Command { RoleName = Roles.Anchor, UserId = Guid.NewGuid().ToString(), ClaimsPrincipal = claimsPrincipal }; UnassignRoleException unassignRoleException = await Assert.ThrowsAsync <UnassignRoleException>(async() => await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None)); Assert.Equal(PolishReadableMessage.UserNotFound, unassignRoleException.Message); userManagerMock.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once); userManagerMock.Verify(x => x.RemoveFromRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Never); }
public async Task ExecuteShouldReturnFailurePasswordMismatch() { //Arrange Mock <UserManager <AppUser> > userManagerMoq = UserManagerMoq.Get(); RegisterModel registerModel = new RegisterModel { Email = "dsa", UserName = "******", Password = "******", RePassword = "******" }; //Act var result = await new RegisterHandler(userManagerMoq.Object).Handle(registerModel, default); //Assert result.IsSuccess.Should().BeFalse("Passwords are invalid"); result.Message.Should().NotBeNullOrEmpty(); userManagerMoq.Verify(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>()), Times.Never); }
public async Task ShouldRemoveRoleFromUser() { SchoolManagementContext context = new ContextBuilder().BuildClean(); Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); ClaimsPrincipal claimsPrincipal = new ClaimsPrincipalBuilder().AddPermissionClaim(Permissions.Anchors.Remove).Build(); var user = new UserBuilder(context).WithEmail("*****@*****.**").BuildAndSave(); userManagerMock.Setup(x => x.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(user); Command cmd = new Command { RoleName = Roles.Anchor, UserId = Guid.NewGuid().ToString(), ClaimsPrincipal = claimsPrincipal }; await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None); userManagerMock.Verify(x => x.FindByIdAsync(It.IsAny <string>()), Times.Once); userManagerMock.Verify(x => x.RemoveFromRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Once); }
public async Task ShouldAssignToRole() { //Arrange var context = new ContextBuilder().BuildClean(); Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); User user = new UserBuilder(context).WithEmail("*****@*****.**").BuildAndSave(); var cmd = new Command() { UserId = user.Id, RoleName = Roles.Admin }; userManagerMock.Setup(x => x.FindByIdAsync(cmd.UserId)).ReturnsAsync(user); userManagerMock.Setup(x => x.IsInRoleAsync(user, cmd.RoleName)).ReturnsAsync(false); //Act await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None); //Assert userManagerMock.Verify(x => x.FindByIdAsync(cmd.UserId), Times.Once); userManagerMock.Verify(x => x.IsInRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Once); userManagerMock.Verify(x => x.AddToRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Once); }
public async Task ExecuteShouldReturnSuccess() { //Arrange Mock <UserManager <AppUser> > userManagerMoq = UserManagerMoq.Get(); userManagerMoq.Setup(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>())).ReturnsAsync(IdentityResult.Success); RegisterModel registerModel = new RegisterModel { Email = "dsa", UserName = "******", Password = "******", RePassword = "******" }; //Act var result = await new RegisterHandler(userManagerMoq.Object).Handle(registerModel, default); //Assert result.IsSuccess.Should().BeTrue(); result.Message.Should().BeNullOrEmpty(); userManagerMoq.Verify(x => x.CreateAsync(It.IsAny <AppUser>(), It.IsAny <string>()), Times.Once); }
public async Task ShouldInvoqueSigninManager() { //Arrange Mock <SignInManager <User> > signInManager = SignInManagerMoq.Get(); SchoolManagementContext context = new ContextBuilder().BuildClean(); signInManager.Setup(x => x.CheckPasswordSignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <string>(), It.IsAny <bool>())) .ReturnsAsync(SignInResult.Success); Model.Domain.User user = new Model.Domain.User() { EmailConfirmed = true }; signInManager.Setup(x => x.SignInAsync(It.IsAny <Model.Domain.User>(), It.IsAny <bool>(), It.IsAny <string>())).Returns(Task.FromResult(user)); signInManager.Setup(x => x.CreateUserPrincipalAsync(It.IsAny <User>())).ReturnsAsync(new ClaimsPrincipal()); Mock <UserManager <User> > userManger = UserManagerMoq.Get(); userManger.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user); Claim adminClaim = new Claim(CustomClaimTypes.Permission, Permissions.Users.SeeAllUser); signInManager.Object.UserManager = userManger.Object; Command cmd = new Command { Password = "******", Email = "*****@*****.**" }; //Act var result = await new Handler(signInManager.Object, context).Handle(cmd, CancellationToken.None); //Assert userManger.Verify(x => x.FindByEmailAsync(It.IsAny <string>()), Times.Once); signInManager.Verify(x => x.CheckPasswordSignInAsync(It.IsAny <User>(), It.IsAny <string>(), It.IsAny <bool>()), Times.Once); signInManager.Verify(x => x.SignInAsync(It.IsAny <User>(), It.IsAny <bool>(), It.IsAny <string>()), Times.Once); result.Status.Should().Be(DataResult.ResultStatus.Success); result.Data.Should().NotBeNull(); }
public async Task WhenUserIsAssignedToRoleAlreadyShouldThrowException() { //Arrange var context = new ContextBuilder().BuildClean(); Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); User user = new UserBuilder(context).WithEmail("*****@*****.**").BuildAndSave(); var cmd = new Command() { UserId = user.Id, RoleName = Roles.Admin }; userManagerMock.Setup(x => x.FindByIdAsync(cmd.UserId)).ReturnsAsync(user); userManagerMock.Setup(x => x.IsInRoleAsync(user, cmd.RoleName)).ReturnsAsync(true); //Act AssignToRoleException exception = await Assert.ThrowsAsync <AssignToRoleException>(async() => await new Handler(userManagerMock.Object).Handle(cmd, CancellationToken.None)); //Assert Assert.Equal(PolishReadableMessage.Assign.UserIsAssignedToRoleAlready, exception.Message); userManagerMock.Verify(x => x.FindByIdAsync(cmd.UserId), Times.Once); userManagerMock.Verify(x => x.IsInRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Once); userManagerMock.Verify(x => x.AddToRoleAsync(It.IsAny <User>(), It.IsAny <string>()), Times.Never); }
public async Task ShouldReturnErrorWhenTokenIsInvalid() { //Arrange Mock <UserManager <User> > userManagerMock = UserManagerMoq.Get(); User user = new User() { EmailConfirmed = false }; userManagerMock.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(user); string tokenEncoded = HttpUtility.HtmlEncode("przykaldoweasdadsa@#+==32323232"); Command command = new Command { Email = "*****@*****.**", Token = tokenEncoded }; string tokenDecoded = HttpUtility.HtmlDecode(command.Token); string invalidTokenError = new PolishIdentityErrorDescriber().InvalidToken().Description; userManagerMock.Setup(x => x.ConfirmEmailAsync(user, tokenDecoded)) .ReturnsAsync(IdentityResult.Failed(new IdentityError() { Description = invalidTokenError })); //Act var result = await new Handler(userManagerMock.Object).Handle(command, CancellationToken.None); //Assert result.Status.Should().Be(DataResult.ResultStatus.Error); Assert.Equal(invalidTokenError, result.Message); userManagerMock.Verify(x => x.FindByEmailAsync(It.IsAny <String>()), Times.Once); userManagerMock.Verify(x => x.ConfirmEmailAsync(user, tokenDecoded), Times.Once); }