public void Test_If_NoneOfThe_Authenticators_Are_Called_If_Already_Whitelisted_And_Authenticated()
        {
            var claimsPrincipal = new ClaimsPrincipal(
                new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, "Foo User"),
            }, AuthConstants.SPNEGO_DEFAULT_SCHEME));

            var whitelistPath     = "/whitelistpath";
            var mockConfiguration = new Dictionary <string, string>()
            {
                { AuthConstants.WHITELIST_PATHS_CSV_NM, whitelistPath }
            };

            request.SetupGet(r => r.Path).Returns("/whitelistpath");

            context.SetupGet(c => c.User).Returns(claimsPrincipal);

            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(mockConfiguration), logger.Object);

            handler.HandleRequest(context.Object);

            cookieAuthenticator.Verify(ca => ca.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Challenge(It.IsAny <AuthenticationProperties>(), context.Object), Times.Never);
            cookieAuthenticator.Verify(ca => ca.SignIn(It.IsAny <AuthenticateResult>(), context.Object), Times.Never);
        }
        public void Test_If_NoneOfThe_Authenticators_Are_Called_ForDefaultWhitelistedPath(string path)
        {
            request.SetupGet(r => r.Path).Returns(path);

            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(), logger.Object);

            handler.HandleRequest(context.Object);

            cookieAuthenticator.Verify(ca => ca.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Challenge(It.IsAny <AuthenticationProperties>(), context.Object), Times.Never);
            cookieAuthenticator.Verify(ca => ca.SignIn(It.IsAny <AuthenticateResult>(), context.Object), Times.Never);
        }
        public void Test_IfChallengeIsCalled_If_Both_Authenticators_ReturnsNotSuccess()
        {
            var serializer = new TicketSerializer();
            var ticket     = new AuthenticationTicket(
                new ClaimsPrincipal(
                    new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, "Foo User"),
            }, AuthConstants.SPNEGO_DEFAULT_SCHEME)),
                AuthConstants.SPNEGO_DEFAULT_SCHEME);

            var encodedTicket = Convert.ToBase64String(serializer.Serialize(ticket));

            var cookie = new HttpCookie(AuthConstants.AUTH_COOKIE_NM)
            {
                Expires = DateTime.Now.AddDays(1),
                Value   = encodedTicket
            };

            cookies.Set(cookie);

            cookieAuthenticator.Setup(ca => ca.Authenticate(context.Object)).Returns(AuthenticateResult.NoResult());
            spnegoAuthenticator.Setup(sa => sa.Authenticate(context.Object)).Returns(AuthenticateResult.NoResult());

            var whitelistPath     = "/whitelistpath";
            var mockConfiguration = new Dictionary <string, string>()
            {
                { AuthConstants.WHITELIST_PATHS_CSV_NM, whitelistPath }
            };

            request.SetupGet(r => r.Path).Returns("/anypath");

            context.SetupGet(c => c.User).Returns(() => { return(null); });

            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(), logger.Object);

            handler.HandleRequest(context.Object);

            cookieAuthenticator.Verify(ca => ca.Authenticate(context.Object), Times.Once);
            spnegoAuthenticator.Verify(sa => sa.Authenticate(context.Object), Times.Once);
            spnegoAuthenticator.Verify(sa => sa.Challenge(It.IsAny <AuthenticationProperties>(), context.Object), Times.Once);
            cookieAuthenticator.Verify(ca => ca.SignIn(It.IsAny <AuthenticateResult>(), context.Object), Times.Never);
            context.VerifySet(c => c.User = ticket.Principal, Times.Never);
        }
        public void Test_If_NoneOfThe_Authenticators_Are_Called_If_Whitelisted()
        {
            var whitelistPath     = "/whitelistpath";
            var mockConfiguration = new Dictionary <string, string>()
            {
                { AuthConstants.WHITELIST_PATHS_CSV_NM, whitelistPath }
            };

            request.SetupGet(r => r.Path).Returns("/whitelistpath");

            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(mockConfiguration), logger.Object);

            handler.HandleRequest(context.Object);

            cookieAuthenticator.Verify(ca => ca.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Authenticate(context.Object), Times.Never);
            spnegoAuthenticator.Verify(sa => sa.Challenge(It.IsAny <AuthenticationProperties>(), context.Object), Times.Never);
            cookieAuthenticator.Verify(ca => ca.SignIn(It.IsAny <AuthenticateResult>(), context.Object), Times.Never);
        }
        public void Test_ApplicationEvent_Is_PostAuthenticateRequest()
        {
            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(), logger.Object);

            Assert.Equal(DynamicHttpHandlerEvent.PostAuthenticateRequest, handler.ApplicationEvent);
        }
        public void Test_PathIsNull()
        {
            var handler = new WindowsAuthenticationHandler(cookieAuthenticator.Object, spnegoAuthenticator.Object, GetConfiguration(), logger.Object);

            Assert.Null(handler.Path);
        }