DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/UserRoleServiceTest.cs

95 lines
3.8 KiB
C#
Raw Normal View History

2022-01-13 14:36:27 +05:00
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using System.Collections.Generic;
using System.Linq;
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);
}
}
}