Пример #1
0
        public void DeleteUserById()
        {
            // Arrange
            var id   = Guid.NewGuid();
            var user = new Database.User()
            {
                Id        = id,
                Email     = "*****@*****.**",
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user.UpdatePassword("Whatever");
            _dbContext.Add(user);
            _dbContext.SaveChanges();

            // Act
            _userService.DeleteUserById(id);

            // Assert
            var userWhichWasInDb = _dbContext.Users.Where(user => user.Id == id).SingleOrDefault();

            Assert.Null(userWhichWasInDb);
        }
Пример #2
0
        public void Login()
        {
            // Arrange
            var userLogin = new Contracts.UserLogin()
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var user = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = userLogin.Email,
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user.UpdatePassword(userLogin.Password);
            _dbContext.Add(user);
            _dbContext.SaveChanges();
            _configurationMock.SetupGet(c => c[It.Is <string>(cv => cv == "JWT-IssuerSigningKey")]).Returns("aYPg2QjKQBY4Uqx8");

            // Act
            var loggedInUser = _userService.Login(userLogin);

            // Assert
            _configurationMock.VerifyGet(c => c["JWT-IssuerSigningKey"], Times.Once());
            Assert.Equal(loggedInUser.Email, userLogin.Email);
        }
Пример #3
0
        public void UpdateUserById()
        {
            // Arrange
            var id         = Guid.NewGuid();
            var updateUser = new Contracts.UserUpdate()
            {
                Email     = "*****@*****.**",
                FirstName = "Iam",
                LastName  = "Different",
                Password  = "******"
            };
            var user = new Database.User()
            {
                Id        = id,
                Email     = "*****@*****.**",
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user.UpdatePassword("Whatever");
            _dbContext.Add(user);
            _dbContext.SaveChanges();

            // Act
            var updatedUser = _userService.UpdateUserById(id, updateUser);

            // Assert
            Assert.Equal(updateUser.Email, updatedUser.Email);
            Assert.Equal(updateUser.FirstName, updatedUser.FirstName);
            Assert.Equal(updateUser.LastName, updatedUser.LastName);
        }
Пример #4
0
        public void GetUserByEmail()
        {
            // Arrange
            var email = "*****@*****.**";
            var user  = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = email,
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user.UpdatePassword("Whatever");
            _dbContext.Add(user);
            _dbContext.SaveChanges();

            // Act
            var userById = _userService.GetUserByEmail(email);

            // Assert
            Assert.Equal(user.Id, userById.Id);
            Assert.Equal(user.Email, userById.Email);
            Assert.Equal(user.FirstName, userById.FirstName);
            Assert.Equal(user.LastName, userById.LastName);
            Assert.Equal(user.Role, userById.Role);
        }
Пример #5
0
        public void Register_EmailAlradyExists()
        {
            // Arrange
            var registration = new Contracts.Registration()
            {
                Email     = "*****@*****.**",
                FirstName = "Unit",
                LastName  = "Test",
                Password  = "******"
            };
            var user = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = registration.Email,
                FirstName = registration.FirstName,
                LastName  = registration.LastName,
                Role      = Database.Role.Trader
            };

            user.UpdatePassword("PasswordBecauseItIsRequired");
            _dbContext.Add(user);
            _dbContext.SaveChanges();

            // Act
            // Assert
            var exception = Assert.Throws <ProblemDetailsException>(() => _userService.Register(registration));

            Assert.Equal(HttpStatusCode.BadRequest, exception.Status);
        }
Пример #6
0
        public void Login_InvalidPassword()
        {
            // Arrange
            var userLogin = new Contracts.UserLogin()
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var user = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = userLogin.Email,
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user.UpdatePassword("AndThisIsAsWellButDifferent");
            _dbContext.Add(user);
            _dbContext.SaveChanges();

            // Act
            // Assert
            var exception = Assert.Throws <ProblemDetailsException>(() => _userService.Login(userLogin));

            Assert.Equal(HttpStatusCode.NotFound, exception.Status);
            _configurationMock.VerifyGet(c => c["JWT-IssuerSigningKey"], Times.Never());
        }
Пример #7
0
        public void GetAllUsers()
        {
            // Arrange
            var user1 = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = "*****@*****.**",
                FirstName = "Unit",
                LastName  = "Test",
                Role      = Database.Role.Trader
            };

            user1.UpdatePassword("Whatever");
            _dbContext.Add(user1);

            var user2 = new Database.User()
            {
                Id        = Guid.NewGuid(),
                Email     = "*****@*****.**",
                FirstName = "Unit2",
                LastName  = "Test2",
                Role      = Database.Role.Admin
            };

            user2.UpdatePassword("Different");
            _dbContext.Add(user2);

            _dbContext.SaveChanges();

            // Act
            var users = _userService.GetAllUsers();

            // Assert
            users = users.OrderBy(user => user.Email);
            Assert.Equal(2, users.Count());
            Assert.Equal(user1.Email, users.ElementAt(0).Email);
            Assert.Equal(user2.Email, users.ElementAt(1).Email);
        }