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

#assert_email_alert_sent(attributes = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 64

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, notifications_url, times: 1, &matcher)
end

#assert_subscribed(subscribable_id, address) ⇒ Object



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

def assert_subscribed(subscribable_id, address)
  assert_requested(:post, subscribe_url) do |req|
    JSON.parse(req.body).symbolize_keys == {
      subscribable_id: subscribable_id,
      address: address
    }
  end
end

#assert_unsubscribed(uuid) ⇒ Object



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

def assert_unsubscribed(uuid)
  assert_requested(:post, unsubscribe_url(uuid), times: 1)
end

#email_alert_api_accepts_alertObject



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

def email_alert_api_accepts_alert
  stub_request(:post, notifications_url)
    .to_return(
      status: 202,
      body: {}.to_json,
    )
end

#email_alert_api_creates_a_subscription(subscribable_id, address, returned_subscription_id) ⇒ Object



108
109
110
111
112
113
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 108

def email_alert_api_creates_a_subscription(subscribable_id, address, returned_subscription_id)
  stub_request(:post, subscribe_url)
    .with(
      body: { subscribable_id: subscribable_id, address: address }.to_json
  ).to_return(status: 201, body: { subscription_id: returned_subscription_id }.to_json)
end

#email_alert_api_creates_an_existing_subscription(subscribable_id, address, returned_subscription_id) ⇒ Object



115
116
117
118
119
120
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 115

def email_alert_api_creates_an_existing_subscription(subscribable_id, address, returned_subscription_id)
  stub_request(:post, subscribe_url)
    .with(
      body: { subscribable_id: subscribable_id, address: address }.to_json
  ).to_return(status: 200, body: { subscription_id: returned_subscription_id }.to_json)
end

#email_alert_api_creates_subscriber_list(attributes) ⇒ Object



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

def email_alert_api_creates_subscriber_list(attributes)
  stub_request(:post, subscriber_lists_url)
    .to_return(
      status: 201,
      body: get_subscriber_list_response(attributes).to_json,
    )
end

#email_alert_api_does_not_have_subscribable(reference:) ⇒ Object



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

def email_alert_api_does_not_have_subscribable(reference:)
  stub_request(:get, subscribable_url(reference))
    .to_return(status: 404)
end

#email_alert_api_does_not_have_subscriber_list(attributes) ⇒ Object



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

def email_alert_api_does_not_have_subscriber_list(attributes)
  stub_request(:get, subscriber_lists_url(attributes))
    .to_return(status: 404)
end

#email_alert_api_has_no_subscription_for_uuid(uuid) ⇒ Object



102
103
104
105
106
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 102

def email_alert_api_has_no_subscription_for_uuid(uuid)
  stub_request(:post, unsubscribe_url(uuid))
    .with(body: "{}")
    .to_return(status: 404)
end

#email_alert_api_has_notification(notification) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 87

def email_alert_api_has_notification(notification)
  url = "#{notifications_url}/#{notification['web_service_bulletin']['to_param']}"

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

#email_alert_api_has_notifications(notifications, start_at = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 75

def email_alert_api_has_notifications(notifications, start_at = nil)
  url = notifications_url
  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

#email_alert_api_has_subscribable(reference:, returned_attributes:) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 142

def email_alert_api_has_subscribable(reference:, returned_attributes:)
  stub_request(:get, subscribable_url(reference))
    .to_return(
      status: 200,
      body: {
        subscribable: returned_attributes
      }.to_json
  )
end

#email_alert_api_has_subscriber_list(attributes) ⇒ Object



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

def email_alert_api_has_subscriber_list(attributes)
  stub_request(:get, subscriber_lists_url(attributes))
    .to_return(
      status: 200,
      body: get_subscriber_list_response(attributes).to_json,
    )
end

#email_alert_api_refuses_to_create_subscriber_listObject



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

def email_alert_api_refuses_to_create_subscriber_list
  stub_request(:post, subscriber_lists_url)
    .to_return(
      status: 422,
    )
end

#email_alert_api_refuses_to_create_subscription(subscribable_id, address) ⇒ Object



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

def email_alert_api_refuses_to_create_subscription(subscribable_id, address)
  stub_request(:post, subscribe_url)
    .with(
      body: { subscribable_id: subscribable_id, address: address }.to_json
  ).to_return(status: 422)
end

#email_alert_api_unsubscribes_a_subscription(uuid) ⇒ Object



96
97
98
99
100
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 96

def email_alert_api_unsubscribes_a_subscription(uuid)
  stub_request(:post, unsubscribe_url(uuid))
    .with(body: "{}")
    .to_return(status: 204)
end

#get_subscriber_list_response(attributes) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 37

def get_subscriber_list_response(attributes)
  {
    "subscriber_list" => {
      "id" => "447135c3-07d6-4c3a-8a3b-efa49ef70e52",
      "subscription_url" => "https://stage-public.govdelivery.com/accounts/UKGOVUK/subscriber/new?topic_id=UKGOVUK_1234",
      "gov_delivery_id" => "UKGOVUK_1234",
      "title" => "Some title",
    }.merge(attributes)
  }
end

#post_alert_responseObject



56
57
58
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 56

def post_alert_response
  {}
end

#stub_any_email_alert_api_callObject



60
61
62
# File 'lib/gds_api/test_helpers/email_alert_api.rb', line 60

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