Exemple #1
0
        public async Task CheckPasswordSignInReturnsLockedOutWhenLockedOut()
        {
            // Setup
            var user = new TestUser {
                UserName = "******"
            };
            var manager = SetupUserManager(user);

            manager.Setup(m => m.SupportsUserLockout).Returns(true).Verifiable();
            manager.Setup(m => m.IsLockedOutAsync(user)).ReturnsAsync(true).Verifiable();

            var context         = new Mock <HttpContext>();
            var contextAccessor = new Mock <IHttpContextAccessor>();

            contextAccessor.Setup(a => a.HttpContext).Returns(context.Object);
            var roleManager     = MockHelpers.MockRoleManager <TestRole>();
            var identityOptions = new IdentityOptions();
            var options         = new Mock <IOptions <IdentityOptions> >();

            options.Setup(a => a.Value).Returns(identityOptions);
            var claimsFactory = new UserClaimsPrincipalFactory <TestUser, TestRole>(manager.Object, roleManager.Object, options.Object);
            var logStore      = new StringBuilder();
            var logger        = MockHelpers.MockILogger <SignInManager <TestUser> >(logStore);
            var helper        = new SignInManager <TestUser>(manager.Object, contextAccessor.Object, claimsFactory, options.Object, logger.Object, new Mock <IAuthenticationSchemeProvider>().Object);

            // Act
            var result = await helper.CheckPasswordSignInAsync(user, "bogus", false);

            // Assert
            Assert.False(result.Succeeded);
            Assert.True(result.IsLockedOut);
            Assert.True(logStore.ToString().Contains($"User {user.Id} is currently locked out."));
            manager.Verify();
        }
Exemple #2
0
        private static SignInManager <TestUser> SetupSignInManager(UserManager <TestUser> manager, HttpContext context, StringBuilder logStore = null, IdentityOptions identityOptions = null)
        {
            var contextAccessor = new Mock <IHttpContextAccessor>();

            contextAccessor.Setup(a => a.HttpContext).Returns(context);
            var roleManager = MockHelpers.MockRoleManager <TestRole>();

            identityOptions = identityOptions ?? new IdentityOptions();
            var options = new Mock <IOptions <IdentityOptions> >();

            options.Setup(a => a.Value).Returns(identityOptions);
            var claimsFactory = new UserClaimsPrincipalFactory <TestUser, TestRole>(manager, roleManager.Object, options.Object);
            var sm            = new SignInManager <TestUser>(manager, contextAccessor.Object, claimsFactory, options.Object, null, new Mock <IAuthenticationSchemeProvider>().Object);

            sm.Logger = MockHelpers.MockILogger <SignInManager <TestUser> >(logStore ?? new StringBuilder()).Object;
            return(sm);
        }