Example #1
0
        public async Task CreateRole()
        {
            // Create a session and role store for this test.
            var session = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role.
            var role = new TestRole { Name = "CreateRoleTest" };
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                transaction.Commit();
            }
            // Check the role has an id.
            Assert.IsNotNull(role.Id);

            // Create a new session and role store for this test, so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            roleStore = new TestRoleStore<TestRole>(session);
            // Load the role.
            TestRole loadRole;
            using (var transaction = session.BeginTransaction())
            {
                loadRole = await roleStore.FindByIdAsync(role.Id);
                transaction.Commit();
            }
            // Check we have the same role.
            Assert.AreEqual(role.Id, loadRole.Id);
            Assert.AreEqual(role.Name, loadRole.Name);
        }
        public async Task IsInRoleReturnsTrueWhenAUserIsInARoleAndFalseWhenTheyAreNot()
        {
            // Create a session and user store for this test.
            var session   = SessionFactory.OpenSession();
            var userStore = new TestUserStore(session);
            var roleStore = new TestRoleStore(session);
            // Create and save a role and a user and add the role to the user.
            int    numberOfOtherRoles = 3;
            string roleName           = "IsInRoleTestRole";
            var    role = new TestRole(roleName);
            var    user = new TestUser("IsInRoleTestUser");

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                await userStore.CreateAsync(user);

                await userStore.AddToRoleAsync(user, role.Name);

                for (int i = 0; i < numberOfOtherRoles; i++)
                {
                    var otherRole = new TestRole(roleName + i);
                    await roleStore.CreateAsync(otherRole);

                    await userStore.AddToRoleAsync(user, otherRole.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfOtherRoles + 1);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user.
            TestUser loadUser;

            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                transaction.Commit();
            }
            // Check we have the same user and that we get true when testing for the correct role and false for non-existent role.
            Assert.AreEqual(loadUser.Id, user.Id);
            bool inRole = await userStore.IsInRoleAsync(loadUser, roleName);

            bool notInRole = await userStore.IsInRoleAsync(loadUser, "NOTINROLETEST_USERNOTINROLE");

            Assert.IsTrue(inRole);
            Assert.IsFalse(notInRole);
        }
        public async Task AddRoleToUser()
        {
            // Create a session and user store for this test.
            var session   = SessionFactory.OpenSession();
            var userStore = new TestUserStore(session);
            var roleStore = new TestRoleStore(session);
            // Create and save a role and a user.
            string roleName = "AddRoleToUserTestRole";
            var    role     = new TestRole(roleName);
            var    user     = new TestUser("AddRoleToUserTestUser");

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                await userStore.CreateAsync(user);

                transaction.Commit();
            }
            // Check the user has an Id and no roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, 0);
            var userId = user.Id;

            // Add the user to the role.
            using (var transaction = session.BeginTransaction())
            {
                await userStore.AddToRoleAsync(user, role.Name);

                transaction.Commit();
            }

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user.
            TestUser loadUser;

            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            var userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);

            Assert.IsNotNull(userRole);
        }
        public async Task GetRolesForAUser()
        {
            // Create a session and user store for this test.
            var session   = SessionFactory.OpenSession();
            var userStore = new TestUserStore(session);
            var roleStore = new TestRoleStore(session);
            // Create and save a user and some roles and add the roles to the user.
            int    numberOfRoles = 5;
            string roleName      = "GetRolesForAUserTestRole";
            var    user          = new TestUser("GetRolesForAUser");

            using (var transaction = session.BeginTransaction())
            {
                await userStore.CreateAsync(user);

                for (int i = 0; i < numberOfRoles; i++)
                {
                    var role = new TestRole(roleName + i);
                    await roleStore.CreateAsync(role);

                    await userStore.AddToRoleAsync(user, role.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfRoles);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user.
            TestUser       loadUser;
            IList <string> roles;

            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                roles = await userStore.GetRolesAsync(user);

                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            Assert.AreEqual(roles.Count, numberOfRoles);
        }
Example #5
0
        public async Task UpdateRole()
        {
            // Create a session and role store for this test.
            var session   = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore(session);
            // Create and save a role.
            string originalRoleName = "UpdateRoleTest";
            string newRoleName      = "NewUpdateRoleTest";
            var    role             = new TestRole {
                Name = originalRoleName
            };

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                transaction.Commit();
            }
            // Check the role has an id and a name is correct.
            Assert.IsNotNull(role.Id);
            Assert.AreEqual(role.Name, originalRoleName);
            // Change the role name.
            using (var transaction = session.BeginTransaction())
            {
                role.Name = newRoleName;
                await roleStore.UpdateAsync(role);

                transaction.Commit();
            }

            // Create a new session and role store so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            roleStore = new TestRoleStore(session);
            // Load the updated the role.
            TestRole updatedRole;

            using (var transaction = session.BeginTransaction())
            {
                updatedRole = await roleStore.FindByIdAsync(role.Id);

                transaction.Commit();
            }

            // Check the role name has been updated and saved.
            Assert.AreEqual(updatedRole.Name, newRoleName);
        }
Example #6
0
        public async Task GetNonExistingRoleByNameReturnsNull()
        {
            // Create a session and role store for this test.
            var      session   = SessionFactory.OpenSession();
            var      roleStore = new TestRoleStore(session);
            TestRole role;

            using (var transaction = session.BeginTransaction())
            {
                role = await roleStore.FindByNameAsync("THISISNOTAROLENAME");

                transaction.Commit();
            }
            // Check that we have no role.
            Assert.IsNull(role);
        }
Example #7
0
        public async Task DeleteRole()
        {
            // Create a session and role store for this test.
            var session   = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore(session);
            // Create and save a role.
            string userName = "******";
            var    role     = new TestRole {
                Name = userName
            };

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                transaction.Commit();
            }
            // Check the role has an id and a name.
            Assert.IsNotNull(role.Id);
            Assert.IsNotNull(role.Name);
            var roleId = role.Id;

            // Create a new session and role store so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            roleStore = new TestRoleStore(session);
            // Load and delete the role.
            using (var transaction = session.BeginTransaction())
            {
                role = await roleStore.FindByIdAsync(roleId);

                await roleStore.DeleteAsync(role);

                transaction.Commit();
            }

            // Check that the role has been deleted.
            var deletedUser = await roleStore.FindByIdAsync(roleId);

            Assert.IsNull(deletedUser);
        }
Example #8
0
        public async Task GetRoleByName()
        {
            // Create a session and role store for this test.
            var session   = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore(session);
            // Create and save a role.
            string roleName = "GetRoleByNameTest";
            var    role     = new TestRole {
                Name = roleName
            };

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                transaction.Commit();
            }
            // Check the role has an id.
            Assert.IsNotNull(role.Id);

            // Create a new session and role store for this test, so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            roleStore = new TestRoleStore(session);
            // Load the role by Name.
            TestRole loadRole;

            using (var transaction = session.BeginTransaction())
            {
                loadRole = await roleStore.FindByNameAsync(roleName);

                transaction.Commit();
            }
            // Check we have the same role.
            Assert.IsNotNull(loadRole);
            Assert.AreEqual(role.Id, loadRole.Id);
            Assert.AreEqual(role.Name, loadRole.Name);
        }
Example #9
0
        public async Task IsInRoleReturnsTrueWhenAUserIsInARoleAndFalseWhenTheyAreNot()
        {
            // Create a session and user store for this test.
            var session = SessionFactory.OpenSession();
            var userStore = new TestUserStore<TestUser>(session);
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role and a user and add the role to the user.
            int numberOfOtherRoles = 3;
            string roleName = "IsInRoleTestRole";
            var role = new TestRole(roleName);
            var user = new TestUser("IsInRoleTestUser");
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                await userStore.CreateAsync(user);
                await userStore.AddToRoleAsync(user, role.Name);
                for (int i = 0; i < numberOfOtherRoles; i++)
                {
                    var otherRole = new TestRole(roleName + i);
                    await roleStore.CreateAsync(otherRole);
                    await userStore.AddToRoleAsync(user, otherRole.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfOtherRoles + 1);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user.
            TestUser loadUser;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                transaction.Commit();
            }
            // Check we have the same user and that we get true when testing for the correct role and false for non-existent role.
            Assert.AreEqual(loadUser.Id, user.Id);
            bool inRole = await userStore.IsInRoleAsync(loadUser, roleName);
            bool notInRole = await userStore.IsInRoleAsync(loadUser, "NOTINROLETEST_USERNOTINROLE");
            Assert.IsTrue(inRole);
            Assert.IsFalse(notInRole);
        }
Example #10
0
        public async Task GetRolesForAUser()
        {
            // Create a session and user store for this test.
            var session = SessionFactory.OpenSession();
            var userStore = new TestUserStore<TestUser>(session);
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a user and some roles and add the roles to the user.
            int numberOfRoles = 5;
            string roleName = "GetRolesForAUserTestRole";
            var user = new TestUser("GetRolesForAUser");
            using (var transaction = session.BeginTransaction())
            {
                await userStore.CreateAsync(user);
                for (int i = 0; i < numberOfRoles; i++)
                {
                    var role = new TestRole(roleName + i);
                    await roleStore.CreateAsync(role);
                    await userStore.AddToRoleAsync(user, role.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfRoles);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user.
            TestUser loadUser;
            IList<string> roles;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                roles = await userStore.GetRolesAsync(user);
                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            Assert.AreEqual(roles.Count, numberOfRoles);
        }
Example #11
0
        public async Task RemoveRoleFromUserOnlyRemovesSingleRole()
        {
            // Create a session and user store for this test.
            var session = SessionFactory.OpenSession();
            var userStore = new TestUserStore<TestUser>(session);
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role and a user and add the role to the user.
            int numberOfOtherRoles = 3;
            string roleName = "RemoveRoleFromUserOnlyRemovesSingleRole";
            var role = new TestRole(roleName);
            var user = new TestUser("RemoveRoleFromUserOnlyRemovesSingleRole");
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                await userStore.CreateAsync(user);
                await userStore.AddToRoleAsync(user, role.Name);
                for (int i = 0; i < numberOfOtherRoles; i++)
                {
                    var otherRole = new TestRole(roleName + i);
                    await roleStore.CreateAsync(otherRole);
                    await userStore.AddToRoleAsync(user, otherRole.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfOtherRoles + 1);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user.
            TestUser loadUser;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            var userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);
            Assert.IsNotNull(userRole);
            // Now remove the role.
            using (var transaction = session.BeginTransaction())
            {
                await userStore.RemoveFromRoleAsync(loadUser, roleName);
                transaction.Commit();
            }

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user again.            
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                transaction.Commit();
            }
            // Check we have the same user and the role has been removed.
            Assert.AreEqual(loadUser.Id, user.Id);
            userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);
            Assert.IsNull(userRole);
        }
Example #12
0
        public async Task AddRoleToUser()
        {
            // Create a session and user store for this test.
            var session = SessionFactory.OpenSession();
            var userStore = new TestUserStore<TestUser>(session);
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role and a user.
            string roleName = "AddRoleToUserTestRole";
            var role = new TestRole(roleName);
            var user = new TestUser("AddRoleToUserTestUser");
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                await userStore.CreateAsync(user);
                transaction.Commit();
            }
            // Check the user has an Id and no roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, 0);
            var userId = user.Id;
            // Add the user to the role.
            using (var transaction = session.BeginTransaction())
            {
                await userStore.AddToRoleAsync(user, role.Name);
                transaction.Commit();
            }

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            userStore = new TestUserStore<TestUser>(session);
            // Load the user.
            TestUser loadUser;
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);
                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            var userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);
            Assert.IsNotNull(userRole);
        }
Example #13
0
        public async Task DeleteRole()
        {
            // Create a session and role store for this test.
            var session = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role.
            string userName = "******";
            var role = new TestRole { Name = userName };
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                transaction.Commit();
            }
            // Check the role has an id and a name.
            Assert.IsNotNull(role.Id);
            Assert.IsNotNull(role.Name);
            var roleId = role.Id;

            // Create a new session and role store so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            roleStore = new TestRoleStore<TestRole>(session);
            // Load and delete the role.
            using (var transaction = session.BeginTransaction())
            {
                role = await roleStore.FindByIdAsync(roleId);
                await roleStore.DeleteAsync(role);
                transaction.Commit();
            }

            // Check that the role has been deleted.
            var deletedUser = await roleStore.FindByIdAsync(roleId);
            Assert.IsNull(deletedUser);
        }
Example #14
0
        public async Task UpdateRole()
        {
            // Create a session and role store for this test.
            var session = SessionFactory.OpenSession();
            var roleStore = new TestRoleStore<TestRole>(session);
            // Create and save a role.
            string originalRoleName = "UpdateRoleTest";
            string newRoleName = "NewUpdateRoleTest";
            var role = new TestRole { Name = originalRoleName };
            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);
                transaction.Commit();
            }
            // Check the role has an id and a name is correct.
            Assert.IsNotNull(role.Id);
            Assert.AreEqual(role.Name, originalRoleName);
            // Change the role name.
            using (var transaction = session.BeginTransaction())
            {
                role.Name = newRoleName;
                await roleStore.UpdateAsync(role);
                transaction.Commit();
            }

            // Create a new session and role store so that we actually hit the database and not the cache.
            roleStore.Dispose();
            session.Dispose();
            session = SessionFactory.OpenSession();
            roleStore = new TestRoleStore<TestRole>(session);
            // Load the updated the role.
            TestRole updatedRole;
            using (var transaction = session.BeginTransaction())
            {
                updatedRole = await roleStore.FindByIdAsync(role.Id);
                transaction.Commit();
            }

            // Check the role name has been updated and saved.
            Assert.AreEqual(updatedRole.Name, newRoleName);
        }
Example #15
0
 public async Task GetNonExistingRoleByNameReturnsNull()
 {
     // Create a session and role store for this test.
     var session = SessionFactory.OpenSession();
     var roleStore = new TestRoleStore<TestRole>(session);
     TestRole role;
     using (var transaction = session.BeginTransaction())
     {
         role = await roleStore.FindByNameAsync("THISISNOTAROLENAME");
         transaction.Commit();
     }
     // Check that we have no role.
     Assert.IsNull(role);
 }
        public async Task RemoveRoleFromUserOnlyRemovesSingleRole()
        {
            // Create a session and user store for this test.
            var session   = SessionFactory.OpenSession();
            var userStore = new TestUserStore(session);
            var roleStore = new TestRoleStore(session);
            // Create and save a role and a user and add the role to the user.
            int    numberOfOtherRoles = 3;
            string roleName           = "RemoveRoleFromUserOnlyRemovesSingleRole";
            var    role = new TestRole(roleName);
            var    user = new TestUser("RemoveRoleFromUserOnlyRemovesSingleRole");

            using (var transaction = session.BeginTransaction())
            {
                await roleStore.CreateAsync(role);

                await userStore.CreateAsync(user);

                await userStore.AddToRoleAsync(user, role.Name);

                for (int i = 0; i < numberOfOtherRoles; i++)
                {
                    var otherRole = new TestRole(roleName + i);
                    await roleStore.CreateAsync(otherRole);

                    await userStore.AddToRoleAsync(user, otherRole.Name);
                }
                transaction.Commit();
            }
            // Check the user has an Id and the roles.
            Assert.IsNotNull(user.Id);
            Assert.AreEqual(user.Roles.Count, numberOfOtherRoles + 1);
            var userId = user.Id;

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user.
            TestUser loadUser;

            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                transaction.Commit();
            }
            // Check we have the same user and it has the role.
            Assert.AreEqual(loadUser.Id, user.Id);
            var userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);

            Assert.IsNotNull(userRole);
            // Now remove the role.
            using (var transaction = session.BeginTransaction())
            {
                await userStore.RemoveFromRoleAsync(loadUser, roleName);

                transaction.Commit();
            }

            // Create a new session and user store for this test, so that we actually hit the database and not the cache.
            userStore.Dispose();
            session.Dispose();
            session   = SessionFactory.OpenSession();
            userStore = new TestUserStore(session);
            // Load the user again.
            using (var transaction = session.BeginTransaction())
            {
                loadUser = await userStore.FindByIdAsync(userId);

                transaction.Commit();
            }
            // Check we have the same user and the role has been removed.
            Assert.AreEqual(loadUser.Id, user.Id);
            userRole = loadUser.Roles.SingleOrDefault(r => r.Name == roleName);
            Assert.IsNull(userRole);
        }