public void GetByLogin_LoginExists()
        {
            var userRepository = new UserRepository(_contextFactory);
            var authenticationRepository = new LoginAuthenticationRepository(_contextFactory);

            var user = new User { Name = "name", Email = "email" };
            var userId = userRepository.Create(user);

            var initalLoginAuth = new LoginAuthentication
            {
                UserId = userId,
                LoginName = "login",
                PasswordHash = Enumerable.Range(0, 32).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 16).Select(i => (byte)i).ToArray()
            };

            authenticationRepository.Save(initalLoginAuth);

            var restoredLoginAuthentication = authenticationRepository.GetByLogin(initalLoginAuth.LoginName);

            Assert.NotNull(restoredLoginAuthentication);
            Assert.AreEqual(initalLoginAuth.UserId, restoredLoginAuthentication.UserId);
            Assert.AreEqual(initalLoginAuth.LoginName, restoredLoginAuthentication.LoginName);
            CollectionAssert.AreEqual(initalLoginAuth.Salt, restoredLoginAuthentication.Salt);
            CollectionAssert.AreEqual(initalLoginAuth.PasswordHash, restoredLoginAuthentication.PasswordHash);
        }
        public void CreateUser_General(long id, string name, string email)
        {
            var user = new User {Id = id, Name = name, Email = email};

            var repository = new UserRepository(_contextFactory);

            var userId = repository.Create(user);
            Assert.Greater(userId, 0);
        }
        public void GetById_UserExists()
        {
            var repository = new UserRepository(_contextFactory);

            var savedUser = new User { Name = "name", Email = "email" };
            var userId = repository.Create(savedUser);
            var retrievedUser = repository.GetById(userId);

            Assert.IsNotNull(retrievedUser);
            Assert.AreEqual(userId, retrievedUser.Id);
            Assert.AreEqual(savedUser.Name, retrievedUser.Name);
            Assert.AreEqual(savedUser.Email, retrievedUser.Email);
        }
Esempio n. 4
0
        public long Create(User user)
        {
            if (user == null) throw new ArgumentNullException(nameof(user));
            if(user.Id != 0) throw new Exception<EntityHasAssignedKey>();
            if (user.Name == null) throw new Exception<RequiredFieldNotInitialized>(new RequiredFieldNotInitialized(nameof(user.Name)));
            if (user.Email == null) throw new Exception<RequiredFieldNotInitialized>(new RequiredFieldNotInitialized(nameof(user.Email)));

            var context = _contextFactory.GetContext();
            var createdUser = context.Users.Add(user);
            context.SaveChanges();

            return createdUser.Id;
        }
        public void GetUser_General(long id)
        {
            var user = new User
            {
                Id = id,
                Name = "name",
                Email = "*****@*****.**"
            };

            var repositoryMock = new Mock<IUserRepository>();
            repositoryMock.Setup(repository => repository.GetById(id)).Returns(user);

            var service = new UserService(repositoryMock.Object);
            var returnedUser = service.GetUser(id);

            Assert.AreSame(user, returnedUser);
        }
        public void CreateUser_General(long id, string name, string email)
        {
            var repositoryMock = new Mock<IUserRepository>(MockBehavior.Strict);
            repositoryMock.Setup(repository => repository.Create(It.IsAny<User>()))
                .Returns(long.MaxValue);

            var newUser = new User
            {
                Id = id,
                Name = name,
                Email = email
            };

            var service = new UserService(repositoryMock.Object);
            var createdUserId = service.CreateUser(newUser);
            Assert.AreEqual(long.MaxValue, createdUserId);

            repositoryMock.Verify(repository => repository.Create(newUser));
        }
        public void Update_IdIsNotZero()
        {
            var userRepository = new UserRepository(_contextFactory);
            var authenticationRepository = new LoginAuthenticationRepository(_contextFactory);

            //Create user
            var user = new User { Name = "name", Email = "email" };
            var userId1 = userRepository.Create(user);

            var user2 = new User { Name = "name2", Email = "email2" };
            var userId2 = userRepository.Create(user2);

            //Save first version login authentication record
            var initialLoginAuthentication = new LoginAuthentication
            {
                Id = 0,
                LoginName = "login",
                UserId = userId1,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray()
            };

            authenticationRepository.Save(initialLoginAuthentication);

            //Update login authentication record
            var updatedLoginAuthentication = authenticationRepository.GetByLogin(initialLoginAuthentication.LoginName);
            updatedLoginAuthentication.LoginName = "loginUpd";
            updatedLoginAuthentication.PasswordHash = Enumerable.Range(0, 10).Select(i => (byte) (i + 1)).ToArray();
            updatedLoginAuthentication.Salt = Enumerable.Range(0, 10).Select(i => (byte) (i + 2)).ToArray();
            updatedLoginAuthentication.UserId = userId2;

            authenticationRepository.Update(updatedLoginAuthentication);

            //Check what's in the DB
            var storedLoginAuthentication = _contextFactory.GetContext().LoginsAuthentication.First();

            Assert.NotNull(storedLoginAuthentication);
            Assert.AreEqual(updatedLoginAuthentication.UserId, storedLoginAuthentication.UserId);
            Assert.AreEqual(updatedLoginAuthentication.LoginName, storedLoginAuthentication.LoginName);
            CollectionAssert.AreEqual(updatedLoginAuthentication.Salt, storedLoginAuthentication.Salt);
            CollectionAssert.AreEqual(updatedLoginAuthentication.PasswordHash, storedLoginAuthentication.PasswordHash);
        }
        public void Save_IdIsZero()
        {
            var userRepository = new UserRepository(_contextFactory);
            var authenticationRepository = new LoginAuthenticationRepository(_contextFactory);

            var user = new User { Name = "name", Email = "email" };
            var userId = userRepository.Create(user);

            var loginAuthentication = new LoginAuthentication
            {
                Id = 0,
                LoginName = "login",
                UserId = userId,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray()
            };

            authenticationRepository.Save(loginAuthentication);

            var savedLoginAuthentication = _contextFactory.GetContext().LoginsAuthentication.First();

            Assert.NotNull(savedLoginAuthentication);
            Assert.AreEqual(loginAuthentication.UserId, savedLoginAuthentication.UserId);
            Assert.AreEqual(loginAuthentication.LoginName, savedLoginAuthentication.LoginName);
            CollectionAssert.AreEqual(loginAuthentication.Salt, savedLoginAuthentication.Salt);
            CollectionAssert.AreEqual(loginAuthentication.PasswordHash, savedLoginAuthentication.PasswordHash);
        }