Files
raven-test-integration/Memo/MemoCrud.cs
2026-03-02 15:51:01 -08:00

66 lines
2.6 KiB
C#

using System;
using Xunit;
using Newtonsoft.Json.Linq;
using FluentAssertions;
namespace raven_integration
{
public class MemoCrud
{
private static long GetUserIdFromToken(string token)
{
var payloadB64 = token.Split('.')[1].Replace('-', '+').Replace('_', '/');
while (payloadB64.Length % 4 != 0) payloadB64 += "=";
var json = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(payloadB64));
return JObject.Parse(json)["id"].Value<long>();
}
/// <summary>
/// Create a memo, read it back, then delete it and confirm it is gone.
/// Memos are immutable after sending so there is no PUT step.
///
/// Note: the normal POST (users=[userId]) returns 202 Accepted with no body — the
/// server creates per-recipient records but returns no id. To get a testable memo id
/// we use the v8 migration compatibility path: users=[-7] authenticated as superuser
/// (id=1). That path creates a single memo and returns 200 with {data:{id:N}}.
/// </summary>
[Fact]
public async Task CreateReadDelete()
{
// Must be superuser (id=1) to use the migration compatibility path
var token = await Util.GetTokenAsync("superuser", "l3tm3in");
var isoNow = DateTime.UtcNow.ToString("o");
var subject = Util.Uniquify("Test Memo");
// CREATE via migration path: users=[-7], superuser auth → 200 with {data:{id:N}}
// fromId=1, toId=1 so that foreign-key constraints are satisfied
var payload = $$"""
{"memo":{"id":0,"concurrency":0,"name":"{{subject}}","notes":"Test memo body text.","wiki":null,"customFields":"{}","tags":[],"viewed":false,"replied":false,"fromId":1,"toId":1,"sent":"{{isoNow}}"},"users":[-7]}
""";
ApiResponse a = await Util.PostAsync("memo", token, payload);
Util.ValidateDataReturnResponseOk(a);
long Id = a.ObjectResponse["data"]["id"].Value<long>();
Id.Should().BeGreaterThan(0);
// GET
a = await Util.GetAsync($"memo/{Id}", token);
Util.ValidateDataReturnResponseOk(a);
a.ObjectResponse["data"]["name"].Value<string>().Should().Be(subject);
// DELETE
a = await Util.DeleteAsync($"memo/{Id}", token);
Util.ValidateHTTPStatusCode(a, 204);
// Confirm deleted
a = await Util.GetAsync($"memo/{Id}", token);
Util.ValidateResponseNotFound(a);
}
}//eoc
}//eons