-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
covering in tests, to cover models included services/booking_responder
- Loading branch information
1 parent
6d84a8b
commit ae5b59b
Showing
15 changed files
with
376 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class Cancellation < Staff::ApplicationRecord | ||
VISITOR_CANCELLED = 'visitor_cancelled'.freeze | ||
|
||
PRISONER_VOS = 'prisoner_vos'.freeze | ||
PRISONER_RELEASED = 'prisoner_released'.freeze | ||
CHILD_PROTECTION_ISSUES = 'child_protection_issues'.freeze | ||
SLOT_UNAVAILABLE = 'slot_unavailable'.freeze | ||
VISITOR_BANNED = 'visitor_banned'.freeze | ||
PRISONER_MOVED = 'prisoner_moved'.freeze | ||
PRISONER_NON_ASSOCIATION = 'prisoner_non_association'.freeze | ||
PRISONER_CANCELLED = 'prisoner_cancelled'.freeze | ||
BOOKED_IN_ERROR = 'booked_in_error'.freeze | ||
CAPACITY_ISSUES = 'capacity_issues'.freeze | ||
|
||
STAFF_REASONS = [ | ||
PRISONER_VOS, | ||
PRISONER_RELEASED, | ||
CHILD_PROTECTION_ISSUES, | ||
SLOT_UNAVAILABLE, | ||
VISITOR_BANNED, | ||
PRISONER_MOVED, | ||
PRISONER_NON_ASSOCIATION, | ||
PRISONER_CANCELLED, | ||
BOOKED_IN_ERROR, | ||
CAPACITY_ISSUES | ||
] | ||
|
||
REASONS = STAFF_REASONS + [VISITOR_CANCELLED] | ||
|
||
belongs_to :visit, class_name: 'Staff::Visit', optional: true | ||
|
||
before_validation :sanitise_reasons | ||
|
||
validate :validate_reasons | ||
validates :reasons, presence: { message: :no_cancellation_reason } | ||
|
||
private | ||
|
||
def validate_reasons | ||
reasons.each do |r| | ||
next if REASONS.include?(r) | ||
|
||
errors.add( | ||
:reasons, | ||
I18n.t( | ||
'activerecord.errors.models.cancellation.invalid_reason', | ||
reason: r | ||
) | ||
) | ||
end | ||
end | ||
|
||
def sanitise_reasons | ||
reasons.reject!(&:empty?) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe AccessibleDate do | ||
let(:attributes) { { year: '2017', month: '12', day: '25' } } | ||
|
||
subject { described_class.new(attributes) } | ||
|
||
it { is_expected.to be_valid } | ||
|
||
describe 'validations' do | ||
it { is_expected.to validate_presence_of :year } | ||
it { is_expected.to validate_presence_of :month } | ||
it { is_expected.to validate_presence_of :day } | ||
|
||
context 'with an invalid date' do | ||
let(:attributes) { { year: '2017', month: '13', day: '25' } } | ||
|
||
it { is_expected.not_to be_valid } | ||
end | ||
|
||
context 'with no date parts set' do | ||
let(:attributes) { { year: '', month: '', day: '' } } | ||
|
||
it { is_expected.to be_valid } | ||
end | ||
end | ||
|
||
describe 'to_date' do | ||
it 'is serialized correctly' do | ||
expect(subject.to_date).to eq(Date.new(2017, 12, 25)) | ||
end | ||
|
||
context 'with no date parts set' do | ||
let(:attributes) { { year: '', month: '', day: '' } } | ||
|
||
it 'returns nil' do | ||
expect(subject.to_date).to be_nil | ||
end | ||
end | ||
|
||
context 'with an argument error' do | ||
before do | ||
allow(Date).to receive(:new).and_raise(ArgumentError) | ||
end | ||
|
||
it 'raise an ArgumentError' do | ||
expect(subject.to_date).to be_falsey | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Rejection, model: true do | ||
subject do | ||
described_class.new( | ||
reasons:, | ||
allowance_renews_on:, | ||
rejection_reason_detail: | ||
) | ||
end | ||
|
||
let(:reasons) { [Rejection::SLOT_UNAVAILABLE] } | ||
let(:rejection_reason_detail) { nil } | ||
let(:allowance_renews_on) do | ||
{ day: '12', month: '11', year: '2017' } | ||
end | ||
|
||
it { is_expected.to be_valid } | ||
|
||
describe 'validation' do | ||
|
||
it 'enforces the foreign key constraint' do | ||
expect { | ||
described_class.create!(visit_id: SecureRandom.uuid, reasons: [described_class::NOT_ON_THE_LIST]) | ||
}.to raise_exception(ActiveRecord::InvalidForeignKey) | ||
end | ||
|
||
context 'when rejecting a visit for any other reason' do | ||
let(:reasons) { [described_class::OTHER_REJECTION_REASON] } | ||
|
||
context 'with an explanation' do | ||
let(:rejection_reason_detail) { 'some reason' } | ||
|
||
it { is_expected.to be_valid } | ||
end | ||
|
||
context 'with no explanation' do | ||
let(:rejection_reason_detail) { nil } | ||
|
||
it { is_expected.to be_invalid } | ||
end | ||
end | ||
|
||
context 'when allowance renews on given date' do | ||
context 'when rejected for no allowance' do | ||
let(:reasons) { [described_class::NO_ALLOWANCE] } | ||
|
||
context 'with a valid date' do | ||
it 'is valid' do | ||
expect(subject).to be_valid | ||
end | ||
end | ||
|
||
context 'with a null date' do | ||
let(:allowance_renews_on) { nil } | ||
|
||
it 'is valid' do | ||
expect(subject).to be_valid | ||
end | ||
end | ||
|
||
context 'with an invalid accessible date' do | ||
let(:allowance_renews_on_attributes) do | ||
{ | ||
'allowance_renews_on(1i)' => '', | ||
'allowance_renews_on(2i)' => '', | ||
'allowance_renews_on(3i)' => '1' | ||
} | ||
end | ||
|
||
it 'is invalid' do | ||
subject.assign_attributes(allowance_renews_on_attributes) | ||
expect(subject).to be_invalid | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#reasons' do | ||
context 'with rejection reasons' do | ||
let(:reasons) do | ||
described_class::REASONS[0..rand(described_class::REASONS.length - 1)] | ||
end | ||
|
||
before do | ||
subject.reasons = reasons | ||
end | ||
|
||
context 'when the reason does not exists' do | ||
let(:reasons) { ['invalid_reason'] } | ||
|
||
it 'for an ivalid reason' do | ||
expect(subject).not_to be_valid | ||
expect(subject.errors.full_messages_for(:reasons)).to eq( | ||
['Reasons invalid_reason is not in the list'] | ||
) | ||
end | ||
end | ||
|
||
it 'returns an array of reasons' do | ||
expect(subject.reasons).to eq(reasons) | ||
end | ||
end | ||
end | ||
|
||
describe 'allowance_will_renew?' do | ||
it 'is true if there is an allowance_renews_on date' do | ||
subject.allowance_renews_on = Date.current | ||
expect(subject).to be_allowance_will_renew | ||
end | ||
|
||
it 'is false if these is no allowance_renews_on date' do | ||
subject.allowance_renews_on = '' | ||
expect(subject).not_to be_allowance_will_renew | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.