300 lines
14 KiB
Ruby
300 lines
14 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
require 'securerandom'
|
|
require_relative '../../../e2e/lib/api_client'
|
|
|
|
RSpec.describe 'Tournament follower sync HTTP E2E' do
|
|
required_env_vars = %w[
|
|
TURNIERE_E2E_EMAIL
|
|
TURNIERE_E2E_PASSWORD
|
|
TURNIERE_SOURCE_E2E_BASE_URL
|
|
TURNIERE_FOLLOWER_E2E_BASE_URL
|
|
].freeze
|
|
|
|
before do
|
|
missing_vars = required_env_vars.reject { |name| ENV.key?(name) }
|
|
skip("HTTP E2E requires #{missing_vars.join(', ')}") unless missing_vars.empty?
|
|
end
|
|
|
|
let(:source_base_url) { ENV.fetch('TURNIERE_SOURCE_E2E_BASE_URL') }
|
|
let(:follower_base_url) { ENV.fetch('TURNIERE_FOLLOWER_E2E_BASE_URL') }
|
|
let(:owner_email) { ENV.fetch('TURNIERE_E2E_EMAIL') }
|
|
let(:owner_password) { ENV.fetch('TURNIERE_E2E_PASSWORD') }
|
|
let(:sync_token) { "sync-#{SecureRandom.hex(8)}" }
|
|
let(:source_owner_client) { login_client(base_url: source_base_url, email: owner_email, password: owner_password) }
|
|
let(:follower_owner_client) { login_client(base_url: follower_base_url, email: owner_email, password: owner_password) }
|
|
let(:source_anonymous_client) { TurniereE2E::ApiClient.new(base_url: source_base_url) }
|
|
let(:follower_anonymous_client) { TurniereE2E::ApiClient.new(base_url: follower_base_url) }
|
|
|
|
it 'pushes full source lifecycle into read only follower tournament' do
|
|
source = create_group_stage_tournament(client: source_owner_client, public: false, name_prefix: 'Sync Source')
|
|
follower = create_follower_tournament(client: follower_owner_client, public: true, name_prefix: 'Sync Follower')
|
|
|
|
configure_sync = source_owner_client.patch("/tournaments/#{source.fetch(:id)}", body: {
|
|
sync_target_url: "#{follower_base_url}/tournaments/#{follower.fetch(:id)}/sync_state",
|
|
sync_auth_token: sync_token
|
|
})
|
|
expect(configure_sync[:status]).to eq(200)
|
|
|
|
source = fetch_tournament(client: source_anonymous_client, tournament_id: source.fetch(:id))
|
|
follower = wait_for_tournament_sync!(source_tournament_id: source.fetch(:id), follower_tournament_id: follower.fetch(:id))
|
|
expect(tournament_signature(follower)).to eq(tournament_signature(source))
|
|
|
|
update_cutoff = source_owner_client.patch("/tournaments/#{source.fetch(:id)}", body: {
|
|
playoff_teams_amount: 4,
|
|
instant_finalists_amount: 4,
|
|
intermediate_round_participants_amount: 0
|
|
})
|
|
expect(update_cutoff[:status]).to eq(200)
|
|
|
|
source_group_stage = fetch_tournament(client: source_anonymous_client, tournament_id: source.fetch(:id))
|
|
.fetch(:stages).find { |stage| stage.fetch(:level) == -1 }
|
|
play_group_with_decider_lifecycle!(source_id: source.fetch(:id), groups: source_group_stage.fetch(:groups).sort_by { |group| group.fetch(:number) })
|
|
|
|
source = fetch_tournament(client: source_anonymous_client, tournament_id: source.fetch(:id))
|
|
follower = wait_for_tournament_sync!(source_tournament_id: source.fetch(:id), follower_tournament_id: follower.fetch(:id))
|
|
expect(tournament_signature(follower)).to eq(tournament_signature(source))
|
|
expect(follower.fetch(:stages).map { |stage| stage.fetch(:level) }).to include(-1, 0, 1)
|
|
|
|
follower_group = follower.fetch(:stages).find { |stage| stage.fetch(:level) == -1 }.fetch(:groups).first
|
|
follower_team = follower_group.fetch(:matches).first.fetch(:match_scores).first.fetch(:team)
|
|
locked_team_update = follower_owner_client.patch("/teams/#{follower_team.fetch(:id)}", body: { name: 'Nope' })
|
|
expect(locked_team_update[:status]).to eq(423)
|
|
expect(locked_team_update.dig(:json, :error)).to eq('Tournament is in read only follower mode')
|
|
|
|
finish_playoff_bracket!(source_id: source.fetch(:id))
|
|
|
|
source = fetch_tournament(client: source_anonymous_client, tournament_id: source.fetch(:id))
|
|
follower = wait_for_tournament_sync!(source_tournament_id: source.fetch(:id), follower_tournament_id: follower.fetch(:id))
|
|
expect(tournament_signature(follower)).to eq(tournament_signature(source))
|
|
|
|
disable_follower = follower_owner_client.patch("/tournaments/#{follower.fetch(:id)}", body: { read_only_mode: false })
|
|
expect(disable_follower[:status]).to eq(200)
|
|
|
|
follower_after_disable = fetch_tournament(client: follower_anonymous_client, tournament_id: follower.fetch(:id))
|
|
writable_team = follower_after_disable.fetch(:teams).first
|
|
takeover_update = follower_owner_client.patch("/teams/#{writable_team.fetch(:id)}", body: { name: "#{writable_team.fetch(:name)} takeover" })
|
|
expect(takeover_update[:status]).to eq(200)
|
|
end
|
|
|
|
def login_client(base_url:, email:, password:)
|
|
client = TurniereE2E::ApiClient.new(base_url: base_url)
|
|
response = client.login!(email: email, password: password)
|
|
|
|
expect(response[:status]).to eq(200)
|
|
expect(client.authenticated?).to eq(true)
|
|
client
|
|
end
|
|
|
|
def create_group_stage_tournament(client:, public:, name_prefix:)
|
|
payload = {
|
|
name: "#{name_prefix} #{unique_suffix}",
|
|
description: 'Follower sync source tournament',
|
|
public: public,
|
|
group_stage: true,
|
|
playoff_teams_amount: 8,
|
|
teams: 4.times.flat_map do |group_index|
|
|
4.times.map do |seed_index|
|
|
{
|
|
name: "#{name_prefix.tr(' ', '')}-G#{group_index + 1}-S#{seed_index + 1}",
|
|
group: group_index
|
|
}
|
|
end
|
|
end
|
|
}
|
|
|
|
response = client.post('/tournaments', body: payload)
|
|
expect(response[:status]).to eq(201)
|
|
fetch_tournament(client: client, tournament_id: response.dig(:json, :id))
|
|
end
|
|
|
|
def create_follower_tournament(client:, public:, name_prefix:)
|
|
response = client.post('/tournaments', body: {
|
|
name: "#{name_prefix} #{unique_suffix}",
|
|
description: 'Follower tournament',
|
|
public: public,
|
|
read_only_mode: true,
|
|
sync_auth_token: sync_token
|
|
})
|
|
expect(response[:status]).to eq(201)
|
|
fetch_tournament(client: client, tournament_id: response.dig(:json, :id))
|
|
end
|
|
|
|
def fetch_tournament(client:, tournament_id:)
|
|
response = client.get("/tournaments/#{tournament_id}")
|
|
expect(response[:status]).to eq(200)
|
|
response.fetch(:json)
|
|
end
|
|
|
|
def wait_for_tournament_sync!(source_tournament_id:, follower_tournament_id:, timeout: 30)
|
|
deadline = Time.now + timeout
|
|
|
|
loop do
|
|
source = fetch_tournament(client: source_anonymous_client, tournament_id: source_tournament_id)
|
|
follower = fetch_tournament(client: follower_anonymous_client, tournament_id: follower_tournament_id)
|
|
return follower if tournament_signature(source) == tournament_signature(follower)
|
|
|
|
raise "Follower did not catch up within #{timeout}s" if Time.now >= deadline
|
|
|
|
sleep 0.5
|
|
end
|
|
end
|
|
|
|
def play_group_with_decider_lifecycle!(source_id:, groups:)
|
|
groups.each_with_index do |group, index|
|
|
if index.zero?
|
|
play_group_matches_for_decider!(group)
|
|
else
|
|
finish_group_clear_winner!(group)
|
|
end
|
|
end
|
|
|
|
group_stage = fetch_tournament(client: source_anonymous_client, tournament_id: source_id)
|
|
.fetch(:stages).find { |stage| stage.fetch(:level) == -1 }
|
|
|
|
stage_finish = source_owner_client.patch("/stages/#{group_stage.fetch(:id)}", body: { state: 'finished' })
|
|
expect(stage_finish[:status]).to eq(422)
|
|
expect(stage_finish.dig(:json, :error)).to eq('Playoffs cannot be generated yet because a decider match is required for an advancing place')
|
|
|
|
blocking_tie = stage_finish.dig(:json, :blocking_ties).first
|
|
decider_create = source_owner_client.post("/groups/#{blocking_tie.fetch(:group_id)}/matches", body: {
|
|
team_ids: blocking_tie.fetch(:team_ids)
|
|
})
|
|
expect(decider_create[:status]).to eq(201)
|
|
decider_match = decider_create.fetch(:json)
|
|
|
|
start_decider = source_owner_client.patch("/matches/#{decider_match.fetch(:id)}", body: { state: 'in_progress' })
|
|
expect(start_decider[:status]).to eq(200)
|
|
decider_match.fetch(:match_scores).sort_by { |score| score.fetch(:id) }.each_with_index do |score, index|
|
|
response = source_owner_client.patch("/match_scores/#{score.fetch(:id)}", body: { hidden_points: index.zero? ? 5 : 3 })
|
|
expect(response[:status]).to eq(200)
|
|
end
|
|
finish_decider = source_owner_client.patch("/matches/#{decider_match.fetch(:id)}", body: { state: 'finished' })
|
|
expect(finish_decider[:status]).to eq(200)
|
|
|
|
finish_stage_after_decider = source_owner_client.patch("/stages/#{group_stage.fetch(:id)}", body: { state: 'finished' })
|
|
expect(finish_stage_after_decider[:status]).to eq(200)
|
|
end
|
|
|
|
def play_group_matches_for_decider!(group)
|
|
teams = group_team_map(group)
|
|
play_group_match!(group, teams.fetch(1), teams.fetch(2), 1, 1)
|
|
play_group_match!(group, teams.fetch(1), teams.fetch(3), 2, 0)
|
|
play_group_match!(group, teams.fetch(1), teams.fetch(4), 2, 0)
|
|
play_group_match!(group, teams.fetch(2), teams.fetch(3), 2, 0)
|
|
play_group_match!(group, teams.fetch(2), teams.fetch(4), 2, 0)
|
|
play_group_match!(group, teams.fetch(3), teams.fetch(4), 1, 0)
|
|
end
|
|
|
|
def finish_group_clear_winner!(group)
|
|
group.fetch(:matches).each_with_index do |match, index|
|
|
play_match!(match_id: match.fetch(:id), first_score: 10 + index, second_score: index)
|
|
end
|
|
end
|
|
|
|
def group_team_map(group)
|
|
group.fetch(:matches)
|
|
.flat_map { |match| match.fetch(:match_scores).map { |score| score.fetch(:team) } }
|
|
.uniq { |team| team.fetch(:id) }
|
|
.sort_by { |team| team.fetch(:name) }
|
|
.each_with_index
|
|
.to_h { |team, index| [index + 1, team] }
|
|
end
|
|
|
|
def play_group_match!(group, team_a, team_b, score_a, score_b)
|
|
match = group.fetch(:matches).find do |candidate|
|
|
team_ids = candidate.fetch(:match_scores).map { |score| score.dig(:team, :id) }.sort
|
|
team_ids == [team_a.fetch(:id), team_b.fetch(:id)].sort
|
|
end
|
|
|
|
play_match!(match_id: match.fetch(:id), first_score: score_a, second_score: score_b)
|
|
end
|
|
|
|
def finish_playoff_bracket!(source_id:)
|
|
loop do
|
|
tournament = fetch_tournament(client: source_anonymous_client, tournament_id: source_id)
|
|
playoff_matches = tournament.fetch(:stages)
|
|
.select { |stage| stage.fetch(:level) >= 0 }
|
|
.flat_map { |stage| stage.fetch(:matches) }
|
|
.select { |match| %w[not_started in_progress].include?(match.fetch(:state)) && match.fetch(:match_scores).size == 2 }
|
|
.sort_by { |match| [match.dig(:stage, :id) || 0, match.fetch(:position)] }
|
|
break if playoff_matches.empty?
|
|
|
|
playoff_matches.each_with_index do |match, index|
|
|
play_match!(match_id: match.fetch(:id), first_score: 20 + index, second_score: index)
|
|
end
|
|
end
|
|
end
|
|
|
|
def play_match!(match_id:, first_score:, second_score:)
|
|
match = source_anonymous_client.get("/matches/#{match_id}")
|
|
expect(match[:status]).to eq(200)
|
|
|
|
unless match.dig(:json, :state) == 'in_progress'
|
|
start_response = source_owner_client.patch("/matches/#{match_id}", body: { state: 'in_progress' })
|
|
expect(start_response[:status]).to eq(200)
|
|
match = source_anonymous_client.get("/matches/#{match_id}")
|
|
end
|
|
|
|
ordered_scores = match.fetch(:json).fetch(:match_scores).sort_by { |score| score.fetch(:id) }
|
|
[first_score, second_score].each_with_index do |points, index|
|
|
response = source_owner_client.patch("/match_scores/#{ordered_scores[index].fetch(:id)}", body: { points: points })
|
|
expect(response[:status]).to eq(200)
|
|
end
|
|
|
|
finish_response = source_owner_client.patch("/matches/#{match_id}", body: { state: 'finished' })
|
|
expect(finish_response[:status]).to eq(200)
|
|
end
|
|
|
|
def tournament_signature(tournament)
|
|
{
|
|
name: tournament.fetch(:name),
|
|
public: tournament.fetch(:public),
|
|
playoff_teams_amount: tournament.fetch(:playoff_teams_amount),
|
|
instant_finalists_amount: tournament.fetch(:instant_finalists_amount),
|
|
intermediate_round_participants_amount: tournament.fetch(:intermediate_round_participants_amount),
|
|
timer_end: tournament[:timer_end],
|
|
teams: tournament.fetch(:teams).map { |team| team.slice(:name, :advancing_from_group_stage) }.sort_by { |team| team.fetch(:name) },
|
|
stages: tournament.fetch(:stages).sort_by { |stage| stage.fetch(:level) }.map do |stage|
|
|
{
|
|
level: stage.fetch(:level),
|
|
state: stage.fetch(:state),
|
|
groups: stage.fetch(:groups).sort_by { |group| group.fetch(:number) }.map do |group|
|
|
{
|
|
number: group.fetch(:number),
|
|
matches: group.fetch(:matches).sort_by { |match| match.fetch(:position) }.map do |match|
|
|
match_signature(match)
|
|
end
|
|
}
|
|
end,
|
|
matches: stage.fetch(:matches).sort_by { |match| match.fetch(:position) }.map do |match|
|
|
match_signature(match)
|
|
end
|
|
}
|
|
end
|
|
}
|
|
end
|
|
|
|
def match_signature(match)
|
|
{
|
|
state: match.fetch(:state),
|
|
position: match.fetch(:position),
|
|
decider: match.fetch(:decider),
|
|
scores: match.fetch(:match_scores).map do |score|
|
|
{
|
|
team_name: score.dig(:team, :name),
|
|
points: score.fetch(:points),
|
|
hidden_points: score.fetch(:hidden_points)
|
|
}
|
|
end.sort_by { |score| score.fetch(:team_name).to_s }
|
|
}
|
|
end
|
|
|
|
def unique_suffix
|
|
@unique_suffix_counter ||= 0
|
|
@unique_suffix_counter += 1
|
|
"#{SecureRandom.hex(3)}-#{@unique_suffix_counter}"
|
|
end
|
|
end
|