This commit is contained in:
2026-07-25 11:46:39 +08:00 Unverified
parent 533ff6b9f6
commit fdca6a3edf
8 changed files with 603 additions and 25 deletions
@@ -65,6 +65,68 @@ public sealed class TeachingTasksControllerTests
task => Assert.Equal(TeachingTaskStatus.Published, task.Status));
}
[Fact]
public async Task Options_ReturnsAllPublishedTasksWithoutListPageTruncation()
{
await using var database = await TestDatabase.CreateAsync();
for (var index = 1; index <= 125; index++)
{
await database.AddTaskAsync(
$"OPTION-{index:D3}",
TeachingTaskStatus.Published);
}
await database.AddTaskAsync("DRAFT", TeachingTaskStatus.Draft);
var result = await database.Controller.GetOptions(
database.TermId,
TeachingTaskStatus.Published,
CancellationToken.None);
var ok = Assert.IsType<OkObjectResult>(result);
var items = Assert.IsAssignableFrom<System.Collections.IEnumerable>(ok.Value);
Assert.Equal(125, items.Cast<object>().Count());
}
[Fact]
public async Task Options_PreservesCollegeDataScope()
{
await using var database = await TestDatabase.CreateAsync(collegeScoped: true);
await database.AddTaskAsync("IN-SCOPE", TeachingTaskStatus.Published);
var otherCollege = new College { Code = "OTHER", Name = "其他学院" };
var otherCourse = new Course
{
Code = "OTHER101",
Name = "范围外课程",
College = otherCollege,
Nature = CourseNature.MajorRequired,
TotalHours = 32,
LectureHours = 32
};
database.Db.Add(new TeachingTask
{
TaskNumber = "TASK-OUT-OF-SCOPE",
Name = "范围外教学任务",
AcademicTermId = database.TermId,
Course = otherCourse,
Capacity = 40,
StartWeek = 1,
EndWeek = 16,
WeeklyHours = 2,
Status = TeachingTaskStatus.Published,
PublishedAt = DateTime.UtcNow
});
await database.Db.SaveChangesAsync();
var result = await database.Controller.GetOptions(
database.TermId,
TeachingTaskStatus.Published,
CancellationToken.None);
var ok = Assert.IsType<OkObjectResult>(result);
var items = Assert.IsAssignableFrom<System.Collections.IEnumerable>(ok.Value);
Assert.Single(items.Cast<object>());
}
private sealed class TestDatabase : IAsyncDisposable
{
private readonly SqliteConnection connection;
@@ -75,7 +137,8 @@ public sealed class TeachingTasksControllerTests
SqliteConnection connection,
AppDbContext db,
AcademicTerm term,
Course course)
Course course,
ICurrentUserDataScope dataScope)
{
this.connection = connection;
Db = db;
@@ -83,13 +146,14 @@ public sealed class TeachingTasksControllerTests
this.course = course;
Controller = new TeachingTasksController(
db,
new TestDataScope());
dataScope);
}
public AppDbContext Db { get; }
public TeachingTasksController Controller { get; }
public Guid TermId => term.Id;
public static async Task<TestDatabase> CreateAsync()
public static async Task<TestDatabase> CreateAsync(bool collegeScoped = false)
{
var connection = new SqliteConnection("Data Source=:memory:");
await connection.OpenAsync();
@@ -120,7 +184,12 @@ public sealed class TeachingTasksControllerTests
};
db.AddRange(college, term, course);
await db.SaveChangesAsync();
return new TestDatabase(connection, db, term, course);
return new TestDatabase(
connection,
db,
term,
course,
new TestDataScope(collegeScoped ? college.Id : null));
}
public async Task<TeachingTask> AddTaskAsync(
@@ -156,11 +225,19 @@ public sealed class TeachingTasksControllerTests
private sealed class TestDataScope : ICurrentUserDataScope
{
public CurrentUserScope Current { get; } = new(
Guid.NewGuid(),
"测试管理员",
null,
DataScope.All,
new HashSet<string>([SystemRoles.SuperAdmin]));
public TestDataScope(Guid? restrictedCollegeId = null)
{
Current = new CurrentUserScope(
Guid.NewGuid(),
"测试管理员",
restrictedCollegeId,
restrictedCollegeId.HasValue ? DataScope.College : DataScope.All,
new HashSet<string>(
restrictedCollegeId.HasValue
? [SystemRoles.CollegeAdmin]
: [SystemRoles.SuperAdmin]));
}
public CurrentUserScope Current { get; }
}
}