From 7b5e7274496d975ffce317b9ac9851c908925975 Mon Sep 17 00:00:00 2001 From: Carlos Pavajeau Date: Thu, 18 Feb 2021 09:19:54 -0500 Subject: [PATCH] test(back-end): Some improvements to the application user tests --- .../ApplicationUserRepositoryTest.cs | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/Infrastructure.Test/Repositories/ApplicationUserRepositoryTest.cs b/Infrastructure.Test/Repositories/ApplicationUserRepositoryTest.cs index 98a9e0e3..ede0e005 100644 --- a/Infrastructure.Test/Repositories/ApplicationUserRepositoryTest.cs +++ b/Infrastructure.Test/Repositories/ApplicationUserRepositoryTest.cs @@ -11,6 +11,8 @@ namespace Infrastructure.Test.Repositories [TestFixture] public class ApplicationUserRepositoryTest : BaseRepositoryTest { + public static string SavedUserId; + private IApplicationUserRepository _applicationUserRepository; [SetUp] @@ -39,9 +41,12 @@ public async Task Save_Valid_User() PhoneNumber = "3167040706" }; - IdentityResult result = await _applicationUserRepository.CreateAsync(applicationUser, "ThisisaSecurePassword321*"); + IdentityResult result = + await _applicationUserRepository.CreateAsync(applicationUser, "ThisisaSecurePassword321*"); Assert.IsTrue(result.Succeeded); + + SavedUserId = applicationUser.Id; } catch (DbUpdateException e) { @@ -61,7 +66,8 @@ public async Task Save_Invalid_User() PhoneNumber = "31670407" }; - IdentityResult result = await _applicationUserRepository.CreateAsync(applicationUser, "notsecurepassword"); + IdentityResult result = + await _applicationUserRepository.CreateAsync(applicationUser, "notsecurepassword"); Assert.IsTrue(!result.Succeeded); } @@ -95,7 +101,8 @@ public async Task Search_User_By_Email() [Order(4)] public async Task Search_User_By_UserName_Or_Email() { - ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("example@example.com"); + ApplicationUser applicationUser = + await _applicationUserRepository.FindByNameOrEmailAsync("example@example.com"); Assert.IsNotNull(applicationUser); Assert.AreEqual("example@example.com", applicationUser.Email); @@ -103,15 +110,24 @@ public async Task Search_User_By_UserName_Or_Email() [Test] [Order(5)] - public async Task Assign_Role_To_User() + public async Task Assign_Roles_To_User() { try { ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin"); - IdentityResult result = await _applicationUserRepository.AddToRoleAsync(applicationUser, "Administrator"); - - Assert.IsTrue(result.Succeeded); + IdentityResult administratorRole = + await _applicationUserRepository.AddToRoleAsync(applicationUser, "Administrator"); + IdentityResult clientRole = await _applicationUserRepository.AddToRoleAsync(applicationUser, "Client"); + IdentityResult officeEmployeeRole = + await _applicationUserRepository.AddToRoleAsync(applicationUser, "OfficeEmployee"); + IdentityResult technicalEmployeeRole = + await _applicationUserRepository.AddToRoleAsync(applicationUser, "TechnicalEmployee"); + + Assert.IsTrue(administratorRole.Succeeded); + Assert.IsTrue(clientRole.Succeeded); + Assert.IsTrue(officeEmployeeRole.Succeeded); + Assert.IsTrue(technicalEmployeeRole.Succeeded); } catch (DbUpdateException e) { @@ -119,17 +135,6 @@ public async Task Assign_Role_To_User() } } - [Test] - [Order(6)] - public async Task Check_User_Role() - { - ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin"); - - string role = await _applicationUserRepository.GetUserRoleAsync(applicationUser); - Assert.IsNotNull(role); - Assert.AreEqual("Administrator", role); - } - [Test] [Order(7)] public async Task Generate_ConfirmEmailToken_And_ConfirmEmail() @@ -137,10 +142,12 @@ public async Task Generate_ConfirmEmailToken_And_ConfirmEmail() ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin"); Assert.IsNotNull(applicationUser); - string confirmationToken = await _applicationUserRepository.GenerateEmailConfirmationTokenAsync(applicationUser); + string confirmationToken = + await _applicationUserRepository.GenerateEmailConfirmationTokenAsync(applicationUser); Assert.IsNotNull(confirmationToken); - ApplicationUser result = await _applicationUserRepository.ConfirmEmailAsync(applicationUser, confirmationToken); + ApplicationUser result = + await _applicationUserRepository.ConfirmEmailAsync(applicationUser, confirmationToken); Assert.IsNotNull(result); Assert.IsTrue(applicationUser.EmailConfirmed); @@ -153,7 +160,8 @@ public async Task ChangePassword_Without_ResetToken() ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin"); Assert.IsNotNull(applicationUser); - IdentityResult result = await _applicationUserRepository.ChangePasswordAsync(applicationUser, "ThisisaSecurePassword321*", "ThisIsMyNewPassword123."); + IdentityResult result = await _applicationUserRepository.ChangePasswordAsync(applicationUser, + "ThisisaSecurePassword321*", "ThisIsMyNewPassword123."); Assert.IsTrue(result.Succeeded); } @@ -165,10 +173,12 @@ public async Task ChangePassword_With_ResetToken() ApplicationUser applicationUser = await _applicationUserRepository.FindByNameOrEmailAsync("admin"); Assert.IsNotNull(applicationUser); - string passwordResetToken = await _applicationUserRepository.GeneratePasswordResetTokenAsync(applicationUser); + string passwordResetToken = + await _applicationUserRepository.GeneratePasswordResetTokenAsync(applicationUser); Assert.IsNotNull(passwordResetToken); - IdentityResult result = await _applicationUserRepository.ResetPasswordAsync(applicationUser, passwordResetToken, "ThisIsMyResetPassword321*"); + IdentityResult result = await _applicationUserRepository.ResetPasswordAsync(applicationUser, + passwordResetToken, "ThisIsMyResetPassword321*"); Assert.IsTrue(result.Succeeded); }