-
Notifications
You must be signed in to change notification settings - Fork 14
/
email_alert_api.rb
600 lines (526 loc) · 22.1 KB
/
email_alert_api.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
require "gds_api/test_helpers/json_client_helper"
require "json"
module GdsApi
module TestHelpers
module EmailAlertApi
EMAIL_ALERT_API_ENDPOINT = Plek.find("email-alert-api")
def stub_email_alert_api_has_updated_subscriber(id, new_address, govuk_account_id: nil)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}")
.to_return(
status: 200,
body: get_subscriber_response(id, new_address, govuk_account_id).to_json,
)
end
def stub_email_alert_api_does_not_have_updated_subscriber(id)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}")
.to_return(status: 404)
end
def stub_email_alert_api_invalid_update_subscriber(id)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}")
.to_return(status: 422)
end
def stub_email_alert_api_has_updated_subscription(subscription_id, frequency)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{subscription_id}")
.to_return(
status: 200,
body: get_subscription_response(subscription_id, frequency:).to_json,
)
end
def stub_email_alert_api_does_not_have_updated_subscription(subscription_id)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{subscription_id}")
.to_return(status: 404)
end
def stub_email_alert_api_has_subscriber_subscriptions(id, address, order = nil, subscriptions: nil)
params = order ? "?order=#{order}" : ""
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}/subscriptions#{params}")
.to_return(
status: 200,
body: get_subscriber_subscriptions_response(id, address, subscriptions:).to_json,
)
end
def stub_email_alert_api_does_not_have_subscriber_subscriptions(id)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}/subscriptions")
.to_return(status: 404)
end
def stub_email_alert_api_has_subscription(
id,
frequency,
title: "Some title",
subscriber_id: 1,
subscriber_list_id: 1000,
ended: false
)
response = get_subscription_response(
id,
frequency:,
title:,
subscriber_id:,
subscriber_list_id:,
ended:,
).to_json
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}")
.to_return(status: 200, body: response)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}/latest")
.to_return(status: 200, body: response)
end
# Stubs the API responses as if each subscription happened in the order they are passed.
# Useful if you need to query the '/latest' endpoint of a subscription.
# Takes an array of hashes.
#
# @example
# stub_email_alert_api_has_subscriptions([
# {
# id: 'id-of-my-subscriber-list',
# frequency: 'weekly',
# ended: true,
# },
# {
# id: 'id-of-my-subscriber-list',
# frequency: 'daily',
# },
# ])
#
# @param subscriptions [Array]
def stub_email_alert_api_has_subscriptions(subscriptions)
subscriptions.map! { |subscription| apply_subscription_defaults(subscription) }
subscriptions.each do |id, params|
latest_id, latest_params = get_latest_matching(params, subscriptions)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}")
.to_return(status: 200, body: get_subscription_response(id, **params).to_json)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/#{id}/latest")
.to_return(status: 200, body: get_subscription_response(latest_id, **latest_params).to_json)
end
end
def apply_subscription_defaults(subscription)
parameters = {
title: "Some title",
subscriber_id: 1,
subscriber_list_id: 1000,
ended: true,
}.merge(subscription)
# Strip out ID as subsequent call to `get_subscription_response` throws `ArgumentError`
id = parameters.delete(:id)
[id, parameters]
end
def get_latest_matching(params, subscriptions)
matching = subscriptions.select do |_current_id, current_params|
params[:subscriber_id] == current_params[:subscriber_id] &&
params[:subscriber_list_id] == current_params[:subscriber_list_id]
end
matching.last
end
def stub_email_alert_api_creates_subscriber_list(attributes)
stub_request(:post, build_subscriber_lists_url)
.to_return(
status: 201,
body: get_subscriber_list_response(attributes).to_json,
)
end
def stub_email_alert_api_has_subscriber_list(attributes)
stub_request(:get, build_subscriber_lists_url(attributes))
.to_return(
status: 200,
body: get_subscriber_list_response(attributes).to_json,
)
end
def stub_email_alert_api_does_not_have_subscriber_list(attributes)
stub_request(:get, build_subscriber_lists_url(attributes))
.to_return(status: 404)
end
def stub_email_alert_api_refuses_to_create_subscriber_list
stub_request(:post, build_subscriber_lists_url)
.to_return(status: 422)
end
def stub_email_alert_api_accepts_content_change
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/content-changes")
.to_return(status: 202, body: {}.to_json)
end
def stub_email_alert_api_accepts_email
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/emails")
.to_return(status: 202, body: {}.to_json)
end
def stub_any_email_alert_api_call
stub_request(:any, %r{\A#{EMAIL_ALERT_API_ENDPOINT}})
end
def stub_get_subscriber_list_metrics(path:, response:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/metrics#{path}")
.to_return(status: 200, body: response)
end
def stub_get_subscriber_list_metrics_not_found(path:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/metrics#{path}")
.to_return(status: 404)
end
def assert_email_alert_api_content_change_created(attributes = nil)
if attributes
matcher = lambda do |request|
payload = JSON.parse(request.body)
payload.select { |k, _| attributes.key?(k) } == attributes
end
end
assert_requested(:post, "#{EMAIL_ALERT_API_ENDPOINT}/content-changes", times: 1, &matcher)
end
def stub_email_alert_api_unsubscribes_a_subscription(uuid)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/unsubscribe/#{uuid}")
.with(body: "{}")
.to_return(status: 204)
end
def stub_email_alert_api_has_no_subscription_for_uuid(uuid)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/unsubscribe/#{uuid}")
.with(body: "{}")
.to_return(status: 404)
end
def stub_email_alert_api_unsubscribes_a_subscriber(subscriber_id)
stub_request(:delete, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{subscriber_id}")
.to_return(status: 204)
end
def stub_email_alert_api_has_no_subscriber(subscriber_id)
stub_request(:delete, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{subscriber_id}")
.to_return(status: 404)
end
def stub_email_alert_api_creates_a_subscription(
subscriber_list_id: nil,
address: nil,
frequency: nil,
returned_subscription_id: nil,
skip_confirmation_email: false,
subscriber_id: nil
)
response = get_subscription_response(
returned_subscription_id,
frequency:,
subscriber_list_id:,
subscriber_id:,
)
request_params = {
subscriber_list_id:,
address:,
frequency:,
skip_confirmation_email:,
}
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions")
.with(body: hash_including(request_params.compact))
.to_return(status: 200, body: response.to_json)
end
def stub_email_alert_api_refuses_to_create_subscription(subscriber_list_id, address, frequency)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions")
.with(
body: hash_including(
subscriber_list_id:,
address:,
frequency:,
),
).to_return(status: 422)
end
def stub_email_alert_api_sends_subscription_verification_email(address, frequency, topic_id)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/auth-token")
.with(
body: { address:, frequency:, topic_id: }.to_json,
).to_return(status: 200)
end
def stub_email_alert_api_subscription_verification_email_invalid(address, frequency, topic_id)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions/auth-token")
.with(
body: { address:, frequency:, topic_id: }.to_json,
).to_return(status: 422)
end
def stub_email_alert_api_sends_subscriber_verification_email(subscriber_id, address, govuk_account_id: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
.to_return(
status: 201,
body: get_subscriber_response(subscriber_id, address, govuk_account_id).to_json,
)
end
def stub_email_alert_api_subscriber_verification_email_invalid
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
.to_return(status: 422)
end
def stub_email_alert_api_subscriber_verification_email_no_subscriber
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
.to_return(status: 404)
end
def stub_email_alert_api_subscriber_verification_email_linked_to_govuk_account
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
.to_return(status: 403)
end
def stub_email_alert_api_authenticate_subscriber_by_govuk_account(govuk_account_session, subscriber_id, address, govuk_account_id: "user-id", new_govuk_account_session: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 200,
body: {
govuk_account_session: new_govuk_account_session,
}.compact.merge(get_subscriber_response(subscriber_id, address, govuk_account_id)).to_json,
)
end
def stub_email_alert_api_authenticate_subscriber_by_govuk_account_session_invalid(govuk_account_session)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 401,
)
end
def stub_email_alert_api_authenticate_subscriber_by_govuk_account_email_unverified(govuk_account_session, new_govuk_account_session: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 403,
body: {
govuk_account_session: new_govuk_account_session,
}.compact.to_json,
)
end
def stub_email_alert_api_authenticate_subscriber_by_govuk_account_no_subscriber(govuk_account_session, new_govuk_account_session: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 404,
body: {
govuk_account_session: new_govuk_account_session,
}.compact.to_json,
)
end
def stub_email_alert_api_link_subscriber_to_govuk_account(govuk_account_session, subscriber_id, address, govuk_account_id: "user-id", new_govuk_account_session: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/link")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 200,
body: {
govuk_account_session: new_govuk_account_session,
}.compact.merge(get_subscriber_response(subscriber_id, address, govuk_account_id)).to_json,
)
end
def stub_email_alert_api_link_subscriber_to_govuk_account_session_invalid(govuk_account_session)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/link")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 401,
)
end
def stub_email_alert_api_link_subscriber_to_govuk_account_email_unverified(govuk_account_session, new_govuk_account_session: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/link")
.with(
body: { govuk_account_session: }.to_json,
).to_return(
status: 403,
body: {
govuk_account_session: new_govuk_account_session,
}.compact.to_json,
)
end
def stub_email_alert_api_find_subscriber_by_govuk_account(govuk_account_id, subscriber_id, address)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/#{govuk_account_id}")
.to_return(
status: 200,
body: get_subscriber_response(subscriber_id, address, govuk_account_id).to_json,
)
end
def stub_email_alert_api_find_subscriber_by_govuk_account_no_subscriber(govuk_account_id)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/govuk-account/#{govuk_account_id}")
.to_return(status: 404)
end
def assert_unsubscribed(uuid)
assert_requested(:post, "#{EMAIL_ALERT_API_ENDPOINT}/unsubscribe/#{uuid}", times: 1)
end
def assert_subscribed(subscriber_list_id, address, frequency = "immediately")
assert_requested(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions") do |req|
JSON.parse(req.body).symbolize_keys == {
subscriber_list_id:,
address:,
frequency:,
}
end
end
def stub_email_alert_api_has_subscriber_list_by_slug(slug:, returned_attributes:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.to_return(
status: 200,
body: {
subscriber_list: returned_attributes,
}.to_json,
)
end
def stub_email_alert_api_does_not_have_subscriber_list_by_slug(slug:)
stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.to_return(status: 404)
end
def stub_email_alert_api_bulk_unsubscribe(slug:)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.to_return(status: 202)
end
def stub_email_alert_api_bulk_unsubscribe_with_message(slug:, body:, sender_message_id:, govuk_request_id: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.with({
body: {
body:,
sender_message_id:,
}.to_json,
}.tap { |attr| attr[:headers] = { "Govuk-Request-Id" => govuk_request_id } if govuk_request_id })
.to_return(status: 202)
end
def stub_email_alert_api_bulk_unsubscribe_not_found(slug:)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.to_return(status: 404)
end
def stub_email_alert_api_bulk_unsubscribe_not_found_with_message(slug:, body:, sender_message_id:, govuk_request_id: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.with({
body: {
body:,
sender_message_id:,
}.to_json,
}.tap { |attr| attr[:headers] = { "Govuk-Request-Id" => govuk_request_id } if govuk_request_id })
.to_return(status: 404)
end
def stub_email_alert_api_bulk_unsubscribe_conflict(slug:)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.to_return(status: 409)
end
def stub_email_alert_api_bulk_unsubscribe_conflict_with_message(slug:, body:, sender_message_id:, govuk_request_id: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.with({
body: {
body:,
sender_message_id:,
}.to_json,
}.tap { |attr| attr[:headers] = { "Govuk-Request-Id" => govuk_request_id } if govuk_request_id })
.to_return(status: 409)
end
def stub_email_alert_api_bulk_unsubscribe_bad_request(slug:)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.to_return(status: 422)
end
def stub_email_alert_api_bulk_unsubscribe_bad_request_with_message(slug:, body:, sender_message_id:, govuk_request_id: nil)
stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}/bulk-unsubscribe")
.with({
body: {
body:,
sender_message_id:,
}.to_json,
}.tap { |attr| attr[:headers] = { "Govuk-Request-Id" => govuk_request_id } if govuk_request_id })
.to_return(status: 422)
end
def stub_update_subscriber_list_details(slug:, params:)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.with(body: params.to_json)
.to_return(
status: 200,
body: get_subscriber_list_response(params).to_json,
)
end
def stub_update_subscriber_list_details_not_found(slug:, params:)
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.with(body: params.to_json)
.to_return(status: 404)
end
def stub_update_subscriber_list_details_unprocessible_entity(slug:, params: {})
stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists/#{slug}")
.with(body: params.to_json)
.to_return(status: 422)
end
private
def get_subscriber_response(id, address, govuk_account_id)
{
"subscriber" => {
"id" => id,
"address" => address,
"govuk_account_id" => govuk_account_id,
},
}
end
def get_subscription_response(
id,
frequency: "daily",
title: "Some title",
subscriber_id: 1,
subscriber_list_id: 1000,
ended: false
)
{
"subscription" => {
"id" => id,
"frequency" => frequency,
"source" => "user_signed_up",
"ended_at" => ended ? Time.now.iso8601 : nil,
"ended_reason" => ended ? "unsubscribed" : nil,
"subscriber" => {
"id" => subscriber_id,
"address" => "[email protected]",
},
"subscriber_list" => {
"id" => subscriber_list_id,
"slug" => "some-thing",
"title" => title,
},
},
}
end
def get_subscriber_subscriptions_response(id, address, subscriptions:)
{
"subscriber" => {
"id" => id,
"address" => address,
},
"subscriptions" => subscriptions || [
{
"subscriber_id" => 1,
"subscriber_list_id" => 1000,
"frequency" => "daily",
"id" => "447135c3-07d6-4c3a-8a3b-efa49ef70e52",
"subscriber_list" => {
"id" => 1000,
"slug" => "some-thing",
},
},
],
}
end
def get_subscriber_list_response(attributes)
{
"subscriber_list" => {
"id" => "447135c3-07d6-4c3a-8a3b-efa49ef70e52",
"title" => "Some title",
"active_subscriptions_count" => 42,
}.merge(attributes),
}
end
def post_alert_response
{}
end
def build_subscriber_lists_url(attributes = nil)
if attributes
tags = attributes["tags"]
links = attributes["links"]
content_id = attributes["content_id"]
document_type = attributes["document_type"]
email_document_supertype = attributes["email_document_supertype"]
government_document_supertype = attributes["government_document_supertype"]
content_purpose_supergroup = attributes["content_purpose_supergroup"]
combine_mode = attributes["combine_mode"]
params = {}
params[:tags] = tags if tags
params[:links] = links if links
params[:content_id] = content_id if content_id
params[:document_type] = document_type if document_type
params[:email_document_supertype] = email_document_supertype if email_document_supertype
params[:government_document_supertype] = government_document_supertype if government_document_supertype
params[:content_purpose_supergroup] = content_purpose_supergroup if content_purpose_supergroup
params[:combine_mode] = combine_mode if combine_mode
query = Rack::Utils.build_nested_query(params)
end
url = "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists"
query ? "#{url}?#{query}" : url
end
# Find and return the subscriber list for a topic if one exists.
def email_alert_api_has_subscriber_list_for_topic(content_id:, list: {})
url = "#{EMAIL_ALERT_API_ENDPOINT}/subscriber-lists?links%5Btopics%5D%5B0%5D=#{content_id}"
stub_request(:get, url).to_return(status: 200, body: { "subscriber_list" => list }.to_json)
end
end
end
end