-
Notifications
You must be signed in to change notification settings - Fork 14
/
account_api_test.rb
336 lines (264 loc) · 13.6 KB
/
account_api_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
require "test_helper"
require "gds_api/account_api"
describe GdsApi::AccountApi do
include PactTest
let(:api_client) { GdsApi::AccountApi.new(account_api_host) }
let(:govuk_account_session) { nil }
let(:headers) { GdsApi::JsonClient.default_request_headers.merge({ "GOVUK-Account-Session" => govuk_account_session }.compact) }
let(:headers_with_json_body) { GdsApi::JsonClient.default_request_with_json_body_headers.merge({ "GOVUK-Account-Session" => govuk_account_session }.compact) }
let(:json_response_headers) { { "Content-Type" => "application/json; charset=utf-8" } }
let(:response_body_with_session_identifier) { { govuk_account_session: Pact.like("user-session-id") } }
describe "#get_sign_in_url" do
let(:path) { "/api/oauth2/sign-in" }
it "responds with 200 OK, an authentication URI, and a state for CSRF protection" do
response_body = {
auth_uri: Pact.like("http://authentication-provider/some/oauth/url"),
state: Pact.like("value-to-use-for-csrf-prevention"),
}
account_api
.upon_receiving("a sign-in request")
.with(method: :get, path: path, headers: headers)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.get_sign_in_url
end
end
describe "#validate_auth_response" do
let(:path) { "/api/oauth2/callback" }
let(:params) { { code: "code", state: "state" } }
it "responds with 200 OK and a govuk_account_session if the parameters are valid" do
account_api
.given("there is a valid OAuth response")
.upon_receiving("a validation request")
.with(method: :post, path: path, headers: headers_with_json_body, body: params)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body_with_session_identifier)
api_client.validate_auth_response(**params)
end
it "responds with 200 OK and includes the redirect_path in the response, if given" do
redirect_path = "/some-arbitrary-path"
response_body = response_body_with_session_identifier.merge(redirect_path: redirect_path)
account_api
.given("there is a valid OAuth response, with the redirect path '#{redirect_path}'")
.upon_receiving("a validation request")
.with(method: :post, path: path, headers: headers_with_json_body, body: params)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.validate_auth_response(**params)
end
it "responds with 200 OK and includes the cookie_consent in the response, if saved" do
response_body = response_body_with_session_identifier.merge(cookie_consent: true)
account_api
.given("there is a valid OAuth response, with cookie consent 'true'")
.upon_receiving("a validation request")
.with(method: :post, path: path, headers: headers_with_json_body, body: params)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.validate_auth_response(**params)
end
it "responds with 401 Unauthorized if the parameters are not valid" do
account_api
.upon_receiving("a validation request")
.with(method: :post, path: path, headers: headers_with_json_body, body: params)
.will_respond_with(status: 401)
assert_raises GdsApi::HTTPUnauthorized do
api_client.validate_auth_response(**params)
end
end
end
describe "#get_end_session_url" do
let(:path) { "/api/oauth2/end-session" }
it "responds with 200 OK and an end-session URI" do
response_body = {
end_session_uri: Pact.like("http://authentication-provider/some/end/session/url"),
}
account_api
.upon_receiving("an end-session request")
.with(method: :get, path: path, headers: headers)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.get_end_session_url
end
end
describe "#update_user_by_subject_identifier" do
let(:subject_identifier) { "the-subject-identifier" }
let(:path) { "/api/oidc-users/#{subject_identifier}" }
before do
email_attributes = {
email: "example.email.address@gov.uk",
email_verified: true,
}
response_body = email_attributes.merge(sub: subject_identifier)
account_api
.upon_receiving("a request to change the user's email attributes")
.with(method: :patch, path: path, headers: headers_with_json_body, body: email_attributes)
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.update_user_by_subject_identifier(
subject_identifier: subject_identifier,
**email_attributes,
)
end
end
describe "#delete_user_by_subject_identifier" do
let(:subject_identifier) { "the-subject-identifier" }
let(:path) { "/api/oidc-users/#{subject_identifier}" }
it "responds with 204 No Content if the user existed and has been deleted" do
account_api
.given("there is a user with subject identifier '#{subject_identifier}'")
.upon_receiving("a delete-user request for '#{subject_identifier}'")
.with(method: :delete, path: path, headers: headers)
.will_respond_with(status: 204)
api_client.delete_user_by_subject_identifier(subject_identifier: subject_identifier)
end
it "responds with 404 if the user does not exist" do
account_api
.upon_receiving("a delete-user request")
.with(method: :delete, path: path, headers: headers)
.will_respond_with(status: 404)
assert_raises GdsApi::HTTPNotFound do
api_client.delete_user_by_subject_identifier(subject_identifier: subject_identifier)
end
end
end
describe "#match_user_by_email" do
let(:email) { "email@example.com" }
let(:path) { "/api/user/match-by-email" }
it "responds with `match: false` if the user exists" do
account_api
.given("there is a user with email address '#{email}'")
.upon_receiving("a match-user-by-email request for '#{email}'")
.with(method: :get, path: path, headers: headers, query: { email: email })
.will_respond_with(status: 200, headers: json_response_headers, body: { match: Pact.like(false) })
api_client.match_user_by_email(email: email)
end
it "responds with 404 if the user does not exist" do
account_api
.upon_receiving("a match-user-by-email request for '#{email}'")
.with(method: :get, path: path, headers: headers, query: { email: email })
.will_respond_with(status: 404)
assert_raises GdsApi::HTTPNotFound do
api_client.match_user_by_email(email: email)
end
end
end
describe "the user is logged in" do
let(:govuk_account_session) { "logged-in-user-session" }
describe "#get_user" do
let(:path) { "/api/user" }
it "responds with 200 OK" do
user_details = response_body_with_session_identifier.merge(
id: Pact.like("user-id"),
mfa: Pact.like(true),
email: Pact.like("user@example.com"),
email_verified: Pact.like(true),
)
account_api
.given("there is a valid user session")
.upon_receiving("a get-user request")
.with(method: :get, path: path, headers: headers)
.will_respond_with(status: 200, headers: json_response_headers, body: user_details)
api_client.get_user(govuk_account_session: govuk_account_session)
end
end
describe "email subscriptions" do
let(:subscription_name) { "wizard-news" }
let(:path) { "/api/email-subscriptions/#{subscription_name}" }
describe "#get_email_subscription" do
it "responds with 200 OK if there is a subscription" do
subscription_json = {
name: subscription_name,
topic_slug: Pact.like("wizard-news-topic-slug"),
}
account_api
.given("there is a valid user session, with a '#{subscription_name}' email subscription")
.upon_receiving("a show-subscription request for '#{subscription_name}'")
.with(method: :get, path: path, headers: headers)
.will_respond_with(status: 200, headers: json_response_headers, body: { email_subscription: subscription_json })
api_client.get_email_subscription(name: subscription_name, govuk_account_session: govuk_account_session)
end
it "responds with 404 Not Found if there is not a subscription" do
account_api
.given("there is a valid user session")
.upon_receiving("a show-subscription request for '#{subscription_name}'")
.with(method: :get, path: path, headers: headers)
.will_respond_with(status: 404)
assert_raises GdsApi::HTTPNotFound do
api_client.get_email_subscription(name: subscription_name, govuk_account_session: govuk_account_session)
end
end
end
describe "#put_email_subscription" do
let(:topic_slug) { "wizard-news-topic-slug" }
let(:subscription_json) { { name: subscription_name, topic_slug: topic_slug } }
it "responds with 200 OK" do
response_body = response_body_with_session_identifier.merge(email_subscription: subscription_json)
account_api
.given("there is a valid user session")
.upon_receiving("a put-subscription request for '#{subscription_name}'")
.with(method: :put, path: path, headers: headers_with_json_body, body: { topic_slug: topic_slug })
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.put_email_subscription(name: subscription_name, topic_slug: topic_slug, govuk_account_session: govuk_account_session)
end
it "responds with 200 OK and updates an existing subscription" do
response_body = response_body_with_session_identifier.merge(email_subscription: subscription_json)
account_api
.given("there is a valid user session, with a '#{subscription_name}' email subscription")
.upon_receiving("a put-subscription request for '#{subscription_name}'")
.with(method: :put, path: path, headers: headers_with_json_body, body: { topic_slug: topic_slug })
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.put_email_subscription(name: subscription_name, topic_slug: topic_slug, govuk_account_session: govuk_account_session)
end
end
describe "#delete_email_subscription" do
it "responds with 204 No Content if there is a subscription" do
account_api
.given("there is a valid user session, with a '#{subscription_name}' email subscription")
.upon_receiving("a delete-subscription request for '#{subscription_name}'")
.with(method: :delete, path: path, headers: headers)
.will_respond_with(status: 204)
api_client.delete_email_subscription(name: subscription_name, govuk_account_session: govuk_account_session)
end
it "responds with 404 Not Found if there is not a subscription" do
account_api
.given("there is a valid user session")
.upon_receiving("a delete-subscription request for '#{subscription_name}'")
.with(method: :delete, path: path, headers: headers)
.will_respond_with(status: 404)
assert_raises GdsApi::HTTPNotFound do
api_client.delete_email_subscription(name: subscription_name, govuk_account_session: govuk_account_session)
end
end
end
end
describe "attributes" do
let(:path) { "/api/attributes" }
describe "#get_attributes" do
let(:attribute_name) { "feedback_consent" }
it "responds with 200 OK and no attributes, if none exist" do
response_body = response_body_with_session_identifier.merge(values: {})
account_api
.given("there is a valid user session")
.upon_receiving("a get-attributes request")
.with(method: :get, path: path, headers: headers, query: { "attributes[]" => [attribute_name] })
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.get_attributes(govuk_account_session: govuk_account_session, attributes: [attribute_name])
end
it "responds with 200 OK and the attributes, if some exist" do
response_body = response_body_with_session_identifier.merge(values: { attribute_name => true })
account_api
.given("there is a valid user session, with an attribute called '#{attribute_name}'")
.upon_receiving("a get-attributes request")
.with(method: :get, path: path, headers: headers, query: { "attributes[]" => [attribute_name] })
.will_respond_with(status: 200, headers: json_response_headers, body: response_body)
api_client.get_attributes(govuk_account_session: govuk_account_session, attributes: [attribute_name])
end
end
describe "#set_attributes" do
let(:attributes) { { feedback_consent: true } }
it "responds with 200 OK" do
account_api
.given("there is a valid user session")
.upon_receiving("a set-attributes request")
.with(method: :patch, path: path, headers: headers_with_json_body, body: { attributes: attributes })
.will_respond_with(status: 200, headers: json_response_headers, body: response_body_with_session_identifier)
api_client.set_attributes(govuk_account_session: govuk_account_session, attributes: attributes)
end
end
end
end
end