Skip to content

Commit 05fa4d2

Browse files
committed
Update parameters used for API requests
1 parent 419b095 commit 05fa4d2

7 files changed

Lines changed: 64 additions & 23 deletions

File tree

app/controllers/api/v1/pushes_controller.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ def create
153153
# when creating file pushes / uploading attachments.
154154
authenticate_user! if requires_authentication_for_create?(permitted_params)
155155

156+
# Extract notify_by_email params before creating the Push
157+
# to avoid ActiveModel::UnknownAttributeError
158+
permitted_notify_by_email_params = permitted_params.delete(:notify_by_email)
159+
156160
@push = Push.new(permitted_params)
157161

158162
if !permitted_params[:kind].present?
@@ -173,15 +177,17 @@ def create
173177

174178
if user_signed_in?
175179
@push.user = current_user
176-
@push.notify_by_email_creator = current_user if permitted_params[:notify_by_email_recipients].present?
177180
end
178181

182+
# Handle nested notify_by_email params
183+
set_notify_by_email(@push, permitted_notify_by_email_params) if permitted_notify_by_email_params.present?
184+
179185
assign_deletable_by_viewer(@push, permitted_params)
180186
assign_retrieval_step(@push, permitted_params)
181187

182188
if @push.save
183189
log_creation(@push)
184-
log_creation_email_send(@push) if permitted_params[:notify_by_email_recipients].present?
190+
log_creation_email_send(@push) if permitted_notify_by_email_params.present?
185191

186192
render template: "pushes/show", status: :created
187193
else
@@ -484,6 +490,13 @@ def set_push
484490
end
485491
end
486492

493+
def set_notify_by_email(push, notify_by_email_params, required: false)
494+
push.notify_by_email_recipients = notify_by_email_params[:recipients]
495+
push.notify_by_email_locale = notify_by_email_params[:locale]
496+
push.notify_by_email_creator = current_user if user_signed_in?
497+
push.notify_by_email_required = required
498+
end
499+
487500
def push_params
488501
if request.path.start_with?("/f")
489502
params.require(:file_push).permit(:name, :expire_after_days, :expire_after_views, :deletable_by_viewer,

app/controllers/api/v2/pushes_controller.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ class Api::V2::PushesController < Api::V1::PushesController
66
before_action :set_push, only: %i[show preview audit destroy notify_by_email]
77

88
def notify_by_email
9-
@push.notify_by_email_recipients = params[:recipients]
10-
@push.notify_by_email_locale = params[:locale]
11-
@push.notify_by_email_creator = current_user if user_signed_in?
12-
@push.notify_by_email_required = true
9+
set_notify_by_email(@push, notify_by_email_params, required: true)
1310

1411
if @push.valid?
1512
log_creation_email_send(@push)
@@ -25,9 +22,20 @@ def force_json_format
2522
request.format = :json
2623
end
2724

25+
def set_notify_by_email(push, permitted_params, required: false)
26+
push.notify_by_email_recipients = permitted_params[:recipients]
27+
push.notify_by_email_locale = permitted_params[:locale]
28+
push.notify_by_email_creator = current_user if user_signed_in?
29+
push.notify_by_email_required = required
30+
end
31+
32+
def notify_by_email_params
33+
params.permit(:recipients, :locale)
34+
end
35+
2836
def push_params
2937
permitted = params.require(:push).permit(:name, :kind, :expire_after_days, :expire_after_views,
30-
:deletable_by_viewer, :retrieval_step, :payload, :note, :passphrase, :notify_by_email_recipients, :notify_by_email_locale, files: [])
38+
:deletable_by_viewer, :retrieval_step, :payload, :note, :passphrase, notify_by_email: [:recipients, :locale], files: [])
3139

3240
# For v2 requests, file uploads imply a file push unless kind is explicit.
3341
if permitted[:kind].blank? && permitted[:files].present?

app/controllers/concerns/log_events.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def log_creation_email_send(push)
3434
notify_by_email = audit_log.build_notify_by_email(recipients: recipients, locale: locale)
3535

3636
audit_log.save!
37-
3837
SendPushCreatedEmailJob.perform_later(notify_by_email.id)
3938
end
4039

app/views/pages/api.html.erb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,9 @@
115115
<tr><td><code>passphrase</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Requires this passphrase to retrieve the payload.") %></td></tr>
116116
<tr><td><code>name</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional label shown to the owner.") %></td></tr>
117117
<tr><td><code>note</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional owner-only note.") %></td></tr>
118-
<tr><td><code>notify_by_email_recipients</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional recipients for the push creation email. Comma-separated list of email addresses. Maximum 5 emails.") %></td></tr>
119-
<tr><td><code>notify_by_email_locale</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional locale for the push creation email. Defaults to #{I18n.default_locale}. Available locales: #{I18n.available_locales.map(&:to_s).join(", ")}") %></td></tr>
118+
<tr><td><code>notify_by_email</code></td><td>object</td><td><%= _("No") %></td><td><%= _("Optional recipients for the push creation email.") %></td></tr>
119+
<tr><td><code>notify_by_email.recipients</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional recipients for the push creation email. Comma-separated list of email addresses. Maximum 5 emails.") %></td></tr>
120+
<tr><td><code>notify_by_email.locale</code></td><td>string</td><td><%= _("No") %></td><td><%= _("Optional locale for the push creation email. Defaults to #{I18n.default_locale}. Available locales: #{I18n.available_locales.map(&:to_s).join(", ")}") %></td></tr>
120121
</tbody>
121122
</table>
122123
</div>
@@ -127,7 +128,11 @@
127128
"expire_after_views": 5,
128129
"passphrase": "optional-passphrase",
129130
"deletable_by_viewer": true,
130-
"retrieval_step": true
131+
"retrieval_step": true,
132+
"notify_by_email": {
133+
"recipients": "email1@example.com, email2@example.com",
134+
"locale": "en"
135+
}
131136
}
132137
}</code></pre>
133138
<p class="mb-2 mt-3"><strong><%= _("cURL example (JSON body):") %></strong></p>
@@ -231,7 +236,7 @@
231236
-H "Authorization: Bearer YOUR_API_TOKEN" \
232237
-H "Content-Type: application/json" \
233238
-d '{
234-
"recipients": "recipient1@example.com,recipient2@example.com",
239+
"recipients": "email1@example.com, email2@example.com",
235240
"locale": "en"
236241
}'</code></pre>
237242
</div>

app/views/pushes/audit.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ json.logs @audit_logs do |audit_log|
1111
json.recipients audit_log.notify_by_email.recipients
1212
json.locale audit_log.notify_by_email.locale
1313
json.status audit_log.notify_by_email.status
14+
json.error_message audit_log.notify_by_email.error_message if audit_log.notify_by_email.error_message.present?
1415
json.successful_sends audit_log.notify_by_email.successful_sends
1516
json.proceed_at audit_log.notify_by_email.proceed_at
1617
end

test/integration/api/api_v2_pushes_test.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,12 @@ def test_audit_includes_notify_by_email_details_for_completed_notify_by_email
166166
notify_by_email = notify_by_emails(:one)
167167
owner = users(:giuliana)
168168

169-
travel_to Time.zone.local(2026, 1, 1, 1, 0, 0) do
170-
SendPushCreatedEmailJob.perform_now(notify_by_email.id)
169+
Settings.stub(:notify_by_email_available?, true) do
170+
travel_to Time.zone.local(2026, 1, 1, 1, 0, 0) do
171+
SendPushCreatedEmailJob.perform_now(notify_by_email.id)
172+
end
171173
end
174+
172175
get "/api/v2/pushes/#{push.url_token}/audit",
173176
headers: bearer_headers(owner),
174177
as: :json
@@ -389,8 +392,10 @@ def test_create_with_notify_by_email_params_adds_a_job_to_the_queue
389392
params: {
390393
push: {
391394
payload: "some-secret",
392-
notify_by_email_recipients: "recipient@example.com",
393-
notify_by_email_locale: "en"
395+
notify_by_email: {
396+
recipients: "recipient@example.com",
397+
locale: "en"
398+
}
394399
}
395400
},
396401
headers: bearer_headers(user),
@@ -420,8 +425,10 @@ def test_create_with_notify_by_email_params_fails_when_email_service_is_not_conf
420425
params: {
421426
push: {
422427
payload: "some-secret",
423-
notify_by_email_recipients: "recipient@example.com",
424-
notify_by_email_locale: "en"
428+
notify_by_email: {
429+
recipients: "recipient@example.com",
430+
locale: "en"
431+
}
425432
}
426433
},
427434
headers: bearer_headers(user),
@@ -441,8 +448,10 @@ def test_create_with_notify_by_email_params_fails_when_user_is_not_signed_in
441448
params: {
442449
push: {
443450
payload: "some-secret",
444-
notify_by_email_recipients: "recipient@example.com",
445-
notify_by_email_locale: "en"
451+
notify_by_email: {
452+
recipients: "recipient@example.com",
453+
locale: "en"
454+
}
446455
}
447456
},
448457
as: :json

test/unit/send_push_created_email_job_test.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ class SendPushCreatedEmailJobTest < ActiveJob::TestCase
1212

1313
test "sends email to specified recipient" do
1414
mails = capture_emails do
15-
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
15+
Settings.stub(:notify_by_email_available?, true) do
16+
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
17+
end
1618
end
1719

1820
mail = mails.first
@@ -21,7 +23,9 @@ class SendPushCreatedEmailJobTest < ActiveJob::TestCase
2123
end
2224

2325
test "perform update notify_by_email status to completed after sending" do
24-
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
26+
Settings.stub(:notify_by_email_available?, true) do
27+
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
28+
end
2529

2630
@notify_by_email.reload
2731
assert_equal "completed", @notify_by_email.status
@@ -33,7 +37,9 @@ class SendPushCreatedEmailJobTest < ActiveJob::TestCase
3337
failing_mail.expect(:deliver_now, -> { raise StandardError, "test error" })
3438

3539
PushCreatedMailer.stub(:with, failing_mail) do
36-
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
40+
Settings.stub(:notify_by_email_available?, true) do
41+
SendPushCreatedEmailJob.perform_now(@notify_by_email.id)
42+
end
3743
end
3844

3945
@notify_by_email.reload

0 commit comments

Comments
 (0)