require 'test_helper'

class DefenseFlagTest < ActiveSupport::TestCase
  def setup
    @game = create(:active_game)
    @defense_team = create(:team)
    @challenge = create(
                  :pentest_challenge,
                  point_value: 100,
                  first_capture_point_bonus: 0,
                  initial_shares: 1000,
                  unsolved_increment_period: 1, # Hours
                  unsolved_increment_points: 2,
                  solved_decrement_period: 1, # Hours
                  solved_decrement_shares: 100,
                  defense_period: 1, # Hours
                  defense_points: 0,
                  flag_count: 0,
                  sponsored: true,
                  sponsor: 'MITRE',
                  sponsor_logo: 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Mitre_Corporation_logo.svg/220px-Mitre_Corporation_logo.svg.png',
                  sponsor_description: 'The Mitre Corporation is an American not-for-profit organization based in Bedford, Massachusetts, and McLean, Virginia.'
                )
    @flag = create(:defense_flag, challenge: @challenge, team: @defense_team)
  end

  test 'point value' do
    solve_team = create(:team)
    unsolve_team = create(:team)
    solved_challenge = create(:pentest_solved_challenge, team: solve_team, challenge: @challenge)
    point_value = solved_challenge.flag.display_point_value(unsolve_team)
    assert_equal 50, point_value
  end

  test 'defense challenge sponsorship' do
    assert @flag.sponsored
    assert_equal 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Mitre_Corporation_logo.svg/220px-Mitre_Corporation_logo.svg.png', @flag.sponsor_logo
    assert_equal 'The Mitre Corporation is an American not-for-profit organization based in Bedford, Massachusetts, and McLean, Virginia.', @flag.sponsor_description
  end

  test 'point value stops increasing and decreasing after game completion' do
    @game.update(start: 3.hours.ago, stop: 2.hours.ago)
    unsolve_team = create(:team)
    point_value = @challenge.defense_flags.sample.display_point_value(unsolve_team)
    assert_equal 102, point_value
  end

  test 'point value stops increasing and decreasing after game completion for solved challenge' do
    @game.update(start: 3.hours.ago, stop: 2.hours.ago)
    solve_team = create(:team)
    unsolve_team = create(:team)
    solved_challenge = create(:pentest_solved_challenge, team: solve_team, challenge: @challenge, created_at: 3.hours.ago)
    # Points decrease at ~3 points per hour with the challenge defined above
    # This game only lasts 1 hour and therefore unsolve_team is shown they would've received
    # 47 points if they solved this challenge right at the end of the game.
    assert_equal 47, solved_challenge.flag.display_point_value(unsolve_team)
  end

  test 'challenge open?' do
    assert @flag.challenge_open?
    @challenge.update(state: :closed)
    assert_not @flag.challenge_open?
  end

  test 'defense flag unique per team' do
    flag2 = build(:defense_flag, challenge: @challenge, team: @defense_team)
    assert_not flag2.valid?
    assert_equal true, flag2.errors.added?(:challenge, I18n.t('flag.challenge_must_be_unique'))
  end
end
