Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conviction check to handle grace window #296

Merged
merged 8 commits into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
irisfaraway marked this conversation as resolved.
Show resolved Hide resolved
# 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