diff --git a/app/helpers/action_links_helper.rb b/app/helpers/action_links_helper.rb index 43fef6c5..f20f581e 100644 --- a/app/helpers/action_links_helper.rb +++ b/app/helpers/action_links_helper.rb @@ -72,12 +72,15 @@ def display_confirmation_letter_link_for?(resource) end def display_renew_links_for?(resource) - can?(:renew, resource) && - resource.renewable? + resource.is_a?(WasteExemptionsEngine::Registration) && + can?(:renew, resource) && + resource.renewable? && + (resource.contact_email.present? || resource.applicant_email.present?) end def display_renew_window_closed_text_for?(resource) - can?(:renew, resource) && + resource.present? && + can?(:renew, resource) && resource.in_renewable_state? && resource.past_renewal_window? end diff --git a/spec/forms/modify_expiry_date_form_spec.rb b/spec/forms/modify_expiry_date_form_spec.rb index 47981d59..778a2b3e 100644 --- a/spec/forms/modify_expiry_date_form_spec.rb +++ b/spec/forms/modify_expiry_date_form_spec.rb @@ -36,15 +36,80 @@ )).to be(true) end - it "updates the registration date for each registration_exemption" do - form.submit( - date_day: valid_modified_date.day.to_s, - date_month: valid_modified_date.month.to_s, - date_year: valid_modified_date.year.to_s - ) - - registration.registration_exemptions.each do |exemption| - expect(exemption.expires_on).to eq(valid_modified_date) + context "with specific exemption types" do + let(:modified_exemption) { registration.registration_exemptions.first } + let(:original_expiry_date) { modified_exemption.expires_on } + + before do + form.submit( + date_day: valid_modified_date.day.to_s, + date_month: valid_modified_date.month.to_s, + date_year: valid_modified_date.year.to_s + ) + end + + shared_examples "all exemptions updated" do + it "updates the expiry date for each registration_exemption" do + registration.registration_exemptions.each do |exemption| + expect(exemption.expires_on).to eq(valid_modified_date) + end + end + end + + shared_examples "no exemptions updated" do + it "does not update the expiry date for any registration_exemption" do + registration.registration_exemptions.each do |exemption| + expect(exemption.expires_on).to eq(original_expiry_date) + end + end + end + + shared_examples "only one exemption updated" do + it "updates the expiry date for the first exemption" do + expect(modified_exemption.expires_on).to eq valid_modified_date + end + end + + it "does not update the expiry date for the other exemptions" do + registration.registration_exemptions.each do |re| + next if re == modified_exemption + + expect(re.expires_on).to eq original_expiry_date + end + end + + context "with all active exemptions" do + it_behaves_like "all exemptions updated", :active + end + + context "with all expired exemptions" do + before { registration.registration_exemptions.update(state: "expired") } + + it_behaves_like "all exemptions updated" + end + + context "with all ceased exemptions" do + before { registration.registration_exemptions.update(state: "ceased") } + + it_behaves_like "no exemptions updated" + end + + context "with all revoked exemptions" do + before { registration.registration_exemptions.update(state: "revoked") } + + it_behaves_like "no exemptions updated" + end + + context "with active and ceased exemptions" do + before { registration.registration_exemptions.first.update(state: "ceased") } + + it_behaves_like "only one exemption updated" + end + + context "with active and revoked exemptions" do + before { registration.registration_exemptions.first.update(state: "revoked") } + + it_behaves_like "only one exemption updated" end end end diff --git a/spec/helpers/action_links_helper_spec.rb b/spec/helpers/action_links_helper_spec.rb index ad9edd6a..0a5966f7 100644 --- a/spec/helpers/action_links_helper_spec.rb +++ b/spec/helpers/action_links_helper_spec.rb @@ -109,26 +109,20 @@ context "when the user has permission" do before { allow(helper).to receive(:can?).and_return(true) } - it "returns true" do - expect(helper.display_resume_link_for?(resource)).to be(true) - end + it { expect(helper.display_resume_link_for?(resource)).to be(true) } end context "when the user does not have permission" do before { allow(helper).to receive(:can?).and_return(false) } - it "returns false" do - expect(helper.display_resume_link_for?(resource)).to be(false) - end + it { expect(helper.display_resume_link_for?(resource)).to be(false) } end end context "when the resource is not a new_registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_resume_link_for?(resource)).to be(false) - end + it { expect(helper.display_resume_link_for?(resource)).to be(false) } end end @@ -141,26 +135,20 @@ context "when the user has permission to update a registration" do let(:can) { true } - it "returns true" do - expect(helper.display_edit_link_for?(resource)).to be(true) - end + it { expect(helper.display_edit_link_for?(resource)).to be(true) } end context "when the user does not have permission to update a registration" do let(:can) { false } - it "returns false" do - expect(helper.display_edit_link_for?(resource)).to be(false) - end + it { expect(helper.display_edit_link_for?(resource)).to be(false) } end end context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_edit_link_for?(resource)).to be(false) - end + it { expect(helper.display_edit_link_for?(resource)).to be(false) } end end @@ -173,26 +161,20 @@ context "when the user has permission to update a registration" do let(:can) { true } - it "returns true" do - expect(helper.display_edit_expiry_date_link_for?(resource)).to be(true) - end + it { expect(helper.display_edit_expiry_date_link_for?(resource)).to be(true) } end context "when the user does not have permission to update a registration" do let(:can) { false } - it "returns false" do - expect(helper.display_edit_expiry_date_link_for?(resource)).to be(false) - end + it { expect(helper.display_edit_expiry_date_link_for?(resource)).to be(false) } end end context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_edit_expiry_date_link_for?(resource)).to be(false) - end + it { expect(helper.display_edit_expiry_date_link_for?(resource)).to be(false) } end end @@ -212,17 +194,13 @@ context "when the user has permission to deregister a registration" do let(:can) { true } - it "returns true" do - expect(helper.display_deregister_link_for?(resource)).to be(true) - end + it { expect(helper.display_deregister_link_for?(resource)).to be(true) } end context "when the user does not have permission to deregister a registration" do let(:can) { false } - it "returns false" do - expect(helper.display_deregister_link_for?(resource)).to be(false) - end + it { expect(helper.display_deregister_link_for?(resource)).to be(false) } end end @@ -233,17 +211,13 @@ registration end - it "returns false" do - expect(helper.display_deregister_link_for?(resource)).to be(false) - end + it { expect(helper.display_deregister_link_for?(resource)).to be(false) } end context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_deregister_link_for?(resource)).to be(false) - end + it { expect(helper.display_deregister_link_for?(resource)).to be(false) } end end @@ -258,29 +232,19 @@ context "when the user has permission to deregister a registration" do let(:can) { true } - it "returns true" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(true) - end - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(true) } - context "when the user does not have permission to deregister a registration" do - let(:can) { false } + context "when the resource is not inside the renewal window" do + before { allow(resource).to receive(:in_renewal_window?).and_return(true) } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end end - context "when the resource is not inside the renewal window" do - let(:can) { true } - - before do - allow(resource).to receive(:in_renewal_window?).and_return(true) - end + context "when the user does not have permission to deregister a registration" do + let(:can) { false } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end end @@ -289,33 +253,25 @@ before { resource.referred_registration = create(:registration) } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end context "when the resource is a ceased registration" do let(:resource) { create(:registration, :with_ceased_exemptions) } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end context "when the resource is an expired registration" do let(:resource) { create(:registration, registration_exemptions: build_list(:registration_exemption, 3, :expired)) } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) - end + it { expect(helper.display_resend_deregistration_email_link_for?(resource)).to be(false) } end end @@ -324,9 +280,7 @@ let(:resource) { create(:registration) } context "when the registration is active" do - it "returns true" do - expect(helper.display_confirmation_letter_link_for?(resource)).to be(true) - end + it { expect(helper.display_confirmation_letter_link_for?(resource)).to be(true) } end context "when the resource is an inactive registration" do @@ -336,18 +290,14 @@ registration end - it "returns false" do - expect(helper.display_confirmation_letter_link_for?(resource)).to be(false) - end + it { expect(helper.display_confirmation_letter_link_for?(resource)).to be(false) } end end context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_confirmation_letter_link_for?(resource)).to be(false) - end + it { expect(helper.display_confirmation_letter_link_for?(resource)).to be(false) } end end @@ -359,6 +309,8 @@ before { allow(resource).to receive(:in_renewal_window?).and_return(true) } context "when the user has permission to renew" do + let(:can) { true } + before { allow(helper).to receive(:can?).with(:renew, resource).and_return(can) } context "when the resource has active exemptions" do @@ -369,11 +321,7 @@ end end - let(:can) { true } - - it "returns true" do - expect(helper.display_renew_links_for?(resource)).to be(true) - end + it { expect(helper.display_renew_links_for?(resource)).to be(true) } end context "when the resource has expired exemptions" do @@ -384,14 +332,20 @@ end end - let(:can) { true } + it { expect(helper.display_renew_links_for?(resource)).to be(true) } + end - it "returns true" do - expect(helper.display_renew_links_for?(resource)).to be(true) + context "when the resource has both ceased and active exemptions" do + before do + re = resource.registration_exemptions.first + re.state = "ceased" + re.save! end + + it { expect(helper.display_renew_links_for?(resource)).to be(true) } end - context "when the resource has no active or expired exemptions" do + context "when the resource has ceased exemptions only" do before do resource.registration_exemptions.each do |re| re.state = "ceased" @@ -399,38 +353,69 @@ end end - let(:can) { true } + it { expect(helper.display_renew_links_for?(resource)).to be(false) } + end - it "returns false" do - expect(helper.display_renew_links_for?(resource)).to be(false) + context "when the resource has both revoked and active exemptions" do + before do + re = resource.registration_exemptions.first + re.state = "revoked" + re.save! end + + it { expect(helper.display_renew_links_for?(resource)).to be(true) } end - end - context "when the user does not have permission to renew" do - let(:can) { false } + context "when the resource has revoked exemptions only" do + before do + resource.registration_exemptions.each do |re| + re.state = "revoked" + re.save! + end + end - it "returns false" do - expect(helper.display_renew_links_for?(resource)).to be(false) + it { expect(helper.display_renew_links_for?(resource)).to be(false) } + end + + context "when the resource has no contact email address but has an applicant email address" do + before do + resource.contact_email = nil + resource.applicant_email = Faker::Internet.email + resource.save! + end + + it { expect(helper.display_renew_links_for?(resource)).to be(true) } + end + + context "when the resource has neither contact email address nor applicant email address" do + before do + resource.contact_email = nil + resource.applicant_email = nil + resource.save! + end + + it { expect(helper.display_renew_links_for?(resource)).to be(false) } + end + + context "when the resource is not in the renewal window" do + before { allow(resource).to receive(:in_renewal_window?).and_return(false) } + + it { expect(helper.display_renew_links_for?(resource)).to be(false) } end end - end - context "when the resource is not in the renewal window" do - before { allow(resource).to receive(:in_renewal_window?).and_return(false) } + context "when the user does not have permission to renew" do + let(:can) { false } - it "returns false" do - expect(helper.display_renew_links_for?(resource)).to be(false) + it { expect(helper.display_renew_links_for?(resource)).to be(false) } end end end context "when the resource is not a registration" do - let(:resource) { nil } + let(:resource) { create(:new_registration) } - it "returns false" do - expect(helper.display_renew_links_for?(resource)).to be(false) - end + it { expect(helper.display_renew_links_for?(resource)).to be(false) } end end @@ -442,7 +427,7 @@ before { allow(resource).to receive(:past_renewal_window?).and_return(true) } context "when the user has permission to renew" do - before { allow(helper).to receive(:can?).with(:renew, resource).and_return(can) } + before { allow(helper).to receive(:can?).with(:renew, resource).and_return(true) } context "when the resource has active exemptions" do before do @@ -452,11 +437,7 @@ end end - let(:can) { true } - - it "returns true" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(true) - end + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(true) } end context "when the resource has expired exemptions" do @@ -467,11 +448,7 @@ end end - let(:can) { true } - - it "returns true" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(true) - end + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(true) } end context "when the resource has no active or expired exemptions" do @@ -482,28 +459,20 @@ end end - let(:can) { true } - - it "returns false" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) - end + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) } end - end - context "when the user does not have permission to renew" do - let(:can) { false } + context "when the resource is past the renewal window" do + before { allow(resource).to receive(:past_renewal_window?).and_return(true) } - it "returns false" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(true) } end end - end - context "when the resource is not in the renewal window" do - before { allow(resource).to receive(:in_renewal_window?).and_return(false) } + context "when the user does not have permission to renew" do + let(:can) { false } - it "returns false" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) } end end end @@ -511,9 +480,7 @@ context "when the resource is not a registration" do let(:resource) { nil } - it "returns false" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) - end + it { expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) } end end @@ -525,59 +492,46 @@ context "when the user has permission to renew" do let(:can) { true } + let(:in_renewal_window) { true } - before do - allow(resource).to receive(:in_renewal_window?).and_return(in_renewal_window) - end + before { allow(resource).to receive(:in_renewal_window?).and_return(in_renewal_window) } context "when the resource is in the renewal window" do let(:in_renewal_window) { true } - before do - allow(resource).to receive(:already_renewed?).and_return(already_renewed) - end + before { allow(resource).to receive(:already_renewed?).and_return(already_renewed) } context "when the resource is already renewed" do let(:already_renewed) { true } - it "returns true" do - expect(helper.display_already_renewed_text_for?(resource)).to be(true) - end + it { expect(helper.display_already_renewed_text_for?(resource)).to be(true) } end context "when the resource is not already renewed" do let(:already_renewed) { false } - it "returns false" do - expect(helper.display_already_renewed_text_for?(resource)).to be(false) - end + it { expect(helper.display_already_renewed_text_for?(resource)).to be(false) } end end context "when the resource is not in the renewal window" do let(:in_renewal_window) { false } - it "returns false" do - expect(helper.display_already_renewed_text_for?(resource)).to be(false) - end + it { expect(helper.display_already_renewed_text_for?(resource)).to be(false) } end end context "when the user has no permission to renew" do let(:can) { false } - it "returns false" do - expect(helper.display_already_renewed_text_for?(resource)).to be(false) - end + it { expect(helper.display_already_renewed_text_for?(resource)).to be(false) } end end context "when the resource is not a registration" do - let(:resource) { nil } + let(:resource) { create(:new_registration) } - it "returns false" do - expect(helper.display_renew_window_closed_text_for?(resource)).to be(false) - end + it { expect(helper.display_already_renewed_text_for?(resource)).to be(false) } end end @@ -585,48 +539,34 @@ context "when the resource is a registration" do let(:resource) { create(:registration) } - before do - allow(helper).to receive(:can?).with(:update, resource).and_return(can) - end + before { allow(helper).to receive(:can?).with(:update, resource).and_return(can) } context "when the user has permission to update a registration" do let(:can) { true } context "when the resource is a limited company or limited liability partnership" do - before do - allow(resource).to receive(:company_no_required?).and_return(true) - end + before { allow(resource).to receive(:company_no_required?).and_return(true) } - it "returns true" do - expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(true) - end + it { expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(true) } end context "when the resource is neither a limited company nor a limited liability partnership" do - before do - allow(resource).to receive(:company_no_required?).and_return(false) - end + before { allow(resource).to receive(:company_no_required?).and_return(false) } - it "returns false" do - expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) - end + it { expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) } end - end - context "when the user doesn't have permission to update a registration" do - let(:can) { false } + context "when the resourse is not a registration" do + let(:resource) { nil } - it "returns false" do - expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) + it { expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) } end end - end - context "when the resourse is not a registration" do - let(:resource) { nil } + context "when the user doesn't have permission to update a registration" do + let(:can) { false } - it "returns false" do - expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) + it { expect(helper.display_refresh_registered_company_name_link_for?(resource)).to be(false) } end end end