Skip to content

Commit 419b095

Browse files
committed
Update an event used to create notify_by_email
1 parent e47b4a5 commit 419b095

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

app/controllers/concerns/log_events.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ def log_creation_email_send(push)
2727
return unless push.notify_by_email_recipients.present?
2828

2929
ip, user_agent, referrer = log_info
30-
audit_log = push.audit_logs.create!(kind: :creation_email_send, user: current_user, ip:, user_agent:, referrer:)
30+
audit_log = push.audit_logs.build(kind: :creation_email_send, user: current_user, ip:, user_agent:, referrer:)
3131

3232
recipients = push.notify_by_email_recipients
3333
locale = push.notify_by_email_locale
34-
notify_by_email = audit_log.create_notify_by_email!(recipients: recipients, locale: locale)
34+
notify_by_email = audit_log.build_notify_by_email(recipients: recipients, locale: locale)
35+
36+
audit_log.save!
3537

3638
SendPushCreatedEmailJob.perform_later(notify_by_email.id)
3739
end

test/controllers/concerns/log_events_test.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class LogEventsTestController < ApplicationController
1010
class LogEventsTest < ActionController::TestCase
1111
tests LogEventsTestController
1212
include Devise::Test::ControllerHelpers
13+
include ActiveJob::TestHelper
1314

1415
setup do
1516
@push = pushes(:test_push)
@@ -161,6 +162,65 @@ class LogEventsTest < ActionController::TestCase
161162
assert_not_equal @push, log.push
162163
end
163164

165+
test "log_creation_email_send creates an audit_log and notify_by_email" do
166+
@request.env["REMOTE_ADDR"] = "172.16.0.1"
167+
@request.env["HTTP_USER_AGENT"] = "TestAgent/1.0"
168+
@request.env["HTTP_REFERER"] = "https://test.com"
169+
170+
sign_in @user
171+
172+
@push.notify_by_email_recipients = "test@test.com"
173+
@push.notify_by_email_locale = "en"
174+
175+
assert_difference "AuditLog.count", 1 do
176+
assert_difference "NotifyByEmail.count", 1 do
177+
@controller.log_creation_email_send(@push)
178+
end
179+
end
180+
181+
assert_equal 1, AuditLog.count
182+
log = AuditLog.last
183+
assert_equal :creation_email_send, log.kind.to_sym
184+
assert_equal @push, log.push
185+
assert_equal "172.16.0.1", log.ip
186+
assert_equal "TestAgent/1.0", log.user_agent
187+
assert_equal "https://test.com", log.referrer
188+
189+
notify_by_email = NotifyByEmail.last
190+
assert_equal "test@test.com", notify_by_email.recipients
191+
assert_equal "en", notify_by_email.locale
192+
assert_equal @push, notify_by_email.push
193+
assert_equal "pending", notify_by_email.status
194+
end
195+
196+
test "log_creation_email_send creates a SendPushCreatedEmailJob" do
197+
sign_in @user
198+
199+
@push.notify_by_email_recipients = "test@test.com"
200+
@push.notify_by_email_locale = "en"
201+
202+
assert_enqueued_with(job: SendPushCreatedEmailJob) do
203+
@controller.log_creation_email_send(@push)
204+
end
205+
206+
notify_by_email = NotifyByEmail.last
207+
assert_enqueued_with(job: SendPushCreatedEmailJob, args: [notify_by_email.id])
208+
end
209+
210+
test "log_creation_email_send does not create a job, audit_log or notify_by_email record when notify_by_email_recipients is blank" do
211+
sign_in @user
212+
213+
@push.notify_by_email_recipients = ""
214+
215+
refute_enqueued_jobs do
216+
assert_no_difference "AuditLog.count" do
217+
assert_no_difference "NotifyByEmail.count" do
218+
@controller.log_creation_email_send(@push)
219+
end
220+
end
221+
end
222+
end
223+
164224
# Test log_failed_passphrase method
165225
test "log_failed_passphrase creates failed_passphrase audit log" do
166226
@request.env["REMOTE_ADDR"] = "192.168.0.1"

0 commit comments

Comments
 (0)