174 lines
6.1 KiB
Ruby
174 lines
6.1 KiB
Ruby
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
|