fix(rank-battle): inherit BaseRequest so auth fields survive translation roundtrip

The translation middleware decrypts + msgpack-decodes the request body
into the action's first-parameter type, then re-serializes that DTO to
JSON for the auth handler to read. Phase 3's DoMatchingRequestDto and
RankBattleFinishRequestDto didn't inherit BaseRequest, so viewer_id /
steam_id / steam_session_ticket were dropped during the msgpack → DTO
→ JSON pivot — the auth handler then saw a body with no auth fields
and 401'd every request.

Fixed by making both DTOs extend BaseRequest, mirroring the Phase 2 TK2
DoMatchingRequest pattern.

Also added [FromBody] BaseRequest parameters to the previously body-less
actions (AiStart × 2, ForceFinish, AddClientLog, GetLatestMasterPoint).
The translation middleware explicitly requires at least one parameter
to bind the decrypted msgpack body (see L130-136 of the middleware);
without it the request would throw InvalidOperationException at runtime.

Tests updated to post viewer_id / steam_id / steam_session_ticket
placeholder values in the request body, matching the existing TK2 test
pattern.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-06-02 09:29:48 -04:00
parent 8723cff998
commit bf783639c1
4 changed files with 74 additions and 39 deletions

View File

@@ -10,6 +10,40 @@ namespace SVSim.UnitTests.Controllers;
[TestFixture]
public class RankBattleControllerTests
{
// BaseRequest fields (viewer_id / steam_id / steam_session_ticket) are required by the
// request DTOs — the ApiController's auto-validation rejects bodies missing them. We
// post placeholder values here; the TestAuthHandler injects the real viewer-id via the
// X-Test-Viewer-Id header set by CreateAuthenticatedClient, so these body values are
// ignored by auth.
private static readonly object DoMatchingBody = new
{
deck_no = 1,
need_init = 1,
log = 0,
viewer_id = "0",
steam_id = 0,
steam_session_ticket = "",
};
private static object FinishBody(int battleResult, int classId = 3) => new
{
battle_result = battleResult,
is_retire = 0,
recovery_data = "{}",
class_id = classId,
total_turn = 5,
viewer_id = "0",
steam_id = 0,
steam_session_ticket = "",
};
private static readonly object EmptyAuthedBody = new
{
viewer_id = "0",
steam_id = 0,
steam_session_ticket = "",
};
[Test]
public async Task DoMatching_rotation_first_poll_returns_3002_RETRY_with_empty_node_server_url()
{
@@ -19,9 +53,7 @@ public class RankBattleControllerTests
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync(
"/rotation_rank_battle/do_matching",
new { deck_no = 1, need_init = 1 });
var resp = await client.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
Assert.That(resp.IsSuccessStatusCode, Is.True, $"Expected 2xx, got {resp.StatusCode}");
var raw = await resp.Content.ReadAsStringAsync();
@@ -44,23 +76,20 @@ public class RankBattleControllerTests
// Alice polls first → parks.
var c1 = factory.CreateAuthenticatedClient(v1);
var r1 = await c1.PostAsJsonAsync(
"/rotation_rank_battle/do_matching", new { deck_no = 1, need_init = 1 });
var r1 = await c1.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
var j1 = JsonDocument.Parse(await r1.Content.ReadAsStringAsync()).RootElement;
Assert.That(j1.GetProperty("matching_state").GetInt32(), Is.EqualTo(3002));
// Bob polls — pairs, returns joiner (3004).
var c2 = factory.CreateAuthenticatedClient(v2);
var r2 = await c2.PostAsJsonAsync(
"/rotation_rank_battle/do_matching", new { deck_no = 1, need_init = 1 });
var r2 = await c2.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
var j2 = JsonDocument.Parse(await r2.Content.ReadAsStringAsync()).RootElement;
Assert.That(j2.GetProperty("matching_state").GetInt32(), Is.EqualTo(3004), "Joiner = 3004.");
Assert.That(j2.GetProperty("battle_id").GetString(), Is.Not.Null.And.Not.Empty);
Assert.That(j2.GetProperty("node_server_url").GetString(), Is.Not.Empty);
// Alice polls again — gets cached match, owner role (3007).
var r3 = await c1.PostAsJsonAsync(
"/rotation_rank_battle/do_matching", new { deck_no = 1, need_init = 0 });
var r3 = await c1.PostAsJsonAsync("/rotation_rank_battle/do_matching", DoMatchingBody);
var j3 = JsonDocument.Parse(await r3.Content.ReadAsStringAsync()).RootElement;
Assert.That(j3.GetProperty("matching_state").GetInt32(), Is.EqualTo(3007), "Owner = 3007.");
Assert.That(j3.GetProperty("battle_id").GetString(), Is.EqualTo(j2.GetProperty("battle_id").GetString()));
@@ -75,7 +104,7 @@ public class RankBattleControllerTests
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", new { });
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
var raw = await resp.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(raw);
@@ -111,7 +140,7 @@ public class RankBattleControllerTests
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", new { });
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
var selfInfo = doc.RootElement.GetProperty("self_info");
@@ -127,7 +156,7 @@ public class RankBattleControllerTests
await factory.SeedDeckAsync(viewerId, Format.Rotation, 1);
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", new { });
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/start", EmptyAuthedBody);
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
var oppoInfo = doc.RootElement.GetProperty("oppo_info");
@@ -143,14 +172,7 @@ public class RankBattleControllerTests
var viewerId = await factory.SeedViewerAsync();
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/finish", new
{
battle_result = 1, // win
is_retire = 0,
recovery_data = "{}",
class_id = 3,
total_turn = 5,
});
var resp = await client.PostAsJsonAsync("/ai_rotation_rank_battle/finish", FinishBody(battleResult: 1));
Assert.That(resp.IsSuccessStatusCode, Is.True);
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
@@ -168,11 +190,7 @@ public class RankBattleControllerTests
var viewerId = await factory.SeedViewerAsync();
var client = factory.CreateAuthenticatedClient(viewerId);
var resp = await client.PostAsJsonAsync("/rotation_rank_battle/finish", new
{
battle_result = 2, // consistency error
class_id = 3,
});
var resp = await client.PostAsJsonAsync("/rotation_rank_battle/finish", FinishBody(battleResult: 2));
using var doc = JsonDocument.Parse(await resp.Content.ReadAsStringAsync());
Assert.That(doc.RootElement.GetProperty("battle_result").GetInt32(), Is.EqualTo(2));