<commit_before>require 'feature_helper'
require 'api_test_helper'
require 'models/session_proxy'
require 'piwik_test_helper'

describe 'user sends authn requests' do
  let(:api_saml_endpoint) { ida_frontend_api_uri('api/session') }

  context 'and it is received successfully' do
    let(:session_start_time) { DateTime.now }
    it 'will redirect the user to /start' do
      stub_api_saml_endpoint

      visit('/test-saml')
      click_button 'saml-post'

      expect(page).to have_title 'Start - GOV.UK Verify - GOV.UK'

      expect(page.get_rack_session['transaction_simple_id']).to eql 'test-rp'
      expect(page.get_rack_session['verify_session_id']).to eql default_session_id
      expect(page.get_rack_session['requested_loa']).to eql 'LEVEL_1'

      cookies = Capybara.current_session.driver.browser.rack_mock_session.cookie_jar
      expected_cookies = CookieNames.session_cookies + [
        CookieNames::VERIFY_LOCALE, CookieNames::AB_TEST, CookieNames::PIWIK_USER_ID
      ]

      expect(cookies.to_hash.keys.to_set).to eql expected_cookies.to_set
      expect(page.get_rack_session['transaction_supports_eidas']).to eql false
    end

    it 'will redirect the user to /confirm-your-identity when journey hint is set' do
      stub_api_idp_list(default_idps)
      set_journey_hint_cookie('http://idcorp.com')
      stub_api_saml_endpoint(transactionSupportsEidas: true)
      visit('/test-saml')
      click_button 'saml-post-journey-hint'
      expect(page).to have_title 'Confirm your identity - GOV.UK Verify - GOV.UK'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql true
    end

    it 'will redirect the user to /choose-a-country for an eidas journey where eidas is enabled' do
      stub_api_saml_endpoint(transactionSupportsEidas: true)
      stub_transactions_list
      stub_countries_list

      visit('/test-saml')
      click_button 'saml-post-eidas'

      expect(page).to have_title 'Choose a country - GOV.UK Verify - GOV.UK'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql true
    end

    it 'will render the something went wrong page for an eidas journey where eidas is disabled' do
      stub_api_saml_endpoint(transactionSupportsEidas: false)
      stub_transactions_list

      visit('/test-saml')
      click_button 'saml-post-eidas'

      expect(page).to have_content 'Sorry, something went wrong'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql false
    end

    it 'will set ab_test cookie' do
      stub_api_saml_endpoint

      visit('/test-saml')
      click_button 'saml-post'

      expect(cookie_value(CookieNames::AB_TEST)).to match(/{"select_documents_v2":"select_documents_v2_control","about_companies":"about_companies_with_logo".*}/)
    end

    it 'will not set ab_test cookie if already set' do
      stub_api_saml_endpoint
      ab_test_cookie_value = {
        'about_companies' => 'about_companies_with_logo',
        'select_documents_v2' => 'select_documents_v2_control',
        'threshold_policy_experiment' => 'threshold_policy_experiment_control'
      }.to_json
      cookie_hash = create_cookie_hash.merge!(ab_test: CGI.escape(ab_test_cookie_value))
      set_cookies!(cookie_hash)

      visit('/test-saml')
      click_button 'saml-post'

      expect_cookie(CookieNames::AB_TEST, ab_test_cookie_value)
    end

    it 'will include both experiments in the ab_test cookie if only one experiment is currently in the ab_test cookie' do
      stub_api_saml_endpoint
      cookie_hash = create_cookie_hash.merge!(ab_test: CGI.escape({ 'about_companies' => 'about_companies_no_logo' }.to_json))
      set_cookies!(cookie_hash)

      visit('/test-saml')
      click_button 'saml-post'

      expect(cookie_value(CookieNames::AB_TEST)).to match(/{"select_documents_v2":"select_documents_v2_control","about_companies":"about_companies_no_logo".*}/)
    end

    it 'will not set ab_test cookie if RP is in AB test blacklist' do
      stub_api_saml_endpoint('transactionSimpleId' => 'test-rp-no-ab-test')

      visit('/test-saml')
      click_button 'saml-post'
<commit_msg>TT-1345: Updated spec for AB test to include this one<commit_after>require 'feature_helper'
require 'api_test_helper'
require 'models/session_proxy'
require 'piwik_test_helper'

describe 'user sends authn requests' do
  let(:api_saml_endpoint) { ida_frontend_api_uri('api/session') }

  context 'and it is received successfully' do
    let(:session_start_time) { DateTime.now }
    it 'will redirect the user to /start' do
      stub_api_saml_endpoint

      visit('/test-saml')
      click_button 'saml-post'

      expect(page).to have_title 'Start - GOV.UK Verify - GOV.UK'

      expect(page.get_rack_session['transaction_simple_id']).to eql 'test-rp'
      expect(page.get_rack_session['verify_session_id']).to eql default_session_id
      expect(page.get_rack_session['requested_loa']).to eql 'LEVEL_1'

      cookies = Capybara.current_session.driver.browser.rack_mock_session.cookie_jar
      expected_cookies = CookieNames.session_cookies + [
        CookieNames::VERIFY_LOCALE, CookieNames::AB_TEST, CookieNames::PIWIK_USER_ID
      ]

      expect(cookies.to_hash.keys.to_set).to eql expected_cookies.to_set
      expect(page.get_rack_session['transaction_supports_eidas']).to eql false
    end

    it 'will redirect the user to /confirm-your-identity when journey hint is set' do
      stub_api_idp_list(default_idps)
      set_journey_hint_cookie('http://idcorp.com')
      stub_api_saml_endpoint(transactionSupportsEidas: true)
      visit('/test-saml')
      click_button 'saml-post-journey-hint'
      expect(page).to have_title 'Confirm your identity - GOV.UK Verify - GOV.UK'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql true
    end

    it 'will redirect the user to /choose-a-country for an eidas journey where eidas is enabled' do
      stub_api_saml_endpoint(transactionSupportsEidas: true)
      stub_transactions_list
      stub_countries_list

      visit('/test-saml')
      click_button 'saml-post-eidas'

      expect(page).to have_title 'Choose a country - GOV.UK Verify - GOV.UK'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql true
    end

    it 'will render the something went wrong page for an eidas journey where eidas is disabled' do
      stub_api_saml_endpoint(transactionSupportsEidas: false)
      stub_transactions_list

      visit('/test-saml')
      click_button 'saml-post-eidas'

      expect(page).to have_content 'Sorry, something went wrong'
      expect(page.get_rack_session['transaction_supports_eidas']).to eql false
    end

    it 'will set ab_test cookie' do
      stub_api_saml_endpoint

      visit('/test-saml')
      click_button 'saml-post'

      expect(cookie_value(CookieNames::AB_TEST)).to match(/{"select_documents_v2":"select_documents_v2_control","about_companies":"about_companies_with_logo".*}/)
    end

    it 'will not set ab_test cookie if already set' do
      stub_api_saml_endpoint
      ab_test_cookie_value = {
        'about_companies' => 'about_companies_with_logo',
        'select_documents_v2' => 'select_documents_v2_control',
        'threshold_policy_experiment' => 'threshold_policy_experiment_control',
        'loa1_radio_picker' => 'loa1_radio_picker_control'
      }.to_json
      cookie_hash = create_cookie_hash.merge!(ab_test: CGI.escape(ab_test_cookie_value))
      set_cookies!(cookie_hash)

      visit('/test-saml')
      click_button 'saml-post'

      expect_cookie(CookieNames::AB_TEST, ab_test_cookie_value)
    end

    it 'will include both experiments in the ab_test cookie if only one experiment is currently in the ab_test cookie' do
      stub_api_saml_endpoint
      cookie_hash = create_cookie_hash.merge!(ab_test: CGI.escape({ 'about_companies' => 'about_companies_no_logo' }.to_json))
      set_cookies!(cookie_hash)

      visit('/test-saml')
      click_button 'saml-post'

      expect(cookie_value(CookieNames::AB_TEST)).to match(/{"select_documents_v2":"select_documents_v2_control","about_companies":"about_companies_no_logo".*}/)
    end

    it 'will not set ab_test cookie if RP is in AB test blacklist' do
      stub_api_saml_endpoint('transactionSimpleId' => 'test-rp-no-ab-test')

      visit('/test-saml')
      click_button 'saml-post'

{'fraction_non_alphanumeric': 0.09606620292636124, 'fraction_numerical': 0.0023986567522187576, 'mean_word_length': 3.963690476190476, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3009543', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2708, 'n_words': 533}