<commit_before>require "rails_helper"
require "controller_helper"
require "feature_helper"
require "spec_helper"
require "api_test_helper"
require "piwik_test_helper"
require "models/display/viewable_identity_provider"

describe SelectDocumentsVariantCController do
  before(:each) do
    experiment = "short_hub_2019_q3-preview"
    variant = "variant_c_2_idp_short_hub"
    set_session_and_cookies_with_loa_and_variant("LEVEL_2", experiment, variant)
    stub_api_idp_list_for_registration([{ "simpleId" => "stub-idp-one",
      "entityId" => "http://idcorp.com",
      "levelsOfAssurance" => %w(LEVEL_2) }], "LEVEL_2")

    session[:selected_answers] = {
      "device_type" => { device_type_other: true },
    }
  end

  context "when form is valid" do
    it "redirects to the advice page when less than three documents are selected" do
      evidence = { has_valid_passport: "t", has_credit_card: "t" }.freeze
      expect(FEDERATION_REPORTER).to receive(:report_user_evidence_attempt)
      .with(
        current_transaction: a_kind_of(Display::RpDisplayData),
        request: a_kind_of(ActionDispatch::Request),
        attempt_number: 1,
        evidence_list: { device_type_other: true }.merge!(evidence).keys,
      )
      post :select_documents, params: { locale: "en", select_documents_variant_c_form: evidence }
      expect(subject).to redirect_to select_documents_advice_path
    end

    it "redirects to the advice page when None of the above is selected" do
      evidence = { has_nothing: "t" }
      expect(FEDERATION_REPORTER).to receive(:report_user_evidence_attempt)
      .with(
        current_transaction: a_kind_of(Display::RpDisplayData),
        request: a_kind_of(ActionDispatch::Request),
        attempt_number: 1,
        evidence_list: { device_type_other: true }.keys,
      )
      post :select_documents, params: { locale: "en", select_documents_variant_c_form: evidence }
      expect(subject).to redirect_to select_documents_advice_path
    end

    it "redirects to the company picker when at least three documents are available" do
      evidence = { has_valid_passport: "t", has_credit_card: "t", has_phone_can_app: "t" }.freeze
      post :select_documents, params: { locale: "en", select_documents_variant_c_form: evidence }

      expect(subject).to redirect_to choose_a_certified_company_path
    end
  end

  context "when form is invalid" do
    subject { post :select_documents, params: { locale: "en" } }

    it "renders itself" do
      expect(subject).to render_template(:index)
    end

    it "does not report to Piwik" do
      expect(FEDERATION_REPORTER).not_to receive(:report_action)
    end
  end
end<commit_msg>HUB-235: dry up expection on federation_reporter<commit_after>require "rails_helper"
require "controller_helper"
require "feature_helper"
require "spec_helper"
require "api_test_helper"
require "piwik_test_helper"
require "models/display/viewable_identity_provider"

describe SelectDocumentsVariantCController do
  before(:each) do
    experiment = "short_hub_2019_q3-preview"
    variant = "variant_c_2_idp_short_hub"
    set_session_and_cookies_with_loa_and_variant("LEVEL_2", experiment, variant)
    stub_api_idp_list_for_registration([{ "simpleId" => "stub-idp-one",
      "entityId" => "http://idcorp.com",
      "levelsOfAssurance" => %w(LEVEL_2) }], "LEVEL_2")

    session[:selected_answers] = {
      "device_type" => { device_type_other: true },
    }
  end

  context "when form is valid" do
    it "redirects to the advice page when less than three documents are selected" do
      evidence = { has_valid_passport: "t", has_credit_card: "t", has_nothing: "t" }.freeze
      expect_federation_reporter_to_receive_user_evidence_when_posted(evidence)
      expect(subject).to redirect_to select_documents_advice_path
    end

    it "redirects to the advice page when None of the above is selected" do
      evidence = { has_nothing: "t" }
      expect_federation_reporter_to_receive_user_evidence_when_posted(evidence)
      expect(subject).to redirect_to select_documents_advice_path
    end

    it "redirects to the company picker when at least three documents are available" do
      evidence = { has_valid_passport: "t", has_credit_card: "t", has_phone_can_app: "t" }.freeze
      post :select_documents, params: { locale: "en", select_documents_variant_c_form: evidence }

      expect(subject).to redirect_to choose_a_certified_company_path
    end
  end

  context "when form is invalid" do
    subject { post :select_documents, params: { locale: "en" } }

    it "renders itself" do
      expect(subject).to render_template(:index)
    end

    it "does not report to Piwik" do
      expect(FEDERATION_REPORTER).not_to receive(:report_action)
    end
  end

  def expect_federation_reporter_to_receive_user_evidence_when_posted(evidence)
    expect(FEDERATION_REPORTER).to receive(:report_user_evidence_attempt)
    .with(
      current_transaction: a_kind_of(Display::RpDisplayData),
      request: a_kind_of(ActionDispatch::Request),
      attempt_number: 1,
      evidence_list: { device_type_other: true }.merge!(evidence).keys - %i(has_nothing),
    )
    post :select_documents, params: { locale: "en", select_documents_variant_c_form: evidence }
  end
end
{'fraction_non_alphanumeric': 0.09224039558767592, 'fraction_numerical': 0.004564473183720046, 'mean_word_length': 4.371807967313585, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6201438', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1656, 'n_words': 382}