RC production acceptance / MySQL, permission, browser and package acceptance (push) Failing after 2m10s
70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using System.Reflection;
|
|
using Jiaowu.Api.Controllers;
|
|
using Jiaowu.Api.Domain.Identity;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
namespace Jiaowu.Api.Tests;
|
|
|
|
public sealed class AuthorizationMatrixTests
|
|
{
|
|
public static TheoryData<Type, string, string[]> CriticalEndpoints => new()
|
|
{
|
|
{
|
|
typeof(GradeAppealsController),
|
|
nameof(GradeAppealsController.Create),
|
|
[SystemRoles.Student]
|
|
},
|
|
{
|
|
typeof(GradeAppealsController),
|
|
nameof(GradeAppealsController.Withdraw),
|
|
[SystemRoles.Student]
|
|
},
|
|
{
|
|
typeof(GradeAppealsController),
|
|
nameof(GradeAppealsController.Review),
|
|
[SystemRoles.SuperAdmin, SystemRoles.AcademicAdmin, SystemRoles.CollegeAdmin, SystemRoles.Teacher]
|
|
},
|
|
{
|
|
typeof(TeachingMonitoringController),
|
|
nameof(TeachingMonitoringController.SaveSetting),
|
|
[SystemRoles.SuperAdmin, SystemRoles.AcademicAdmin]
|
|
},
|
|
{
|
|
typeof(TeachingMonitoringController),
|
|
nameof(TeachingMonitoringController.Remind),
|
|
[SystemRoles.SuperAdmin, SystemRoles.AcademicAdmin, SystemRoles.CollegeAdmin, SystemRoles.Leader]
|
|
},
|
|
{
|
|
typeof(ApprovalsController),
|
|
nameof(ApprovalsController.FinalApproveGradeMod),
|
|
[SystemRoles.SuperAdmin, SystemRoles.AcademicAdmin]
|
|
}
|
|
};
|
|
|
|
[Theory]
|
|
[MemberData(nameof(CriticalEndpoints))]
|
|
public void Critical_endpoint_keeps_its_server_side_role_contract(
|
|
Type controllerType,
|
|
string methodName,
|
|
string[] expectedRoles)
|
|
{
|
|
Assert.NotEmpty(controllerType.GetCustomAttributes<AuthorizeAttribute>(inherit: true));
|
|
|
|
var method = controllerType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
|
|
Assert.NotNull(method);
|
|
Assert.Empty(method!.GetCustomAttributes<AllowAnonymousAttribute>(inherit: true));
|
|
|
|
var actualRoles = method
|
|
.GetCustomAttributes<AuthorizeAttribute>(inherit: true)
|
|
.SelectMany(x => (x.Roles ?? string.Empty).Split(',', StringSplitOptions.RemoveEmptyEntries))
|
|
.Select(x => x.Trim())
|
|
.Distinct(StringComparer.Ordinal)
|
|
.Order(StringComparer.Ordinal)
|
|
.ToArray();
|
|
|
|
Assert.Equal(
|
|
expectedRoles.Order(StringComparer.Ordinal).ToArray(),
|
|
actualRoles);
|
|
}
|
|
}
|