Skip to content

Commit

Permalink
Merge pull request #890 from alphagov/add-list-headers
Browse files Browse the repository at this point in the history
Add support for List-Unsubscribe-Post email header
  • Loading branch information
pixeltrix authored Jan 29, 2024
2 parents 929598e + 0f320c8 commit 03eaa01
Show file tree
Hide file tree
Showing 13 changed files with 424 additions and 37 deletions.
36 changes: 26 additions & 10 deletions app/mailers/archived/petition_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,35 @@ def email_signer(petition, signature, email)

mail to: @signature.email,
subject: subject_for(:email_signer),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def email_creator(petition, signature, email)
@petition, @signature, @email = petition, signature, email

mail to: @signature.email,
subject: subject_for(:email_creator),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def mailshot_for_signer(petition, signature, mailshot)
@petition, @signature, @mailshot = petition, signature, mailshot

mail to: @signature.email,
subject: subject_for(:mailshot_for_signer),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def mailshot_for_creator(petition, signature, mailshot)
@petition, @signature, @mailshot = petition, signature, mailshot

mail to: @signature.email,
subject: subject_for(:mailshot_for_creator),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_signer_of_threshold_response(petition, signature)
Expand All @@ -40,7 +44,8 @@ def notify_signer_of_threshold_response(petition, signature)

mail to: @signature.email,
subject: subject_for(:notify_signer_of_threshold_response),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_threshold_response(petition, signature)
Expand All @@ -49,22 +54,25 @@ def notify_creator_of_threshold_response(petition, signature)

mail to: @signature.email,
subject: subject_for(:notify_creator_of_threshold_response),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_signer_of_debate_scheduled(petition, signature)
@petition, @signature = petition, signature

mail to: @signature.email,
subject: subject_for(:notify_signer_of_debate_scheduled),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_debate_scheduled(petition, signature)
@petition, @signature = petition, signature
mail to: @signature.email,
subject: subject_for(:notify_creator_of_debate_scheduled),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_signer_of_debate_outcome(petition, signature)
Expand All @@ -76,7 +84,9 @@ def notify_signer_of_debate_outcome(petition, signature)
subject = subject_for(:notify_signer_of_negative_debate_outcome)
end

mail to: @signature.email, subject: subject, list_unsubscribe: unsubscribe_url
mail to: @signature.email, subject: subject,
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_debate_outcome(petition, signature)
Expand All @@ -88,7 +98,9 @@ def notify_creator_of_debate_outcome(petition, signature)
subject = subject_for(:notify_creator_of_negative_debate_outcome)
end

mail to: @signature.email, subject: subject, list_unsubscribe: unsubscribe_url
mail to: @signature.email, subject: subject,
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

private
Expand Down Expand Up @@ -124,5 +136,9 @@ def i18n_options
def unsubscribe_url
"<#{unsubscribe_archived_signature_url(@signature, token: @signature.unsubscribe_token)}>"
end

def one_click_unsubscribe
"List-Unsubscribe=One-Click"
end
end
end
42 changes: 30 additions & 12 deletions app/mailers/petition_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,35 @@ def email_signer(petition, signature, email)

mail to: @signature.email,
subject: subject_for(:email_signer),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def email_creator(petition, signature, email)
@petition, @signature, @email = petition, signature, email

mail to: @signature.email,
subject: subject_for(:email_creator),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def mailshot_for_signer(petition, signature, mailshot)
@petition, @signature, @mailshot = petition, signature, mailshot

mail to: @signature.email,
subject: subject_for(:mailshot_for_signer),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def mailshot_for_creator(petition, signature, mailshot)
@petition, @signature, @mailshot = petition, signature, mailshot

mail to: @signature.email,
subject: subject_for(:mailshot_for_creator),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def special_resend_of_email_confirmation_for_signer(signature)
Expand All @@ -59,7 +63,8 @@ def notify_creator_that_petition_is_published(signature)

mail to: @signature.email,
subject: subject_for(:notify_creator_that_petition_is_published),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_sponsor_that_petition_is_published(signature)
Expand All @@ -82,15 +87,17 @@ def notify_signer_of_threshold_response(petition, signature)

mail to: @signature.email,
subject: subject_for(:notify_signer_of_threshold_response),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_threshold_response(petition, signature)
@petition, @signature, @government_response = petition, signature, petition.government_response

mail to: @signature.email,
subject: subject_for(:notify_creator_of_threshold_response),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_closing_date_change(signature, petitions, remaining = 0)
Expand Down Expand Up @@ -143,7 +150,9 @@ def notify_signer_of_debate_outcome(petition, signature)
subject = subject_for(:notify_signer_of_negative_debate_outcome)
end

mail to: @signature.email, subject: subject, list_unsubscribe: unsubscribe_url
mail to: @signature.email, subject: subject,
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_debate_outcome(petition, signature)
Expand All @@ -155,22 +164,26 @@ def notify_creator_of_debate_outcome(petition, signature)
subject = subject_for(:notify_creator_of_negative_debate_outcome)
end

mail to: @signature.email, subject: subject, list_unsubscribe: unsubscribe_url
mail to: @signature.email, subject: subject,
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_signer_of_debate_scheduled(petition, signature)
@petition, @signature = petition, signature

mail to: @signature.email,
subject: subject_for(:notify_signer_of_debate_scheduled),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_of_debate_scheduled(petition, signature)
@petition, @signature = petition, signature
mail to: @signature.email,
subject: subject_for(:notify_creator_of_debate_scheduled),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def notify_creator_that_moderation_is_delayed(signature, subject, body)
Expand All @@ -179,7 +192,8 @@ def notify_creator_that_moderation_is_delayed(signature, subject, body)

mail to: @signature.email,
subject: subject_for(:notify_creator_that_moderation_is_delayed),
list_unsubscribe: unsubscribe_url
list_unsubscribe: unsubscribe_url,
list_unsubscribe_post: one_click_unsubscribe
end

def privacy_policy_update_email(privacy_notification)
Expand Down Expand Up @@ -230,4 +244,8 @@ def i18n_options
def unsubscribe_url
"<#{unsubscribe_signature_url(@signature, token: @signature.unsubscribe_token)}>"
end

def one_click_unsubscribe
"List-Unsubscribe=One-Click"
end
end
5 changes: 5 additions & 0 deletions app/views/feedback_mailer/send_feedback.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@
<strong>Browser:</strong><br>
<%= @feedback.user_agent %>
</p>

<p>
<strong>IP Address:</strong><br>
<%= @feedback.ip_address %>
</p>
3 changes: 3 additions & 0 deletions app/views/feedback_mailer/send_feedback.text.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ Email:

Browser:
<%= @feedback.user_agent %>

IP Address:
<%= @feedback.ip_address %>
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@

member do
get 'verify'
get 'unsubscribe'
get 'signed'

match 'unsubscribe', via: %i[get post]
end
end
end
Expand All @@ -91,7 +92,7 @@
resources :petitions, only: %i[index show]

resources :signatures, only: [] do
get 'unsubscribe', on: :member
match 'unsubscribe', via: %i[get post], on: :member
end
end

Expand Down
51 changes: 40 additions & 11 deletions spec/controllers/archived/signatures_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@
expect(signature).to receive(:unsubscribe!).with("token")
end

it "renders the action template" do
get :unsubscribe, params: { id: "1", token: "token" }
expect(response).to render_template(:unsubscribe)
context "and the request method is GET" do
it "renders the action template" do
get :unsubscribe, params: { id: "1", token: "token" }
expect(response).to render_template(:unsubscribe)
end
end

context "and the request method is POST" do
it "renders the action template" do
post :unsubscribe, params: { id: "1", token: "token" }
expect(response).to render_template(:unsubscribe)
end
end
end

Expand All @@ -28,10 +37,20 @@
expect(signature).to receive(:fraudulent?).and_return(true)
end

it "raises an ActiveRecord::RecordNotFound error" do
expect {
get :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
context "and the request method is GET" do
it "raises an ActiveRecord::RecordNotFound error" do
expect {
get :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "and the request method is POST" do
it "raises an ActiveRecord::RecordNotFound error" do
expect {
post :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

Expand All @@ -40,10 +59,20 @@
expect(signature).to receive(:invalidated?).and_return(true)
end

it "raises an ActiveRecord::RecordNotFound error" do
expect {
get :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
context "and the request method is GET" do
it "raises an ActiveRecord::RecordNotFound error" do
expect {
get :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "and the request method is POST" do
it "raises an ActiveRecord::RecordNotFound error" do
expect {
get :unsubscribe, params: { id: "1", token: "token" }
}.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end
Expand Down
Loading

0 comments on commit 03eaa01

Please sign in to comment.