Module: GdsApi::TestHelpers::AccountApi

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

Constant Summary collapse

ACCOUNT_API_ENDPOINT =
Plek.find("account-api")

Instance Method Summary collapse

Instance Method Details

#stub_account_api_delete_email_subscription(name:, **options) ⇒ Object

DELETE /api/email-subscriptions/:name


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

def (name:, **options)
  (
    :delete,
    "/api/email-subscriptions/#{name}",
    response_status: 204,
    **options,
  )
end

#stub_account_api_delete_email_subscription_does_not_exist(name:, **options) ⇒ Object


251
252
253
254
255
256
257
258
# File 'lib/gds_api/test_helpers/account_api.rb', line 251

def (name:, **options)
  (
    :delete,
    "/api/email-subscriptions/#{name}",
    response_status: 404,
    **options,
  )
end

#stub_account_api_delete_user_by_subject_identifier(subject_identifier:) ⇒ Object

DELETE /api/oidc-users/:subject_identifier


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

def (subject_identifier:)
  (
    :delete,
    "/api/oidc-users/#{subject_identifier}",
    response_status: 204,
  )
end

#stub_account_api_delete_user_by_subject_identifier_does_not_exist(subject_identifier:) ⇒ Object


151
152
153
154
155
156
157
# File 'lib/gds_api/test_helpers/account_api.rb', line 151

def (subject_identifier:)
  (
    :delete,
    "/api/oidc-users/#{subject_identifier}",
    response_status: 404,
  )
end

#stub_account_api_forbidden_has_attributes(attributes: [], **options) ⇒ Object


292
293
294
295
296
297
298
299
300
# File 'lib/gds_api/test_helpers/account_api.rb', line 292

def (attributes: [], **options)
  querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
  (
    :get,
    "/api/attributes?#{querystring}",
    response_status: 403,
    **options,
  )
end

#stub_account_api_forbidden_set_attributes(attributes: nil, **options) ⇒ Object


324
325
326
327
328
329
330
331
332
# File 'lib/gds_api/test_helpers/account_api.rb', line 324

def (attributes: nil, **options)
  (
    :patch,
    "/api/attributes",
    with: { body: hash_including({ attributes: attributes }.compact) },
    response_status: 403,
    **options,
  )
end

#stub_account_api_get_email_subscription(name:, topic_slug: "slug", email_alert_api_subscription_id: "12345", **options) ⇒ Object

GET /api/email-subscriptions/:name


178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/gds_api/test_helpers/account_api.rb', line 178

def (name:, topic_slug: "slug", email_alert_api_subscription_id: "12345", **options)
  (
    :get,
    "/api/email-subscriptions/#{name}",
    response_body: {
      email_subscription: {
        name: name,
        topic_slug: topic_slug,
        email_alert_api_subscription_id: email_alert_api_subscription_id,
      },
    },
    **options,
  )
end

#stub_account_api_get_email_subscription_does_not_exist(name:, **options) ⇒ Object


193
194
195
196
197
198
199
200
# File 'lib/gds_api/test_helpers/account_api.rb', line 193

def (name:, **options)
  (
    :get,
    "/api/email-subscriptions/#{name}",
    response_status: 404,
    **options,
  )
end

#stub_account_api_get_end_session_url(govuk_account_session: nil, end_session_uri: "http://auth/provider") ⇒ Object

GET /api/oauth2/end-session


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gds_api/test_helpers/account_api.rb', line 52

def (govuk_account_session: nil, end_session_uri: "http://auth/provider")
  if 
    stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/end-session")
      .with(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME =>  })
      .to_return(
        status: 200,
        body: { end_session_uri: end_session_uri }.to_json,
      )
  else
    stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/end-session")
      .to_return(
        status: 200,
        body: { end_session_uri: end_session_uri }.to_json,
      )
  end
end

#stub_account_api_get_sign_in_url(redirect_path: nil, mfa: false, auth_uri: "http://auth/provider", state: "state") ⇒ Object

GET /api/oauth2/sign-in


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

def (redirect_path: nil, mfa: false, auth_uri: "http://auth/provider", state: "state")
  querystring = Rack::Utils.build_nested_query({ redirect_path: redirect_path, mfa: mfa }.compact)
  stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/sign-in?#{querystring}")
    .to_return(
      status: 200,
      body: { auth_uri: auth_uri, state: state }.to_json,
    )
end

#stub_account_api_has_attributes(attributes: [], values: {}, **options) ⇒ Object

GET /api/attributes


272
273
274
275
276
277
278
279
280
# File 'lib/gds_api/test_helpers/account_api.rb', line 272

def (attributes: [], values: {}, **options)
  querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
  (
    :get,
    "/api/attributes?#{querystring}",
    response_body: { values: values },
    **options,
  )
end

#stub_account_api_match_user_by_email_does_not_exist(email:, **options) ⇒ Object


130
131
132
133
134
135
136
137
# File 'lib/gds_api/test_helpers/account_api.rb', line 130

def (email:, **options)
  (
    :get,
    "/api/user/match-by-email?#{Rack::Utils.build_nested_query({ email: email })}",
    response_status: 404,
    **options,
  )
end

#stub_account_api_match_user_by_email_does_not_match(email:, **options) ⇒ Object


119
120
121
122
123
124
125
126
127
128
# File 'lib/gds_api/test_helpers/account_api.rb', line 119

def (email:, **options)
  (
    :get,
    "/api/user/match-by-email?#{Rack::Utils.build_nested_query({ email: email })}",
    response_body: {
      match: false,
    },
    **options,
  )
end

#stub_account_api_match_user_by_email_matches(email:, **options) ⇒ Object

GET /api/user/match-by-email


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

def (email:, **options)
  (
    :get,
    "/api/user/match-by-email?#{Rack::Utils.build_nested_query({ email: email })}",
    response_body: {
      match: true,
    },
    **options,
  )
end

#stub_account_api_put_email_subscription(name:, topic_slug: nil, **options) ⇒ Object

PUT /api/email-subscriptions/:name


214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/gds_api/test_helpers/account_api.rb', line 214

def (name:, topic_slug: nil, **options)
  (
    :put,
    "/api/email-subscriptions/#{name}",
    with: { body: hash_including({ topic_slug: topic_slug }.compact) },
    response_body: {
      email_subscription: {
        name: name,
        topic_slug: topic_slug || "slug",
      },
    },
    **options,
  )
end

#stub_account_api_rejects_auth_response(code: nil, state: nil) ⇒ Object


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

def (code: nil, state: nil)
  stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/callback")
    .with(body: hash_including({ code: code, state: state }.compact))
    .to_return(status: 401)
end

#stub_account_api_request(method, path, with: {}, response_status: 200, response_body: {}, govuk_account_session: nil, new_govuk_account_session: nil) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/gds_api/test_helpers/account_api.rb', line 8

def (method, path, with: {}, response_status: 200, response_body: {}, govuk_account_session: nil, new_govuk_account_session: nil)
  with.merge!(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME =>  }) if 
   = nil if response_status >= 400
  to_return = { status: response_status, body: response_body.merge(govuk_account_session: ).compact.to_json }
  if with.empty?
    stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").to_return(**to_return)
  else
    stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").with(**with).to_return(**to_return)
  end
end

#stub_account_api_set_attributes(attributes: nil, **options) ⇒ Object

PATCH /api/attributes


305
306
307
308
309
310
311
312
# File 'lib/gds_api/test_helpers/account_api.rb', line 305

def (attributes: nil, **options)
  (
    :patch,
    "/api/attributes",
    with: { body: hash_including({ attributes: attributes }.compact) },
    **options,
  )
end

#stub_account_api_unauthorized_delete_email_subscription(name:, **options) ⇒ Object


260
261
262
263
264
265
266
267
# File 'lib/gds_api/test_helpers/account_api.rb', line 260

def (name:, **options)
  (
    :delete,
    "/api/email-subscriptions/#{name}",
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_get_email_subscription(name:, **options) ⇒ Object


202
203
204
205
206
207
208
209
# File 'lib/gds_api/test_helpers/account_api.rb', line 202

def (name:, **options)
  (
    :get,
    "/api/email-subscriptions/#{name}",
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_has_attributes(attributes: [], **options) ⇒ Object


282
283
284
285
286
287
288
289
290
# File 'lib/gds_api/test_helpers/account_api.rb', line 282

def (attributes: [], **options)
  querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
  (
    :get,
    "/api/attributes?#{querystring}",
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_put_email_subscription(name:, topic_slug: nil, **options) ⇒ Object


229
230
231
232
233
234
235
236
237
# File 'lib/gds_api/test_helpers/account_api.rb', line 229

def (name:, topic_slug: nil, **options)
  (
    :put,
    "/api/email-subscriptions/#{name}",
    with: { body: hash_including({ topic_slug: topic_slug }.compact) },
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_set_attributes(attributes: nil, **options) ⇒ Object


314
315
316
317
318
319
320
321
322
# File 'lib/gds_api/test_helpers/account_api.rb', line 314

def (attributes: nil, **options)
  (
    :patch,
    "/api/attributes",
    with: { body: hash_including({ attributes: attributes }.compact) },
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_user_info(**options) ⇒ Object


95
96
97
98
99
100
101
102
# File 'lib/gds_api/test_helpers/account_api.rb', line 95

def (**options)
  (
    :get,
    "/api/user",
    response_status: 401,
    **options,
  )
end

#stub_account_api_user_info(id: "user-id", mfa: false, email: "[email protected]", email_verified: true, services: {}, **options) ⇒ Object

GET /api/user


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

def (id: "user-id", mfa: false, email: "[email protected]", email_verified: true, services: {}, **options)
  (
    :get,
    "/api/user",
    response_body: {
      id: id,
      mfa: mfa,
      email: email,
      email_verified: email_verified,
      services: services,
    },
    **options,
  )
end

#stub_account_api_user_info_service_state(service:, service_state: "yes", **options) ⇒ Object


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

def (service:, service_state: "yes", **options)
  (
    **options.merge(
      services: options.fetch(:services, {}).merge(service => service_state),
    ),
  )
end

#stub_account_api_validates_auth_response(code: nil, state: nil, govuk_account_session: "govuk-account-session", redirect_path: "/", ga_client_id: "ga-client-id", cookie_consent: false, feedback_consent: false) ⇒ Object

POST /api/oauth2/callback


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

def (code: nil, state: nil, govuk_account_session: "govuk-account-session", redirect_path: "/", ga_client_id: "ga-client-id", cookie_consent: false, feedback_consent: false)
  stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/callback")
    .with(body: hash_including({ code: code, state: state }.compact))
    .to_return(
      status: 200,
      body: { govuk_account_session: , redirect_path: redirect_path, ga_client_id: ga_client_id, cookie_consent: cookie_consent, feedback_consent: feedback_consent }.to_json,
    )
end

#stub_update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, old_email: nil, old_email_verified: nil) ⇒ Object

PATCH /api/oidc-users/:subject_identifier


162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/gds_api/test_helpers/account_api.rb', line 162

def stub_update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, old_email: nil, old_email_verified: nil)
  (
    :patch,
    "/api/oidc-users/#{subject_identifier}",
    with: { body: hash_including({ email: email, email_verified: email_verified }.compact) },
    response_body: {
      sub: subject_identifier,
      email: email || old_email,
      email_verified: email_verified || old_email_verified,
    },
  )
end