-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
0da447b
commit 5bec44d
Showing
5 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
AnalysisData/TestProject/mahdi-test/PermissionServiceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> { "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); | ||
// } | ||
// | ||
// | ||
} |
165 changes: 165 additions & 0 deletions
165
AnalysisData/TestProject/mahdi-test/RoleManagementServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IRoleRepository>(); | ||
_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<Role> | ||
{ | ||
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<Role>(); | ||
|
||
_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<RoleNotFoundException>(() => _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<DuplicateRoleExistException>(() => _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<Role>(r => | ||
r.RoleName == roleName.ToLower() && r.RolePolicy == rolePolicy.ToLower())); } | ||
} |