From 0df26873e4ffd2f160c3f02154628e1a0a23c966 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Mon, 2 Dec 2024 13:26:32 +0000 Subject: [PATCH] Allow moderators to start anonymising as specified A miscommunication meant that the anonymisation process couldn't be started automatically as parliament hadn't reopened for six months. The actual date specified was six months after dissolution. --- app/models/parliament.rb | 24 ++++++++++++++++--- config/locales/activerecord.en-GB.yml | 3 ++- features/admin/anonymize_petitions.feature | 2 +- features/step_definitions/common_steps.rb | 10 ++++++++ .../admin/parliaments_controller_spec.rb | 16 ++++++++++--- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/models/parliament.rb b/app/models/parliament.rb index 6ccdeed30..4a5c21423 100644 --- a/app/models/parliament.rb +++ b/app/models/parliament.rb @@ -27,6 +27,10 @@ def current where(archived_at: nil).order(created_at: :asc) end + def previous(opening_at) + where(opening_at: ...opening_at).order(opening_at: :desc).first + end + def government instance.government end @@ -198,8 +202,10 @@ def defaults validate on: :anonymize_petitions do errors.add(:opening_at, :blank) unless opening_at? - if opening_at? - errors.add(:opening_at, :too_soon) unless opening_at < 6.months.ago + if previous_dissolution_at.present? + errors.add(:opening_at, :too_soon) unless previous_dissolution_at < 6.months.ago + else + errors.add(:opening_at, :previous_blank) end end @@ -229,6 +235,18 @@ def period? period.present? end + def previous + return @previous if defined?(@previous) + + if previous_parliament = self.class.previous(opening_at) + @previous = self.class.previous(opening_at) + end + end + + def previous_dissolution_at + previous.present? && previous.dissolution_at + end + def dissolving?(now = Time.current) dissolution_at? && dissolution_at > now end @@ -272,7 +290,7 @@ def start_archiving!(now = Time.current) end end - def start_anonymizing!(now = Time.current) + def start_anonymizing! if can_anonymize_petitions? Archived::AnonymizePetitionsJob.set(wait_until: midnight).perform_later(midnight.iso8601) end diff --git a/config/locales/activerecord.en-GB.yml b/config/locales/activerecord.en-GB.yml index 183af4b2c..75d645d71 100644 --- a/config/locales/activerecord.en-GB.yml +++ b/config/locales/activerecord.en-GB.yml @@ -104,7 +104,8 @@ en-GB: blank: Please enter a notification cut-off opening_at: blank: Please enter the date and time when parliament will reopen - too_soon: Please wait until six months after reopening before anonymizing petitions + previous_blank: There are no previous parliaments to anonymize + too_soon: Please wait until six months after dissolution before anonymizing petitions registration_closed_at: blank: Please enter the date and time at which voter registration closes show_dissolution_notification: diff --git a/features/admin/anonymize_petitions.feature b/features/admin/anonymize_petitions.feature index a5e5e3afd..bd9944e06 100644 --- a/features/admin/anonymize_petitions.feature +++ b/features/admin/anonymize_petitions.feature @@ -9,7 +9,7 @@ Feature: An admin anonymizes petitions @javascript Scenario: Admin anonymizes petitions 6 months after parliament closes Given 2 archived petitions exist - And 6 months has passed since parliament opened + And 6 months has passed since the previous parliament dissolved When I go to the admin parliament page And I press "Anonymize petitions" And I accept the alert diff --git a/features/step_definitions/common_steps.rb b/features/step_definitions/common_steps.rb index 7d874485d..895a0c08f 100644 --- a/features/step_definitions/common_steps.rb +++ b/features/step_definitions/common_steps.rb @@ -44,6 +44,16 @@ Parliament.update!(opening_at: number.months.ago) end +Given('{int} months has passed since the previous parliament dissolved') do |number| + Parliament.instance.previous.update!( + dissolution_at: number.months.ago, + dissolution_heading: "Parliament is dissolving", + dissolution_message: "This means all petitions will close in 2 weeks", + dissolved_heading: "Parliament has been dissolved", + dissolved_message: "All petitions have been closed" + ) +end + Given(/^the request is not local$/) do page.driver.options[:headers] = { "REMOTE_ADDR" => "192.168.1.128" } end diff --git a/spec/controllers/admin/parliaments_controller_spec.rb b/spec/controllers/admin/parliaments_controller_spec.rb index c3724b956..ed7e83e0d 100644 --- a/spec/controllers/admin/parliaments_controller_spec.rb +++ b/spec/controllers/admin/parliaments_controller_spec.rb @@ -374,9 +374,17 @@ context "when clicking the 'Anonymize petitions' button" do let(:anonymized_at) { nil } + let(:dissolution_at) { 7.months.ago } + + let(:archived_parliament) do + FactoryBot.create(:parliament, :dissolved, :archived, + opening_at: 4.years.ago, + dissolution_at: dissolution_at + ) + end before do - FactoryBot.create(:archived_petition, anonymized_at: anonymized_at) + FactoryBot.create(:archived_petition, parliament: archived_parliament, anonymized_at: anonymized_at) patch :update, params: { parliament: params, button: "anonymize_petitions" } end @@ -401,9 +409,11 @@ end end - context "and the params are valid, but the site has been reopened for less than six months" do + context "and the params are valid, but it is less than 6 months since dissolution" do + let(:dissolution_at) { 5.months.ago } + let :params do - { government: "Conservative", opening_at: 5.months.ago.iso8601 } + { government: "Conservative", opening_at: 2.months.ago.iso8601 } end it_behaves_like "an invalid request"