Files
Academic-Affairs-System/tests/Jiaowu.Api.Tests/MoodleCourseClientTests.cs
T

81 lines
3.6 KiB
C#

using System.Net;
using Jiaowu.Api.Infrastructure.Teaching;
using Microsoft.Extensions.Configuration;
namespace Jiaowu.Api.Tests;
public sealed class MoodleCourseClientTests
{
[Fact]
public async Task CreatesHiddenCourseAndRecoversUsingStableIdentifiers()
{
var taskId = Guid.NewGuid();
var key = "jiaowu-" + taskId.ToString("N");
var handler = new StubHandler();
var client = Client(handler);
var first = await client.CreateOrRecoverAsync(taskId, "测试课程", CancellationToken.None);
Assert.Equal("https://moodle.example.edu/sub/course/view.php?id=42", first);
Assert.Contains("courses%5B0%5D%5Bvisible%5D=0", handler.Requests[1]);
Assert.Contains(key, handler.Requests[1]);
handler.ExistingKey = key;
var second = await client.CreateOrRecoverAsync(taskId, "测试课程", CancellationToken.None);
Assert.Equal(first, second);
Assert.Equal(3, handler.Requests.Count);
}
[Fact]
public async Task RetryRecoversCourseAfterCreateResponseIsLost()
{
var taskId = Guid.NewGuid();
var handler = new StubHandler { FailCreateOnce = true, RecoveryKey = "jiaowu-" + taskId.ToString("N") };
var client = Client(handler);
await Assert.ThrowsAsync<MoodleIntegrationException>(() => client.CreateOrRecoverAsync(taskId, "课程", CancellationToken.None));
Assert.Equal("https://moodle.example.edu/sub/course/view.php?id=42", await client.CreateOrRecoverAsync(taskId, "课程", CancellationToken.None));
Assert.Equal(3, handler.Requests.Count);
}
[Fact]
public async Task RejectsExistingCourseWithUnexpectedIdNumber()
{
var handler = new StubHandler { ExistingKey = "different" };
await Assert.ThrowsAsync<MoodleIntegrationException>(() => Client(handler).CreateOrRecoverAsync(Guid.NewGuid(), "课程", CancellationToken.None));
Assert.Single(handler.Requests);
}
private static MoodleCourseClient Client(StubHandler handler)
{
var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?>
{
["Moodle:BaseUrl"] = "https://moodle.example.edu/sub", ["Moodle:Token"] = "test-token", ["Moodle:CategoryId"] = "4"
}).Build();
return new MoodleCourseClient(new Factory(new HttpClient(handler)), config);
}
private sealed class Factory(HttpClient client) : IHttpClientFactory
{
public HttpClient CreateClient(string name) => client;
}
private sealed class StubHandler : HttpMessageHandler
{
public List<string> Requests { get; } = [];
public string? ExistingKey { get; set; }
public string? RecoveryKey { get; set; }
public bool FailCreateOnce { get; set; }
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var body = await request.Content!.ReadAsStringAsync(cancellationToken);
Requests.Add(body);
if (FailCreateOnce && body.Contains("core_course_create_courses"))
{
FailCreateOnce = false;
ExistingKey = RecoveryKey;
throw new HttpRequestException("response lost");
}
var response = body.Contains("core_course_create_courses") ? "[{\"id\":42,\"shortname\":\"x\"}]" :
ExistingKey is null ? "{\"courses\":[]}" : $"{{\"courses\":[{{\"id\":42,\"idnumber\":\"{ExistingKey}\"}}]}}";
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(response) };
}
}
}