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 GetUserId_CheckPassword(bool loginCorrect, bool passwordCorrect)
        {
            const string login = "******";
            var inputPass = "******";
            var dbPass = passwordCorrect ? inputPass : "******";

            var repositoryMock = new Mock<ILoginAuthenticationRepository>(MockBehavior.Loose);
            var passwordValidatorMock = new Mock<IPasswordValidator>(MockBehavior.Loose);
            var saltedHashGenerator = new SaltedHashGenerator();

            byte[] salt;
            var dbHash = saltedHashGenerator.GenerateSaltedHash(dbPass, out salt, 32);

            var loginAuthentication = new LoginAuthentication
            {
                Id = long.MaxValue - 1,
                UserId = long.MaxValue,
                Salt = salt,
                PasswordHash = dbHash
            };

            repositoryMock.Setup(repository => repository.GetByLogin(login)).Returns(loginCorrect ? loginAuthentication : null);

            var service = new LoginAuthenticationService(repositoryMock.Object, passwordValidatorMock.Object, saltedHashGenerator);
            var userId = service.GetUserId(login, inputPass);

            if (loginCorrect && passwordCorrect)
            {
                Assert.NotNull(userId);
                Assert.AreEqual(loginAuthentication.UserId, userId.Value);
            }
            else
            {
                Assert.IsNull(userId);
            }
        }
        public void Update_UserIdIsZero()
        {
            var loginAuthentication = new LoginAuthentication
            {
                Id = long.MaxValue,
                LoginName = "login",
                UserId = 0,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray()
            };

            var exception = Assert.Throws<Exception<RequiredFieldNotInitialized>>(
                () => new LoginAuthenticationRepository(_contextFactory).Update(loginAuthentication)
            );

            Assert.AreEqual(nameof(loginAuthentication.UserId), exception.ExceptionData.FieldName);
        }
        public void Update_IdIsZero()
        {
            var loginAuthentication = new LoginAuthentication
            {
                Id = 0,
                LoginName = "login",
                UserId = 0,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray()
            };

            Assert.Throws<Exception<EntityHasNotAssignedKey>>(
                () => new LoginAuthenticationRepository(_contextFactory).Update(loginAuthentication)
            );
        }
        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_UserIdIsLessThanZero()
        {
            var loginAuthentication = new LoginAuthentication
            {
                LoginName = "login",
                UserId = -1,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray()
            };

            var exception = Assert.Throws<Exception<ForeignKeyOutOfRange>>(
                () => new LoginAuthenticationRepository(_contextFactory).Save(loginAuthentication)
            );

            Assert.AreEqual(nameof(loginAuthentication.UserId), exception.ExceptionData.FieldName);
        }
        public void Save_SaltIsNull()
        {
            var loginAuthentication = new LoginAuthentication
            {
                LoginName = "login",
                UserId = 1,
                PasswordHash = Enumerable.Range(0, 10).Select(i => (byte)i).ToArray(),
                Salt = null
            };

            var exception = Assert.Throws<Exception<RequiredFieldNotInitialized>>(
                () => new LoginAuthenticationRepository(_contextFactory).Save(loginAuthentication)
            );

            Assert.AreEqual(nameof(loginAuthentication.Salt), exception.ExceptionData.FieldName);
        }
        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);
        }