Skip to content

Commit

Permalink
Merge pull request #883 from alphagov/allow-restore-of-previously-pub…
Browse files Browse the repository at this point in the history
…lished-petitions

Allow petitions that have been taken down to be restored
  • Loading branch information
pixeltrix authored Nov 7, 2023
2 parents dd218c7 + 575f460 commit 149bd8f
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/models/petition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ def moderate(params)
when 'dormant'
update!(state: DORMANT_STATE)
when 'restore'
update!(state: SPONSORED_STATE)
update!(state: SPONSORED_STATE, open_at: nil, closed_at: nil)
else
errors.add :moderation, :blank
raise ActiveRecord::RecordNotSaved, "Unable to moderate petition"
Expand Down
32 changes: 16 additions & 16 deletions app/views/admin/moderation/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
<%= f.label :moderation_reject, "Reject", for: "petition_moderation_reject" %>
</div>
<% end %>
<% if f.object.restorable? %>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'restore' %>
<%= f.label :moderation_restore, "Restore", for: "petition_moderation_restore" %>
</div>
<% end %>
<% if f.object.flaggable? %>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'flag' %>
<%= f.label :moderation_flag, "Flag", for: "petition_moderation_flag" %>
</div>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'dormant' %>
<%= f.label :moderation_dormant, "Dormant", for: "petition_moderation_dormant" %>
</div>
<% end %>
<% end %>
<% if f.object.restorable? %>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'restore' %>
<%= f.label :moderation_restore, "Restore", for: "petition_moderation_restore" %>
</div>
<% end %>
<% if f.object.flaggable? %>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'flag' %>
<%= f.label :moderation_flag, "Flag", for: "petition_moderation_flag" %>
</div>
<div class="multiple-choice">
<%= f.radio_button :moderation, 'dormant' %>
<%= f.label :moderation_dormant, "Dormant", for: "petition_moderation_dormant" %>
</div>
<% end %>
<% end %>
Expand Down
19 changes: 19 additions & 0 deletions features/admin/takedown.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ Feature: Terry (or Sheila) takes down a petition
And I take down the petition with a reason code "Duplicate petition"
Then the petition is not available for signing
And I should not be able to take down the petition

Scenario: A sysadmin can restore a petition that has been taken down
Given I am logged in as a sysadmin
And a published petition has been taken down
When I visit the petition
Then the petition can no longer be rejected
And the petition can no longer be marked as dormant
But it can still be approved
And it can still be restored
When I restore to a sponsored state
Then the creator should not receive a notification email
And the petition is not available for searching or viewing
But the petition will still show up in the back-end reporting

Scenario: A moderator can't restore a petition that has been taken down
Given I am logged in as a moderator
And a published petition has been taken down
When I visit the petition
Then there are no moderation options
20 changes: 19 additions & 1 deletion features/step_definitions/moderation_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
expect(page).to have_no_field('Dormant', visible: false)
end

When(/^I revisit the petition$/) do
When(/^I (?:re)?visit the petition$/) do
visit admin_petition_url(@petition)
end

Expand Down Expand Up @@ -204,3 +204,21 @@
Then /^the petition should still be unmoderated$/ do
expect(@petition).not_to be_visible
end

Given(/^a published petition has been taken down$/) do
steps %Q(
When I view all petitions
And I follow "Mistakenly published petition"
And I take down the petition with a reason code "Offensive or a joke"
Then the petition is not available for searching or viewing
And I should not be able to take down the petition
)
end

Then(/^there are no moderation options$/) do
expect(page).to have_no_field('Approve', visible: false)
expect(page).to have_no_field('Reject', visible: false)
expect(page).to have_no_field('Restore', visible: false)
expect(page).to have_no_field('Flag', visible: false)
expect(page).to have_no_field('Dormant', visible: false)
end
96 changes: 96 additions & 0 deletions spec/models/petition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,102 @@
}.from('rejected').to('open')
end
end

context "when restoring a petition" do
shared_examples "restoring a rejected petition" do
it "restores it to the 'sponsored' state" do
expect {
petition.moderate(params)
}.to change {
petition.reload.state
}.from('rejected').to('sponsored')
end

it "removes the rejection" do
expect {
petition.moderate(params)
}.to change {
petition.reload.rejection
}.from(an_instance_of(Rejection)).to(nil)
end

it "resets the rejected_at timestamp" do
expect {
petition.moderate(params)
}.to change {
petition.reload.rejected_at
}.from(within(1.minute).of(rejected_at)).to(nil)
end
end

let(:params) do
{ moderation: "restore" }
end

let(:rejected_at) do
2.weeks.ago
end

context "that was previously rejected" do
let(:petition) do
FactoryBot.create(
:rejected_petition,
rejected_at: rejected_at
)
end

it_behaves_like "restoring a rejected petition"
end

context "that was previously published and taken down" do
let(:petition) do
FactoryBot.create(
:rejected_petition,
rejected_at: rejected_at,
open_at: 5.months.ago
)
end

it_behaves_like "restoring a rejected petition"

it "resets the open_at timestamp" do
expect {
petition.moderate(params)
}.to change {
petition.reload.open_at
}.from(within(1.minute).of(5.months.ago)).to(nil)
end
end

context "that was previously published, closed and taken down" do
let(:petition) do
FactoryBot.create(
:rejected_petition,
rejected_at: rejected_at,
open_at: 7.months.ago,
closed_at: 1.month.ago
)
end

it_behaves_like "restoring a rejected petition"

it "resets the open_at timestamp" do
expect {
petition.moderate(params)
}.to change {
petition.reload.open_at
}.from(within(1.minute).of(7.months.ago)).to(nil)
end

it "resets the closed_at timestamp" do
expect {
petition.moderate(params)
}.to change {
petition.reload.closed_at
}.from(within(1.minute).of(1.month.ago)).to(nil)
end
end
end
end

describe '#publish!' do
Expand Down

0 comments on commit 149bd8f

Please sign in to comment.