Skip to content

Commit

Permalink
covering in tests, to cover models included services/booking_responder
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphwortatmoj committed May 7, 2024
1 parent 6d84a8b commit ae5b59b
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 73 deletions.
56 changes: 56 additions & 0 deletions app/models/cancellation.rb
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
11 changes: 11 additions & 0 deletions app/models/concerns/principal_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,15 @@ module PrincipalVisitor
def principal_visitor
visitors.first
end

def principal_visitor_id
principal_visitor&.id
end

def principal_visitor_id=(visitor_id)
vst_id = visitor_ids.detect { |v_id| v_id == visitor_id }
if vst_id
@principal_visitor = Staff::Visitor.find(vst_id)
end
end
end
7 changes: 0 additions & 7 deletions app/models/staff/visit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,6 @@ def acceptance_message
visit_state_change_id: visit_state_changes.booked.pick(:id))
end

def rejection_message
messages
.where.not(visit_state_change_id: nil)
.find_by(
visit_state_change_id: visit_state_changes.rejected.pick(:id))
end

def last_visit_state
visit_state_changes.order('created_at desc').first
end
Expand Down
16 changes: 0 additions & 16 deletions app/services/booking_responder/accept.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/services/booking_responder/cancel.rb

This file was deleted.

32 changes: 0 additions & 32 deletions app/services/booking_responder/reject.rb

This file was deleted.

1 change: 1 addition & 0 deletions spec/factories/staff_visits.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FactoryBot.define do
factory :staff_visit, class: 'Staff::Visit' do
association :prison, factory: :staff_prison

prisoner

contact_email_address do
Expand Down
51 changes: 51 additions & 0 deletions spec/models/accessible_date_spec.rb
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
118 changes: 118 additions & 0 deletions spec/models/rejection_spec.rb
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
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
config.use_transactional_fixtures = true
config.include FactoryBot::Syntax::Methods
config.include ActiveSupport::Testing::TimeHelpers
config.include StaffResponseHelper

config.infer_spec_type_from_file_location!

Expand Down
Loading

0 comments on commit ae5b59b

Please sign in to comment.