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



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 100

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_sent(attributes = nil) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 160

def assert_email_alert_sent(attributes = nil)
  if attributes
    matcher = ->(request) do
      payload = JSON.parse(request.body)
      payload.select { |k, _| attributes.key?(k) } == attributes
    end
  end

  assert_requested(:post, "#{EMAIL_ALERT_API_ENDPOINT}/notifications", times: 1, &matcher)
end

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



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

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



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

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

#get_latest_matching(params, subscriptions) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 112

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



156
157
158
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 156

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

#stub_email_alert_api_accepts_alertObject Also known as: email_alert_api_accepts_alert



151
152
153
154
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 151

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

#stub_email_alert_api_accepts_unpublishing_messageObject Also known as: email_alert_api_accepts_unpublishing_message



146
147
148
149
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 146

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 Also known as: email_alert_api_creates_a_subscription



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

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_auth_token(subscriber_id, address) ⇒ Object Also known as: email_alert_api_creates_an_auth_token



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

def stub_email_alert_api_creates_an_auth_token(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_creates_an_existing_subscription(subscriber_list_id, address, frequency, returned_subscription_id) ⇒ Object Also known as: email_alert_api_creates_an_existing_subscription



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

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 Also known as: email_alert_api_creates_subscriber_list



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

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 Also known as: email_alert_api_does_not_have_subscriber_list



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

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 Also known as: email_alert_api_does_not_have_subscriber_list_by_slug



267
268
269
270
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 267

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 Also known as: email_alert_api_does_not_have_subscriber_subscriptions



43
44
45
46
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 43

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 Also known as: email_alert_api_does_not_have_updated_subscriber



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 Also known as: email_alert_api_does_not_have_updated_subscription



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

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 Also known as: email_alert_api_has_no_subscriber



209
210
211
212
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 209

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 Also known as: email_alert_api_has_no_subscription_for_uuid



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

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_notification(notification) ⇒ Object Also known as: email_alert_api_has_notification



183
184
185
186
187
188
189
190
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 183

def stub_email_alert_api_has_notification(notification)
  url = "#{EMAIL_ALERT_API_ENDPOINT}/notifications/#{notification['web_service_bulletin']['to_param']}"

  stub_request(:get, url).to_return(
    status: 200,
    body: notification.to_json
  )
end

#stub_email_alert_api_has_notifications(notifications, start_at = nil) ⇒ Object Also known as: email_alert_api_has_notifications



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 171

def stub_email_alert_api_has_notifications(notifications, start_at = nil)
  url = "#{EMAIL_ALERT_API_ENDPOINT}/notifications"
  url += "?start_at=#{start_at}" if start_at
  url_regexp = Regexp.new("^#{Regexp.escape(url)}$")

  stub_request(:get, url_regexp)
    .to_return(
      status: 200,
      body: notifications.to_json
    )
end

#stub_email_alert_api_has_subscriber_list(attributes) ⇒ Object Also known as: email_alert_api_has_subscriber_list



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

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 Also known as: email_alert_api_has_subscriber_list_by_slug



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

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) ⇒ Object Also known as: email_alert_api_has_subscriber_subscriptions



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

def stub_email_alert_api_has_subscriber_subscriptions(id, address)
  stub_request(:get, "#{EMAIL_ALERT_API_ENDPOINT}/subscribers/#{id}/subscriptions")
    .to_return(
      status: 200,
      body: get_subscriber_subscriptions_response(id, address).to_json,
    )
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 48

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 Also known as: email_alert_api_has_subscriptions

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.

  frequency: ‘weekly’,

  ended: true,
},
{
  id: 'id-of-my-subscriber-list',

  frequency: ‘daily’,

  },
])

Examples:

stub_email_alert_api_has_subscriptions([
  {
    id: 'id-of-my-subscriber-list',

Parameters:

  • subscriptions (Array)


89
90
91
92
93
94
95
96
97
98
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 89

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 Also known as: email_alert_api_has_updated_subscriber



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 Also known as: email_alert_api_has_updated_subscription



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

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_refuses_to_create_subscriber_listObject Also known as: email_alert_api_refuses_to_create_subscriber_list



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

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 Also known as: email_alert_api_refuses_to_create_subscription



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

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_unsubscribes_a_subscriber(subscriber_id) ⇒ Object Also known as: email_alert_api_unsubscribes_a_subscriber



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

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 Also known as: email_alert_api_unsubscribes_a_subscription



192
193
194
195
196
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 192

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