<commit_before>require "rails_helper"

RSpec.describe Setup::InvoicesController, type: :controller do
  include WebmockDhis2Helpers

  describe "When non authenticated #new" do
    it "should redirect to sign on" do
      get :new, params: { project_id: 1 }
      expect(response).to redirect_to("/users/sign_in")
    end
  end

  describe "When authenticated" do
    include_context "basic_context"
    before(:each) do
      sign_in user
    end

    let(:org_unit_id) { "cDw53Ej8rju" }

    it "displays the form for entities" do
      get :new, params: { project_id: full_project.id }
      expect(response).to have_http_status(:success)
      expect(assigns(:invoicing_request)).not_to be nil
    end

    it "schedule worker when push_to_dhis2 clicked" do
      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "0"
        },
        push_to_dhis2:     true
      }
      expect(InvoiceForProjectAnchorWorker).to have_enqueued_sidekiq_job(full_project.project_anchor.id, "2017", "1", [org_unit_id])
    end

    it "calculate invoices" do
      stub_all_pyramid(full_project)
      stub_all_data_compound(full_project)
      stub_data_value_sets

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "1"
        },
        simulate_draft:    true
      }

      expect(response).to have_http_status(:success)
    end

    it "calculate invoices with new engine" do
      stub_new_pyramid
      stub_all_data_compound(full_project)
      stub_data_value_sets_for_new_engine

      verif_state = full_project.states.find { |s| s.name == "Verified" }
      maxscore_state = full_project.states.find { |s| s.name == "Max. Score" }
      full_project.activities.first.activity_states.create(
        [
          { name: "Vaccination Verified ", state: verif_state, formula: "0", kind: "formula" },
          { name: "Vaccination max ", state: maxscore_state, formula: "0", kind: "formula" }
        ]
      )

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "2"
        },
        simulate_draft:    true
      }

      expect(assigns(:invoicing_request).invoices.size).to eq(5)

      expect(response).to have_http_status(:success)
    end

    it "calculate invoices with mock_values" do
      stub_all_pyramid(full_project)
      stub_all_data_compound(full_project)

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          mock_values:    "1",
          engine_version: "1"
        },
        simulate_draft:    true
      }

      expect(response).to have_http_status(:success)
    end

    def stub_data_value_sets
      stub_request(:get, "http://play.dhis2.org/demo/api/dataValueSets?children=false&endDate=2017-12-31&orgUnit=#{org_unit_id}&startDate=2017-01-01")
        .to_return(status: 200, body: "")
    end
<commit_msg>Fix spec to use new stubs<commit_after>require "rails_helper"

RSpec.describe Setup::InvoicesController, type: :controller do
  include WebmockDhis2Helpers

  describe "When non authenticated #new" do
    it "should redirect to sign on" do
      get :new, params: { project_id: 1 }
      expect(response).to redirect_to("/users/sign_in")
    end
  end

  describe "When authenticated" do
    include_context "basic_context"
    before(:each) do
      sign_in user
    end

    let(:org_unit_id) { "cDw53Ej8rju" }

    it "displays the form for entities" do
      get :new, params: { project_id: full_project.id }
      expect(response).to have_http_status(:success)
      expect(assigns(:invoicing_request)).not_to be nil
    end

    it "schedule worker when push_to_dhis2 clicked" do
      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "3"
        },
        push_to_dhis2:     true
      }
      expect(InvoiceForProjectAnchorWorker).to have_enqueued_sidekiq_job(full_project.project_anchor.id, "2017", "1", [org_unit_id])
    end

    it "calculate invoices" do
      stub_new_pyramid
      stub_all_data_compound(full_project)
      stub_data_value_sets_for_new_engine

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "3"
        },
        simulate_draft:    true
      }

      expect(response).to have_http_status(:success)
    end

    it "calculate invoices with new engine" do
      stub_new_pyramid
      stub_all_data_compound(full_project)
      stub_data_value_sets_for_new_engine

      verif_state = full_project.states.find { |s| s.name == "Verified" }
      maxscore_state = full_project.states.find { |s| s.name == "Max. Score" }
      full_project.activities.first.activity_states.create(
        [
          { name: "Vaccination Verified ", state: verif_state, formula: "0", kind: "formula" },
          { name: "Vaccination max ", state: maxscore_state, formula: "0", kind: "formula" }
        ]
      )

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          engine_version: "2"
        },
        simulate_draft:    true
      }

      expect(assigns(:invoicing_request).invoices.size).to eq(5)

      expect(response).to have_http_status(:success)
    end

    it "calculate invoices with mock_values" do
      stub_new_pyramid
      stub_all_data_compound(full_project)

      post :create, params: {
        project_id:        full_project.id,
        invoicing_request: {
          entity:         org_unit_id,
          year:           "2017",
          quarter:        "1",
          mock_values:    "1",
          engine_version: "3"
        },
        simulate_draft:    true
      }

      expect(response).to have_http_status(:success)
    end

    def stub_data_value_sets
      stub_request(:get, "http://play.dhis2.org/demo/api/dataValueSets?children=false&endDate=2017-12-31&orgUnit=#{org_unit_id}&startDate=2017-01-01")
        .to_return(status: 200, body: "")
    end

{'fraction_non_alphanumeric': 0.09742620328631671, 'fraction_numerical': 0.017449469245310455, 'mean_word_length': 2.193129062209842, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '973223', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2190, 'n_words': 435}