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);
        }
Example #2
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 #3
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 #4
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 #5
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 #6
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);
        }