Exemple #1
0
        public async Task When_Handling_Query_Then_Returns_Token_Model(LoginQueryModel model)
        {
            // Act
            var response = await sut.Handle(model, default);

            // Assert
            response.Should().BeOfType <TokenModel>();
        }
        public async Task HandleShouldReturnFailWhenWrongPassword()
        {
            // Arrange
            var id = Guid.NewGuid();

            var userFinderMock = new Mock <IUserFinder>();

            userFinderMock.Setup(x => x.Get(It.IsAny <string>())).Returns(Task.FromResult(new User(id, new Login("login"), "password", new FullName(new FirstName("FirstName"), new LastName("FirstName")))));
            var userFinder = userFinderMock.Object;

            var passwordProviderMock = new Mock <IPasswordProvider>();

            passwordProviderMock.Setup(x => x.Verify(It.IsAny <string>(), It.IsAny <string>())).Returns(false);
            var passwordProvider = passwordProviderMock.Object;

            var query = new LoginQuery(null, null);

            var handler = new LoginQueryHandler(userFinder, passwordProvider);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.Failures.Should().OnlyContain(x => x.Code == HandlerFaultCode.InvalidCredentials.Name &&
                                                 x.Message == HandlerFailures.InvalidCredentials &&
                                                 x.Target == "credentials");
        }
        public async Task HandleShouldReturnOk()
        {
            // Arrange
            var id = Guid.NewGuid();

            var userFinderMock = new Mock <IUserFinder>();

            userFinderMock.Setup(x => x.Get(It.IsAny <string>())).Returns(Task.FromResult(new User(id, new Login("login"), "password", new FullName(new FirstName("FirstName"), new LastName("FirstName")))));
            var userFinder = userFinderMock.Object;

            var passwordProviderMock = new Mock <IPasswordProvider>();

            passwordProviderMock.Setup(x => x.Verify(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            var passwordProvider = passwordProviderMock.Object;

            var query = new LoginQuery(null, null);

            var handler = new LoginQueryHandler(userFinder, passwordProvider);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeFalse();
            result.Should().BeOfType(typeof(Result <AuthenticatedUserModel>));
        }
        public async Task HandleShouldReturnFailWhenDatabaseSpecificErrorOccurs()
        {
            var id = Guid.NewGuid();

            var userFinderMock = new Mock <IUserFinder>();

            userFinderMock.Setup(x => x.Get(id)).Throws <SomeDatabaseSpecificException>();
            var userFinder = userFinderMock.Object;

            var passwordProviderMock = new Mock <IPasswordProvider>();

            passwordProviderMock.Setup(x => x.Verify(It.IsAny <string>(), It.IsAny <string>())).Returns(true);
            var passwordProvider = passwordProviderMock.Object;

            var query = new LoginQuery(null, null);

            var handler = new LoginQueryHandler(userFinder, passwordProvider);

            // Act
            var result = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.IsFailure.Should().BeTrue();
            result.Failures.Should().OnlyContain(x => x.Message == CustomFailures.LoginUserFailure);
        }
        public async Task ShouldReturnFail_AfterHandle_PrioritiesNull()
        {
            // _collateralPriorityRepository.GetCollateralPriority().Returns((IEnumerable<CollateralPriority>)null);

            Response <ValidateLoginResponse> response = await _handler.Handle(new ValidateLoginQuery("login", "senha"), CancellationToken.None);

            response.IsFailure.Should().BeTrue();
            response.Messages.Contains(Messages.UnavailableCollateralPriorityList).Should().BeTrue();
        }
Exemple #6
0
        public IActionResult Login([FromQuery] LoginQueryBinder loginQueryBinder)
        {
            try
            {
                _loginQueryHandler.Handle(
                    loginQueryBinder.GetQuery()
                    );
            }
            catch (InvalidCredentialsException e)
            {
                return(BadRequest());
            }

            return(Ok());
        }
        public HttpResponseMessage Login([FromUri] UserDomainModel user)
        {
            if (string.IsNullOrEmpty(user.Username))
            {
                throw new NullReferenceException(nameof(user.Username));
            }

            if (string.IsNullOrEmpty(user.PasswordHash))
            {
                throw new NullReferenceException(nameof(user.PasswordHash));
            }

            LoginQuery query = new LoginQuery()
            {
                PasswordHash = user.PasswordHash,
                Username     = user.Username
            };

            LoginQueryHandler handler = new LoginQueryHandler(query, UnityContainer);


            return(Request.CreateResponse(HttpStatusCode.OK, handler.Handle()));
        }
 public IActionResult Post([FromBody] LoginQuery query)
 {
     return(StatusCode(201, _queryHandler.Handle(query)));
 }
Exemple #9
0
        public HttpResponseMessage Login([FromBody] JObject data)
        {
            JToken parameterUsername = data["Username"];
            JToken parameterPassword = data["Password"];

            try
            {
                if (string.IsNullOrEmpty(parameterUsername.ToString()))
                {
                    throw new ArgumentNullException(nameof(parameterUsername));
                }

                if (string.IsNullOrEmpty(parameterPassword.ToString()))
                {
                    throw new ArgumentNullException(nameof(parameterPassword));
                }

                LoginQuery loginQuery = new LoginQuery()
                {
                    Username = parameterUsername.ToString(),
                    Password = parameterPassword.ToString()
                };

                LoginQueryHandler loginQueryHandler = new LoginQueryHandler(loginQuery, UnityContainer);

                Guid personUid = loginQueryHandler.Handle();

                HttpCookie requestCookie = HttpContext.Current.Request.Cookies.Get(ConfigurationManager.AppSettings["SessionCookieName"]);

                if (requestCookie == null)
                {
                    CreateSessionQuery createSessionQuery = new CreateSessionQuery()
                    {
                        Session = new SessionDomainModel()
                        {
                            LoginUID   = personUid,
                            ExpireDate = DateTimeOffset.Now.AddDays(1)
                        }
                    };

                    CreateSessionQueryHandler createSessionQueryHandler = new CreateSessionQueryHandler(createSessionQuery, new UnityContainer());

                    var guid = createSessionQueryHandler.Handle().ToString();

                    HttpCookie cookie = new HttpCookie(ConfigurationManager.AppSettings["SessionCookieName"])
                    {
                        Value    = guid,
                        Domain   = HttpContext.Current.Request.Url.Host,
                        Expires  = DateTime.Now.AddDays(1),
                        HttpOnly = false,
                    };

                    var ersponse = HttpContext.Current.Response;

                    HttpContext.Current.Response.AppendCookie(cookie);

                    return(Request.CreateResponse(HttpStatusCode.OK, guid));
                }

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }