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);
        }
Пример #2
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);
        }