95 lines
3.4 KiB
Ruby
95 lines
3.4 KiB
Ruby
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
|