public async Task SuccessIfLongEnoughTests(string input)
        {
            var manager = MockHelpers.TestUserManager <PocoUser>();
            var valid   = new PasswordValidator <PocoUser>();

            manager.Options.Password.RequireUppercase       = false;
            manager.Options.Password.RequireNonAlphanumeric = false;
            manager.Options.Password.RequireLowercase       = false;
            manager.Options.Password.RequireDigit           = false;
            IdentityResultAssert.IsSuccess(await valid.ValidateAsync(manager, null, input));
        }
        public async Task FailsIfTooShortTests(string input)
        {
            const string error   = "Passwords must be at least 6 characters.";
            var          manager = MockHelpers.TestUserManager <PocoUser>();
            var          valid   = new PasswordValidator <PocoUser>();

            manager.Options.Password.RequireUppercase       = false;
            manager.Options.Password.RequireNonAlphanumeric = false;
            manager.Options.Password.RequireLowercase       = false;
            manager.Options.Password.RequireDigit           = false;
            IdentityResultAssert.IsFailure(await valid.ValidateAsync(manager, null, input), error);
        }
Example #3
0
        public async Task ValidateThrowsWithNull()
        {
            // Setup
            var manager   = MockHelpers.TestUserManager(new NoopUserStore());
            var validator = new UserValidator <PocoUser>();

            // Act
            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>("manager", () => validator.ValidateAsync(null, null));

            await Assert.ThrowsAsync <ArgumentNullException>("user", () => validator.ValidateAsync(manager, null));
        }
        public async Task FailsWithoutRequiredNonAlphanumericTests(string input)
        {
            var manager = MockHelpers.TestUserManager <PocoUser>();
            var valid   = new PasswordValidator <PocoUser>();

            manager.Options.Password.RequireUppercase       = false;
            manager.Options.Password.RequireNonAlphanumeric = true;
            manager.Options.Password.RequireLowercase       = false;
            manager.Options.Password.RequireDigit           = false;
            manager.Options.Password.RequiredLength         = 0;
            IdentityResultAssert.IsFailure(await valid.ValidateAsync(manager, null, input),
                                           "Passwords must have at least one non alphanumeric character.");
        }
        public async Task SucceedsWithRequiredUniqueCharsTests(string input, int uniqueChars)
        {
            var manager = MockHelpers.TestUserManager <PocoUser>();
            var valid   = new PasswordValidator <PocoUser>();

            manager.Options.Password.RequireUppercase       = false;
            manager.Options.Password.RequireNonAlphanumeric = false;
            manager.Options.Password.RequireLowercase       = false;
            manager.Options.Password.RequireDigit           = false;
            manager.Options.Password.RequiredLength         = 0;
            manager.Options.Password.RequiredUniqueChars    = uniqueChars;
            IdentityResultAssert.IsSuccess(await valid.ValidateAsync(manager, null, input));
        }
        public async Task FailsWithoutRequiredUniqueCharsTests(string input, int uniqueChars)
        {
            var manager = MockHelpers.TestUserManager <PocoUser>();
            var valid   = new PasswordValidator <PocoUser>();

            manager.Options.Password.RequireUppercase       = false;
            manager.Options.Password.RequireNonAlphanumeric = false;
            manager.Options.Password.RequireLowercase       = false;
            manager.Options.Password.RequireDigit           = false;
            manager.Options.Password.RequiredLength         = 0;
            manager.Options.Password.RequiredUniqueChars    = uniqueChars;
            IdentityResultAssert.IsFailure(await valid.ValidateAsync(manager, null, input),
                                           $"Passwords must use at least {uniqueChars} different characters.");
        }
Example #7
0
        public async Task ValidateFailsWithTooShortUserNames(string input)
        {
            // Setup
            var manager   = MockHelpers.TestUserManager(new NoopUserStore());
            var validator = new UserValidator <PocoUser>();
            var user      = new PocoUser {
                UserName = input
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            IdentityResultAssert.IsFailure(result, new IdentityErrorDescriber().InvalidUserName(input));
        }
        public async Task UberMixedRequiredTests(string input, Errors errorMask)
        {
            const string alphaError  = "Passwords must have at least one non alphanumeric character.";
            const string upperError  = "Passwords must have at least one uppercase ('A'-'Z').";
            const string lowerError  = "Passwords must have at least one lowercase ('a'-'z').";
            const string digitError  = "Passwords must have at least one digit ('0'-'9').";
            const string lengthError = "Passwords must be at least 6 characters.";
            var          manager     = MockHelpers.TestUserManager <PocoUser>();
            var          valid       = new PasswordValidator <PocoUser>();
            var          errors      = new List <string>();

            if ((errorMask & Errors.Length) != Errors.None)
            {
                errors.Add(lengthError);
            }
            if ((errorMask & Errors.Alpha) != Errors.None)
            {
                errors.Add(alphaError);
            }
            if ((errorMask & Errors.Digit) != Errors.None)
            {
                errors.Add(digitError);
            }
            if ((errorMask & Errors.Lower) != Errors.None)
            {
                errors.Add(lowerError);
            }
            if ((errorMask & Errors.Upper) != Errors.None)
            {
                errors.Add(upperError);
            }
            var result = await valid.ValidateAsync(manager, null, input);

            if (errors.Count == 0)
            {
                IdentityResultAssert.IsSuccess(result);
            }
            else
            {
                IdentityResultAssert.IsFailure(result);
                foreach (var error in errors)
                {
                    Assert.Contains(result.Errors, e => e.Description == error);
                }
            }
        }
Example #9
0
        public async Task SignOutCallsContextResponseSignOut()
        {
            // Setup
            var manager = MockHelpers.TestUserManager <PocoUser>();
            var context = new DefaultHttpContext();
            var auth    = MockAuth(context);

            auth.Setup(a => a.SignOutAsync(context, IdentityConstants.ApplicationScheme, It.IsAny <AuthenticationProperties>())).Returns(Task.FromResult(0)).Verifiable();
            auth.Setup(a => a.SignOutAsync(context, IdentityConstants.TwoFactorUserIdScheme, It.IsAny <AuthenticationProperties>())).Returns(Task.FromResult(0)).Verifiable();
            auth.Setup(a => a.SignOutAsync(context, IdentityConstants.ExternalScheme, It.IsAny <AuthenticationProperties>())).Returns(Task.FromResult(0)).Verifiable();
            var helper = SetupSignInManager(manager, context, null, manager.Options);

            // Act
            await helper.SignOutAsync();

            // Assert
            auth.Verify();
        }
Example #10
0
        public async Task DefaultAlphaNumericOnlyUserNameValidation(string userName, bool expectSuccess)
        {
            // Setup
            var manager   = MockHelpers.TestUserManager(new NoopUserStore());
            var validator = new UserValidator <PocoUser>();
            var user      = new PocoUser {
                UserName = userName
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            if (expectSuccess)
            {
                IdentityResultAssert.IsSuccess(result);
            }
            else
            {
                IdentityResultAssert.IsFailure(result);
            }
        }
Example #11
0
        public async Task SignOutCallsContextResponseSignOut(string authenticationScheme)
        {
            // Setup
            var manager = MockHelpers.TestUserManager <TestUser>();

            manager.Options.Cookies.ApplicationCookie.AuthenticationScheme = authenticationScheme;
            var context = new Mock <HttpContext>();
            var auth    = new Mock <AuthenticationManager>();

            context.Setup(c => c.Authentication).Returns(auth.Object).Verifiable();
            auth.Setup(a => a.SignOutAsync(authenticationScheme)).Returns(Task.FromResult(0)).Verifiable();
            auth.Setup(a => a.SignOutAsync(manager.Options.Cookies.TwoFactorUserIdCookieAuthenticationScheme)).Returns(Task.FromResult(0)).Verifiable();
            auth.Setup(a => a.SignOutAsync(manager.Options.Cookies.ExternalCookieAuthenticationScheme)).Returns(Task.FromResult(0)).Verifiable();
            var helper = SetupSignInManager(manager, context.Object, null, manager.Options);

            // Act
            await helper.SignOutAsync();

            // Assert
            context.Verify();
            auth.Verify();
        }
Example #12
0
        public async Task CanAllowNonAlphaNumericUserName(string userName, bool expectSuccess)
        {
            // Setup
            var manager = MockHelpers.TestUserManager(new NoopUserStore());

            manager.Options.User.AllowedUserNameCharacters = null;
            var validator = new UserValidator <PocoUser>();
            var user      = new PocoUser {
                UserName = userName
            };

            // Act
            var result = await validator.ValidateAsync(manager, user);

            // Assert
            if (expectSuccess)
            {
                IdentityResultAssert.IsSuccess(result);
            }
            else
            {
                IdentityResultAssert.IsFailure(result);
            }
        }