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
        private async Task <LocalLoginViewModel> BuildLoginViewModelWithErrorsAsync(LocalLoginRequest request, IEnumerable <string> errors)
        {
            var vm = await BuildLoginViewModelAsync(request.ReturnUrl);

            vm.SetErrors(errors);
            vm.Email         = request.Email;
            vm.RememberLogin = request.RememberLogin;
            vm.Password      = request.Password;
            return(vm);
        }
Ejemplo n.º 3
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.º 4
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.º 5
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.º 6
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.º 7
0
        public async Task <IActionResult> Login(LocalLoginRequest request)
        {
            IEnumerable <string> errors;
            var localLoginResultOutput = await _localLoginInteractor.ExecuteAsync(request.Email, request.Password, request.RememberLogin, request.ReturnUrl);

            if (localLoginResultOutput.Success)
            {
                if (localLoginResultOutput.IsInTheContextOfAuthorizationRequest)
                {
                    if (localLoginResultOutput.IsNativeClient.HasValue && localLoginResultOutput.IsNativeClient.Value)
                    {
                        return(this.LoadingPage("Redirect", request.ReturnUrl));
                    }
                    return(Redirect(request.ReturnUrl));
                }

                if (Url.IsLocalUrl(request.ReturnUrl))
                {
                    return(Redirect(request.ReturnUrl));
                }
                if (string.IsNullOrWhiteSpace(request.ReturnUrl))
                {
                    return(Redirect("~/"));
                }
                errors = new List <string> {
                    LocalLoginErrorMessage.InvalidReturnUrl
                };
            }
            else
            {
                errors = MapToErrorMessages(localLoginResultOutput.Errors);
            }

            var vm = await BuildLoginViewModelWithErrorsAsync(request, errors);

            return(View(vm));
        }