Module: GdsApi::TestHelpers::EmailAlertApi

Defined in:
lib/gds_api/test_helpers/email_alert_api.rb

Constant Summary collapse

EMAIL_ALERT_API_ENDPOINT =
Plek.find("email-alert-api")

Instance Method Summary collapse

Instance Method Details

#apply_subscription_defaults(subscription) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 107

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

#assert_email_alert_api_content_change_created(attributes = nil) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 177

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

#assert_email_alert_api_message_created(attributes = nil) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 188

def assert_email_alert_api_message_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}/messages", times: 1, &matcher)
end

#assert_subscribed(subscriber_list_id, address, frequency = "immediately") ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 278

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: subscriber_list_id,
      address: address,
      frequency: frequency,
    }
  end
end

#assert_unsubscribed(uuid) ⇒ Object



274
275
276
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 274

def assert_unsubscribed(uuid)
  assert_requested(:post, "#{EMAIL_ALERT_API_ENDPOINT}/unsubscribe/#{uuid}", times: 1)
end

#get_latest_matching(params, subscriptions) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 119

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

#stub_any_email_alert_api_callObject



173
174
175
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 173

def stub_any_email_alert_api_call
  stub_request(:any, %r{\A#{EMAIL_ALERT_API_ENDPOINT}})
end

#stub_email_alert_api_accepts_content_changeObject



158
159
160
161
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 158

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

#stub_email_alert_api_accepts_emailObject



168
169
170
171
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 168

def stub_email_alert_api_accepts_email
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/emails")
    .to_return(status: 202, body: {}.to_json)
end

#stub_email_alert_api_accepts_messageObject



163
164
165
166
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 163

def stub_email_alert_api_accepts_message
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/messages")
    .to_return(status: 202, body: {}.to_json)
end

#stub_email_alert_api_accepts_unpublishing_messageObject



153
154
155
156
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 153

def stub_email_alert_api_accepts_unpublishing_message
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/unpublish-messages")
    .to_return(status: 202, body: {}.to_json)
end

#stub_email_alert_api_creates_a_subscription(subscriber_list_id, address, frequency, returned_subscription_id) ⇒ Object



221
222
223
224
225
226
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 221

def stub_email_alert_api_creates_a_subscription(subscriber_list_id, address, frequency, returned_subscription_id)
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions")
    .with(
      body: { subscriber_list_id: subscriber_list_id, address: address, frequency: frequency }.to_json,
    ).to_return(status: 201, body: { subscription_id: returned_subscription_id }.to_json)
end

#stub_email_alert_api_creates_an_existing_subscription(subscriber_list_id, address, frequency, returned_subscription_id) ⇒ Object



228
229
230
231
232
233
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 228

def stub_email_alert_api_creates_an_existing_subscription(subscriber_list_id, address, frequency, returned_subscription_id)
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions")
    .with(
      body: { subscriber_list_id: subscriber_list_id, address: address, frequency: frequency }.to_json,
    ).to_return(status: 200, body: { subscription_id: returned_subscription_id }.to_json)
end

#stub_email_alert_api_creates_subscriber_list(attributes) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 140

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

#stub_email_alert_api_does_not_have_subscriber_list(attributes) ⇒ Object



135
136
137
138
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 135

def stub_email_alert_api_does_not_have_subscriber_list(attributes)
  stub_request(:get, build_subscriber_lists_url(attributes))
    .to_return(status: 404)
end

#stub_email_alert_api_does_not_have_subscriber_list_by_slug(slug:) ⇒ Object



298
299
300
301
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 298

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

#stub_email_alert_api_does_not_have_subscriber_subscriptions(id) ⇒ Object



50
51
52
53
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 50

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

#stub_email_alert_api_does_not_have_updated_subscriber(id) ⇒ Object



17
18
19
20
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 17

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

#stub_email_alert_api_does_not_have_updated_subscription(subscription_id) ⇒ Object



35
36
37
38
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 35

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

#stub_email_alert_api_has_no_subscriber(subscriber_id) ⇒ Object



216
217
218
219
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 216

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

#stub_email_alert_api_has_no_subscription_for_uuid(uuid) ⇒ Object



205
206
207
208
209
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 205

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

#stub_email_alert_api_has_subscriber_list(attributes) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 127

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

#stub_email_alert_api_has_subscriber_list_by_slug(slug:, returned_attributes:) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 288

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

#stub_email_alert_api_has_subscriber_subscriptions(id, address, order = nil, subscriptions: nil) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 40

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: subscriptions).to_json,
    )
end

#stub_email_alert_api_has_subscription(id, frequency, title: "Some title", subscriber_id: 1, subscriber_list_id: 1000, ended: false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 55

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: frequency,
    title: title,
    subscriber_id: subscriber_id,
    subscriber_list_id: subscriber_list_id,
    ended: 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

#stub_email_alert_api_has_subscriptions(subscriptions) ⇒ Object

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.

Examples:

stub_email_alert_api_has_subscriptions([
  {
    id: 'id-of-my-subscriber-list',
    frequency: 'weekly',
    ended: true,
  },
  {
    id: 'id-of-my-subscriber-list',
    frequency: 'daily',
  },
])

Parameters:

  • subscriptions (Array)


96
97
98
99
100
101
102
103
104
105
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 96

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

#stub_email_alert_api_has_updated_subscriber(id, new_address) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 9

def stub_email_alert_api_has_updated_subscriber(id, new_address)
  stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}")
    .to_return(
      status: 200,
      body: get_subscriber_response(id, new_address).to_json,
    )
end

#stub_email_alert_api_has_updated_subscription(subscription_id, frequency) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 27

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: frequency).to_json,
    )
end

#stub_email_alert_api_invalid_update_subscriber(id) ⇒ Object



22
23
24
25
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 22

def stub_email_alert_api_invalid_update_subscriber(id)
  stub_request(:patch, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}")
    .to_return(status: 422)
end

#stub_email_alert_api_refuses_to_create_subscriber_listObject



148
149
150
151
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 148

def stub_email_alert_api_refuses_to_create_subscriber_list
  stub_request(:post, build_subscriber_lists_url)
    .to_return(status: 422)
end

#stub_email_alert_api_refuses_to_create_subscription(subscriber_list_id, address, frequency) ⇒ Object



235
236
237
238
239
240
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 235

def stub_email_alert_api_refuses_to_create_subscription(subscriber_list_id, address, frequency)
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscriptions")
    .with(
      body: { subscriber_list_id: subscriber_list_id, address: address, frequency: frequency }.to_json,
    ).to_return(status: 422)
end

#stub_email_alert_api_sends_subscriber_verification_email(subscriber_id, address) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 256

def stub_email_alert_api_sends_subscriber_verification_email(subscriber_id, address)
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
    .to_return(
      status: 201,
      body: get_subscriber_response(subscriber_id, address).to_json,
    )
end

#stub_email_alert_api_sends_subscription_verification_email(address, frequency, topic_id) ⇒ Object



242
243
244
245
246
247
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 242

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: address, frequency: frequency, topic_id: topic_id }.to_json,
    ).to_return(status: 200)
end

#stub_email_alert_api_subscriber_verification_email_invalidObject



264
265
266
267
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 264

def stub_email_alert_api_subscriber_verification_email_invalid
  stub_request(:post, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/auth-token")
    .to_return(status: 422)
end

#stub_email_alert_api_subscriber_verification_email_no_subscriberObject



269
270
271
272
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 269

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

#stub_email_alert_api_subscription_verification_email_invalid(address, frequency, topic_id) ⇒ Object



249
250
251
252
253
254
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 249

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: address, frequency: frequency, topic_id: topic_id }.to_json,
    ).to_return(status: 422)
end

#stub_email_alert_api_unsubscribes_a_subscriber(subscriber_id) ⇒ Object



211
212
213
214
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 211

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

#stub_email_alert_api_unsubscribes_a_subscription(uuid) ⇒ Object



199
200
201
202
203
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 199

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