Skip to content

Commit ec76abd

Browse files
authored
Merge pull request #667 from alphagov/make-verify-idempotent
Make signature verification idempotent
2 parents e65dfac + 88ebc83 commit ec76abd

7 files changed

Lines changed: 20 additions & 35 deletions

File tree

app/controllers/signatures_controller.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,17 @@ def create
4848
end
4949

5050
def signed
51-
if @signature.seen_signed_confirmation_page?
52-
redirect_to petition_url(@petition)
53-
else
51+
unless @signature.seen_signed_confirmation_page?
5452
@signature.mark_seen_signed_confirmation_page!
53+
end
5554

56-
respond_to do |format|
57-
format.html
58-
end
55+
respond_to do |format|
56+
format.html
5957
end
6058
end
6159

6260
def verify
63-
if @signature.validated?
64-
flash[:notice] = "You’ve already signed this petition"
65-
else
61+
unless @signature.validated?
6662
@signature.validate!
6763
end
6864

app/controllers/sponsors_controller.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,13 @@ class SponsorsController < SignaturesController
88
before_action :validate_creator, only: [:new]
99

1010
def verify
11-
if @signature.validated?
12-
flash[:notice] = "You’ve already supported this petition"
13-
else
11+
unless @signature.validated?
1412
@signature.validate!
1513
end
1614

1715
redirect_to signed_sponsor_url(@signature, token: @signature.perishable_token)
1816
end
1917

20-
def signed
21-
unless @signature.seen_signed_confirmation_page?
22-
@signature.mark_seen_signed_confirmation_page!
23-
end
24-
25-
respond_to do |format|
26-
format.html
27-
end
28-
end
29-
3018
private
3119

3220
def retrieve_petition

features/laura_signs_charlies_petition_as_a_sponsor.feature

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ Feature: As Laura, a sponsor of my friend Charlie's petition
8080
When I have sponsored a petition
8181
When I confirm my email address
8282
Then I should see a heading called "Thanks"
83+
And I should see "Your signature has been added to this petition as a supporter"
8384
And I should have fully signed the petition as a sponsor
84-
When I confirm my email address
85+
When I confirm my email address again
8586
Then I should see a heading called "Thanks"
86-
And I should see "You’ve already supported this petition"
87+
And I should see "Your signature has been added to this petition as a supporter"
8788
And I should see /This petition needs [0-9]+ supporters to go live/

features/step_definitions/signature_steps.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,15 @@
2020
expect(find_email(address, with_subject: "Please confirm your email address")).to be_present
2121
end
2222

23-
When(/^I confirm my email address$/) do
23+
When(/^I confirm my email address(?: again)?$/) do
2424
steps %Q(
2525
And I open the email with subject "Please confirm your email address"
2626
When I click the first link in the email
2727
)
2828
end
2929

3030
def should_be_signature_count_of(count)
31-
visit petition_url(@petition)
32-
expect(page).to have_css("p.signature-count-number", :text => count.to_s + " signatures")
31+
expect(Petition.find(@petition.id).signature_count).to eq(count)
3332
end
3433

3534
Then /^I should have signed the petition$/ do

features/suzie_signs_a_petition.feature

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ Feature: Suzie signs a petition
104104
When I fill in my details and sign a petition
105105
And I confirm my email address
106106
And I should see "2 signatures"
107+
And I should see "We've added your signature to the petition"
107108
And I can click on a link to return to the petition
108109
And I should have signed the petition
109-
When I confirm my email address
110+
When I confirm my email address again
110111
And I should see "2 signatures"
112+
And I should see "We've added your signature to the petition"
111113
And I can click on a link to return to the petition
112-
Then I should see that I have already signed the petition
113114

114115
Scenario: Eric clicks the link shared to him by Suzie
115116
When Suzie has already signed the petition and validated her email

spec/controllers/signatures_controller_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@
595595
context "and the signature has already been validated" do
596596
let(:signature) { FactoryBot.create(:validated_signature, petition: petition) }
597597

598-
it "sets the flash :notice message" do
599-
expect(flash[:notice]).to eq("You’ve already signed this petition")
598+
it "doesn't set the flash :notice message" do
599+
expect(flash[:notice]).to be_nil
600600
end
601601
end
602602
end
@@ -767,8 +767,8 @@
767767
context "and the signature has already seen the confirmation page" do
768768
let(:signature) { FactoryBot.create(:validated_signature, petition: petition) }
769769

770-
it "redirects to the petition page" do
771-
expect(response).to redirect_to("/petitions/#{petition.id}")
770+
it "doesn't redirect to the petition page" do
771+
expect(response).not_to redirect_to("/petitions/#{petition.id}")
772772
end
773773
end
774774
end

spec/controllers/sponsors_controller_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,8 @@
645645
context "and the signature has already been validated" do
646646
let(:signature) { FactoryBot.create(:validated_signature, petition: petition, sponsor: true) }
647647

648-
it "sets the flash :notice message" do
649-
expect(flash[:notice]).to eq("You’ve already supported this petition")
648+
it "doesn't set the flash :notice message" do
649+
expect(flash[:notice]).to be_nil
650650
end
651651
end
652652

0 commit comments

Comments
 (0)