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);
        }
示例#2
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);
        }