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

Allow moderators to start anonymising as specified #1084

Merged
merged 1 commit into from
Dec 2, 2024
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
24 changes: 21 additions & 3 deletions app/models/parliament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion config/locales/activerecord.en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion features/admin/anonymize_petitions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions features/step_definitions/common_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions spec/controllers/admin/parliaments_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down