Skip to content

Commit

Permalink
payment requests: block if PFMP or RIBs instances are not valid
Browse files Browse the repository at this point in the history
This means including all the model validation like correct dates and
SEPA-ribs, etc.
  • Loading branch information
freesteph committed Apr 4, 2024
1 parent 3c2d9e1 commit 2c5e1fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
8 changes: 8 additions & 0 deletions app/models/asp/payment_request_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/ribs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 28 additions & 24 deletions spec/models/asp/payment_request_state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -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
Expand All @@ -80,19 +88,15 @@
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
before do
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

Expand Down

0 comments on commit 2c5e1fa

Please sign in to comment.