require 'test_helper'

class GamesControllerTest < ActionController::TestCase

  def setup
    @game = create(:active_game, enable_completion_certificates: true)
  end

  test "should get show" do
    get :show
    assert_response :success
  end

  test 'cannot get summary before game is open' do
    create(:unstarted_game)

    get :summary
    assert_redirected_to @controller.user_root_path
    assert_equal flash[:alert], I18n.t('game.before_competition')
  end

  test "should get summary during game" do
    get :summary
    assert_response :success
  end

  test "should get summary after game" do
    create(:ended_game)

    get :summary
    assert_response :success
  end

  [0, 1].each do |num|
    test "summary page displays successfully with 0 teams and #{num} divisions" do
      Division.destroy_all
      create_list(:division, num)

      get :summary

      assert_response :success
      assert_select "h4.zero-items-text", {:count=>1, :text=>I18n.t('teams.summary.zero_teams_text')}, "Nothing to Report text is missing from Team Summary page"
    end
  end

  [1, 4, 5, 6].each do |team_count|
    test "summary page displays correctly with #{team_count} teams" do
      Team.destroy_all
      create_list(:team, team_count, compete_for_prizes: true)

      get :summary

      assert_response :success
      assert_select 'tbody' do
        Team.all.each.with_index(1) do |team, idx|
          assert_select "tr" do
            assert_select "td", {:count=>1, :text=>"#{idx}"},
                          "#{team_count} teams was created but no #{idx} entry was found in the table"
            assert_select "td", {:count=>1, :text=>"#{team.team_name}"}
          end
        end
      end
    end
  end

  test 'guest and user cannot access show as markdown' do
    assert_raises(ActiveRecord::RecordNotFound) do
      get :show, format: :markdown # Nobody is signed in
    end
    sign_in create(:user)
    assert_raises(ActiveRecord::RecordNotFound) do
      get :show, format: :markdown # User is signed in
    end
  end

  test 'admin can access show as markdown' do
    sign_in create(:admin)
    get :show, format: :markdown
    assert_response :success
    assert_equal "text/markdown; charset=utf-8", response.content_type
  end

  test 'admin can access certificate template' do
    admin = create(:admin)
    sign_in admin
    get :completion_certificate_template
    assert_response :success
    assert_equal 'image/jpeg', response.content_type
  end

  test 'admin redirect when certificate template unavailable' do
    @game.destroy
    @game = create(:active_game)
    sign_in create(:admin)
    get :completion_certificate_template
    assert_redirected_to rails_admin_path
    assert_equal I18n.t('admin.download_not_available'), flash[:alert]
  end

  test 'show pentest game' do
    Game.first.destroy
    game = create(:active_game)
    create(:team_with_pentest_flags)
    get :show
    assert_response :success
  end

  [:jeopardy, :teams_x_challenges, :multiple_categories, :title_and_description].each do |board_layout|
    test "show #{board_layout} board layout" do
      create(:active_game, board_layout: board_layout)
      create_list(:standard_challenge, 3)
      create_list(:share_challenge, 3)
      get :show
      assert_response :success
    end
  end


end
