add private endpoint for match reporting
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe InternalSignatureService do
|
||||
let(:secret) { "test-secret-key-base" }
|
||||
let(:body) { '{"status":"player1_won","duration_seconds":42}' }
|
||||
let(:path) { "/api/internal/matches/abc/result" }
|
||||
let(:timestamp) { 1_700_000_000 }
|
||||
|
||||
it "produces a stable, cross-implementation signature" do
|
||||
# Vector shared with the game-server (Go) test suite. Do not change the
|
||||
# formula without updating both sides.
|
||||
expect(described_class.sign(body, path, secret: secret, timestamp: timestamp))
|
||||
.to eq("e0a7f8b5508639b1afe604b41f00070dd282d02b6444db5b4856959f175e8361")
|
||||
end
|
||||
|
||||
describe ".valid?" do
|
||||
def request_double(path: "/api/internal/matches/abc/result",
|
||||
body: '{"status":"player1_won","duration_seconds":42}',
|
||||
headers: {})
|
||||
request = instance_double("ActionDispatch::Http::Request")
|
||||
allow(request).to receive(:path).and_return(path)
|
||||
allow(request).to receive(:raw_post).and_return(body)
|
||||
allow(request).to receive(:headers).and_return(headers)
|
||||
request
|
||||
end
|
||||
|
||||
def valid_headers(secret: "test-secret-key-base", path: "/api/internal/matches/abc/result",
|
||||
body: '{"status":"player1_won","duration_seconds":42}', timestamp: Time.current.to_i)
|
||||
signature = described_class.sign(body, path, secret: secret, timestamp: timestamp)
|
||||
{
|
||||
"X-Request-Timestamp" => timestamp.to_s,
|
||||
"X-Request-Signature" => "sha256=#{signature}"
|
||||
}
|
||||
end
|
||||
|
||||
it "accepts a correctly signed request" do
|
||||
headers = valid_headers
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(true)
|
||||
end
|
||||
|
||||
it "accepts a signature without the sha256= prefix" do
|
||||
headers = valid_headers
|
||||
headers["X-Request-Signature"] = headers["X-Request-Signature"].delete_prefix("sha256=")
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(true)
|
||||
end
|
||||
|
||||
it "rejects missing headers" do
|
||||
expect(described_class.valid?(request_double(headers: {}), secret: secret)).to be(false)
|
||||
end
|
||||
|
||||
it "rejects a non-numeric timestamp" do
|
||||
headers = valid_headers
|
||||
headers["X-Request-Timestamp"] = "not-a-number"
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
||||
end
|
||||
|
||||
it "rejects a wrong signature" do
|
||||
headers = valid_headers
|
||||
headers["X-Request-Signature"] = "sha256=#{"a" * 64}"
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
||||
end
|
||||
|
||||
it "rejects a timestamp too far in the past" do
|
||||
timestamp = (Time.current - described_class::TOLERANCE - 60).to_i
|
||||
headers = valid_headers(timestamp: timestamp)
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
||||
end
|
||||
|
||||
it "rejects a timestamp too far in the future" do
|
||||
timestamp = (Time.current + described_class::TOLERANCE + 60).to_i
|
||||
headers = valid_headers(timestamp: timestamp)
|
||||
expect(described_class.valid?(request_double(headers: headers), secret: secret)).to be(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user