Add Moodle course links for teaching tasks

This commit is contained in:
biss committed 2026-11-21 13:32:19 +08:00
1 parent aa4bb8f378
commit eacc21fbc7
13 files changed
+13058 -2

No files matched your search

@@ -12,6 +12,41 @@ namespace Jiaowu.Api.Tests;
public sealed class TeachingTasksControllerTests
{
[Fact]
public async Task MoodleLink_CollegeAdminCannotChangeAnotherCollegesTask()
{
await using var database = await TestDatabase.CreateAsync(collegeScoped: true);
var task = new TeachingTask
{
TaskNumber = "OUTSIDE-MOODLE", Name = "其他学院课程", AcademicTermId = database.TermId,
Course = new Course { Code = "OUTSIDE", Name = "其他课程", College = new College { Code = "OUT", Name = "其他学院" }, Nature = CourseNature.MajorRequired, TotalHours = 32, LectureHours = 32 },
Capacity = 40, Status = TeachingTaskStatus.Published
};
database.Db.TeachingTasks.Add(task);
await database.Db.SaveChangesAsync();
Assert.IsType<NotFoundResult>(await database.Controller.SetMoodleCourse(task.Id, new("https://moodle.example.edu/course/view.php?id=1"), CancellationToken.None));
await database.Db.Entry(task).ReloadAsync();
Assert.Null(task.MoodleCourseUrl);
}
[Fact]
public async Task MoodleLink_CanBeSetAndClearedOnPublishedTask_RejectsUnsafeUrls()
{
await using var database = await TestDatabase.CreateAsync();
var task = await database.AddTaskAsync("MOODLE", TeachingTaskStatus.Published);
var saved = await database.Controller.SetMoodleCourse(task.Id, new(" https://moodle.example.edu/course/view.php?id=42 "), CancellationToken.None);
Assert.IsType<NoContentResult>(saved);
await database.Db.Entry(task).ReloadAsync();
Assert.Equal("https://moodle.example.edu/course/view.php?id=42", task.MoodleCourseUrl);
Assert.IsType<ObjectResult>(await database.Controller.SetMoodleCourse(task.Id, new("javascript:alert(1)"), CancellationToken.None));
Assert.IsType<ObjectResult>(await database.Controller.SetMoodleCourse(task.Id, new("https://user:password@example.edu/"), CancellationToken.None));
await database.Db.Entry(task).ReloadAsync();
Assert.NotNull(task.MoodleCourseUrl);
Assert.IsType<NoContentResult>(await database.Controller.SetMoodleCourse(task.Id, new(null), CancellationToken.None));
await database.Db.Entry(task).ReloadAsync();
Assert.Null(task.MoodleCourseUrl);
}
[Fact]
public async Task Delete_AllowsArchivedTask_ButRejectsPublishedTask()
{