183 lines
8.2 KiB
C#
183 lines
8.2 KiB
C#
|
|
using System.Net;
|
||
|
|
using System.Net.Http.Json;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace WordVersioning.Api.IntegrationTests;
|
||
|
|
|
||
|
|
/// <summary>FR-13 — commit message ต่อ version: รับจากทุก flow ที่สร้าง version (upload + generate),
|
||
|
|
/// normalize ว่าง/whitespace → null, trim, จำกัด 2048 อักขระ, echo ใน history (AC-16)</summary>
|
||
|
|
[Collection(IntegrationTestCollection.Name)]
|
||
|
|
public sealed class VersionMessageTests(IntegrationTestFixture fixture)
|
||
|
|
{
|
||
|
|
[Fact]
|
||
|
|
public async Task Upload_WithMessage_PersistsRow_AndEchoesInHistory()
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
|
||
|
|
using var response = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: "แก้ไขข้อ 3 หลังตรวจสอบ"));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
|
||
|
|
Assert.Equal("แก้ไขข้อ 3 หลังตรวจสอบ", version.Message);
|
||
|
|
|
||
|
|
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
|
||
|
|
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
|
||
|
|
Assert.Equal("แก้ไขข้อ 3 หลังตรวจสอบ", items!.Single().Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>ไม่ส่ง / ส่งค่าว่าง / ส่ง whitespace ล้วน → เก็บ null ทั้ง DB และ history</summary>
|
||
|
|
[Theory]
|
||
|
|
[InlineData(null)]
|
||
|
|
[InlineData("")]
|
||
|
|
[InlineData(" ")]
|
||
|
|
public async Task Upload_BlankOrMissingMessage_StoredAsNull(string? sent)
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
|
||
|
|
using var response = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: sent));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
|
||
|
|
Assert.Null(version.Message);
|
||
|
|
|
||
|
|
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
|
||
|
|
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
|
||
|
|
Assert.Null(items!.Single().Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Upload_TrimsSurroundingWhitespace()
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
|
||
|
|
using var response = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: " แก้ไขข้อ 3 "));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
|
||
|
|
Assert.Equal("แก้ไขข้อ 3", version.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Upload_OversizeMessage_Returns400_NoSideEffect()
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
|
||
|
|
using var response = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: new string('ก', 2049)));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
Assert.False(await db.Documents.AsNoTracking().AnyAsync(d => d.Id == documentId)); // ไม่มี side effect
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Upload_MessageExactly2048_Succeeds()
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
var message = new string('ก', 2048);
|
||
|
|
|
||
|
|
using var response = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx(), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: message));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
var version = await db.DocumentVersions.AsNoTracking().SingleAsync(v => v.DocumentId == documentId);
|
||
|
|
Assert.Equal(message, version.Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task SecondUpload_KeepsPerVersionMessages()
|
||
|
|
{
|
||
|
|
var documentId = Guid.NewGuid();
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
|
||
|
|
using var first = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx("v1"), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: "ฉบับแรก"));
|
||
|
|
using var second = await client.PostAsync(
|
||
|
|
$"api/documents/{documentId}/versions",
|
||
|
|
ApiExtensions.VersionForm(
|
||
|
|
TestDocx.BuildPlainDocx("v2"), "doc.docx", Guid.NewGuid().ToString(),
|
||
|
|
message: "แก้ไขเงินเดือน"));
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.Created, second.StatusCode);
|
||
|
|
|
||
|
|
// history ใหม่ → เก่า พร้อม message ของแต่ละ version เป็นของตัวเอง
|
||
|
|
using var history = await client.GetAsync($"api/documents/{documentId}/versions");
|
||
|
|
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
|
||
|
|
Assert.Equal(2, items!.Count);
|
||
|
|
Assert.Equal(2, items[0].VersionNumber);
|
||
|
|
Assert.Equal("แก้ไขเงินเดือน", items[0].Message);
|
||
|
|
Assert.Equal(1, items[1].VersionNumber);
|
||
|
|
Assert.Equal("ฉบับแรก", items[1].Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Generate_WithMessage_CreatesV1WithMessage()
|
||
|
|
{
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
var (templateId, _) = await TestFlow.CreateConfiguredTemplateAsync(client, "msg-gen");
|
||
|
|
|
||
|
|
var generated = await TestFlow.GenerateDocumentAsync(
|
||
|
|
client, templateId, TestFlow.DefaultValues, message: "ฉบับเสนอลงนามครั้งแรก");
|
||
|
|
|
||
|
|
await using var db = fixture.CreateDb();
|
||
|
|
var version = await db.DocumentVersions.AsNoTracking()
|
||
|
|
.SingleAsync(v => v.DocumentId == generated.DocumentId);
|
||
|
|
Assert.Equal(1, version.VersionNumber);
|
||
|
|
Assert.Equal("ฉบับเสนอลงนามครั้งแรก", version.Message);
|
||
|
|
|
||
|
|
using var history = await client.GetAsync($"api/documents/{generated.DocumentId}/versions");
|
||
|
|
var items = await history.Content.ReadFromJsonAsync<List<VersionItem>>(IntegrationTestFixture.Json);
|
||
|
|
Assert.Equal("ฉบับเสนอลงนามครั้งแรก", items!.Single().Message);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>400 ต้องเกิดก่อนแตะ MinIO/template — ไม่ทิ้ง orphan object ใน bucket</summary>
|
||
|
|
[Fact]
|
||
|
|
public async Task Generate_OversizeMessage_Returns400()
|
||
|
|
{
|
||
|
|
using var client = fixture.CreateClient();
|
||
|
|
var (templateId, _) = await TestFlow.CreateConfiguredTemplateAsync(client, "msg-gen-oversize");
|
||
|
|
|
||
|
|
using var response = await client.PostAsJsonAsync(
|
||
|
|
"api/documents/generate",
|
||
|
|
new { templateId, values = TestFlow.DefaultValues, message = new string('ก', 2049) },
|
||
|
|
IntegrationTestFixture.Json);
|
||
|
|
|
||
|
|
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||
|
|
}
|
||
|
|
}
|