Example #1
0
        public void MultipleAccountTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string user1Email = "*****@*****.**";
            const string user2Email = "*****@*****.**";
            const string password   = "******";

            var user1 = CreateUserAccountWithStandardLogin(authDb, user1Email, password);
            var user2 = CreateUserAccountWithStandardLogin(authDb, user2Email, password);

            Assert.IsNotNull(user1);
            Assert.IsTrue(tAuthDb.SaveCalled);

            ILoginManager <TestUser> loginManager = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);
            var res = loginManager.AttemptLogin(user1Email, password);

            Assert.AreEqual(LoginResult <TestUser> .Type.success, res.ResultType, "LoginManager returned failiure.");
            Assert.AreEqual(user1, res.User, "User returned from LoginManager was not correct.");

            res = loginManager.AttemptLogin(user2Email, password);

            Assert.AreEqual(LoginResult <TestUser> .Type.success, res.ResultType, "LoginManager returned failiure.");
            Assert.AreEqual(user2, res.User);
            Assert.AreEqual(user2, res.User, "User returned from LoginManager was not correct.");
        }
Example #2
0
        public void InvalidEmptySaltTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";
            const string password     = "******";

            var user = CreateUserAccountWithStandardLogin(authDb, primaryEmail, password);

            Assert.IsNotNull(user);
            Assert.IsTrue(tAuthDb.SaveCalled);

            (tAuthDb.Backing[typeof(TestLogin)][0] as TestLogin).Salt = string.Empty;

            var loginManager = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);

            var threw = false;

            try
            {
                loginManager.AttemptLogin(primaryEmail, password);
            }
            catch (InvalidOperationException)
            {
                threw = true;
            }
            Assert.IsTrue(threw);
        }
Example #3
0
        public void UserPersists()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";

            CreateUserAccount(authDb, primaryEmail);
            Assert.IsTrue(tAuthDb.SaveCalled, "Save was not called on db");

            IUserManager <TestUser> otherUserManager = new UserManager <TestUser>(authDb, new EmailAddressValidator());

            Assert.IsTrue(otherUserManager.UserExistsByEmail(primaryEmail), "New UserManager backed by same db, user did not exist.");
        }
Example #4
0
        public void AssureDistinctHashTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string password = "******";

            var user1 = CreateUserAccountWithStandardLogin(authDb, "*****@*****.**", password);
            var user2 = CreateUserAccountWithStandardLogin(authDb, "*****@*****.**", password);

            var login1 = tAuthDb.Backing[typeof(TestLogin)][0] as Login <TestUser>;
            var login2 = tAuthDb.Backing[typeof(TestLogin)][1] as Login <TestUser>;

            Assert.AreNotEqual(login1.Hash, login2.Hash, "Two user accounts shared the same hash");
        }
Example #5
0
        public void AccountManagerCreatesUserTest()
        {
            var authDb = new TestAuthDb();

            ILoginManager <TestUser> loginManager = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);
            IUserManager <TestUser>  userManager  = new UserManager <TestUser>(authDb, new EmailAddressValidator());

            IAccountManager <TestUser> accountManager = new StandardAccountManager <TestUser, TestLogin>(userManager, loginManager);

            string userName = "******";
            string password = "******";

            accountManager.CreateUserWithLogin(userName, password);

            Assert.AreEqual(LoginResult <TestUser> .Type.success, accountManager.ValidLogin(userName, password).ResultType, "Newly created user account failed to login");
        }
Example #6
0
        public void LoginNonAuthenticatePersists()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";
            const string password     = "******";

            var user = CreateUserAccountWithStandardLogin(authDb, primaryEmail, password);

            Assert.IsTrue(tAuthDb.SaveCalled);

            ILoginManager <TestUser> otherLoginManager = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);

            Assert.IsTrue(otherLoginManager.LoginExists(user), "LoginUsername did not persist through new LoginManager");
        }
Example #7
0
        public void InvalidSitePepperLoginTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";
            const string password     = "******";

            var user = CreateUserAccountWithStandardLogin(authDb, primaryEmail, password);

            Assert.IsNotNull(user);
            Assert.IsTrue(tAuthDb.SaveCalled);

            ILoginManager <TestUser> loginManager = new LoginManager <TestUser, TestLogin>(authDb, $"{DefaultSitePepper}1", _userNameValidator);
            var res = loginManager.AttemptLogin(primaryEmail, password);

            Assert.AreEqual(LoginResult <TestUser> .Type.failiure, res.ResultType, "LoginManager incorrectly authenticated login.");
        }
Example #8
0
        public void InvalidPrimaryEmailAddressTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "garethmu @gmail.com";

            Assert.IsFalse(new EmailAddressValidator().IsValid(primaryEmail), "EmailAddressValidator accepted invalid username");

            var threw = false;

            try
            {
                CreateUserAccount(authDb, primaryEmail);
            }
            catch (InvalidDataException)
            {
                threw = true;
            }
            Assert.IsTrue(threw, "Creating user account did not throw expected error with invalid email address");
        }
Example #9
0
        public void InvalidLoginUserNameAddressTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "garethmu @gmail.com";

            var lm = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);

            var threw = false;

            try
            {
                lm.CreateLogin(null, primaryEmail, "password");
            }
            catch (InvalidDataException)
            {
                threw = true;
            }
            Assert.IsTrue(threw, "LoginManager did not throw error with invalid login name");
        }
Example #10
0
        public void NoDuplicatePrimaryEmailAddressTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";

            CreateUserAccount(authDb, primaryEmail);

            var threw = false;

            try
            {
                CreateUserAccount(authDb, primaryEmail);
            }
            catch (DuplicateAccountException)
            {
                threw = true;
            }

            Assert.IsTrue(threw);
        }
Example #11
0
        public void InvalidIncorrectSaltTest()
        {
            var     tAuthDb = new TestAuthDb();
            IAuthDb authDb  = tAuthDb;

            const string primaryEmail = "*****@*****.**";
            const string password     = "******";

            var user = CreateUserAccountWithStandardLogin(authDb, primaryEmail, password);

            Assert.IsNotNull(user);
            Assert.IsTrue(tAuthDb.SaveCalled);
            var login = tAuthDb.Backing[typeof(TestLogin)][0] as TestLogin;

            login.Salt = $"{login.Salt}1";

            ILoginManager <TestUser> loginManager = new LoginManager <TestUser, TestLogin>(authDb, DefaultSitePepper, _userNameValidator);

            var res = loginManager.AttemptLogin(primaryEmail, password);

            Assert.AreEqual(LoginResult <TestUser> .Type.failiure, res.ResultType, "LoginManager allowed user to login despite salt having changed");
        }