2022-01-13 15:51:06 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-01-13 14:36:27 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
2022-01-13 15:51:06 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-01-13 14:36:27 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-01-13 15:51:06 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2022-01-13 14:36:27 +05:00
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Tests.ServicesTests
|
|
|
|
|
{
|
|
|
|
|
public class UserRoleServiceTest
|
|
|
|
|
{
|
|
|
|
|
private readonly AsbCloudDbContext context;
|
|
|
|
|
private readonly CacheDb cacheDb;
|
|
|
|
|
|
|
|
|
|
private readonly List<UserRole> roles = new() {
|
|
|
|
|
new UserRole { Id = 1_000_001, Caption = "role 1 level 0" },
|
|
|
|
|
new UserRole { Id = 1_000_002, Caption = "role 2 level 1" },
|
|
|
|
|
new UserRole { Id = 1_000_003, Caption = "role 3 level 1" },
|
|
|
|
|
new UserRole { Id = 1_000_004, Caption = "role 4 level 2" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly List<RelationUserRoleUserRole> relationRoleRole = new()
|
|
|
|
|
{
|
|
|
|
|
new RelationUserRoleUserRole { Id = 1_000_002, IdInclude = 1_000_001 },
|
|
|
|
|
new RelationUserRoleUserRole { Id = 1_000_003, IdInclude = 1_000_001 },
|
|
|
|
|
new RelationUserRoleUserRole { Id = 1_000_004, IdInclude = 1_000_002 },
|
|
|
|
|
new RelationUserRoleUserRole { Id = 1_000_004, IdInclude = 1_000_003 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly List<Permission> permissions = new()
|
|
|
|
|
{
|
|
|
|
|
new Permission { Id = 2_000_001, Name = "permission 1" },
|
|
|
|
|
new Permission { Id = 2_000_002, Name = "permission 2" },
|
|
|
|
|
new Permission { Id = 2_000_003, Name = "permission 3" },
|
|
|
|
|
new Permission { Id = 2_000_004, Name = "permission 4" },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private readonly List<RelationUserRolePermission> relationRolePermission = new()
|
|
|
|
|
{
|
|
|
|
|
new RelationUserRolePermission { IdUserRole = 1_000_001, IdPermission = 2_000_001 },
|
|
|
|
|
new RelationUserRolePermission { IdUserRole = 1_000_002, IdPermission = 2_000_002 },
|
|
|
|
|
new RelationUserRolePermission { IdUserRole = 1_000_003, IdPermission = 2_000_003 },
|
|
|
|
|
new RelationUserRolePermission { IdUserRole = 1_000_004, IdPermission = 2_000_004 },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public UserRoleServiceTest()
|
|
|
|
|
{
|
|
|
|
|
cacheDb = new CacheDb();
|
|
|
|
|
context = TestHelpter.MakeTestContext();
|
|
|
|
|
context.UserRoles.RemoveRange(roles);
|
|
|
|
|
context.Permissions.RemoveRange(permissions);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
context.UserRoles.AddRange(roles);
|
|
|
|
|
context.Permissions.AddRange(permissions);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
context.RelationUserRoleUserRoles.AddRange(relationRoleRole);
|
|
|
|
|
context.RelationUserRolePermissions.AddRange(relationRolePermission);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~UserRoleServiceTest(){
|
|
|
|
|
context.UserRoles.RemoveRange(roles);
|
|
|
|
|
context.Permissions.RemoveRange(permissions);
|
|
|
|
|
context.RelationUserRoleUserRoles.RemoveRange(relationRoleRole);
|
|
|
|
|
context.RelationUserRolePermissions.RemoveRange(relationRolePermission);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
context.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void GetNestedById_return_4_items()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var nestedRoles = service.GetNestedById(1_000_004);
|
|
|
|
|
Assert.Equal(roles.Count, nestedRoles.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void HasPermission_return_true()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var result = service.HasPermission(new int[] { 1_000_004 }, "permission 1");
|
|
|
|
|
Assert.True(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void HasPermission_return_false()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var result = service.HasPermission(new int[] { 1_000_003 }, "permission 2");
|
|
|
|
|
Assert.False(result);
|
|
|
|
|
}
|
2022-01-13 15:51:06 +05:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task InsertAsync_returns_id()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var newRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Caption = "new role",
|
|
|
|
|
IdType = 0,
|
|
|
|
|
};
|
|
|
|
|
var id = await service.InsertAsync(newRole, CancellationToken.None );
|
|
|
|
|
Assert.NotEqual(0, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task InsertAsync_updates_relation_to_permission()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var newRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Caption = "new role",
|
|
|
|
|
IdType = 0,
|
|
|
|
|
Permissions = new[] { new PermissionDto{ Id = 2_000_001 } },
|
|
|
|
|
};
|
|
|
|
|
var id = await service.InsertAsync(newRole, CancellationToken.None);
|
|
|
|
|
var entity = await service.GetAsync(id);
|
|
|
|
|
Assert.Equal(newRole.Permissions.Count(), entity.Permissions.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task InsertAsync_updates_relation_to_role()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
var newRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Caption = "new role",
|
|
|
|
|
IdType = 0,
|
|
|
|
|
Roles = new [] { new UserRoleDto { Id = 1_000_001 } }
|
|
|
|
|
};
|
|
|
|
|
var id = await service.InsertAsync(newRole, CancellationToken.None);
|
|
|
|
|
var entity = await service.GetAsync(id);
|
|
|
|
|
Assert.Equal(newRole.Roles.Count, entity.Roles.Count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdateAsync_returns_id()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
const int updateId = 1_000_002;
|
|
|
|
|
var modRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Id = updateId,
|
|
|
|
|
Caption = "role 2 level 1"
|
|
|
|
|
};
|
|
|
|
|
var id = await service.UpdateAsync(1_000_002, modRole, CancellationToken.None);
|
|
|
|
|
Assert.Equal(updateId, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdateAsync_updates_relation_to_permission()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
const int updateId = 1_000_002;
|
|
|
|
|
var modRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Id = updateId,
|
|
|
|
|
Caption = "role 2 level 1",
|
|
|
|
|
Permissions = new[] { new PermissionDto { Id = 2_000_001 } },
|
|
|
|
|
};
|
|
|
|
|
var id = await service.UpdateAsync(1_000_002, modRole, CancellationToken.None);
|
|
|
|
|
var entity = await service.GetAsync(id);
|
|
|
|
|
Assert.Equal(modRole.Permissions.Count(), entity.Permissions.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdateAsync_updates_relation_to_role()
|
|
|
|
|
{
|
|
|
|
|
var service = new UserRoleService(context, cacheDb);
|
|
|
|
|
const int updateId = 1_000_002;
|
|
|
|
|
var modRole = new UserRoleDto
|
|
|
|
|
{
|
|
|
|
|
Id = updateId,
|
|
|
|
|
Caption = "role 2 level 1",
|
|
|
|
|
Roles = new[] { new UserRoleDto { Id = 1_000_001 } }
|
|
|
|
|
};
|
|
|
|
|
var id = await service.UpdateAsync(1_000_002, modRole, CancellationToken.None);
|
|
|
|
|
var entity = await service.GetAsync(id);
|
|
|
|
|
Assert.Equal(modRole.Roles.Count(), entity.Roles.Count());
|
|
|
|
|
}
|
2022-01-13 14:36:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|