<commit_before>require 'rails_helper'

RSpec.describe Advocates::ClaimsController, type: :controller, focus: true do
  let!(:advocate) { create(:advocate) }

  before { sign_in advocate.user }

  describe 'GET #landing' do
    before { get :landing }

    it 'returns http success' do
      expect(response).to have_http_status(:success)
    end

    it 'renders the template' do
      expect(response).to render_template(:landing)
    end
  end

  describe "GET #index" do
    before { get :index }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    context 'advocate' do
      before do
        create(:claim,            advocate: advocate)
        create(:submitted_claim,  advocate: advocate)
        create(:completed_claim,  advocate: advocate)
        create(:rejected_claim,   advocate: advocate)
      end

      it 'assigns @submitted_claims' do
        expect(assigns(:submitted_claims)).to eq(advocate.reload.claims.submitted.order(created_at: :desc))
      end

      it 'assigns @submitted_or_allocated_claims' do
        expect(assigns(:submitted_or_allocated_claims)).to eq(advocate.reload.claims.submitted_or_allocated.order(created_at: :desc))
      end

      it 'assigns @rejected_claims' do
        expect(assigns(:rejected_claims)).to eq(advocate.reload.claims.rejected.order(created_at: :desc))
      end

      it 'assigns @allocated_claims' do
        expect(assigns(:allocated_claims)).to eq(advocate.reload.claims.allocated.order(created_at: :desc))
      end

      it 'assigns @part_paid_claims' do
        expect(assigns(:part_paid_claims)).to eq(advocate.reload.claims.part_paid.order(created_at: :desc))
      end

      it 'assigns @completed_claims' do
        expect(assigns(:completed_claims)).to eq(advocate.reload.claims.completed.order(created_at: :desc))
      end

      it 'assigns @draft_claims' do
        expect(assigns(:draft_claims)).to eq(advocate.reload.claims.draft.order(created_at: :desc))
      end
    end

    context 'advocate admin' do
      let(:chamber) { create(:chamber) }
      let(:advocate_admin) { create(:advocate, :admin, chamber_id: chamber.id) }

      before do
        create(:claim, advocate: advocate)
        create(:submitted_claim, advocate: advocate)
        create(:completed_claim, advocate: advocate)

        advocate.update_column(:chamber_id, chamber.id)
        create(:claim, advocate: advocate.reload)

        sign_in advocate_admin.user
      end

      it 'assigns @submitted_claims' do
        expect(assigns(:submitted_claims)).to eq(advocate.reload.chamber.claims.submitted.order(created_at: :desc))
      end

      it 'assigns @rejected_claims' do
        expect(assigns(:rejected_claims)).to eq(advocate.reload.chamber.claims.rejected.order(created_at: :desc))
      end

      it 'assigns @allocated_claims' do
        expect(assigns(:allocated_claims)).to eq(advocate.reload.chamber.claims.allocated.order(created_at: :desc))
      end

      it 'assigns @part_paid_claims' do
        expect(assigns(:part_paid_claims)).to eq(advocate.reload.chamber.claims.part_paid.order(created_at: :desc))
      end

      it 'assigns @completed_claims' do
        expect(assigns(:completed_claims)).to eq(advocate.reload.chamber.claims.completed.order(created_at: :desc))
      end

      it 'assigns @draft_claims' do
        expect(assigns(:draft_claims)).to eq(advocate.reload.chamber.claims.draft.order(created_at: :desc))
      end
    end

    it 'renders the template' do
      expect(response).to render_template(:index)
    end
  end

  describe "GET #show" do
    subject { create(:claim, advocate: advocate) }

    before { get :show, id: subject }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    it 'assigns @claim' do
      expect(assigns(:claim)).to eq(subject)
    end

    it 'renders the template' do
      expect(response).to render_template(:show)
    end
  end

  describe "GET #new" do
    before { get :new }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    it 'assigns @claim' do
      expect(assigns(:claim)).to be_new_record
    end

    it 'renders the template' do
      expect(response).to render_template(:new)
    end
  end

  describe "GET #edit" do
    before { get :edit, id: subject }

    context 'editable claim' do
      subject { create(:claim, advocate: advocate) }

      it "returns http success" do
        expect(response).to have_http_status(:success)
      end

      it 'assigns @claim' do
        expect(assigns(:claim)).to eq(subject)
      end

      it 'renders the template' do
        expect(response).to render_template(:edit)
      end
    end

    context 'uneditable claim' do
      subject { create(:allocated_claim, advocate: advocate) }

      it 'redirects to advocates claims index' do
        expect(response).to redirect_to(advocates_claims_url)
      end
    end
  end

  describe "POST #create" do
    context 'when advocate signed in' do
      context 'and the input is valid' do
        let(:court) { create(:court) }
        let(:offence) { create(:offence) }
        let(:claim_params) do
          {
             additional_information: 'foo',
             court_id: court,
             case_type: 'trial',
             offence_id: offence,
             case_number: '12345',
             advocate_category: 'QC',
             prosecuting_authority: 'cps',
          }
        end

        it 'creates a claim' do
          expect {
            post :create, claim: claim_params
          }.to change(Claim, :count).by(1)
        end

        it 'redirects to claim summary' do
          post :create, claim: claim_params
          expect(response).to redirect_to(summary_advocates_claim_path(Claim.first))
        end

        it 'sets the created claim\'s advocate to the signed in advocate' do
          post :create, claim: claim_params
          expect(Claim.first.advocate).to eq(advocate)
        end
      end

      context 'and the input is invalid' do
        it 'does not create a claim' do
          expect {
            post :create, claim: { additional_information: 'foo' }
          }.to_not change(Claim, :count)
        end

        it 'render new template' do
          post :create, claim: { additional_information: 'foo' }
          expect(response).to render_template(:new)
        end
      end

      context 'basic and non-basic fees' do
        let!(:basic_fee_type_1)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 1' }
        let!(:basic_fee_type_2)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 2' }
        let!(:basic_fee_type_3)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 3' }
        let!(:basic_fee_type_4)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 4' }
        let!(:misc_fee_type_1)          { FactoryGirl.create :fee_type, :misc, description: 'Miscellaneous Fee Type 1' }
        let!(:misc_fee_type_2)          { FactoryGirl.create :fee_type, :misc, description: 'Miscellaneous Fee Type 2' }
        let!(:fixed_fee_type_1)         { FactoryGirl.create :fee_type, :fixed, description: 'Fixed Fee Type 1' }

        let(:court)                     { create(:court) }
        let(:offence)                   { create(:offence) }
        let(:claim_params)              { full_valid_params }
        let(:invalid_claim_params)      { full_valid_params.reject{ |k,v| k == 'prosecuting_authority'} }

        context 'valid params' do
          it 'should create a claim with all basic fees and the specified non-basic fees' do
            post :create, claim: claim_params
            claim = assigns(:claim)

            expect(claim.basic_fees.size).to eq 4             # one record for every basic fee regardless of whether blank or not
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_1.id }.amount.to_f ).to eq 1000.0
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_3.id }.amount.to_f ).to eq 9000.45
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_4.id }.amount.to_f ).to eq 125.0
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_2.id }).to be_blank

            expect(claim.non_basic_fees.size).to eq 2
            expect(claim.non_basic_fees.detect{ |f| f.fee_type_id == misc_fee_type_2.id }.amount.to_f ).to eq 250.0
            expect(claim.non_basic_fees.detect{ |f| f.fee_type_id == fixed_fee_type_1.id }.amount.to_f ).to eq 2500.0

            expect(claim.reload.fees_total).to eq 12_875.45
          end
        end

        context 'invalid params' do
          render_views
          it 'should redisplay the page with error messages and all the entered data in basic and non basic fees' do
            post :create, claim: invalid_claim_params
            expect(response.status).to eq 200
            expect(response).to render_template(:new)
            expect(response.body).to have_content("Prosecuting authority can't be blank")
            claim = assigns(:claim)
            expect(claim.basic_fees.size).to eq 4
            expect(claim.non_basic_fees.size).to eq 2

            bf1 = claim.basic_fees.detect{ |f| f.description == 'Basic Fee Type 1' }
            expect(bf1.quantity).to eq 10
            expect(bf1.rate).to eq 100
            expect(bf1.amount).to eq 1000
<commit_msg>Add debug message<commit_after>require 'rails_helper'

RSpec.describe Advocates::ClaimsController, type: :controller, focus: true do
  let!(:advocate) { create(:advocate) }

  before { sign_in advocate.user }

  describe 'GET #landing' do
    before { get :landing }

    it 'returns http success' do
      expect(response).to have_http_status(:success)
    end

    it 'renders the template' do
      expect(response).to render_template(:landing)
    end
  end

  describe "GET #index" do
    before { get :index }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    context 'advocate' do
      before do
        create(:claim,            advocate: advocate)
        create(:submitted_claim,  advocate: advocate)
        create(:completed_claim,  advocate: advocate)
        create(:rejected_claim,   advocate: advocate)
      end

      it 'assigns @submitted_claims' do
        expect(assigns(:submitted_claims)).to eq(advocate.reload.claims.submitted.order(created_at: :desc))
      end

      it 'assigns @submitted_or_allocated_claims' do
        expect(assigns(:submitted_or_allocated_claims)).to eq(advocate.reload.claims.submitted_or_allocated.order(created_at: :desc))
      end

      it 'assigns @rejected_claims' do
        expect(assigns(:rejected_claims)).to eq(advocate.reload.claims.rejected.order(created_at: :desc))
      end

      it 'assigns @allocated_claims' do
        expect(assigns(:allocated_claims)).to eq(advocate.reload.claims.allocated.order(created_at: :desc))
      end

      it 'assigns @part_paid_claims' do
        expect(assigns(:part_paid_claims)).to eq(advocate.reload.claims.part_paid.order(created_at: :desc))
      end

      it 'assigns @completed_claims' do
        expect(assigns(:completed_claims)).to eq(advocate.reload.claims.completed.order(created_at: :desc))
      end

      it 'assigns @draft_claims' do
        expect(assigns(:draft_claims)).to eq(advocate.reload.claims.draft.order(created_at: :desc))
      end
    end

    context 'advocate admin' do
      let(:chamber) { create(:chamber) }
      let(:advocate_admin) { create(:advocate, :admin, chamber_id: chamber.id) }

      before do
        create(:claim, advocate: advocate)
        create(:submitted_claim, advocate: advocate)
        create(:completed_claim, advocate: advocate)

        advocate.update_column(:chamber_id, chamber.id)
        create(:claim, advocate: advocate.reload)

        sign_in advocate_admin.user
      end

      it 'assigns @submitted_claims' do
        expect(assigns(:submitted_claims)).to eq(advocate.reload.chamber.claims.submitted.order(created_at: :desc))
      end

      it 'assigns @rejected_claims' do
        expect(assigns(:rejected_claims)).to eq(advocate.reload.chamber.claims.rejected.order(created_at: :desc))
      end

      it 'assigns @allocated_claims' do
        expect(assigns(:allocated_claims)).to eq(advocate.reload.chamber.claims.allocated.order(created_at: :desc))
      end

      it 'assigns @part_paid_claims' do
        expect(assigns(:part_paid_claims)).to eq(advocate.reload.chamber.claims.part_paid.order(created_at: :desc))
      end

      it 'assigns @completed_claims' do
        expect(assigns(:completed_claims)).to eq(advocate.reload.chamber.claims.completed.order(created_at: :desc))
      end

      it 'assigns @draft_claims' do
        expect(assigns(:draft_claims)).to eq(advocate.reload.chamber.claims.draft.order(created_at: :desc))
      end
    end

    it 'renders the template' do
      expect(response).to render_template(:index)
    end
  end

  describe "GET #show" do
    subject { create(:claim, advocate: advocate) }

    before { get :show, id: subject }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    it 'assigns @claim' do
      expect(assigns(:claim)).to eq(subject)
    end

    it 'renders the template' do
      expect(response).to render_template(:show)
    end
  end

  describe "GET #new" do
    before { get :new }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end

    it 'assigns @claim' do
      expect(assigns(:claim)).to be_new_record
    end

    it 'renders the template' do
      expect(response).to render_template(:new)
    end
  end

  describe "GET #edit" do
    before { get :edit, id: subject }

    context 'editable claim' do
      subject { create(:claim, advocate: advocate) }

      it "returns http success" do
        expect(response).to have_http_status(:success)
      end

      it 'assigns @claim' do
        expect(assigns(:claim)).to eq(subject)
      end

      it 'renders the template' do
        expect(response).to render_template(:edit)
      end
    end

    context 'uneditable claim' do
      subject { create(:allocated_claim, advocate: advocate) }

      it 'redirects to advocates claims index' do
        expect(response).to redirect_to(advocates_claims_url)
      end
    end
  end

  describe "POST #create" do
    context 'when advocate signed in' do
      context 'and the input is valid' do
        let(:court) { create(:court) }
        let(:offence) { create(:offence) }
        let(:claim_params) do
          {
             additional_information: 'foo',
             court_id: court,
             case_type: 'trial',
             offence_id: offence,
             case_number: '12345',
             advocate_category: 'QC',
             prosecuting_authority: 'cps',
          }
        end

        it 'creates a claim' do
          expect {
            post :create, claim: claim_params
          }.to change(Claim, :count).by(1)
        end

        it 'redirects to claim summary' do
          post :create, claim: claim_params
          expect(response).to redirect_to(summary_advocates_claim_path(Claim.first))
        end

        it 'sets the created claim\'s advocate to the signed in advocate' do
          post :create, claim: claim_params
          expect(Claim.first.advocate).to eq(advocate)
        end
      end

      context 'and the input is invalid' do
        it 'does not create a claim' do
          expect {
            post :create, claim: { additional_information: 'foo' }
          }.to_not change(Claim, :count)
        end

        it 'render new template' do
          post :create, claim: { additional_information: 'foo' }
          expect(response).to render_template(:new)
        end
      end

      context 'basic and non-basic fees' do
        let!(:basic_fee_type_1)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 1' }
        let!(:basic_fee_type_2)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 2' }
        let!(:basic_fee_type_3)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 3' }
        let!(:basic_fee_type_4)         { FactoryGirl.create :fee_type, :basic, description: 'Basic Fee Type 4' }
        let!(:misc_fee_type_1)          { FactoryGirl.create :fee_type, :misc, description: 'Miscellaneous Fee Type 1' }
        let!(:misc_fee_type_2)          { FactoryGirl.create :fee_type, :misc, description: 'Miscellaneous Fee Type 2' }
        let!(:fixed_fee_type_1)         { FactoryGirl.create :fee_type, :fixed, description: 'Fixed Fee Type 1' }

        let(:court)                     { create(:court) }
        let(:offence)                   { create(:offence) }
        let(:claim_params)              { full_valid_params }
        let(:invalid_claim_params)      { full_valid_params.reject{ |k,v| k == 'prosecuting_authority'} }

        context 'valid params' do
          it 'should create a claim with all basic fees and the specified non-basic fees' do
            post :create, claim: claim_params
            claim = assigns(:claim)
            puts ">>>>>>>>>>>>>>>> DEBUG message    #{__FILE__}::#{__LINE__} <<<<<<<<<<"
            ap claim, plain: true
            puts ">>>>>>>>>>>>>>>> DEBUG errors    #{__FILE__}::#{__LINE__} <<<<<<<<<<"
            ap claim.errors.full_messages


            expect(claim.basic_fees.size).to eq 4             # one record for every basic fee regardless of whether blank or not
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_1.id }.amount.to_f ).to eq 1000.0
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_3.id }.amount.to_f ).to eq 9000.45
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_4.id }.amount.to_f ).to eq 125.0
            expect(claim.basic_fees.detect{ |f| f.fee_type_id == basic_fee_type_2.id }).to be_blank

            expect(claim.non_basic_fees.size).to eq 2
            expect(claim.non_basic_fees.detect{ |f| f.fee_type_id == misc_fee_type_2.id }.amount.to_f ).to eq 250.0
            expect(claim.non_basic_fees.detect{ |f| f.fee_type_id == fixed_fee_type_1.id }.amount.to_f ).to eq 2500.0

            expect(claim.reload.fees_total).to eq 12_875.45
          end
        end

        context 'invalid params' do
          render_views
          it 'should redisplay the page with error messages and all the entered data in basic and non basic fees' do
            post :create, claim: invalid_claim_params
            expect(response.status).to eq 200
            expect(response).to render_template(:new)
            expect(response.body).to have_content("Prosecuting authority can't be blank")
            claim = assigns(:claim)
            expect(claim.basic_fees.size).to eq 4
            expect(claim.non_basic_fees.size).to eq 2

            bf1 = claim.basic_fees.detect{ |f| f.description == 'Basic Fee Type 1' }
            expect(bf1.quantity).to eq 10
            expect(bf1.rate).to eq 100
            expect(bf1.amount).to eq 1000

{'fraction_non_alphanumeric': 0.1055907063582616, 'fraction_numerical': 0.0080904470490613, 'mean_word_length': 2.5868675595238093, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '821851', 'n_tokens_mistral': 6140, 'n_tokens_neox': 5788, 'n_words': 1430}