Exemplo n.º 1
0
        public async Task MissingAuthorizationHeader_ExpectFailure()
        {
            var httpContext = new DefaultHttpContext();
            var httpRequest = httpContext.Request;
            var scheme      = new AuthenticationScheme("BasicAuthorisation", "BasicAuthorisation", typeof(BasicAuthenticationHandler));
            var userService = new Mock <IUserService>();

            var authenticateResult = await BasicAuthenticationHandlerCore.HandleAuthenticateAsync(httpRequest, scheme, userService.Object);

            Assert.IsNotNull(authenticateResult.Failure);
            Assert.IsFalse(authenticateResult.Succeeded);
        }
Exemplo n.º 2
0
        public async Task AuthorizationHeaderPresentWithInvalidCredentials_ExpectFailure()
        {
            var httpContext = new DefaultHttpContext();
            var httpRequest = httpContext.Request;
            var scheme      = new AuthenticationScheme("BasicAuthorisation", "BasicAuthorisation", typeof(BasicAuthenticationHandler));
            var userService = new Mock <IUserService>();

            // Add authorization header - username = abc  password = xyz
            httpRequest.Headers.Add("Authorization", "Basic YWJjOnh5eg==");

            var authenticateResult = await BasicAuthenticationHandlerCore.HandleAuthenticateAsync(httpRequest, scheme, userService.Object);

            Assert.IsNotNull(authenticateResult.Failure);
            Assert.IsFalse(authenticateResult.Succeeded);
        }
Exemplo n.º 3
0
        public async Task ExpectUserNameAndPasswordToBePassedToUserService()
        {
            var httpContext = new DefaultHttpContext();
            var httpRequest = httpContext.Request;
            var scheme      = new AuthenticationScheme("BasicAuthorisation", "BasicAuthorisation", typeof(BasicAuthenticationHandler));
            var userService = new Mock <IUserService>();

            userService.Setup(t => t.Authenticate(It.IsAny <string>(), It.IsAny <string>())).Returns(async() => new User("abc"));

            // Add authorization header - username = abc  password = xyz
            httpRequest.Headers.Add("Authorization", "Basic YWJjOnh5eg==");

            var authenticateResult = await BasicAuthenticationHandlerCore.HandleAuthenticateAsync(httpRequest, scheme, userService.Object);

            // Exception thrown if this condition has not been met
            userService.Verify(t => t.Authenticate("abc", "xyz"));
        }
Exemplo n.º 4
0
        public async Task AuthorizationHeaderPresentWithValidCredentials_ExpectSuccess()
        {
            var httpContext = new DefaultHttpContext();
            var httpRequest = httpContext.Request;
            var scheme      = new AuthenticationScheme("BasicAuthorisation", "BasicAuthorisation", typeof(BasicAuthenticationHandler));
            var userService = new Mock <IUserService>();

            userService.Setup(t => t.Authenticate(It.IsAny <string>(), It.IsAny <string>())).Returns(async() =>
            {
                var user = new User("abc");
                await user.AuthenticateUser("123");
                return(user);
            });

            // Add authorization header - username = abc  password = xyz
            httpRequest.Headers.Add("Authorization", "Basic YWJjOnh5eg==");

            var authenticateResult = await BasicAuthenticationHandlerCore.HandleAuthenticateAsync(httpRequest, scheme, userService.Object);

            Assert.IsNull(authenticateResult.Failure);
            Assert.IsTrue(authenticateResult.Succeeded);
        }