@@ -10,6 +10,7 @@ class LogEventsTestController < ApplicationController
1010class 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