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

#cannot_save_page_problem_detail(option = {}) ⇒ Object



353
354
355
356
357
358
359
360
# File 'lib/gds_api/test_helpers/account_api.rb', line 353

def cannot_save_page_problem_detail(option = {})
  {
    title: "Cannot save page",
    detail: "Cannot save page with path #{option['page_path']}, check it is not blank, and is a well formatted url path.",
    type: "https://github.com/alphagov/account-api/blob/main/docs/api.md#cannot-save-page",
    **option,
  }
end

#stub_account_api_create_registration_state(attributes: nil, state_id: "state-id") ⇒ Object



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

def (attributes: nil, state_id: "state-id")
  stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/state")
    .with(body: hash_including({ attributes: attributes }.compact))
    .to_return(
      status: 200,
      body: { state_id: state_id }.to_json,
    )
end

#stub_account_api_delete_saved_page(page_path:, **options) ⇒ Object

DELETE /api/saved-pages/:page_path



365
366
367
368
369
370
371
372
# File 'lib/gds_api/test_helpers/account_api.rb', line 365

def (page_path:, **options)
  (
    :delete,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 204,
    **options,
  )
end

#stub_account_api_delete_saved_page_does_not_exist(page_path:, **options) ⇒ Object



374
375
376
377
378
379
380
381
# File 'lib/gds_api/test_helpers/account_api.rb', line 374

def (page_path:, **options)
  (
    :delete,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 404,
    **options,
  )
end

#stub_account_api_delete_saved_page_unauthorised(page_path:, **options) ⇒ Object



383
384
385
386
387
388
389
390
# File 'lib/gds_api/test_helpers/account_api.rb', line 383

def (page_path:, **options)
  (
    :delete,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 401,
    **options,
  )
end

#stub_account_api_does_not_have_email_subscription(**options) ⇒ Object



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

def (**options)
  (
    :get,
    "/api/transition-checker-email-subscription",
    response_body: { has_subscription: false },
    **options,
  )
end

#stub_account_api_does_not_have_saved_page(page_path:, **options) ⇒ Object



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

def (page_path:, **options)
  (
    :get,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 404,
    **options,
  )
end

#stub_account_api_forbidden_get_attributes_names(attributes: [], needed_level_of_authentication: "level1", **options) ⇒ Object



233
234
235
236
237
238
239
240
241
242
# File 'lib/gds_api/test_helpers/account_api.rb', line 233

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

#stub_account_api_forbidden_get_email_subscription(needed_level_of_authentication: "level1", **options) ⇒ Object



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

def (needed_level_of_authentication: "level1", **options)
  (
    :get,
    "/api/transition-checker-email-subscription",
    response_status: 403,
    response_body: { needed_level_of_authentication: needed_level_of_authentication },
    **options,
  )
end

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



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

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

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



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

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

#stub_account_api_forbidden_set_email_subscription(slug: nil, needed_level_of_authentication: "level1", **options) ⇒ Object



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

def (slug: nil, needed_level_of_authentication: "level1", **options)
  (
    :post,
    "/api/transition-checker-email-subscription",
    with: { body: hash_including({ slug: slug }.compact) },
    response_status: 403,
    response_body: { needed_level_of_authentication: needed_level_of_authentication },
    **options,
  )
end

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



213
214
215
216
217
218
219
220
221
# File 'lib/gds_api/test_helpers/account_api.rb', line 213

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

#stub_account_api_get_saved_page(page_path:, content_id: "46163ed2-1777-4ee6-bdd4-6a2007e49d8f", title: "Ministry of Magic", **options) ⇒ Object

GET /api/saved_pages/:page_path



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

def (page_path:, content_id: "46163ed2-1777-4ee6-bdd4-6a2007e49d8f", title: "Ministry of Magic", **options)
  (
    :get,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_body: {
      saved_page: {
        page_path: page_path,
        content_id: content_id,
        title: title,
      },
    },
    **options,
  )
end

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



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

def (redirect_path: nil, state_id: nil, level_of_authentication: nil, auth_uri: "http://auth/provider", state: "state")
  querystring = Rack::Utils.build_nested_query({ redirect_path: redirect_path, state_id: state_id, level_of_authentication: level_of_authentication }.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



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

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_has_email_subscription(**options) ⇒ Object



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

def (**options)
  (
    :get,
    "/api/transition-checker-email-subscription",
    response_body: { has_subscription: true },
    **options,
  )
end

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



26
27
28
29
30
# File 'lib/gds_api/test_helpers/account_api.rb', line 26

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



244
245
246
247
248
249
250
251
252
253
# File 'lib/gds_api/test_helpers/account_api.rb', line 244

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_returning_saved_pages(saved_pages: [], **options) ⇒ Object

GET /api/saved-pages



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

def (saved_pages: [], **options)
  (
    :get,
    "/api/saved-pages",
    response_body: { saved_pages: saved_pages },
    **options,
  )
end

#stub_account_api_save_page(page_path:, content_id: "c840bfa2-011a-42cc-ac7a-a6da990aff0b", title: "Ministry of Magic", **options) ⇒ Object

PUT /api/saved-pages/:page_path



315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/gds_api/test_helpers/account_api.rb', line 315

def (page_path:, content_id: "c840bfa2-011a-42cc-ac7a-a6da990aff0b", title: "Ministry of Magic", **options)
  (
    :put,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_body: {
      saved_page: {
        page_path: page_path,
        content_id: content_id,
        title: title,
      },
    },
    **options,
  )
end

#stub_account_api_save_page_already_exists(page_path:, **options) ⇒ Object



330
331
332
# File 'lib/gds_api/test_helpers/account_api.rb', line 330

def (page_path:, **options)
  (page_path: page_path, **options)
end

#stub_account_api_save_page_cannot_save_page(page_path:, **options) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'lib/gds_api/test_helpers/account_api.rb', line 334

def (page_path:, **options)
  (
    :put,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 422,
    response_body: cannot_save_page_problem_detail({ page_path: page_path }),
    **options,
  )
end

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



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

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

#stub_account_api_set_email_subscription(slug: nil, **options) ⇒ Object



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

def (slug: nil, **options)
  (
    :post,
    "/api/transition-checker-email-subscription",
    with: { body: hash_including({ slug: slug }.compact) },
    **options,
  )
end

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



223
224
225
226
227
228
229
230
231
# File 'lib/gds_api/test_helpers/account_api.rb', line 223

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

#stub_account_api_unauthorized_get_email_subscription(**options) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/gds_api/test_helpers/account_api.rb', line 103

def (**options)
  (
    :get,
    "/api/transition-checker-email-subscription",
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_get_saved_page(page_path:, **options) ⇒ Object



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

def (page_path:, **options)
  (
    :get,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_get_saved_pages(**options) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/gds_api/test_helpers/account_api.rb', line 267

def (**options)
  (
    :get,
    "/api/saved-pages",
    response_status: 401,
    **options,
  )
end

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



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

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_save_page(page_path:, **options) ⇒ Object



344
345
346
347
348
349
350
351
# File 'lib/gds_api/test_helpers/account_api.rb', line 344

def (page_path:, **options)
  (
    :put,
    "/api/saved-pages/#{CGI.escape(page_path)}",
    response_status: 401,
    **options,
  )
end

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



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

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

#stub_account_api_unauthorized_set_email_subscription(slug: nil, **options) ⇒ Object



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

def (slug: nil, **options)
  (
    :post,
    "/api/transition-checker-email-subscription",
    with: { body: hash_including({ slug: slug }.compact) },
    response_status: 401,
    **options,
  )
end

#stub_account_api_unauthorized_user_info(**options) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/gds_api/test_helpers/account_api.rb', line 63

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

#stub_account_api_user_info(level_of_authentication: "level0", email: "[email protected]", email_verified: true, services: {}, **options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gds_api/test_helpers/account_api.rb', line 41

def (level_of_authentication: "level0", email: "[email protected]", email_verified: true, services: {}, **options)
  (
    :get,
    "/api/user",
    response_body: {
      level_of_authentication: level_of_authentication,
      email: email,
      email_verified: email_verified,
      services: services,
    },
    **options,
  )
end

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



55
56
57
58
59
60
61
# File 'lib/gds_api/test_helpers/account_api.rb', line 55

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") ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/gds_api/test_helpers/account_api.rb', line 17

def (code: nil, state: nil, govuk_account_session: "govuk-account-session", redirect_path: "/", ga_client_id: "ga-client-id")
  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 }.to_json,
    )
end

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



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

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