Beispiel #1
0
        public async Task When_User_Enter_Invalid_Password_Throw_Exception(LoginUserQuery model)
        {
            //Arrange
            _passwordManager.Setup(x => x.CreatePasswordHash(It.IsAny <string>())).Returns(new Tuple <byte[], byte[]>(CreateString(), CreateString()));
            _passwordManager.Setup(x => x.VerifyPassword(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <byte[]>())).Returns(false);
            var user = CreateUser("*****@*****.**", _passwordManager.Object);

            _userRepository.Setup(x => x.GetByEmail(It.IsAny <string>())).ReturnsAsync(user);

            //Assert
            await Assert.ThrowsAsync <AppException>(() => _sut.Handle(model, default));
        }
Beispiel #2
0
        [HttpPost] //done
        public HttpResponseMessage Index(LoginUserRequest request)
        {
            var handler = new LoginUserHandler(_connectionString);

            var response = handler.Handle(request);

            var httpSatusCode = ResolveStatusCode(response);

            return(Request.CreateResponse(httpSatusCode, response));
        }
Beispiel #3
0
        public async Task Should_ReturnNotFound_When_PassUnknownUsername(string username, string password)
        {
            var cancellationToken = new CancellationToken();

            var jwtSecurityTokenHandler = Substitute.For <JwtSecurityTokenHandler>();
            var userPasswordUtility     = Substitute.For <IUserPasswordUtility>();

            var configuration = Substitute.For <IConfiguration>();

            configuration["Jwt:Issuer"] = "TestIssuer";
            configuration["Jwt:Secret"] = "TestSecret";


            var databaseContext = Create.MockedDbContextFor <DatabaseContext>();

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 1,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password1")
            });

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 2,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password2")
            });

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 3,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password3")
            });

            databaseContext.SaveChanges();

            var entity = new LoginUserRequest(username, password);

            var handler = new LoginUserHandler(databaseContext, userPasswordUtility, configuration, jwtSecurityTokenHandler);
            var result  = await handler.Handle(entity, cancellationToken);

            Assert.Multiple(() =>
            {
                Assert.IsFalse(result.Success);
                Assert.AreEqual(result.StatusCode, HttpStatusCode.NotFound);
            });
        }
Beispiel #4
0
        public async Task Should_ReturnToken_When_PassCorrectParameters(string username, string password)
        {
            var cancellationToken = new CancellationToken();

            var jwtSecurityTokenHandler = Substitute.For <JwtSecurityTokenHandler>();
            var userPasswordUtility     = Substitute.For <IUserPasswordUtility>();
            //userPasswordUtility.When(x => x.IsPasswordCorrect(default, default, default)).DoNotCallBase();
            var salt = Encoding.ASCII.GetBytes("UserPasswordTest");

            userPasswordUtility.IsPasswordCorrect(password, salt, password).Returns(true);

            var configuration = Substitute.For <IConfiguration>();

            configuration["Jwt:Issuer"] = "TestIssuer";
            configuration["Jwt:Secret"] = "TestSecret";


            var databaseContext = Create.MockedDbContextFor <DatabaseContext>();

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 1,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password1")
            });

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 2,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password2")
            });

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 3,
                Password = "******",
                Username = "******",
                Salt     = Encoding.ASCII.GetBytes("password3")
            });

            databaseContext.Set <SystemUser>().Add(new SystemUser()
            {
                Id       = 4,
                Password = password,
                Username = username,
                Salt     = salt
            });

            databaseContext.SaveChanges();

            var entity = new LoginUserRequest(username, password);

            var handler = new LoginUserHandler(databaseContext, userPasswordUtility, configuration, jwtSecurityTokenHandler);
            var result  = await handler.Handle(entity, cancellationToken);

            Assert.Multiple(() =>
            {
                Assert.IsTrue(result.Success);
                Assert.AreEqual(result.StatusCode, HttpStatusCode.OK);
            });
        }