diff --git a/app/models/concerns/waste_carriers_engine/can_change_workflow_status.rb b/app/models/concerns/waste_carriers_engine/can_change_workflow_status.rb index 08d144e45..14eeba189 100644 --- a/app/models/concerns/waste_carriers_engine/can_change_workflow_status.rb +++ b/app/models/concerns/waste_carriers_engine/can_change_workflow_status.rb @@ -262,14 +262,23 @@ module CanChangeWorkflowStatus transitions from: :worldpay_form, to: :renewal_received_form, if: :pending_worldpay_payment_or_convictions_check?, - success: :send_renewal_received_email + success: :send_renewal_received_email, + # TODO: This don't get triggered if in the `success` + # callback block, hence we went for `after` + after: :set_metadata_route transitions from: :worldpay_form, - to: :renewal_complete_form + to: :renewal_complete_form, + # TODO: This don't get triggered if in the `success` + # callback block, hence we went for `after` + after: :set_metadata_route transitions from: :bank_transfer_form, to: :renewal_received_form, - success: :send_renewal_received_email + success: :send_renewal_received_email, + # TODO: This don't get triggered if in the `success` + # callback block, hence we went for `after` + after: :set_metadata_route end event :back do diff --git a/app/models/waste_carriers_engine/transient_registration.rb b/app/models/waste_carriers_engine/transient_registration.rb index d08b60412..4747ff4d5 100644 --- a/app/models/waste_carriers_engine/transient_registration.rb +++ b/app/models/waste_carriers_engine/transient_registration.rb @@ -153,6 +153,12 @@ def stuck? true end + def set_metadata_route + metaData.route = Rails.configuration.metadata_route + + save + end + private def copy_data_from_registration diff --git a/app/services/waste_carriers_engine/renewal_completion_service.rb b/app/services/waste_carriers_engine/renewal_completion_service.rb index 00b424bac..7843c2f41 100644 --- a/app/services/waste_carriers_engine/renewal_completion_service.rb +++ b/app/services/waste_carriers_engine/renewal_completion_service.rb @@ -54,7 +54,7 @@ def update_registration end def update_meta_data - @registration.metaData.route = Rails.configuration.metadata_route + @registration.metaData.route = @transient_registration.metaData.route @registration.metaData.renew @registration.metaData.date_registered = Time.now @registration.metaData.date_activated = @registration.metaData.date_registered diff --git a/spec/models/waste_carriers_engine/transient_registration_spec.rb b/spec/models/waste_carriers_engine/transient_registration_spec.rb index 62feac1f9..d7275aefc 100644 --- a/spec/models/waste_carriers_engine/transient_registration_spec.rb +++ b/spec/models/waste_carriers_engine/transient_registration_spec.rb @@ -12,6 +12,47 @@ module WasteCarriersEngine expect(transient_registration).to have_state(:renewal_start_form) end end + + context "when transitioning from bank_transfer_form to renewal_received_form succesfully" do + it "set the transient registration metadata route" do + expect(transient_registration).to receive(:set_metadata_route).once + + transient_registration.update_attributes(workflow_state: :bank_transfer_form) + transient_registration.next + end + end + + context "when transitioning from worldpay_form to renewal_complete_form succesfully" do + it "set the transient registration metadata route" do + expect(transient_registration).to receive(:set_metadata_route).once + expect(transient_registration).to receive(:pending_worldpay_payment_or_convictions_check?).and_return(false) + + transient_registration.update_attributes(workflow_state: :worldpay_form) + transient_registration.next + end + end + + context "when transitioning from worldpay_form to renewal_received_form succesfully" do + it "set the transient registration metadata route" do + expect(transient_registration).to receive(:set_metadata_route).once + expect(transient_registration).to receive(:pending_worldpay_payment_or_convictions_check?).and_return(true) + + transient_registration.update_attributes(workflow_state: :worldpay_form) + transient_registration.next + end + end + end + + describe "#set_metadata_route" do + it "updates the transient registration's metadata route" do + metadata_route = double(:metadata_route) + + expect(Rails.configuration).to receive(:metadata_route).and_return(metadata_route) + expect(transient_registration.metaData).to receive(:route=).with(metadata_route) + expect(transient_registration).to receive(:save) + + transient_registration.set_metadata_route + end end describe "reg_identifier" do diff --git a/spec/requests/waste_carriers_engine/bank_transfer_forms_spec.rb b/spec/requests/waste_carriers_engine/bank_transfer_forms_spec.rb index b5975fabf..593d35412 100644 --- a/spec/requests/waste_carriers_engine/bank_transfer_forms_spec.rb +++ b/spec/requests/waste_carriers_engine/bank_transfer_forms_spec.rb @@ -50,6 +50,45 @@ module WasteCarriersEngine include_examples "POST without params form", "bank_transfer_form" + describe "POST new_bank_transfer_form" do + context "when a valid user is signed in" do + let(:user) { create(:user) } + + before(:each) do + sign_in(user) + end + + context "when a renewal is in progress" do + let(:transient_registration) do + create(:transient_registration, + :has_required_data, + :has_addresses, + :has_key_people, + :has_unpaid_balance, + account_email: user.email) + end + + context "when the workflow_state matches the requested form" do + before do + transient_registration.update_attributes(workflow_state: :bank_transfer_form) + end + + context "when the request is successful" do + it "updates the transient registration metadata attributes from application configuration" do + allow(Rails.configuration).to receive(:metadata_route).and_return("ASSISTED_DIGITAL") + + expect(transient_registration.reload.metaData.route).to be_nil + + post_form_with_params(:bank_transfer_form, reg_identifier: transient_registration.reg_identifier) + + expect(transient_registration.reload.metaData.route).to eq("ASSISTED_DIGITAL") + end + end + end + end + end + end + describe "GET back_bank_transfer_forms_path" do context "when a valid user is signed in" do let(:user) { create(:user) } diff --git a/spec/requests/waste_carriers_engine/worldpay_forms_spec.rb b/spec/requests/waste_carriers_engine/worldpay_forms_spec.rb index 77d9ad9a2..1f6c2ea12 100644 --- a/spec/requests/waste_carriers_engine/worldpay_forms_spec.rb +++ b/spec/requests/waste_carriers_engine/worldpay_forms_spec.rb @@ -76,6 +76,16 @@ module WasteCarriersEngine expect(response).to redirect_to(new_renewal_complete_form_path(reg_id)) end + it "updates the transient registration metadata attributes from application configuration" do + allow(Rails.configuration).to receive(:metadata_route).and_return("ASSISTED_DIGITAL") + + expect(transient_registration.reload.metaData.route).to be_nil + + get success_worldpay_forms_path(reg_id), params + + expect(transient_registration.reload.metaData.route).to eq("ASSISTED_DIGITAL") + end + context "when it has been flagged for conviction checks" do before do transient_registration.conviction_sign_offs = [build(:conviction_sign_off)] @@ -86,6 +96,16 @@ module WasteCarriersEngine expect(response).to redirect_to(new_renewal_received_form_path(reg_id)) end + it "updates the transient registration metadata attributes from application configuration" do + allow(Rails.configuration).to receive(:metadata_route).and_return("ASSISTED_DIGITAL") + + expect(transient_registration.reload.metaData.route).to be_nil + + get success_worldpay_forms_path(reg_id), params + + expect(transient_registration.reload.metaData.route).to eq("ASSISTED_DIGITAL") + end + context "when the mailer fails" do before do allow(Rails.configuration.action_mailer).to receive(:raise_delivery_errors).and_return(true) diff --git a/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb b/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb index 0c46bc202..01740b2a4 100644 --- a/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb +++ b/spec/services/waste_carriers_engine/renewal_completion_service_spec.rb @@ -63,6 +63,14 @@ module WasteCarriersEngine expect(registration.reload.finance_details.payments).to include(new_payment) end + it "copies the registration's route from the transient_registration to the registration" do + transient_registration.metaData.route = "ASSISTED_DIGITAL_FROM_TRANSIENT_REGISTRATION" + + renewal_completion_service.complete_renewal + + expect(registration.reload.metaData.route).to eq("ASSISTED_DIGITAL_FROM_TRANSIENT_REGISTRATION") + end + it "keeps existing orders" do old_order = registration.finance_details.orders.first renewal_completion_service.complete_renewal @@ -147,17 +155,6 @@ module WasteCarriersEngine expect(registration.reload.contact_address.last_name).to eq(last_name) end - context "when the metadata_route is set" do - before do - allow(Rails.configuration).to receive(:metadata_route).and_return("ASSISTED_DIGITAL") - end - - it "updates the registration's route to the correct value" do - renewal_completion_service.complete_renewal - expect(registration.reload.metaData.route).to eq("ASSISTED_DIGITAL") - end - end - it "deletes the transient registration" do renewal_completion_service.complete_renewal expect(TransientRegistration.where(reg_identifier: transient_registration.reg_identifier).count).to eq(0)