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

188 lines
7.1 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudDb.Model;
2022-01-13 14:36:27 +05:00
using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using System.Collections.Generic;
using System.Linq;
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();
}
2022-06-15 14:57:37 +05:00
~UserRoleServiceTest()
{
2022-01-13 14:36:27 +05:00
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);
}
[Fact]
public async Task InsertAsync_returns_id()
{
var service = new UserRoleService(context, cacheDb);
var newRole = new UserRoleDto
{
Caption = "new role",
IdType = 0,
};
2022-06-15 14:57:37 +05:00
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,
2022-06-15 14:57:37 +05:00
Permissions = new[] { new PermissionDto { Id = 2_000_001 } },
};
2022-06-15 14:57:37 +05:00
var id = await service.InsertAsync(newRole, CancellationToken.None);
2022-06-17 15:03:14 +05:00
var entity = await service.GetOrDefaultAsync(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,
2022-06-15 14:57:37 +05:00
Roles = new[] { new UserRoleDto { Id = 1_000_001 } }
};
var id = await service.InsertAsync(newRole, CancellationToken.None);
2022-06-17 15:03:14 +05:00
var entity = await service.GetOrDefaultAsync(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;
2022-06-15 14:57:37 +05:00
var modRole = new UserRoleDto
{
Id = updateId,
Caption = "role 2 level 1"
};
2022-06-09 13:36:14 +05:00
var id = await service.UpdateAsync(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 } },
};
2022-06-09 13:36:14 +05:00
var id = await service.UpdateAsync(modRole, CancellationToken.None);
2022-06-17 15:03:14 +05:00
var entity = await service.GetOrDefaultAsync(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 } }
};
2022-06-09 13:36:14 +05:00
var id = await service.UpdateAsync(modRole, CancellationToken.None);
2022-06-17 15:03:14 +05:00
var entity = await service.GetOrDefaultAsync(id);
Assert.Equal(modRole.Roles.Count(), entity.Roles.Count());
}
2022-01-13 14:36:27 +05:00
}
}