-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathsend_notify_by_email_job_test.rb
More file actions
106 lines (83 loc) 路 3.51 KB
/
Copy pathsend_notify_by_email_job_test.rb
File metadata and controls
106 lines (83 loc) 路 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true
require "test_helper"
class SendNotifyByEmailJobTest < ActiveJob::TestCase
include ActionMailer::TestHelper
setup do
Settings.mail.smtp_address = "smtp.example.com"
@push = pushes(:test_push)
@notify_by_email = notify_by_emails(:one)
end
teardown do
Settings.reload!
end
test "sends email to specified recipient" do
mails = capture_emails do
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
end
mail = mails.first
assert_equal ["one@example.com"], mail.to
assert_equal "You have received a push", mail.subject
end
test "perform update notify_by_email status to completed after sending" do
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
@notify_by_email.reload
assert_equal "completed", @notify_by_email.status
assert_equal "one@example.com", @notify_by_email.successful_sends
end
test "perform update notify_by_email status to failed after sending" do
failing_mail = Minitest::Mock.new
failing_mail.expect(:deliver_now, -> { raise StandardError, "test error" })
PushCreatedMailer.stub(:with, failing_mail) do
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
end
@notify_by_email.reload
assert_equal "failed", @notify_by_email.status
assert_equal "", @notify_by_email.successful_sends
assert_equal "No emails were sent successfully.", @notify_by_email.error_message
end
test "perform does not send mail when notify_by_email is not pending" do
@notify_by_email.update(status: "processing")
assert_emails 0 do
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
end
end
test "perform does not send mail when recipients are blank" do
# Bypass readonly attribute
@notify_by_email.update_columns(recipients_ciphertext: "")
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
@notify_by_email.reload
assert_equal "failed", @notify_by_email.status
assert @notify_by_email.successful_sends.blank?
assert_equal "Recipient emails can't be blank.", @notify_by_email.error_message
end
test "perform does not send mail when notifying by email is not available" do
Settings.mail.smtp_address = nil
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
@notify_by_email.reload
assert_equal "failed", @notify_by_email.status, "Status should be failed"
assert_equal "Recipient emails are not available. Notification language is not available. Notify by email feature is not enabled.", @notify_by_email.error_message
end
test "perform logs error and does not send mail if notify_by_email is not found" do
invalid_id = -1
logger = Minitest::Mock.new
logger.expect(:error, nil, ["[SendNotifyByEmailJob] NotifyByEmail not found: #{invalid_id}"])
Rails.stub(:logger, logger) do
SendNotifyByEmailJob.perform_now(invalid_id)
end
assert logger.verify
end
test "perform logs error and does not send mail if an unexpected error occurs" do
@notify_by_email.stub(:processing!, -> { raise StandardError, "test error" }) do
NotifyByEmail.stub(:find_by, @notify_by_email) do
SendNotifyByEmailJob.perform_now(@notify_by_email.id)
end
end
@notify_by_email.reload
assert_equal "failed", @notify_by_email.status
assert_empty @notify_by_email.successful_sends
assert_equal "An unexpected error occurred while sending the email.", @notify_by_email.error_message
end
test "perform uses default queue" do
assert_equal "default", SendNotifyByEmailJob.new.queue_name
end
end