public void TestAddUsersToRoles()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user2", "123456", "*****@*****.**", null, null, true, null, out status);

            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");

            roleProvider.AddUsersToRoles(new[] { "user1", "user2" }, new[] { "role1", "role2" });
            Assert.IsTrue(roleProvider.IsUserInRole("user1", "role1"));
            Assert.IsTrue(roleProvider.IsUserInRole("user1", "role2"));
            Assert.IsTrue(roleProvider.IsUserInRole("user2", "role1"));
            Assert.IsTrue(roleProvider.IsUserInRole("user2", "role2"));
        }
        public void TestRoleExists()
        {
            var config = new NameValueCollection(_roleConfig);

            var provider = new MongoRoleProvider();
            provider.Initialize(DefaultRoleName, config);

            Assert.IsFalse(provider.RoleExists("test"));
            provider.CreateRole("test");
            Assert.IsTrue(provider.RoleExists("test"));
        }
        public void TestRemoveUsersFromRolesWithNonExistantUserNames()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");

            Assert.Throws<ProviderException>(
                () => roleProvider.RemoveUsersFromRoles(new[] { "user1", "user2" }, new[] { "role1", "role2" }));
        }
        public void TestRemoveUsersFromRolesWithNonExistantRoles()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user2", "123456", "*****@*****.**", null, null, true, null, out status);

            Assert.Throws<ProviderException>(
                () => roleProvider.RemoveUsersFromRoles(new[] { "user1", "user2" }, new[] { "role1", "role2" }));
        }
        public void TestIsUserInRoleWhenUserDoesNotExist()
        {
            var roleConfig = new NameValueCollection(_roleConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);
            roleProvider.CreateRole("role1");

            Assert.Throws<ArgumentException>(() => roleProvider.IsUserInRole("user1", "role1"));
        }
        public void TestInitializeWhenCalledTwice()
        {
            var config = new NameValueCollection(_roleConfig);

            var provider = new MongoRoleProvider();
            Assert.Throws<InvalidOperationException>(() => {
                provider.Initialize(DefaultRoleName, config);
                provider.Initialize(DefaultRoleName, config);
            });
        }
        public void TestIsUserInRoleWhenRoleDoesNotExist()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);

            Assert.Throws<ArgumentException>(() => roleProvider.IsUserInRole("user1", "role1"));
        }
        public void TestGetRolesForUserWhenHasUserDoesNotExist()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user2", "123456", "*****@*****.**", null, null, true, null, out status);
            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");
            roleProvider.CreateRole("role3");
            roleProvider.AddUsersToRoles(new[] { "user1", }, new[] { "role1", "role2" });

            var roles = roleProvider.GetRolesForUser("user3");
            Assert.AreEqual(0, roles.Length);
        }
        public void TestGetUsersInRoleWhenNoUsersInRole()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user2", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user3", "123456", "*****@*****.**", null, null, true, null, out status);

            roleProvider.CreateRole("role1");

            var userNames = roleProvider.GetUsersInRole("role1");
            Assert.AreEqual(0, userNames.Length);
        }
        public void TestGetAllRoles()
        {
            var roleConfig = new NameValueCollection(_roleConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");
            roleProvider.CreateRole("role3");

            var roles = roleProvider.GetAllRoles();
            Assert.AreEqual(3, roles.Length);
            Assert.Contains("role1", roles);
            Assert.Contains("role2", roles);
            Assert.Contains("role3", roles);
        }
        public void TestGetAllRolesWhenNoRoles()
        {
            var roleConfig = new NameValueCollection(_roleConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            var roles = roleProvider.GetAllRoles();
            Assert.AreEqual(0, roles.Length);
        }
        public void TestFindUsersInRoleWithNonRegex()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("aaaa", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("aabb", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("bbaa", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("aacc", "123456", "*****@*****.**", null, null, true, null, out status);

            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");

            roleProvider.AddUsersToRoles(new[] { "aaaa", "aabb", "bbaa" }, new[] { "role1" });
            roleProvider.AddUsersToRoles(new[] { "aacc" }, new[] { "role2" });

            var userNames = roleProvider.FindUsersInRole("role1", "aa");
            Assert.AreEqual(3, userNames.Length);
            Assert.Contains("aaaa", userNames);
            Assert.Contains("aabb", userNames);
            Assert.Contains("bbaa", userNames);
        }
        public void TestDeleteRoleWhenPopulatedWithThrowOnPopulated()
        {
            var membershipConfig = new NameValueCollection(_membershipConfig);
            var roleConfig = new NameValueCollection(_roleConfig);

            var membershipProvider = new MongoMembershipProvider();
            membershipProvider.Initialize(DefaultMembershipName, membershipConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            MembershipCreateStatus status;
            membershipProvider.CreateUser("user1", "123456", "*****@*****.**", null, null, true, null, out status);
            membershipProvider.CreateUser("user2", "123456", "*****@*****.**", null, null, true, null, out status);

            roleProvider.CreateRole("role1");
            roleProvider.CreateRole("role2");

            roleProvider.AddUsersToRoles(new[] { "user1", "user2" }, new[] { "role1", "role2" });

            Assert.Throws<ProviderException>(() => roleProvider.DeleteRole("role1", true));
        }
        public void TestDeleteRole()
        {
            var roleConfig = new NameValueCollection(_roleConfig);

            var roleProvider = new MongoRoleProvider();
            roleProvider.Initialize(DefaultRoleName, roleConfig);

            roleProvider.CreateRole("role1");
            Assert.IsTrue(roleProvider.RoleExists("role1"));
            roleProvider.DeleteRole("role1", true);
            Assert.IsFalse(roleProvider.RoleExists("role1"));
        }
        public void TestCreateRoleWithDuplicateRoleName()
        {
            var config = new NameValueCollection(_roleConfig);

            var provider = new MongoRoleProvider();
            provider.Initialize(DefaultRoleName, config);

            provider.CreateRole("test");
            Assert.Throws<ProviderException>(() => provider.CreateRole("test"));
        }
        public void TestCreateRole()
        {
            var config = new NameValueCollection(_roleConfig);

            var provider = new MongoRoleProvider();
            provider.Initialize(DefaultRoleName, config);

            provider.CreateRole("test");
        }