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); }
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); }