protected void InternalSetup() { HttpContext.Current = CreateHttpContext(userLoggedIn: false); var testStore = new TestUserStore(); CurrentUserManagerMock = new Mock<ApplicationUserManager>(testStore); CurrentUserManagerMock.Setup(x => x.FindByEmailAsync(It.IsAny<string>())) .Returns(Task.FromResult(testStore.FindByIdAsync(UserId).Result)); CurrentUserManagerMock.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>())) .Returns(Task.FromResult(IdentityResult.Success)); CurrentUserManagerMock.Setup(u => u.RequestPhoneNumberConfirmationTokenAsync(It.IsAny<string>())) .Returns(Task.FromResult(0)); CurrentUserManagerMock.Setup(u => u.ConfirmPhoneNumberAsync(It.IsAny<string>(), It.IsAny<string>())) .Returns(Task.FromResult(IdentityResult.Failed(ApplicationMessages.InvalidVerificationCode))); CurrentUserManagerMock.Setup(u => u.ConfirmPhoneNumberAsync(UserId, VALID_CODE)) .Returns(Task.FromResult(IdentityResult.Success)) .Callback(() => testStore.FindByIdAsync(UserId).Result.PhoneNumberConfirmed = true); CurrentUserManagerMock.Setup(u => u.FindByIdAsync(UserId)) .Returns(Task.FromResult(testStore.FindByIdAsync(UserId).Result)); var authenticationManager = new Mock<IAuthenticationManager>(); CurrentSignInManagerMock = new Mock<ApplicationSignInManager>(CurrentUserManagerMock.Object, authenticationManager.Object); }
public async Task CreateUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user. var user = new TestUser { UserName = "******" }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id. Assert.IsNotNull(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(user.Id); transaction.Commit(); } // Check we have the same user. Assert.AreEqual(user.Id, loadUser.Id); Assert.AreEqual(user.UserName, loadUser.UserName); }
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 GetClaimsForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user with some claims. var user = new TestUser { UserName = "******" }; int numberOfClaims = 5; var claimType = ClaimTypes.Role; var claimValue = "Admin_GetClaimsForUserTest"; var claim = new Claim(claimType, claimValue); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddClaimsAsync(user, new[] { claim }); for (int i = 0; i < numberOfClaims - 1; i++) { var loopClaim = new Claim(claimType, "Admin_GetClaimsForUserTest_" + i); await userStore.AddClaimsAsync(user, new[] { loopClaim }); } transaction.Commit(); } // Check the user has an id and the claims. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Claims.Count, numberOfClaims); 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 claims. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Claims.Count, numberOfClaims); var userClaims = await userStore.GetClaimsAsync(loadUser); var userClaim = userClaims.SingleOrDefault(c => c.Type == claimType && c.Value == claimValue); Assert.IsNotNull(userClaim); }
public async Task DeleteUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user. string userName = "******"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); var userId = user.Id; // Create a new session and user store so that we actually hit the database and not the cache. userStore.Dispose(); session.Dispose(); session = SessionFactory.OpenSession(); userStore = new TestUserStore(session); // Load and delete the user. using (var transaction = session.BeginTransaction()) { user = await userStore.FindByIdAsync(userId); await userStore.DeleteAsync(user); transaction.Commit(); } // Check that the user has been deleted. var deletedUser = await userStore.FindByIdAsync(userId); Assert.IsNull(deletedUser); }
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); }
public async Task UpdateUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user. string userName = "******"; string email = "*****@*****.**"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); Assert.IsNull(user.Email); // Update the user's email address. using (var transaction = session.BeginTransaction()) { user.Email = email; await userStore.UpdateAsync(user); transaction.Commit(); } // Create a new session and user store so that we actually hit the database and not the cache. userStore.Dispose(); session.Dispose(); session = SessionFactory.OpenSession(); userStore = new TestUserStore(session); // Load and update the user. TestUser updatedUser; using (var transaction = session.BeginTransaction()) { updatedUser = await userStore.FindByIdAsync(user.Id); transaction.Commit(); } // Check the email has been updated and saved. Assert.AreEqual(updatedUser.Email, email); }
public async Task GetLoginsForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user with a login. var user = new TestUser { UserName = "******" }; int numberOfLogins = 5; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); for (int i = 0; i < numberOfLogins; i++) { var login = new UserLoginInfo("TestProviderList" + i, "ProviderKeyRemove" + i, "TestProviderList" + i); await userStore.AddLoginAsync(user, login); } transaction.Commit(); } // Check the user has an id and all the logins have been saved. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Logins.Count, numberOfLogins); 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 <UserLoginInfo> logins; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); logins = await userStore.GetLoginsAsync(user); transaction.Commit(); } // Check we have the same user and that they have all of the logins. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Logins.Count, numberOfLogins); Assert.AreEqual(logins.Count, numberOfLogins); }
public async Task GetNonExistingUserByIdReturnsNull() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); TestUser user; using (var transaction = session.BeginTransaction()) { user = await userStore.FindByIdAsync("THISISNOTAUSERID"); transaction.Commit(); } // Check that we have no user. Assert.IsNull(user); }
public async Task RemoveClaimForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user with a claim. var user = new TestUser { UserName = "******" }; var claimType = ClaimTypes.Role; var claimValue = "Admin_RemoveClaimForUserTest"; var claim = new Claim(claimType, claimValue); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddClaimsAsync(user, new[] { claim }); transaction.Commit(); } // Check the user has an id and the claim. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Claims.Count, 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 and remove the claim. TestUser loadUser; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); await userStore.RemoveClaimsAsync(loadUser, new[] { claim }); transaction.Commit(); } // Check we have the same user and it now has no claims. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Claims.Count, 0); }
public async Task RemoveLoginForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user with a login. var user = new TestUser { UserName = "******" }; var login = new UserLoginInfo("TestProviderRemove", "ProviderKeyRemove", "TestProviderRemove"); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddLoginAsync(user, login); transaction.Commit(); } // Check the user has an id and the login. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Logins.Count, 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 and remove the login. TestUser loadUser; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); await userStore.RemoveLoginAsync(loadUser, login.LoginProvider, login.ProviderKey); transaction.Commit(); } // Check we have the same user and that the login has been removed. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Logins.Count, 0); }
public async Task GetUserById() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore(session); // Create and save a user. string userName = "******"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); 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. Assert.AreEqual(user.Id, loadUser.Id); Assert.AreEqual(user.UserName, loadUser.UserName); }
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 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); }
public async Task GetClaimsForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user with some claims. var user = new TestUser { UserName = "******" }; int numberOfClaims = 5; var claimType = ClaimTypes.Role; var claimValue = "Admin_GetClaimsForUserTest"; var claim = new Claim(claimType, claimValue); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddClaimAsync(user, claim); for (int i = 0; i < numberOfClaims - 1; i++) { var loopClaim = new Claim(claimType, "Admin_GetClaimsForUserTest_" + i); await userStore.AddClaimAsync(user, loopClaim); } transaction.Commit(); } // Check the user has an id and the claims. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Claims.Count, numberOfClaims); 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 claims. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Claims.Count, numberOfClaims); var userClaims = await userStore.GetClaimsAsync(loadUser); var userClaim = userClaims.SingleOrDefault(c => c.Type == claimType && c.Value == claimValue); Assert.IsNotNull(userClaim); }
public async Task RemoveClaimForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user with a claim. var user = new TestUser { UserName = "******" }; var claimType = ClaimTypes.Role; var claimValue = "Admin_RemoveClaimForUserTest"; var claim = new Claim(claimType, claimValue); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddClaimAsync(user, claim); transaction.Commit(); } // Check the user has an id and the claim. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Claims.Count, 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 and remove the claim. TestUser loadUser; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); await userStore.RemoveClaimAsync(loadUser, claim); transaction.Commit(); } // Check we have the same user and it now has no claims. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Claims.Count, 0); }
public async Task GetLoginsForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user with a login. var user = new TestUser { UserName = "******" }; int numberOfLogins = 5; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); for (int i = 0; i < numberOfLogins; i++) { var login = new UserLoginInfo("TestProviderList" + i, "ProviderKeyRemove" + i); await userStore.AddLoginAsync(user, login); } transaction.Commit(); } // Check the user has an id and all the logins have been saved. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Logins.Count, numberOfLogins); 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<UserLoginInfo> logins; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); logins = await userStore.GetLoginsAsync(user); transaction.Commit(); } // Check we have the same user and that they have all of the logins. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Logins.Count, numberOfLogins); Assert.AreEqual(logins.Count, numberOfLogins); }
public async Task RemoveLoginForUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user with a login. var user = new TestUser { UserName = "******" }; var login = new UserLoginInfo("TestProviderRemove", "ProviderKeyRemove"); using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); await userStore.AddLoginAsync(user, login); transaction.Commit(); } // Check the user has an id and the login. Assert.IsNotNull(user.Id); Assert.AreEqual(user.Logins.Count, 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 and remove the login. TestUser loadUser; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); await userStore.RemoveLoginAsync(loadUser, login); transaction.Commit(); } // Check we have the same user and that the login has been removed. Assert.AreEqual(loadUser.Id, user.Id); Assert.AreEqual(loadUser.Logins.Count, 0); }
public async Task DeleteUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user. string userName = "******"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); var userId = user.Id; // Create a new session and user store so that we actually hit the database and not the cache. userStore.Dispose(); session.Dispose(); session = SessionFactory.OpenSession(); userStore = new TestUserStore<TestUser>(session); // Load and delete the user. using (var transaction = session.BeginTransaction()) { user = await userStore.FindByIdAsync(userId); await userStore.DeleteAsync(user); transaction.Commit(); } // Check that the user has been deleted. var deletedUser = await userStore.FindByIdAsync(userId); Assert.IsNull(deletedUser); }
public async Task UpdateUser() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user. string userName = "******"; string email = "*****@*****.**"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); Assert.IsNull(user.Email); // Update the user's email address. using (var transaction = session.BeginTransaction()) { user.Email = email; await userStore.UpdateAsync(user); transaction.Commit(); } // Create a new session and user store so that we actually hit the database and not the cache. userStore.Dispose(); session.Dispose(); session = SessionFactory.OpenSession(); userStore = new TestUserStore<TestUser>(session); // Load and update the user. TestUser updatedUser; using (var transaction = session.BeginTransaction()) { updatedUser = await userStore.FindByIdAsync(user.Id); transaction.Commit(); } // Check the email has been updated and saved. Assert.AreEqual(updatedUser.Email, email); }
public async Task GetNonExistingUserByIdReturnsNull() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); TestUser user; using (var transaction = session.BeginTransaction()) { user = await userStore.FindByIdAsync("THISISNOTAUSERID"); transaction.Commit(); } // Check that we have no user. Assert.IsNull(user); }
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); }
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); }
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); }
public async Task GetUserByIdUsesCache() { // Create a session and user store for this test. var session = SessionFactory.OpenSession(); var userStore = new TestUserStore<TestUser>(session); // Create and save a user. string userName = "******"; var user = new TestUser { UserName = userName }; using (var transaction = session.BeginTransaction()) { await userStore.CreateAsync(user); transaction.Commit(); } // Check the user has an id and a username. Assert.IsNotNull(user.Id); Assert.IsNotNull(user.UserName); var userId = user.Id; userStore = new TestUserStore<TestUser>(session); // Load the user inside the same session, this should use the cache and not hit the database. TestUser loadUser; using (var transaction = session.BeginTransaction()) { loadUser = await userStore.FindByIdAsync(userId); transaction.Commit(); } // Check we have the same user. Assert.AreEqual(user.Id, loadUser.Id); Assert.AreEqual(user.UserName, loadUser.UserName); }