Ejemplo n.º 1
0
        public async Task Login_Should_Return_RedirectResult_When_IsInTheContextOfAuthorizationRequest_And_IsNativeClient_Are_False_And_ReturnUrl_Is_Not_Defined()
        {
            var localLoginRequest = new LocalLoginRequest
            {
                Email         = "*****@*****.**",
                Password      = "******",
                RememberLogin = false
            };
            var localLoginResultOutput = LocalLoginResultOutput.Ok(false, false);
            var urlHelperMock          = new Mock <IUrlHelper>(MockBehavior.Strict);

            _controller.Url = urlHelperMock.Object;

            _localLoginInteractorMock
            .Setup(x => x.ExecuteAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                       It.IsAny <string>())).ReturnsAsync(localLoginResultOutput);
            urlHelperMock.Setup(x => x.IsLocalUrl(It.IsAny <string>())).Returns(false);

            var result = await _controller.Login(localLoginRequest);

            var redirectResult = result.As <RedirectResult>();

            redirectResult.Url.Should().Be("~/");
            redirectResult.Permanent.Should().BeFalse();
        }
Ejemplo n.º 2
0
        public async Task Login_Should_Return_ViewResult_With_Errors_After_Unsuccessful_Credentials_Verification_When_Account_Is_Not_Found()
        {
            var localLoginRequest = new LocalLoginRequest
            {
                Email         = "*****@*****.**",
                Password      = "******",
                RememberLogin = false,
                ReturnUrl     = "~/"
            };
            var localLoginResultOutputErrors = new Collection <IError>
            {
                new Error(AccountErrorCodeEnumeration.NotFound, AccountErrorMessage.NotFound)
            };
            var localLoginResultOutput = LocalLoginResultOutput.Fail(false, localLoginResultOutputErrors);
            var externalProviders      = new List <LocalLoginExternalProviderOutput>
            {
                new LocalLoginExternalProviderOutput("google", AuthenticationExtension.GoogleAuthScheme),
                new LocalLoginExternalProviderOutput("facebook", AuthenticationExtension.FacebookAuthScheme)
            };
            var localLoginOutput            = new LocalLoginOutput(true, externalProviders);
            var expectedLocalLoginViewModel =
                new LocalLoginViewModel(true, true, true, string.Empty, string.Empty, string.Empty)
            {
                ReturnUrl     = localLoginRequest.ReturnUrl,
                Email         = localLoginRequest.Email,
                Password      = localLoginRequest.Password,
                RememberLogin = localLoginRequest.RememberLogin
            };
            var localLoginViewModelErrors = new List <string> {
                LocalLoginErrorMessage.InvalidCredentials
            };

            expectedLocalLoginViewModel.SetErrors(localLoginViewModelErrors);

            _localLoginInteractorMock
            .Setup(x => x.ExecuteAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                       It.IsAny <string>())).ReturnsAsync(localLoginResultOutput);
            _localLoginInteractorMock.Setup(x => x.ExecuteAsync(It.IsAny <string>())).ReturnsAsync(localLoginOutput);

            var result = await _controller.Login(localLoginRequest);

            var viewResult = result.As <ViewResult>();

            viewResult.Model.Should().BeEquivalentTo(expectedLocalLoginViewModel);
        }
Ejemplo n.º 3
0
        public async Task Login_Should_Return_ViewResult_With_Errors_When_IsInTheContextOfAuthorizationRequest_And_IsNativeClient_Are_False_And_ReturnUrl_Is_Invalid()
        {
            var localLoginRequest = new LocalLoginRequest
            {
                Email         = "*****@*****.**",
                Password      = "******",
                RememberLogin = false,
                ReturnUrl     = "http://nonEmptyNonLocalUrl.com"
            };
            var localLoginResultOutput = LocalLoginResultOutput.Ok(false, false);
            var urlHelperMock          = new Mock <IUrlHelper>(MockBehavior.Strict);

            _controller.Url = urlHelperMock.Object;
            var externalProviders = new List <LocalLoginExternalProviderOutput>
            {
                new LocalLoginExternalProviderOutput("google", AuthenticationExtension.GoogleAuthScheme),
                new LocalLoginExternalProviderOutput("facebook", AuthenticationExtension.FacebookAuthScheme)
            };
            var localLoginOutput            = new LocalLoginOutput(true, externalProviders);
            var expectedLocalLoginViewModel =
                new LocalLoginViewModel(true, true, true, string.Empty, string.Empty, string.Empty)
            {
                ReturnUrl     = localLoginRequest.ReturnUrl,
                Email         = localLoginRequest.Email,
                Password      = localLoginRequest.Password,
                RememberLogin = localLoginRequest.RememberLogin
            };
            var localLoginViewModelErrors = new List <string> {
                LocalLoginErrorMessage.InvalidReturnUrl
            };

            expectedLocalLoginViewModel.SetErrors(localLoginViewModelErrors);

            _localLoginInteractorMock
            .Setup(x => x.ExecuteAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                       It.IsAny <string>())).ReturnsAsync(localLoginResultOutput);
            urlHelperMock.Setup(x => x.IsLocalUrl(It.IsAny <string>())).Returns(false);
            _localLoginInteractorMock.Setup(x => x.ExecuteAsync(It.IsAny <string>())).ReturnsAsync(localLoginOutput);

            var result = await _controller.Login(localLoginRequest);

            var viewResult = result.As <ViewResult>();

            viewResult.Model.Should().BeEquivalentTo(expectedLocalLoginViewModel);
        }
Ejemplo n.º 4
0
        public async Task ExecuteAsync_Should_Return_LocalLoginResultOutput_With_Success_Re_When_ClientId_Is_Not_Provided_For_Given_ReturnUrl()
        {
            const string redirectUri = "http://localhost";
            var          authRequest = new AuthorizationRequest("idP", "clientId", redirectUri);
            var          account     = Account.Builder()
                                       .SetId(Guid.NewGuid())
                                       .SetEmail("*****@*****.**")
                                       .SetConfirmed(true)
                                       .SetPasswordHash("PasswordHash")
                                       .SetSecurityStamp(Guid.NewGuid())
                                       .SetCreated(DateTimeOffset.UtcNow)
                                       .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                                       .Build();
            var getAccountResult = GetResult <Account> .Ok(account);

            var accountCanBeAuthenticatedVerificationResult = VerificationResult.Ok();
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Email, account.Email)
            };
            var expectedResult = LocalLoginResultOutput.Ok(true, false);

            _authorizationServiceMock.Setup(x => x.GetAuthorizationRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(authRequest);
            _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>()))
            .ReturnsAsync(getAccountResult);
            _accountVerificationServiceMock
            .Setup(x => x.VerifyAccountCanBeAuthenticated(It.IsAny <Account>(), It.IsAny <string>()))
            .Returns(accountCanBeAuthenticatedVerificationResult);
            _accountClaimsCreatorServiceMock.Setup(x => x.CreateAccountClaimsAsync(It.IsAny <Account>()))
            .ReturnsAsync(claims);
            _signInServiceMock
            .Setup(x => x.SignInAsync(It.IsAny <Guid>(), It.IsAny <string>(), It.IsAny <bool>(),
                                      It.IsAny <IEnumerable <Claim> >())).Returns(Task.CompletedTask);

            var result = await _localLoginInteractor.ExecuteAsync(account.Email, "Password", true, redirectUri);

            result.Should().BeEquivalentTo(expectedResult);
        }
Ejemplo n.º 5
0
        public async Task ExecuteAsync_Should_Return_LocalLoginResultOutput_With_Fail_When_Account_Does_Not_Exist()
        {
            const string redirectUri = "http://localhost";
            var          authRequest = new AuthorizationRequest("idP", "clientId", redirectUri);
            var          errors      = new Collection <IError>
            {
                new Error(AccountErrorCodeEnumeration.NotFound, AccountErrorMessage.NotFound)
            };
            var getAccountResult = GetResult <Account> .Fail(errors);

            var expectedResult = LocalLoginResultOutput.Fail(true, errors);

            _authorizationServiceMock.Setup(x => x.GetAuthorizationRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(authRequest);
            _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>()))
            .ReturnsAsync(getAccountResult);

            var result = await _localLoginInteractor.ExecuteAsync("*****@*****.**", "Password", true, redirectUri);

            result.Should().BeEquivalentTo(expectedResult);
        }
Ejemplo n.º 6
0
        public async Task Login_Should_Return_RedirectResult_When_IsInTheContextOfAuthorizationRequest_Is_True_And_IsNativeClient_Is_False()
        {
            var localLoginRequest = new LocalLoginRequest
            {
                Email         = "*****@*****.**",
                Password      = "******",
                ReturnUrl     = "http://returnUrl.com",
                RememberLogin = false
            };
            var localLoginResultOutput = LocalLoginResultOutput.Ok(true, false);

            _localLoginInteractorMock
            .Setup(x => x.ExecuteAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                       It.IsAny <string>())).ReturnsAsync(localLoginResultOutput);

            var result = await _controller.Login(localLoginRequest);

            var redirectResult = result.As <RedirectResult>();

            redirectResult.Url.Should().BeEquivalentTo(localLoginRequest.ReturnUrl);
        }
Ejemplo n.º 7
0
        public async Task Login_Should_Return_ViewResult_With_Redirect_When_IsInTheContextOfAuthorizationRequest_And_IsNativeClient_Are_True()
        {
            var localLoginRequest = new LocalLoginRequest
            {
                Email         = "*****@*****.**",
                Password      = "******",
                ReturnUrl     = "http://returnUrl.com",
                RememberLogin = false
            };
            var localLoginResultOutput    = LocalLoginResultOutput.Ok(true, true);
            var expectedRedirectViewModel = new RedirectViewModel(localLoginRequest.ReturnUrl);

            _localLoginInteractorMock
            .Setup(x => x.ExecuteAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <bool>(),
                                       It.IsAny <string>())).ReturnsAsync(localLoginResultOutput);

            var result = await _controller.Login(localLoginRequest);

            var viewResult = result.As <ViewResult>();

            viewResult.ViewName.Should().BeEquivalentTo("Redirect");
            viewResult.Model.Should().BeEquivalentTo(expectedRedirectViewModel);
        }
Ejemplo n.º 8
0
        public async Task ExecuteAsync_Should_Return_LocalLoginResultOutput_With_Fail_When_Account_Cannot_Be_Authenticated()
        {
            const string redirectUri = "http://localhost";
            var          authRequest = new AuthorizationRequest("idP", "clientId", redirectUri);
            var          account     = Account.Builder()
                                       .SetId(Guid.NewGuid())
                                       .SetEmail("*****@*****.**")
                                       .SetConfirmed(true)
                                       .SetPasswordHash("PasswordHash")
                                       .SetSecurityStamp(Guid.NewGuid())
                                       .SetCreated(DateTimeOffset.UtcNow)
                                       .SetRoles(new List <Guid> {
                Guid.NewGuid()
            })
                                       .Build();
            var getAccountResult = GetResult <Account> .Ok(account);

            var errors = new Collection <IError>
            {
                new Error(AccountErrorCodeEnumeration.NotConfirmed, AccountErrorMessage.NotConfirmed)
            };
            var accountCanBeAuthenticatedVerificationResult = VerificationResult.Fail(errors);
            var expectedResult = LocalLoginResultOutput.Fail(true, errors);

            _authorizationServiceMock.Setup(x => x.GetAuthorizationRequestAsync(It.IsAny <string>()))
            .ReturnsAsync(authRequest);
            _accountGetterServiceMock.Setup(x => x.GetByEmailAsync(It.IsAny <string>()))
            .ReturnsAsync(getAccountResult);
            _accountVerificationServiceMock
            .Setup(x => x.VerifyAccountCanBeAuthenticated(It.IsAny <Account>(), It.IsAny <string>()))
            .Returns(accountCanBeAuthenticatedVerificationResult);

            var result = await _localLoginInteractor.ExecuteAsync("*****@*****.**", "Password", true, redirectUri);

            result.Should().BeEquivalentTo(expectedResult);
        }