From 5bec44d988c830c251fc5e33e00b4c6ee7e50bd9 Mon Sep 17 00:00:00 2001 From: Mahdi Jafari <126339078+mahdijafariii@users.noreply.github.com> Date: Wed, 4 Sep 2024 17:28:45 +0330 Subject: [PATCH] Role and permission service test (#73) * feat(Role) : add role test * feat(Role) : add role test * feat(Role) : add permission test * fix(Test) : fix test --- .../AnalysisData.sln.DotSettings.user | 3 + AnalysisData/AnalysisData/AnalysisData.csproj | 6 + AnalysisData/TestProject/TestProject.csproj | 6 +- .../mahdi-test/PermissionServiceTest.cs | 76 ++++++++ .../mahdi-test/RoleManagementServiceTests.cs | 165 ++++++++++++++++++ 5 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 AnalysisData/TestProject/mahdi-test/PermissionServiceTest.cs create mode 100644 AnalysisData/TestProject/mahdi-test/RoleManagementServiceTests.cs diff --git a/AnalysisData/AnalysisData.sln.DotSettings.user b/AnalysisData/AnalysisData.sln.DotSettings.user index 7f9b11c..d200967 100644 --- a/AnalysisData/AnalysisData.sln.DotSettings.user +++ b/AnalysisData/AnalysisData.sln.DotSettings.user @@ -74,6 +74,9 @@ <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.User.Repository.UserRepository.UserRepositoryTests.GetUserByUsernameAsync_ShouldReturnsUserWithInputUsername_WhenUserWithInputUsernameExists</TestId> <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.User.Repository.UserRepository.UserRepositoryTests</TestId> </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="PermissionServiceTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Project Location="C:\Users\Mahdi\Desktop\New folder (2)\Summer1403-Project-Group03-Backend\AnalysisData\TestProject" Presentation="&lt;TestProject&gt;" /> </SessionState> <SessionState ContinuousTestingMode="0" Name="AddRole_ShouldAddsRoleToDatabase_Whenever" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> diff --git a/AnalysisData/AnalysisData/AnalysisData.csproj b/AnalysisData/AnalysisData/AnalysisData.csproj index d72f13a..bb4529e 100644 --- a/AnalysisData/AnalysisData/AnalysisData.csproj +++ b/AnalysisData/AnalysisData/AnalysisData.csproj @@ -10,10 +10,16 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + all diff --git a/AnalysisData/TestProject/TestProject.csproj b/AnalysisData/TestProject/TestProject.csproj index 6351c55..b3d10e8 100644 --- a/AnalysisData/TestProject/TestProject.csproj +++ b/AnalysisData/TestProject/TestProject.csproj @@ -10,7 +10,11 @@ - + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + diff --git a/AnalysisData/TestProject/mahdi-test/PermissionServiceTest.cs b/AnalysisData/TestProject/mahdi-test/PermissionServiceTest.cs new file mode 100644 index 0000000..9cc7a92 --- /dev/null +++ b/AnalysisData/TestProject/mahdi-test/PermissionServiceTest.cs @@ -0,0 +1,76 @@ +using System.Reflection; +using System.Security.Claims; +using AnalysisData.User.Services.PermissionService; +using NSubstitute; + +namespace TestProject.mahdi_test; + +public class PermissionServiceTest +{ + private readonly PermissionService _sut; + + // public PermissionServiceTest() + // { + // _sut = new PermissionService(); + // } + + // [Fact] + // public void GetPermission_ShouldReturnsPermissions_WhenUserHasRole() + // { + // // Arrange + // var role = "Admin"; + // var userClaims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + // { + // new Claim(ClaimTypes.Role, role) + // })); + // + // // Act + // var result = _sut.GetPermission(userClaims); + // + // // Assert + // var expectedPermissions = new List { "permission1", "permission2", "permission3", "permission4" }; + // + // // Convert result to a list and compare + // var resultList = result.ToList(); + // Assert.Equal(expectedPermissions.Count, resultList.Count); + // foreach (var permission in expectedPermissions) + // { + // Assert.Contains(permission, resultList); + // } + // } + + // [Fact] + // public void GetPermission_ShouldReturnsEmpty_WhenUserHasNoRole() + // { + // // Arrange + // var userClaims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + // { + // new Claim(ClaimTypes.Role, string.Empty) + // })); + // + // // Act + // var result = _sut.GetPermission(userClaims); + // + // // Assert + // Assert.Empty(result); + // } + // + // [Fact] + // public void GetPermission_ShouldReturnsEmpty_WhenRoleHasNoPermissions() + // { + // // Arrange + // var role = "Admin"; + // var userClaims = new ClaimsPrincipal(new ClaimsIdentity(new Claim[] + // { + // new Claim(ClaimTypes.Role, role) + // })); + // + // // Act + // var result = _sut.GetPermission(userClaims); + // + // // Assert + // Assert.Empty(result); + // } + // + // +} \ No newline at end of file diff --git a/AnalysisData/TestProject/mahdi-test/RoleManagementServiceTests.cs b/AnalysisData/TestProject/mahdi-test/RoleManagementServiceTests.cs new file mode 100644 index 0000000..4360132 --- /dev/null +++ b/AnalysisData/TestProject/mahdi-test/RoleManagementServiceTests.cs @@ -0,0 +1,165 @@ +using AnalysisData.Exception; +using AnalysisData.Exception.UserException; + +using AnalysisData.User.Model; +using AnalysisData.User.Repository.RoleRepository.Abstraction; +using AnalysisData.User.Services.RoleService; +using AnalysisData.User.UserDto.RoleDto; +using NSubstitute; + +namespace TestProject.mahdi_test; + +public class RoleManagementServiceTests +{ + private readonly IRoleRepository _roleRepositoryMock; + private readonly RoleManagementService _sut; + + public RoleManagementServiceTests() + { + _roleRepositoryMock = Substitute.For(); + _sut = new RoleManagementService(_roleRepositoryMock); + } + + + [Fact] + public async Task GetRoleCount_ShouldReturnsCorrectCount_WhenHaveManyRolesInRepository() + { + // Arrange + int expectedCount = 5; + _roleRepositoryMock.GetRolesCountAsync() + .Returns(expectedCount); + + // Act + int result = await _sut.GetRoleCount(); + + // Assert + Assert.Equal(expectedCount, result); + } + + [Fact] + public async Task GetRoleCount_ShouldReturnReturnsZero_WhenNoRolesExist() + { + // Arrange + int expectedCount = 0; + _roleRepositoryMock.GetRolesCountAsync() + .Returns(expectedCount); + + // Act + int result = await _sut.GetRoleCount(); + + // Assert + Assert.Equal(expectedCount, result); + } + + [Fact] + public async Task GetRolePagination_ShouldReturnsCorrectPagedRoles_WhenMultipleRolesExist() + { + // Arrange + int page = 1; + int limit = 10; + var roles = new List + { + new Role { Id = 1, RoleName = "Admin", RolePolicy = "AdminPolicy" }, + new Role { Id = 2, RoleName = "User", RolePolicy = "UserPolicy" } + }; + + _roleRepositoryMock.GetAllRolesPaginationAsync(page, limit) + .Returns(Task.FromResult(roles)); + + var expectedRoles = roles.Select(x => new RolePaginationDto + { + Id = x.Id.ToString(), + Name = x.RoleName, + Policy = x.RolePolicy + }).ToList(); + + // Act + var result = await _sut.GetRolePagination(page, limit); + + // Assert + Assert.Equivalent(expectedRoles, result); + } + + + [Fact] + public async Task GetRolePagination_ShouldReturnsCorrectPagedRoles_WhenNoRolesExist() + { + // Arrange + int page = 1; + int limit = 10; + var roles = new List(); + + _roleRepositoryMock.GetAllRolesPaginationAsync(page, limit) + .Returns(Task.FromResult(roles)); + + var expectedRoles = roles.Select(x => new RolePaginationDto + { + Id = x.Id.ToString(), + Name = x.RoleName, + Policy = x.RolePolicy + }).ToList(); + + // Act + var result = await _sut.GetRolePagination(page, limit); + + // Assert + Assert.Equivalent(expectedRoles, result); + } + + [Fact] + public async Task DeleteRole_ShouldDeleteRole_WhenRoleExists() + { + // Arrange + string roleName = "Admin"; + var existingRole = new Role { RoleName = roleName }; + _roleRepositoryMock.GetRoleByNameAsync(roleName).Returns(existingRole); + + // Act + await _sut.DeleteRole(roleName); + + // Assert + await _roleRepositoryMock.Received(1).DeleteRoleAsync(roleName); + } + + [Fact] + public async Task DeleteRole_ShouldThrowRoleNotFoundException_WhenRoleDoesNotExist() + { + // Arrange + string roleName = "NonExistentRole"; + _roleRepositoryMock.GetRoleByNameAsync(roleName).Returns((Role)null); + + // Act & Assert + await Assert.ThrowsAsync(() => _sut.DeleteRole(roleName)); + } + + [Fact] + public async Task AddRole_ShouldNotAddRole_WhenRoleExistsBefore() + { + // Arrange + string roleName = "admin"; + var existingRole = new Role { RoleName = roleName , RolePolicy = "gold"}; + _roleRepositoryMock.GetRoleByNameAsync(roleName).Returns(existingRole); + + // Assert && Act + await Assert.ThrowsAsync(() => _sut.AddRole(roleName, "gold")); + } + + [Fact] + public async Task AddRole_ShouldAddRole_WhenRoleNotExistsBefore() + { + // Arrange + string roleName = "Admin"; + string rolePolicy = "Gold"; + + var newRole = new Role { RoleName = roleName.ToLower(), RolePolicy = rolePolicy.ToLower() }; + + _roleRepositoryMock.GetRoleByNameAsync(roleName).Returns((Role)null); + + // Act + await _sut.AddRole(roleName, rolePolicy); + + + // Assert + await _roleRepositoryMock.Received(1).AddRoleAsync(Arg.Is(r => + r.RoleName == roleName.ToLower() && r.RolePolicy == rolePolicy.ToLower())); } +} \ No newline at end of file