Skip to content

Commit

Permalink
Fix conviction check to handle grace window (#296)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WC-512

During testing of the grace window it was spotted that if a renewal was expired, and it matched during conviction checks, then it could not be continued because the button to access convictions approvals did not display.

This was because `TransientRegistration.pending_manual_conviction_check?` had a guard clause that checked the status was `ACTIVE`. This will no longer be the case for registrations which are EXPIRED but in the grace window, hence we change it to use `may_renew?`.

We also learnt doing this that `may_renew?` will only work if called on a `metaData` object that is linked to a registration, because of the call to `registration.identifier` in `CanChangeRegistrationStatus.renewal_application_submitted?`. Hence we had to tweak the tests and code to account for this.

Also included

- some fixes which resolve tests that failed locally where the grace window was set, that didn't fail in Travis-CI where it wasn't
- resolution of some rubocop violations (now rubocop is working again)
  • Loading branch information
Cruikshanks authored Nov 7, 2018
1 parent 80a323f commit b535c6c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module WasteCarriersEngine
module CanChangeRegistrationStatus
extend ActiveSupport::Concern
Expand Down Expand Up @@ -144,11 +146,13 @@ def registered_in_gmt_and_expires_in_bst?

def registered_in_daylight_savings?
return true if registration.metaData.date_registered.in_time_zone("London").dst?

false
end

def expires_in_daylight_savings?
return true if registration.expires_on.in_time_zone("London").dst?

false
end
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/waste_carriers_engine/transient_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def pending_worldpay_payment?
end

def pending_manual_conviction_check?
return false unless metaData.ACTIVE?
registration = Registration.where(reg_identifier: reg_identifier).first
return false unless registration.metaData.may_renew?
renewal_application_submitted? && conviction_check_required?
end

Expand Down
36 changes: 24 additions & 12 deletions spec/models/waste_carriers_engine/registration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "rails_helper"

module WasteCarriersEngine
Expand Down Expand Up @@ -308,7 +310,7 @@ module WasteCarriersEngine
build(:registration,
:has_required_data,
key_people: [key_person_a,
key_person_b])
key_person_b])
end

it "is valid" do
Expand Down Expand Up @@ -451,15 +453,21 @@ module WasteCarriersEngine
end
end

context "when the registration expiration date is today" do
context "when the registration expiration date was three days ago, it cannot be renewed today" do
before { allow(Rails.configuration).to receive(:grace_window).and_return(3) }

let(:registration) { build(:registration, :is_active, expires_on: Date.today) }

it "cannot be renewed" do
expect(registration.metaData).to_not allow_event :renew
it "cannot be renewed in 3 days time" do
Timecop.freeze(registration.expires_on + 3.days) do
expect(registration.metaData).to_not allow_event :renew
end
end
end

context "when the registration was created in BST and expires in GMT" do
before { allow(Rails.configuration).to receive(:grace_window).and_return(3) }

let(:registration) { create(:registration, :has_required_data, :is_active, expires_on: 3.years.from_now) }
# Registration is made during British Summer Time (BST)
# UK local time is 00:30 on 28 March 2017
Expand All @@ -485,16 +493,19 @@ module WasteCarriersEngine
expect(registration.metaData).to allow_event :renew
end

it "expires when it reaches the expiry date in the UK" do
# Skip ahead to the start of the day a reg should expire
Timecop.freeze(Time.find_zone("London").local(2020, 3, 28, 0, 1))
it "cannot be renewed when it reaches the expiry date plus 'grace window' in the UK" do
# Skip ahead to the start of the day a reg should expire, plus the
# grace window
Timecop.freeze(Time.find_zone("London").local(2020, 3, 31, 0, 1))
# GMT is now in effect (not BST)
# UK local time & UTC are both 00:01 on 28 March 2020
expect(registration.metaData).to_not allow_event :renew
end
end

context "when the registration was created in GMT and expires in BST" do
before { allow(Rails.configuration).to receive(:grace_window).and_return(3) }

let(:registration) { create(:registration, :has_required_data, :is_active, expires_on: 3.years.from_now) }
# Registration is made in during Greenwich Mean Time (GMT)
# UK local time & UTC are both 23:30 on 27 October 2015
Expand All @@ -520,9 +531,10 @@ module WasteCarriersEngine
expect(registration.metaData).to allow_event :renew
end

it "expires when it reaches the expiry date in the UK" do
# Skip ahead to the start of the day a reg should expire
Timecop.freeze(Time.find_zone("London").local(2018, 10, 27, 0, 1))
it "cannot be renewed when it reaches the expiry date plus 'grace window' in the UK" do
# Skip ahead to the start of the day a reg should expire, plus the
# grace window
Timecop.freeze(Time.find_zone("London").local(2018, 10, 30, 0, 1))
# BST is now in effect (not GMT)
# UK local time is 00:01 on 27 October 2018
# UTC time is 23:01 on 26 October 2018
Expand All @@ -545,14 +557,14 @@ module WasteCarriersEngine
it "updates the registration's date_registered" do
Timecop.freeze do
registration.metaData.renew
expect(registration.metaData.date_registered).to eq(DateTime.current)
expect(registration.metaData.date_registered).to eq(Time.current)
end
end

it "updates the registration's date_activated" do
Timecop.freeze do
registration.metaData.renew
expect(registration.metaData.date_activated).to eq(DateTime.current)
expect(registration.metaData.date_activated).to eq(Time.current)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ module WasteCarriersEngine
end

context "when the registration is not active" do
before do
transient_registration.metaData.revoke!
let(:revoked_transient_registration) do
registration = create(:registration, :has_required_data, :is_revoked)
TransientRegistration.new(reg_identifier: registration.reg_identifier)
end

it "returns false" do
expect(transient_registration.pending_manual_conviction_check?).to eq(false)
expect(revoked_transient_registration.pending_manual_conviction_check?).to eq(false)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module WasteCarriersEngine
end

context "when the registration cannot be renewed" do
before(:each) { registration.update_attributes(expires_on: Date.today + Rails.configuration.grace_window) }
before(:each) { registration.update_attributes(expires_on: Date.today - Rails.configuration.grace_window) }

it "redirects to the unrenewable error page" do
get new_renewal_start_form_path(registration[:reg_identifier])
Expand Down Expand Up @@ -208,7 +208,7 @@ module WasteCarriersEngine
end

context "when the registration cannot be renewed" do
before(:each) { registration.update_attributes(expires_on: Date.today + Rails.configuration.grace_window) }
before(:each) { registration.update_attributes(expires_on: Date.today - Rails.configuration.grace_window) }

it "redirects to the unrenewable error page" do
get new_renewal_start_form_path(registration[:reg_identifier])
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples/request_post_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
valid_params[:reg_identifier] = transient_registration.reg_identifier
# But the registration is now expired
registration = WasteCarriersEngine::Registration.where(reg_identifier: transient_registration.reg_identifier).first
registration.update_attributes(expires_on: Date.today + Rails.configuration.grace_window)
registration.update_attributes(expires_on: Date.today - Rails.configuration.grace_window)
end

it "does not update the transient registration, including workflow_state" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
before do
# Params are otherwise valid, but the registration is now expired
registration = WasteCarriersEngine::Registration.where(reg_identifier: transient_registration.reg_identifier).first
registration.update_attributes(expires_on: Date.today + Rails.configuration.grace_window)
registration.update_attributes(expires_on: Date.today - Rails.configuration.grace_window)
end

it "does not update the transient registration, including workflow_state" do
Expand Down

0 comments on commit b535c6c

Please sign in to comment.