AuthenticateAsync_AuthorizationBasic_AuthorizationParameterSet_LoginDetailsParsed_Authenticated_PrincipalSet
            ()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var authorizationParameter = "gdzfdhhtgfhf";
            var context      = GetHttpAuthenticationContext("Basic", authorizationParameter);
            var loginDetails = new LoginDetails("a", "b");

            authorisationParameterParserMock.Setup(x => x.Parse(authorizationParameter, It.IsAny <char>()))
            .Returns(loginDetails);
            var principal = new Mock <IPrincipal>().Object;

            authenticationServiceMock.Setup(
                x =>
                x.AuthenticateAsync(loginDetails.Username, loginDetails.Password,
                                    It.IsAny <CancellationToken>()))
            .ReturnsAsync(principal);

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Equal(principal, context.Principal);
        }
        AuthenticateAsync_AuthorizationBasic_AuthorizationParameterSet_LoginDetailsParsed_NotAuthenticated_ErrorResultSet
            ()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var authorizationParameter = "gdzfdhhtgfhf";
            var context      = GetHttpAuthenticationContext("Basic", authorizationParameter);
            var loginDetails = new LoginDetails("a", "b");

            authorisationParameterParserMock.Setup(x => x.Parse(authorizationParameter, It.IsAny <char>()))
            .Returns(loginDetails);
            authenticationServiceMock.Setup(
                x =>
                x.AuthenticateAsync(loginDetails.Username, loginDetails.Password,
                                    It.IsAny <CancellationToken>()))
            .ReturnsAsync((IPrincipal)null);

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Equal("Invalid username or password",
                         ((AuthenticationFailureResult)context.ErrorResult).ReasonPhrase);
        }
        public async Task AuthenticateAsync_AuthorizationBasic_AuthorizationParameterNull_ErrorResultSet()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var context = GetHttpAuthenticationContext("Basic");

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Equal("Missing credentials", ((AuthenticationFailureResult)context.ErrorResult).ReasonPhrase);
        }
        public async Task AuthenticateAsync_AuthorizationNotBasic_PrincipalNull()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var context = GetHttpAuthenticationContext("hjbsjhbdshj");

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Null(context.Principal);
        }
        public async Task AuthenticateAsync_AuthorizationNull_ErrorResultNull()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var context = GetHttpAuthenticationContext();

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Null(context.ErrorResult);
        }
        AuthenticateAsync_AuthorizationBasic_AuthorizationParameterSet_LoginDetailsNotParsed_PrincipalNull()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var authorizationParameter = "gdzfdhhtgfhf";
            var context = GetHttpAuthenticationContext("Basic", authorizationParameter);

            authorisationParameterParserMock.Setup(x => x.Parse(authorizationParameter, It.IsAny <char>()))
            .Returns((LoginDetails)null);

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Null(context.Principal);
        }
        AuthenticateAsync_AuthorizationBasic_AuthorizationParameterSet_LoginDetailsNotParsed_ErrorResultSet()
        {
            //Arrange
            var authenticationServiceMock        = new Mock <IAuthenticationService>();
            var authorisationParameterParserMock = new Mock <IAuthorisationParameterParser>();
            var sut = new BasicAuthenticationFilter(authenticationServiceMock.Object,
                                                    authorisationParameterParserMock.Object);
            var authorizationParameter = "gdzfdhhtgfhf";
            var context = GetHttpAuthenticationContext("Basic", authorizationParameter);

            authorisationParameterParserMock.Setup(x => x.Parse(authorizationParameter, It.IsAny <char>()))
            .Returns((LoginDetails)null);

            //Act
            await
            sut.AuthenticateAsync(context,
                                  CancellationToken.None);

            //Assert
            Assert.Equal("Invalid credentials", ((AuthenticationFailureResult)context.ErrorResult).ReasonPhrase);
        }