public void TestUpdateUser()
        {
            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, _config);

            MembershipCreateStatus status;
            var createdUser = provider.CreateUser("test", "123456", "*****@*****.**", null, null, true, null, out status);
            createdUser.Email = "*****@*****.**";
            createdUser.Comment = "comment";
            createdUser.IsApproved = false;
            createdUser.LastLoginDate = new DateTime(1982, 04, 28);
            createdUser.LastActivityDate = new DateTime(1982, 04, 30);

            provider.UpdateUser(createdUser);
            var updatedUser = provider.GetUser(createdUser.ProviderUserKey, false);
            Assert.NotNull(updatedUser);
            Assert.AreEqual("*****@*****.**", updatedUser.Email);
            Assert.AreEqual("comment", updatedUser.Comment);
            Assert.AreEqual(false, updatedUser.IsApproved);
            Assert.AreEqual(new DateTime(1982, 04, 28), updatedUser.LastLoginDate);
            Assert.AreEqual(new DateTime(1982, 04, 30), updatedUser.LastActivityDate);
        }
        public void TestGetUserByUserNameWhenNonExistent()
        {
            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, _config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", null, null, true, null, out status);
            var retrievedUser = provider.GetUser("foo", false);

            Assert.IsNull(retrievedUser);
        }
        public void TestUnlockUser()
        {
            var config = new NameValueCollection(_config);
            config["enablePasswordRetrieval"] = "true";
            config["requiresQuestionAndAnswer"] = "true";
            config["maxInvalidPasswordAttempts"] = "1";
            config["passwordFormat"] = "clear";

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);

            try {
                provider.GetPassword("test", "Wrong!");
            } catch (MembershipPasswordException) {
            }

            // User will now be locked.
            var lockedOutUser = provider.GetUser("test", false);
            Assert.IsTrue(lockedOutUser.IsLockedOut);

            var unlocked = provider.UnlockUser("test");
            Assert.IsTrue(unlocked);

            // User will now be unlocked.
            var user = provider.GetUser("test", false);
            Assert.IsFalse(user.IsLockedOut);
        }
        public void TestGetUserByProviderUserKey()
        {
            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, _config);

            MembershipCreateStatus status;
            var createdUser = provider.CreateUser("test", "123456", "*****@*****.**", null, null, true, null, out status);
            var retrievedUser = provider.GetUser(createdUser.ProviderUserKey, false);

            Assert.NotNull(retrievedUser);
            Assert.AreEqual(createdUser.ProviderUserKey.ToString(), retrievedUser.ProviderUserKey.ToString());
            Assert.AreEqual(createdUser.LastActivityDate, retrievedUser.LastActivityDate);
        }
        public void TestGetUserByUserNameAndSetOnline()
        {
            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, _config);

            MembershipCreateStatus status;
            var createdUser = provider.CreateUser("test", "123456", "*****@*****.**", null, null, true, null, out status);
            var retrievedUser = provider.GetUser("test", true);

            Assert.NotNull(retrievedUser);
            Assert.AreEqual("test", retrievedUser.UserName);
            Assert.LessOrEqual(createdUser.LastActivityDate, retrievedUser.LastActivityDate);
        }
        public void TestGetNumberOfUsersOnline()
        {
            var config = new NameValueCollection(_config);

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test1", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);
            provider.CreateUser("test2", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);

            // This will set the user online.
            provider.GetUser("test1", true);

            var numberOfUsersOnline = provider.GetNumberOfUsersOnline();
            Assert.AreEqual(1, numberOfUsersOnline);
        }
        public void TestDeleteUser()
        {
            var config = new NameValueCollection(_config);

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);

            var deleted = provider.DeleteUser("test", true);
            Assert.IsTrue(deleted);

            var deletedUser = provider.GetUser("test", false);
            Assert.IsNull(deletedUser);
        }
        private void TestGetPasswordWithTooManyWrongAnswersWithPasswordFormat(string passwordFormat)
        {
            var config = new NameValueCollection(_config);
            config["enablePasswordRetrieval"] = "true";
            config["requiresQuestionAndAnswer"] = "true";
            config["maxInvalidPasswordAttempts"] = "2";
            config["passwordFormat"] = passwordFormat;

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);

            // This is used later to make sure the last lockout date was updated.
            var dateTimeAtStart = DateTime.Now;

            try {
                provider.GetPassword("test", "Wrong!");
            } catch (MembershipPasswordException) {
            }

            try {
                provider.GetPassword("test", "Wrong!");
            } catch (MembershipPasswordException) {
            }

            var user = provider.GetUser("test", false);

            Assert.IsTrue(user.IsLockedOut);
            Assert.GreaterOrEqual(user.LastLockoutDate, dateTimeAtStart);
        }
        private void TestGetPasswordWithTooFewWrongAnswersWithPasswordFormat(string passwordFormat)
        {
            var config = new NameValueCollection(_config);
            config["enablePasswordRetrieval"] = "true";
            config["requiresQuestionAndAnswer"] = "true";
            config["maxInvalidPasswordAttempts"] = "2";
            config["passwordFormat"] = passwordFormat;

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", "Question", "Answer", true, null, out status);

            try {
                provider.GetPassword("test", "Wrong!");
            } catch (MembershipPasswordException) {
            }

            var user = provider.GetUser("test", false);

            Assert.IsFalse(user.IsLockedOut);
        }
        private void TestChangePasswordQuestionAndAnswerWithWrongPasswordWithPasswordFormat(string passwordFormat)
        {
            var config = new NameValueCollection(_config);
            config["passwordFormat"] = passwordFormat;

            var provider = new MongoMembershipProvider();
            provider.Initialize(DefaultName, config);

            MembershipCreateStatus status;
            provider.CreateUser("test", "123456", "*****@*****.**", "OldQuestion", "OldAnswer", true, null, out status);

            var changed = provider.ChangePasswordQuestionAndAnswer("test", "XXXXXX", "Question", "Answer");
            Assert.IsFalse(changed);

            var user = provider.GetUser("test", false);
            Assert.IsNotNull(user);
            Assert.AreEqual("OldQuestion", user.PasswordQuestion);
            // TODO Check answer when GetPassword implemented.
        }