add private endpoint for match reporting

This commit is contained in:
2026-11-09 23:17:55 +03:00
parent f0616c7c51
commit dcb76fa570
11 changed files with 738 additions and 4 deletions
@@ -0,0 +1,173 @@
require "rails_helper"
RSpec.describe "Internal match results API", type: :request do
let(:player1) { User.create!(nickname: "player_one", password: "secret123") }
let(:player2) { User.create!(nickname: "player_two", password: "secret123") }
let(:match) do
Match.create!(
player1: player1,
player2: player2,
status: :ongoing,
game_server: "ws://game-server:8080/ws",
started_at: Time.current,
player1_rating_before: 100,
player2_rating_before: 100,
player1_rating_change: 0,
player2_rating_change: 0
)
end
let(:path) { "/api/internal/matches/#{match.uuid}/result" }
let(:payload) { { status: "player1_won", duration_seconds: 125 } }
let(:body) { payload.to_json }
def signed_headers(body:, path:, timestamp: Time.current.to_i, secret: Rails.application.secret_key_base)
signature = InternalSignatureService.sign(body, path, secret: secret, timestamp: timestamp)
{
"Content-Type" => "application/json",
"X-Request-Timestamp" => timestamp.to_s,
"X-Request-Signature" => "sha256=#{signature}"
}
end
describe "POST /api/internal/matches/:uuid/result" do
it "marks an ongoing match as finished with the reported outcome" do
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:ok)
match.reload
expect(match.status).to eq("player1_won")
expect(match.duration_seconds).to eq(125)
expect(match.finished_at).to be_present
json = JSON.parse(response.body)
expect(json.dig("match", "id")).to eq(match.uuid)
expect(json.dig("match", "status")).to eq("player1_won")
expect(json.dig("match", "duration_seconds")).to eq(125)
expect(json.dig("match", "player1", "rating_change")).to eq(0)
expect(json.dig("match", "player2", "rating_change")).to eq(0)
end
%w[player2_won draw abandoned].each do |status|
it "accepts the #{status} outcome" do
body = { status: status, duration_seconds: 5 }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:ok)
expect(match.reload.status).to eq(status)
expect(match.duration_seconds).to eq(5)
end
end
it "rejects an invalid signature" do
headers = signed_headers(body: body, path: path)
headers["X-Request-Signature"] = "sha256=deadbeef"
post path, params: body, headers: headers
expect(response).to have_http_status(:unauthorized)
expect(match.reload.status).to eq("ongoing")
end
it "rejects a signature made with a different secret" do
headers = signed_headers(body: body, path: path, secret: "wrong-secret")
post path, params: body, headers: headers
expect(response).to have_http_status(:unauthorized)
expect(match.reload.status).to eq("ongoing")
end
it "rejects a signature bound to a different path" do
headers = signed_headers(body: body, path: "/api/internal/matches/other/result")
post path, params: body, headers: headers
expect(response).to have_http_status(:unauthorized)
end
it "rejects a tampered body" do
headers = signed_headers(body: body, path: path)
tampered = { status: "player2_won", duration_seconds: 125 }.to_json
post path, params: tampered, headers: headers
expect(response).to have_http_status(:unauthorized)
expect(match.reload.status).to eq("ongoing")
end
it "rejects a stale timestamp" do
headers = signed_headers(body: body, path: path, timestamp: 10.minutes.ago.to_i)
post path, params: body, headers: headers
expect(response).to have_http_status(:unauthorized)
end
it "rejects a request without signature headers" do
post path, params: body, headers: { "Content-Type" => "application/json" }
expect(response).to have_http_status(:unauthorized)
end
it "returns 404 for an unknown match" do
missing_path = "/api/internal/matches/00000000-0000-0000-0000-000000000000/result"
post missing_path, params: body, headers: signed_headers(body: body, path: missing_path)
expect(response).to have_http_status(:not_found)
end
it "rejects an invalid status" do
body = { status: "ongoing", duration_seconds: 10 }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:unprocessable_content)
expect(match.reload.status).to eq("ongoing")
end
it "rejects a missing duration" do
body = { status: "draw" }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:unprocessable_content)
expect(match.reload.status).to eq("ongoing")
end
it "rejects a negative duration" do
body = { status: "draw", duration_seconds: -1 }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:unprocessable_content)
end
it "rejects a non-integer duration" do
body = { status: "draw", duration_seconds: "soon" }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:unprocessable_content)
end
it "is idempotent for a repeated report of the same outcome" do
post path, params: body, headers: signed_headers(body: body, path: path)
finished_at = match.reload.finished_at
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:ok)
expect(match.reload.finished_at).to eq(finished_at)
expect(match.status).to eq("player1_won")
end
it "rejects a report that conflicts with an already recorded outcome" do
match.update!(status: :player1_won, duration_seconds: 10, finished_at: Time.current)
body = { status: "player2_won", duration_seconds: 10 }.to_json
post path, params: body, headers: signed_headers(body: body, path: path)
expect(response).to have_http_status(:conflict)
expect(match.reload.status).to eq("player1_won")
end
end
end
@@ -0,0 +1,94 @@
require "rails_helper"
RSpec.describe "Match details API", type: :request do
let(:player1) { User.create!(nickname: "player_one", password: "secret123") }
let(:player2) { User.create!(nickname: "player_two", password: "secret123") }
let(:outsider) { User.create!(nickname: "outsider", password: "secret123") }
let(:match) do
Match.create!(
player1: player1,
player2: player2,
status: :player1_won,
game_server: "ws://game-server:8080/ws",
started_at: 10.minutes.ago,
finished_at: Time.current,
duration_seconds: 600,
player1_rating_before: player1.rating,
player2_rating_before: player2.rating,
player1_rating_change: 12,
player2_rating_change: -12
)
end
def token_for(user)
JwtService.encode(user_id: user.id)
end
describe "GET /api/matches/:uuid" do
it "returns match details with player1's rating delta for player1" do
get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(player1)}" }
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["match"]["id"]).to eq(match.uuid)
expect(json["match"]["status"]).to eq("player1_won")
expect(json["match"]["duration_seconds"]).to eq(600)
expect(json["match"]["game_server"]).to eq("ws://game-server:8080/ws")
expect(json["match"]["your_rating_change"]).to eq(12)
expect(json["match"]["your_rating_before"]).to eq(player1.rating)
expect(json["match"]["player1"]["nickname"]).to eq("player_one")
expect(json["match"]["player2"]["rating_change"]).to eq(-12)
end
it "returns player2's own rating delta for player2" do
get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(player2)}" }
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["match"]["your_rating_change"]).to eq(-12)
expect(json["match"]["your_rating_before"]).to eq(player2.rating)
end
it "returns 404 for a user who is not a participant" do
get "/api/matches/#{match.uuid}", headers: { "Authorization" => "Bearer #{token_for(outsider)}" }
expect(response).to have_http_status(:not_found)
end
it "returns 404 for an unknown match uuid" do
get "/api/matches/00000000-0000-0000-0000-000000000000",
headers: { "Authorization" => "Bearer #{token_for(player1)}" }
expect(response).to have_http_status(:not_found)
end
it "returns 401 without a token" do
get "/api/matches/#{match.uuid}"
expect(response).to have_http_status(:unauthorized)
end
it "returns the current state for an ongoing match" do
ongoing = Match.create!(
player1: player1,
player2: player2,
status: :ongoing,
game_server: "ws://game-server:8080/ws",
started_at: Time.current,
player1_rating_before: player1.rating,
player2_rating_before: player2.rating,
player1_rating_change: 0,
player2_rating_change: 0
)
get "/api/matches/#{ongoing.uuid}", headers: { "Authorization" => "Bearer #{token_for(player1)}" }
expect(response).to have_http_status(:ok)
json = JSON.parse(response.body)
expect(json["match"]["status"]).to eq("ongoing")
expect(json["match"]["finished_at"]).to be_nil
expect(json["match"]["duration_seconds"]).to be_nil
end
end
end