Expand approval center and configure reviewer routing

This commit is contained in:
biss committed 2026-11-07 20:13:38 +08:00
1 parent cc1b60450f
commit 2d1a43fd39
14 files changed
+12632 -78

No files matched your search

@@ -7,11 +7,124 @@ using Jiaowu.Api.Infrastructure.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging.Abstractions;
namespace Jiaowu.Api.Tests;
public sealed class ApprovalsControllerTests
{
[Fact]
public async Task Clearance_pending_only_includes_the_responsible_role()
{
await using var fixture = await ApprovalFixture.CreateAsync();
var batch = new GraduationClearanceBatch { Name = "离校", GraduationYear = 2026 };
var collegeItem = new GraduationClearanceItem { GraduationClearanceBatchId = batch.Id, Code = "COL", Name = "学院确认", ResponsibleUnit = "学院", ResponsibleRole = SystemRoles.CollegeAdmin };
var academicItem = new GraduationClearanceItem { GraduationClearanceBatchId = batch.Id, Code = "ACA", Name = "教务确认", ResponsibleUnit = "教务", ResponsibleRole = SystemRoles.AcademicAdmin };
fixture.Db.AddRange(batch, collegeItem, academicItem,
new GraduationClearanceRecord { GraduationClearanceItemId = collegeItem.Id, StudentId = fixture.Student.Id },
new GraduationClearanceRecord { GraduationClearanceItemId = academicItem.Id, StudentId = fixture.Student.Id });
await fixture.Db.SaveChangesAsync();
var controller = new ApprovalsController(fixture.Db, new CollegeDataScope(fixture.CurrentCourse.CollegeId));
var response = await controller.GetPending(1, 20, CancellationToken.None);
var result = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(response.Result).Value);
Assert.Equal("GraduationClearance", Assert.Single(result.Items).Type);
Assert.Equal(1, result.Counts["GraduationClearance"]);
}
[Fact]
public async Task Sqlite_route_settings_upgrade_preserves_data_and_is_repeatable()
{
await using var fixture = await ApprovalFixture.CreateAsync();
await fixture.Db.Database.ExecuteSqlRawAsync("DROP TABLE ApprovalRouteSettings");
await fixture.Db.Database.ExecuteSqlRawAsync("ALTER TABLE CourseExemptions DROP COLUMN ReviewerRoleSnapshot");
await fixture.Db.Database.ExecuteSqlRawAsync("ALTER TABLE DeferredExams DROP COLUMN ReviewerRoleSnapshot");
await fixture.Db.Database.ExecuteSqlRawAsync("ALTER TABLE CourseSubstitutions DROP COLUMN ReviewerRoleSnapshot");
var migrator = new DevelopmentSqliteMigrator(fixture.Db, NullLogger<DevelopmentSqliteMigrator>.Instance);
await migrator.MigrateAsync();
await migrator.MigrateAsync();
fixture.Db.ChangeTracker.Clear();
Assert.Equal(fixture.Student.Name, (await fixture.Db.Students.FindAsync(fixture.Student.Id))!.Name);
fixture.Db.ApprovalRouteSettings.Add(new ApprovalRouteSetting { Type = "CourseExemption", ReviewerRole = "AnyManager" });
await fixture.Db.SaveChangesAsync();
Assert.Single(await fixture.Db.ApprovalRouteSettings.ToListAsync());
}
[Fact]
public async Task Routing_changes_only_affect_new_requests_and_are_enforced_for_review()
{
await using var fixture = await ApprovalFixture.CreateAsync();
var setting = new ApprovalRouteSetting { Type = "CourseExemption", ReviewerRole = SystemRoles.AcademicAdmin };
fixture.Db.ApprovalRouteSettings.Add(setting);
await fixture.Db.SaveChangesAsync();
Assert.IsType<CreatedResult>(await fixture.Controller.ApplyExemption(
new ExemptionRequest(fixture.CurrentTask.Id, "申请免修"), CancellationToken.None));
var exemption = Assert.Single(fixture.Db.CourseExemptions);
Assert.Equal(SystemRoles.AcademicAdmin, exemption.ReviewerRoleSnapshot);
setting.ReviewerRole = SystemRoles.CollegeAdmin;
await fixture.Db.SaveChangesAsync();
var college = new ApprovalsController(fixture.Db, new CollegeDataScope(fixture.CurrentCourse.CollegeId));
Assert.IsType<ForbidResult>(await college.RejectExemption(exemption.Id, new ReviewBody("无权处理"), CancellationToken.None));
var pending = await college.GetPending(1, 20, CancellationToken.None);
var page = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(pending.Result).Value);
Assert.DoesNotContain(page.Items, x => x.Id == exemption.Id);
Assert.Equal(ApprovalStatus.Submitted, exemption.Status);
}
[Fact]
public async Task Pending_status_changes_are_restricted_to_the_reviewers_stage()
{
await using var fixture = await ApprovalFixture.CreateAsync();
var collegeItem = new StudentStatusChange { StudentId = fixture.Student.Id, Reason = "学院审核", State = StudentStatusChangeState.CounselorApproved };
var finalItem = new StudentStatusChange { StudentId = fixture.Student.Id, Reason = "校级审核", State = StudentStatusChangeState.CollegeApproved };
fixture.Db.AddRange(collegeItem, finalItem);
await fixture.Db.SaveChangesAsync();
var controller = new ApprovalsController(fixture.Db, new CollegeDataScope(fixture.CurrentCourse.CollegeId));
var result = await controller.GetPending(1, 20, CancellationToken.None);
var page = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(result.Result).Value);
Assert.Equal(collegeItem.Id, Assert.Single(page.Items).Id);
}
[Fact]
public async Task Exemption_review_rejects_other_colleges_and_already_processed_items()
{
await using var fixture = await ApprovalFixture.CreateAsync();
var exemption = new CourseExemption { StudentId = fixture.Student.Id, TeachingTaskId = fixture.CurrentTask.Id, Reason = "测试" };
fixture.Db.CourseExemptions.Add(exemption);
await fixture.Db.SaveChangesAsync();
var outsider = new ApprovalsController(fixture.Db, new CollegeDataScope(Guid.NewGuid()));
Assert.IsType<NotFoundResult>(await outsider.RejectExemption(exemption.Id, new ReviewBody("拒绝"), CancellationToken.None));
Assert.Equal(ApprovalStatus.Submitted, exemption.Status);
exemption.Status = ApprovalStatus.Approved;
await fixture.Db.SaveChangesAsync();
var reviewer = new ApprovalsController(fixture.Db, new CollegeDataScope(fixture.CurrentCourse.CollegeId));
Assert.IsType<ConflictObjectResult>(await reviewer.RejectExemption(exemption.Id, new ReviewBody("重复操作"), CancellationToken.None));
Assert.Equal(ApprovalStatus.Approved, exemption.Status);
}
[Fact]
public async Task Pending_course_applications_follow_teacher_college_scope()
{
await using var fixture = await ApprovalFixture.CreateAsync();
var otherCollege = new College { Code = "OTHER", Name = "其他学院" };
var ownTeacher = new Teacher { TeacherNumber = "T1", Name = "本院教师", CollegeId = fixture.CurrentCourse.CollegeId };
var otherTeacher = new Teacher { TeacherNumber = "T2", Name = "外院教师", CollegeId = otherCollege.Id };
fixture.Db.AddRange(otherCollege, ownTeacher, otherTeacher,
new TeacherCourseApplication { TeacherId = ownTeacher.Id, CourseId = fixture.CurrentCourse.Id, AcademicTermId = fixture.CurrentTerm.Id },
new TeacherCourseApplication { TeacherId = otherTeacher.Id, CourseId = fixture.SubstituteCourse.Id, AcademicTermId = fixture.CurrentTerm.Id });
await fixture.Db.SaveChangesAsync();
var controller = new ApprovalsController(fixture.Db, new CollegeDataScope(fixture.CurrentCourse.CollegeId));
var result = await controller.GetPending(1, 20, CancellationToken.None);
var page = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(result.Result).Value);
var item = Assert.Single(page.Items);
Assert.Equal("TeacherCourseApplication", item.Type);
Assert.Contains("本院教师", item.Title);
Assert.Equal(1, page.Total);
}
[Fact]
public async Task Pending_returns_a_single_server_page_with_total()
{
@@ -39,11 +152,18 @@ public sealed class ApprovalsControllerTests
var result = await controller.GetPending(2, 1, CancellationToken.None);
var ok = Assert.IsType<OkObjectResult>(result.Result);
var page = Assert.IsType<PagedResult<ApprovalItem>>(ok.Value);
var page = Assert.IsType<ApprovalPendingResult>(ok.Value);
Assert.Equal(2, page.Total);
Assert.Equal(1, page.Counts["CourseExemption"]);
Assert.Equal(1, page.Counts["DeferredExam"]);
Assert.Equal(2, page.Page);
var item = Assert.Single(page.Items);
Assert.Equal("较早提交", item.Desc);
var oldest = await controller.GetPending(1, 1, CancellationToken.None, oldestFirst: true);
var oldestPage = Assert.IsType<ApprovalPendingResult>(Assert.IsType<OkObjectResult>(oldest.Result).Value);
Assert.Equal("较早提交", Assert.Single(oldestPage.Items).Desc);
Assert.Equal(2, oldestPage.Total);
}
[Fact]
@@ -350,4 +470,11 @@ public sealed class ApprovalsControllerTests
DataScope.All,
new HashSet<string>([SystemRoles.SuperAdmin]));
}
private sealed class CollegeDataScope(Guid collegeId) : ICurrentUserDataScope
{
public CurrentUserScope Current { get; } = new(
Guid.NewGuid(), "学院管理员", collegeId, DataScope.College,
new HashSet<string>([SystemRoles.CollegeAdmin]));
}
}