-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
notifier_test.rb
239 lines (197 loc) · 7.34 KB
/
notifier_test.rb
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require "test_helper"
class NotifierTest < ActiveSupport::TestCase
include ActiveJob::TestHelper
class RecipientsBlock < Noticed::Event
recipients do
params.fetch(:recipients)
end
deliver_by :test
end
class RecipientsLambda < Noticed::Event
recipients -> { params.fetch(:recipients) }
deliver_by :test
end
class RecipientsMethod < Noticed::Event
deliver_by :test
recipients :recipients
def recipients
params.fetch(:recipients)
end
end
class RecipientsLambdaEphemeral < Noticed::Ephemeral
recipients -> { params.fetch(:recipients) }
deliver_by :test
end
test "includes Rails urls" do
assert_equal "http://localhost:3000/", SimpleNotifier.new.url
end
test "notifiers inherit required params" do
assert_equal [:message], InheritedNotifier.required_params
end
test "notification_methods adds methods to Noticed::Notifications" do
user = users(:one)
event = SimpleNotifier.with(message: "test").deliver(user)
assert_equal "hello #{user.email}", event.notifications.last.message
end
test "notification_methods url helpers" do
assert_equal "http://localhost:3000/", SimpleNotifier::Notification.new.url
end
test "serializes globalid objects with text column" do
user = users(:one)
notification = Noticed::Event.create!(type: "SimpleNotifier", params: {user: user})
assert_equal({user: user}, notification.params)
end
test "assigns record association from params" do
user = users(:one)
notifier = RecordNotifier.with(record: user)
assert_equal user, notifier.record
assert_empty notifier.params
end
test "can add validations for record association" do
notifier = RecordNotifier.with({})
refute notifier.valid?
assert_equal ["can't be blank"], notifier.errors[:record]
end
test "recipients block" do
event = RecipientsBlock.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient
end
test "recipients lambda" do
event = RecipientsLambda.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient
end
test "recipients" do
event = RecipientsMethod.with(recipients: [User.create!(email: "foo"), User.create!(email: "bar")]).deliver
assert_equal 2, event.notifications.count
assert_equal User.find_by(email: "foo"), event.notifications.first.recipient
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last]) do
perform_enqueued_jobs
end
end
test "recipients ephemeral" do
users = [User.create!(email: "foo"), User.create!(email: "bar")]
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, "NotifierTest::RecipientsLambdaEphemeral::Notification", {recipient: User.find_by(email: "foo"), params: {recipients: users}}]) do
RecipientsLambdaEphemeral.with(recipients: users).deliver
end
end
test "deliver without recipients" do
assert_nothing_raised do
ReceiptNotifier.deliver
end
end
test "deliver creates an event" do
assert_difference "Noticed::Event.count" do
ReceiptNotifier.deliver(User.first)
end
end
test "deliver creates notifications for each recipient" do
assert_no_difference "Noticed::Notification.count" do
event = ReceiptNotifier.deliver
assert_equal 0, event.notifications_count
end
assert_difference "Noticed::Notification.count" do
event = ReceiptNotifier.deliver(User.first)
assert_equal 1, event.notifications_count
end
assert_difference "Noticed::Notification.count", User.count do
event = ReceiptNotifier.deliver(User.all)
assert_equal User.count, event.notifications_count
end
assert_difference "Noticed::Notification.count", -1 do
event = noticed_events(:one)
event.notifications.destroy_all
assert_equal 0, event.notifications_count
end
end
test "deliver to STI recipient writes base class" do
admin = Admin.first
assert_difference "Noticed::Notification.count" do
ReceiptNotifier.deliver(admin)
end
notification = Noticed::Notification.last
assert_equal "User", notification.recipient_type
assert_equal admin, notification.recipient
end
test "creates jobs for deliveries" do
# Delivering a notification creates records
assert_enqueued_jobs 1, only: Noticed::EventJob do
ReceiptNotifier.deliver(User.first)
end
# Run the Event Job
assert_enqueued_jobs 1, only: Noticed::DeliveryMethods::Test do
perform_enqueued_jobs
end
# Run the individual deliveries
perform_enqueued_jobs
assert_equal Noticed::Notification.last, Noticed::DeliveryMethods::Test.delivered.last
end
test "creates jobs for bulk deliveries" do
assert_enqueued_jobs 1, only: Noticed::EventJob do
BulkNotifier.deliver
end
assert_enqueued_jobs 1, only: Noticed::BulkDeliveryMethods::Webhook do
perform_enqueued_jobs
end
end
test "creates jobs for bulk ephemeral deliveries" do
assert_enqueued_jobs 1, only: Noticed::BulkDeliveryMethods::Test do
EphemeralNotifier.deliver
end
assert_difference("Noticed::BulkDeliveryMethods::Test.delivered.length" => 1) do
perform_enqueued_jobs
end
end
test "deliver wait" do
freeze_time
assert_enqueued_with job: Noticed::EventJob, at: 5.minutes.from_now do
ReceiptNotifier.deliver(User.first, wait: 5.minutes)
end
end
test "deliver queue" do
freeze_time
assert_enqueued_with job: Noticed::EventJob, queue: "low_priority" do
ReceiptNotifier.deliver(User.first, queue: :low_priority)
end
end
test "wait delivery method option" do
freeze_time
event = WaitNotifier.deliver(User.first)
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last], at: 5.minutes.from_now) do
perform_enqueued_jobs
end
end
test "wait_until delivery method option" do
freeze_time
event = WaitUntilNotifier.deliver(User.first)
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last], at: 1.hour.from_now) do
perform_enqueued_jobs
end
end
test "queue delivery method option" do
event = QueueNotifier.deliver(User.first)
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last], queue: "example_queue") do
perform_enqueued_jobs
end
end
# assert_enqeued_with doesn't support priority before Rails 7
if Rails.gem_version >= Gem::Version.new("7.0.0.alpha1")
test "priority delivery method option" do
event = PriorityNotifier.deliver(User.first)
assert_enqueued_with(job: Noticed::DeliveryMethods::Test, args: [:test, event.notifications.last], priority: 2) do
perform_enqueued_jobs
end
end
end
test "deprecations don't cause problems" do
assert_nothing_raised do
Noticed.deprecator.silence do
DeprecatedNotifier.with(message: "test").deliver_later
end
end
end
test "inherits notification_methods from application notifier" do
assert SimpleNotifier::Notification.new.respond_to?(:inherited_method)
end
end