From 2c5e1fa7d71d122ca6c188b6ab71e1916f3f2a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Maniaci?= Date: Thu, 4 Apr 2024 08:13:28 +0200 Subject: [PATCH] payment requests: block if PFMP or RIBs instances are not valid This means including all the model validation like correct dates and SEPA-ribs, etc. --- .../asp/payment_request_state_machine.rb | 8 +++ spec/factories/ribs.rb | 4 ++ .../asp/payment_request_state_machine_spec.rb | 52 ++++++++++--------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/app/models/asp/payment_request_state_machine.rb b/app/models/asp/payment_request_state_machine.rb index 4a810dab5..a0a83f1be 100644 --- a/app/models/asp/payment_request_state_machine.rb +++ b/app/models/asp/payment_request_state_machine.rb @@ -45,6 +45,14 @@ class PaymentRequestStateMachine !request.student.rib.reused? end + guard_transition(to: :ready) do |request| + request.student.rib.valid? + end + + guard_transition(to: :ready) do |request| + request.pfmp.valid? + end + guard_transition(to: :ready) do |request| !request.student.lost end diff --git a/spec/factories/ribs.rb b/spec/factories/ribs.rb index 66efca41b..8abf39b3f 100644 --- a/spec/factories/ribs.rb +++ b/spec/factories/ribs.rb @@ -8,5 +8,9 @@ archived_at { nil } name { Faker::Name.name } personal { Faker::Boolean.boolean } + + trait :outside_sepa do + iban { Faker::Bank.iban(country_code: "sa") } + end end end diff --git a/spec/models/asp/payment_request_state_machine_spec.rb b/spec/models/asp/payment_request_state_machine_spec.rb index 606aa641a..759c73e6e 100644 --- a/spec/models/asp/payment_request_state_machine_spec.rb +++ b/spec/models/asp/payment_request_state_machine_spec.rb @@ -9,6 +9,12 @@ it { is_expected.to be_in_state :pending } + shared_examples "a blocked request" do + it "cannot transition to ready" do + expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError + end + end + describe "mark_ready!" do let(:asp_payment_request) { create(:asp_payment_request, :sendable) } @@ -29,49 +35,51 @@ context "when the schooling status is unknown" do before { asp_payment_request.schooling.update!(status: nil) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the schooling is for an apprentice" do before { asp_payment_request.schooling.update!(status: :apprentice) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the student is a lost record" do before { asp_payment_request.student.update!(lost: true) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the RIB has been reused somewhere else" do before { create(:rib, iban: asp_payment_request.student.rib.iban) } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end + # rubocop:disable Rails/SkipsModelValidations + context "when the PFMP is not valid" do + before { asp_payment_request.pfmp.update_column(:start_date, Date.new(2002, 1, 1)) } + + it_behaves_like "a blocked request" + end + + context "when the rib is not valid" do + before { asp_payment_request.student.rib.update_columns(attributes_for(:rib, :outside_sepa)) } + + it_behaves_like "a blocked request" + end + # rubocop:enable Rails/SkipsModelValidations + context "when the request is missing information" do before { student.rib&.destroy } - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the PFMP is zero-amount" do before { asp_payment_request.pfmp.update!(amount: 0) } - it "raises an error" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the request belongs to a student over 18 with an external rib" do @@ -80,9 +88,7 @@ student.rib.update!(personal: false) end - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end context "when the attributive decision has not been attached" do @@ -90,9 +96,7 @@ asp_payment_request.pfmp.schooling.attributive_decision.detach end - it "blocks the transition" do - expect { asp_payment_request.mark_ready! }.to raise_error Statesman::GuardFailedError - end + it_behaves_like "a blocked request" end end