require 'test_helper'

class PentestSolvedChallengeTest < ActiveSupport::TestCase
  def setup
    create(:active_game)
    @team = create(:team)
    @defense = create(:team)
    @challenge = create(:pentest_challenge_with_flags)
  end

  test 'team cannot solve pentest challenge twice' do
    create(:pentest_solved_challenge, challenge: @challenge, team: @team, flag: @defense.defense_flags.first)
    # Without reloading the challenge, the SolvedChallenges aren't populated on the model which causes
    # the team_can_solve_challenge validator to not fire.
    @challenge.reload
    bad_solve = build(:pentest_solved_challenge, challenge: @challenge, team: @team, flag: @defense.defense_flags.first)
    assert_not bad_solve.valid?
    assert_equal true, bad_solve.errors.added?(:base, I18n.t('challenges.already_solved'))
  end

  test 'team cannot solve own pentest challenge' do
    self_solve = build(:pentest_solved_challenge, team: @team, challenge: @challenge, flag: @team.defense_flags.first)
    assert_not self_solve.valid?
    assert_equal true, self_solve.errors.added?(:base, I18n.t('challenges.already_solved'))
  end
end
