gds-api-adapters 71.0.0 → 71.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f43e07de0971fb6d31f613c0e6ecf3894ddc198755ad003da36e6c43e37dedf5
4
- data.tar.gz: 66369531af1d7c4412b79994759e360b78cb67b2e842a3d6f82e10267bd18876
3
+ metadata.gz: 40bafd0ac54082d307befb62d5a9f4af3d21c53ff42ec51bcc5ee4c2e7897e9a
4
+ data.tar.gz: '029eeb35b60b20ad1da0c4f5da7162db15aa768ce539f67e39ea44e78df7e807'
5
5
  SHA512:
6
- metadata.gz: ca1f75189c94449dac72f8e3826b67c36d57e55a40103a69aa2cc48dce9cbc75eff901b060d2d360d301ec482bca4937f5178a57b3b53a1e497bb96ca1a84490
7
- data.tar.gz: 2cca74c89f2a0f827a09917fe6817aeac7199871f1f4c990845668b68c7daff9889dd2f885c809399c732d98b8c074f607072c24db8ad438a287b23f96a5dcb9
6
+ metadata.gz: f025fac520f508122ffa5a7cf17569ec1b00a60d10b418fa1d4d3e57681f15497dd3e8eaba990b29a52ca87309945e2de90cae5682a265b89ac340bc0e08547e
7
+ data.tar.gz: d0c513eda3423fba67d49250aa6d18359a09ad816a15d0523b215555e828003a6ccaf108412d91bbabcb80bed6ccf6fe67cba2590b6010c09a4667cf6ff719cb
@@ -12,10 +12,17 @@ class GdsApi::AccountApi < GdsApi::Base
12
12
  #
13
13
  # @param [String, nil] redirect_path path on GOV.UK to send the user to after authentication
14
14
  # @param [String, nil] state_id identifier originally returned by #create_registration_state
15
+ # @param [String, nil] level_of_authentication either "level1" (require MFA) or "level0" (do not require MFA)
15
16
  #
16
17
  # @return [Hash] An authentication URL and the OAuth state parameter (for CSRF protection)
17
- def get_sign_in_url(redirect_path: nil, state_id: nil)
18
- querystring = nested_query_string({ redirect_path: redirect_path, state_id: state_id }.compact)
18
+ def get_sign_in_url(redirect_path: nil, state_id: nil, level_of_authentication: nil)
19
+ querystring = nested_query_string(
20
+ {
21
+ redirect_path: redirect_path,
22
+ state_id: state_id,
23
+ level_of_authentication: level_of_authentication,
24
+ }.compact,
25
+ )
19
26
  get_json("#{endpoint}/api/oauth2/sign-in?#{querystring}")
20
27
  end
21
28
 
@@ -38,6 +45,33 @@ class GdsApi::AccountApi < GdsApi::Base
38
45
  post_json("#{endpoint}/api/oauth2/state", attributes: attributes)
39
46
  end
40
47
 
48
+ # Get all the information about a user needed to render the account home page
49
+ #
50
+ # @param [String] govuk_account_session Value of the session header
51
+ #
52
+ # @return [Hash] Information about the user and the services they've used, and a new session header
53
+ def get_user(govuk_account_session:)
54
+ get_json("#{endpoint}/api/user", auth_headers(govuk_account_session))
55
+ end
56
+
57
+ # Update the user record with privileged information from the auth service. Only the auth service will call this.
58
+ #
59
+ # @param [String] subject_identifier The identifier of the user, shared between the auth service and GOV.UK.
60
+ # @param [String, nil] email The user's current
61
+ # @param [Boolean, nil] email_verified Whether the user's current email address is verified
62
+ # @param [Boolean, nil] has_unconfirmed_email Whether the user has a new, pending, email address
63
+ #
64
+ # @return [Hash] The user's subject identifier and email attributes
65
+ def update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, has_unconfirmed_email: nil)
66
+ params = {
67
+ email: email,
68
+ email_verified: email_verified,
69
+ has_unconfirmed_email: has_unconfirmed_email,
70
+ }.compact
71
+
72
+ patch_json("#{endpoint}/api/oidc-users/#{subject_identifier}", params)
73
+ end
74
+
41
75
  # Check if a user has an email subscription for the Transition Checker
42
76
  #
43
77
  # @param [String] govuk_account_session Value of the session header
@@ -78,6 +112,86 @@ class GdsApi::AccountApi < GdsApi::Base
78
112
  patch_json("#{endpoint}/api/attributes", { attributes: attributes }, auth_headers(govuk_account_session))
79
113
  end
80
114
 
115
+ # Look up the names of a user's attributes
116
+ #
117
+ # @param [String] attributes Names of the attributes to check
118
+ # @param [String] govuk_account_session Value of the session header
119
+ #
120
+ # @return [Hash] The attribute names (if present), and a new session header
121
+ def get_attributes_names(attributes:, govuk_account_session:)
122
+ querystring = nested_query_string({ attributes: attributes }.compact)
123
+ get_json("#{endpoint}/api/attributes/names?#{querystring}", auth_headers(govuk_account_session))
124
+ end
125
+
126
+ # Get the details of an account-linked email subscription.
127
+ #
128
+ # @param [String] name Name of the subscription
129
+ # @param [String] govuk_account_session Value of the session header
130
+ #
131
+ # @return [Hash] Details of the subscription, if it exists.
132
+ def get_email_subscription(name:, govuk_account_session:)
133
+ get_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", auth_headers(govuk_account_session))
134
+ end
135
+
136
+ # Create or update an account-linked email subscription.
137
+ #
138
+ # @param [String] name Name of the subscription
139
+ # @param [String] topic_slug The email-alert-api topic slug to subscribe to
140
+ # @param [String] govuk_account_session Value of the session header
141
+ #
142
+ # @return [Hash] Details of the newly created subscription.
143
+ def put_email_subscription(name:, topic_slug:, govuk_account_session:)
144
+ put_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", { topic_slug: topic_slug }, auth_headers(govuk_account_session))
145
+ end
146
+
147
+ # Unsubscribe and delete an account-linked email subscription.
148
+ #
149
+ # @param [String] name Name of the subscription
150
+ # @param [String] govuk_account_session Value of the session header
151
+ def delete_email_subscription(name:, govuk_account_session:)
152
+ delete_json("#{endpoint}/api/email-subscriptions/#{CGI.escape(name)}", {}, auth_headers(govuk_account_session))
153
+ end
154
+
155
+ # Look up all pages saved by a user in their Account
156
+ #
157
+ # @param [String] govuk_account_session Value of the session header
158
+ #
159
+ # @return [Hash] containing :saved_pages, an array of single saved page hashes
160
+ def get_saved_pages(govuk_account_session:)
161
+ get_json("#{endpoint}/api/saved-pages", auth_headers(govuk_account_session))
162
+ end
163
+
164
+ # Return a single page by unique URL
165
+ #
166
+ # @param [String] the path of a page to check
167
+ # @param [String] govuk_account_session Value of the session header
168
+ #
169
+ # @return [Hash] containing :saved_page, a hash of a single saved page value
170
+ def get_saved_page(page_path:, govuk_account_session:)
171
+ get_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", auth_headers(govuk_account_session))
172
+ end
173
+
174
+ # Upsert a single saved page entry in a users account
175
+ #
176
+ # @param [String] the path of a page to check
177
+ # @param [String] govuk_account_session Value of the session header
178
+ #
179
+ # @return [Hash] A single saved page value (if sucessful)
180
+ def save_page(page_path:, govuk_account_session:)
181
+ put_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", {}, auth_headers(govuk_account_session))
182
+ end
183
+
184
+ # Delete a single saved page entry from a users account
185
+ #
186
+ # @param [String] the path of a page to check
187
+ # @param [String] govuk_account_session Value of the session header
188
+ #
189
+ # @return [GdsApi::Response] A status code of 204 indicates the saved page has been successfully deleted.
190
+ # A status code of 404 indicates there is no saved page with this path.
191
+ def delete_saved_page(page_path:, govuk_account_session:)
192
+ delete_json("#{endpoint}/api/saved-pages/#{CGI.escape(page_path)}", {}, auth_headers(govuk_account_session))
193
+ end
194
+
81
195
  private
82
196
 
83
197
  def nested_query_string(params)
@@ -23,12 +23,13 @@ module GdsApi
23
23
 
24
24
  # Superclass for all 4XX and 5XX errors
25
25
  class HTTPErrorResponse < BaseError
26
- attr_accessor :code, :error_details
26
+ attr_accessor :code, :error_details, :http_body
27
27
 
28
- def initialize(code, message = nil, error_details = nil)
28
+ def initialize(code, message = nil, error_details = nil, http_body = nil)
29
29
  super(message)
30
30
  @code = code
31
31
  @error_details = error_details
32
+ @http_body = http_body
32
33
  end
33
34
  end
34
35
 
@@ -72,7 +73,7 @@ module GdsApi
72
73
  def build_specific_http_error(error, url, details = nil)
73
74
  message = "URL: #{url}\nResponse body:\n#{error.http_body}"
74
75
  code = error.http_code
75
- error_class_for_code(code).new(code, message, details)
76
+ error_class_for_code(code).new(code, message, details, error.http_body)
76
77
  end
77
78
 
78
79
  def error_class_for_code(code)
@@ -180,10 +180,10 @@ module GdsApi
180
180
  raise GdsApi::EndpointNotFound, "Could not connect to #{url}"
181
181
  rescue RestClient::Exceptions::Timeout => e
182
182
  logger.error loggable.merge(status: "timeout", error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
183
- raise GdsApi::TimedOutException
183
+ raise GdsApi::TimedOutException, e.message
184
184
  rescue URI::InvalidURIError => e
185
185
  logger.error loggable.merge(status: "invalid_uri", error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
186
- raise GdsApi::InvalidUrl
186
+ raise GdsApi::InvalidUrl, e.message
187
187
  rescue RestClient::Exception => e
188
188
  # Log the error here, since we have access to loggable, but raise the
189
189
  # exception up to the calling method to deal with
@@ -192,10 +192,10 @@ module GdsApi
192
192
  raise
193
193
  rescue Errno::ECONNRESET => e
194
194
  logger.error loggable.merge(status: "connection_reset", error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
195
- raise GdsApi::TimedOutException
195
+ raise GdsApi::TimedOutException, e.message
196
196
  rescue SocketError => e
197
197
  logger.error loggable.merge(status: "socket_error", error_message: e.message, error_class: e.class.name, end_time: Time.now.to_f).to_json
198
- raise GdsApi::SocketErrorException
198
+ raise GdsApi::SocketErrorException, e.message
199
199
  end
200
200
  end
201
201
  end
@@ -89,37 +89,6 @@ class GdsApi::LinkCheckerApi < GdsApi::Base
89
89
  )
90
90
  end
91
91
 
92
- # Update or create a set of links to be monitored for a resource.
93
- #
94
- # Makes a +POST+ request to the link checker api to create a resource monitor.
95
- #
96
- # @param links [Array] A list of URIs to monitor.
97
- # @param reference [String] A unique id for the resource being monitored
98
- # @param app [String] The name of the service the call originated e.g. 'whitehall'
99
- # @return [MonitorReport] A +SimpleDelegator+ of the +GdsApi::Response+ which
100
- # responds to:
101
- # :id the ID of the created resource monitor
102
- #
103
- # @raise [HTTPErrorResponse] if the request returns an error
104
-
105
- def upsert_resource_monitor(links, app, reference)
106
- payload = {
107
- links: links,
108
- app: app,
109
- reference: reference,
110
- }
111
-
112
- response = post_json("#{endpoint}/monitor", payload)
113
-
114
- MonitorReport.new(response.to_hash)
115
- end
116
-
117
- class MonitorReport < SimpleDelegator
118
- def id
119
- self["id"]
120
- end
121
- end
122
-
123
92
  class LinkReport < SimpleDelegator
124
93
  def uri
125
94
  self["uri"]
@@ -5,8 +5,22 @@ module GdsApi
5
5
  module AccountApi
6
6
  ACCOUNT_API_ENDPOINT = Plek.find("account-api")
7
7
 
8
- def stub_account_api_get_sign_in_url(redirect_path: nil, state_id: nil, auth_uri: "http://auth/provider", state: "state")
9
- querystring = Rack::Utils.build_nested_query({ redirect_path: redirect_path, state_id: state_id }.compact)
8
+ def stub_account_api_request(method, path, with: {}, response_status: 200, response_body: {}, govuk_account_session: nil, new_govuk_account_session: nil)
9
+ with.merge!(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session }) if govuk_account_session
10
+ new_govuk_account_session = nil if response_status >= 400
11
+ to_return = { status: response_status, body: response_body.merge(govuk_account_session: new_govuk_account_session).compact.to_json }
12
+ if with.empty?
13
+ stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").to_return(**to_return)
14
+ else
15
+ stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").with(**with).to_return(**to_return)
16
+ end
17
+ end
18
+
19
+ #########################
20
+ # GET /api/oauth2/sign-in
21
+ #########################
22
+ def stub_account_api_get_sign_in_url(redirect_path: nil, state_id: nil, level_of_authentication: nil, auth_uri: "http://auth/provider", state: "state")
23
+ querystring = Rack::Utils.build_nested_query({ redirect_path: redirect_path, state_id: state_id, level_of_authentication: level_of_authentication }.compact)
10
24
  stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/sign-in?#{querystring}")
11
25
  .to_return(
12
26
  status: 200,
@@ -14,6 +28,9 @@ module GdsApi
14
28
  )
15
29
  end
16
30
 
31
+ ###########################
32
+ # POST /api/oauth2/callback
33
+ ###########################
17
34
  def stub_account_api_validates_auth_response(code: nil, state: nil, govuk_account_session: "govuk-account-session", redirect_path: "/", ga_client_id: "ga-client-id")
18
35
  stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/callback")
19
36
  .with(body: hash_including({ code: code, state: state }.compact))
@@ -29,6 +46,9 @@ module GdsApi
29
46
  .to_return(status: 401)
30
47
  end
31
48
 
49
+ ########################
50
+ # POST /api/oauth2/state
51
+ ########################
32
52
  def stub_account_api_create_registration_state(attributes: nil, state_id: "state-id")
33
53
  stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/state")
34
54
  .with(body: hash_including({ attributes: attributes }.compact))
@@ -38,62 +58,461 @@ module GdsApi
38
58
  )
39
59
  end
40
60
 
41
- def stub_account_api_has_email_subscription(govuk_account_session: nil, new_govuk_account_session: nil)
42
- if govuk_account_session
43
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
44
- .with(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
45
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, has_subscription: true }.compact.to_json)
46
- else
47
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
48
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, has_subscription: true }.compact.to_json)
49
- end
61
+ ###############
62
+ # GET /api/user
63
+ ###############
64
+ def stub_account_api_user_info(level_of_authentication: "level0", email: "email@example.com", email_verified: true, has_unconfirmed_email: false, services: {}, **options)
65
+ stub_account_api_request(
66
+ :get,
67
+ "/api/user",
68
+ response_body: {
69
+ level_of_authentication: level_of_authentication,
70
+ email: email,
71
+ email_verified: email_verified,
72
+ has_unconfirmed_email: has_unconfirmed_email,
73
+ services: services,
74
+ },
75
+ **options,
76
+ )
50
77
  end
51
78
 
52
- def stub_account_api_does_not_have_email_subscription(govuk_account_session: nil, new_govuk_account_session: nil)
53
- if govuk_account_session
54
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
55
- .with(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
56
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, has_subscription: false }.compact.to_json)
57
- else
58
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
59
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, has_subscription: false }.compact.to_json)
60
- end
79
+ def stub_account_api_user_info_service_state(service:, service_state: "yes", **options)
80
+ stub_account_api_user_info(
81
+ **options.merge(
82
+ services: options.fetch(:services, {}).merge(service => service_state),
83
+ ),
84
+ )
61
85
  end
62
86
 
63
- def stub_account_api_set_email_subscription(govuk_account_session: nil, slug: "slug", new_govuk_account_session: nil)
64
- if govuk_account_session
65
- stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
66
- .with(body: hash_including({ slug: slug }.compact), headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
67
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
68
- else
69
- stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/transition-checker-email-subscription")
70
- .with(body: hash_including({ slug: slug }.compact))
71
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
72
- end
87
+ def stub_account_api_unauthorized_user_info(**options)
88
+ stub_account_api_request(
89
+ :get,
90
+ "/api/user",
91
+ response_status: 401,
92
+ **options,
93
+ )
94
+ end
95
+
96
+ ###########################################
97
+ # PATCH /api/oidc-users/:subject_identifier
98
+ ###########################################
99
+ def stub_update_user_by_subject_identifier(subject_identifier:, email: nil, email_verified: nil, has_unconfirmed_email: nil, old_email: nil, old_email_verified: nil, old_has_unconfirmed_email: nil)
100
+ stub_account_api_request(
101
+ :patch,
102
+ "/api/oidc-users/#{subject_identifier}",
103
+ with: { body: hash_including({ email: email, email_verified: email_verified, has_unconfirmed_email: has_unconfirmed_email }.compact) },
104
+ response_body: {
105
+ sub: subject_identifier,
106
+ email: email || old_email,
107
+ email_verified: email_verified || old_email_verified,
108
+ has_unconfirmed_email: has_unconfirmed_email || old_has_unconfirmed_email,
109
+ },
110
+ )
111
+ end
112
+
113
+ ####################################
114
+ # GET /api/email-subscriptions/:name
115
+ ####################################
116
+ def stub_account_api_get_email_subscription(name:, topic_slug: "slug", email_alert_api_subscription_id: "12345", **options)
117
+ stub_account_api_request(
118
+ :get,
119
+ "/api/email-subscriptions/#{name}",
120
+ response_body: {
121
+ email_subscription: {
122
+ name: name,
123
+ topic_slug: topic_slug,
124
+ email_alert_api_subscription_id: email_alert_api_subscription_id,
125
+ },
126
+ },
127
+ **options,
128
+ )
129
+ end
130
+
131
+ def stub_account_api_get_email_subscription_does_not_exist(name:, **options)
132
+ stub_account_api_request(
133
+ :get,
134
+ "/api/email-subscriptions/#{name}",
135
+ response_status: 404,
136
+ **options,
137
+ )
138
+ end
139
+
140
+ def stub_account_api_get_email_subscription_unauthorized(name:, **options)
141
+ stub_account_api_request(
142
+ :get,
143
+ "/api/email-subscriptions/#{name}",
144
+ response_status: 401,
145
+ **options,
146
+ )
147
+ end
148
+
149
+ ####################################
150
+ # PUT /api/email-subscriptions/:name
151
+ ####################################
152
+ def stub_account_api_put_email_subscription(name:, topic_slug: nil, **options)
153
+ stub_account_api_request(
154
+ :put,
155
+ "/api/email-subscriptions/#{name}",
156
+ with: { body: hash_including({ topic_slug: topic_slug }.compact) },
157
+ response_body: {
158
+ email_subscription: {
159
+ name: name,
160
+ topic_slug: topic_slug || "slug",
161
+ },
162
+ },
163
+ **options,
164
+ )
165
+ end
166
+
167
+ def stub_account_api_unauthorized_put_email_subscription(name:, topic_slug: nil, **options)
168
+ stub_account_api_request(
169
+ :put,
170
+ "/api/email-subscriptions/#{name}",
171
+ with: { body: hash_including({ topic_slug: topic_slug }.compact) },
172
+ response_status: 401,
173
+ **options,
174
+ )
175
+ end
176
+
177
+ #######################################
178
+ # DELETE /api/email-subscriptions/:name
179
+ #######################################
180
+ def stub_account_api_delete_email_subscription(name:, **options)
181
+ stub_account_api_request(
182
+ :delete,
183
+ "/api/email-subscriptions/#{name}",
184
+ response_status: 204,
185
+ **options,
186
+ )
187
+ end
188
+
189
+ def stub_account_api_delete_email_subscription_does_not_exist(name:, **options)
190
+ stub_account_api_request(
191
+ :delete,
192
+ "/api/email-subscriptions/#{name}",
193
+ response_status: 404,
194
+ **options,
195
+ )
196
+ end
197
+
198
+ def stub_account_api_unauthorized_delete_email_subscription(name:, **options)
199
+ stub_account_api_request(
200
+ :delete,
201
+ "/api/email-subscriptions/#{name}",
202
+ response_status: 401,
203
+ **options,
204
+ )
205
+ end
206
+
207
+ ################################################
208
+ # GET /api/transition-checker-email-subscription
209
+ ################################################
210
+ def stub_account_api_has_email_subscription(**options)
211
+ stub_account_api_request(
212
+ :get,
213
+ "/api/transition-checker-email-subscription",
214
+ response_body: { has_subscription: true },
215
+ **options,
216
+ )
217
+ end
218
+
219
+ def stub_account_api_does_not_have_email_subscription(**options)
220
+ stub_account_api_request(
221
+ :get,
222
+ "/api/transition-checker-email-subscription",
223
+ response_body: { has_subscription: false },
224
+ **options,
225
+ )
73
226
  end
74
227
 
75
- def stub_account_api_has_attributes(govuk_account_session: nil, attributes: [], values: {}, new_govuk_account_session: nil)
228
+ def stub_account_api_unauthorized_get_email_subscription(**options)
229
+ stub_account_api_request(
230
+ :get,
231
+ "/api/transition-checker-email-subscription",
232
+ response_status: 401,
233
+ **options,
234
+ )
235
+ end
236
+
237
+ def stub_account_api_forbidden_get_email_subscription(needed_level_of_authentication: "level1", **options)
238
+ stub_account_api_request(
239
+ :get,
240
+ "/api/transition-checker-email-subscription",
241
+ response_status: 403,
242
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
243
+ **options,
244
+ )
245
+ end
246
+
247
+ #################################################
248
+ # POST /api/transition-checker-email-subscription
249
+ #################################################
250
+ def stub_account_api_set_email_subscription(slug: nil, **options)
251
+ stub_account_api_request(
252
+ :post,
253
+ "/api/transition-checker-email-subscription",
254
+ with: { body: hash_including({ slug: slug }.compact) },
255
+ **options,
256
+ )
257
+ end
258
+
259
+ def stub_account_api_unauthorized_set_email_subscription(slug: nil, **options)
260
+ stub_account_api_request(
261
+ :post,
262
+ "/api/transition-checker-email-subscription",
263
+ with: { body: hash_including({ slug: slug }.compact) },
264
+ response_status: 401,
265
+ **options,
266
+ )
267
+ end
268
+
269
+ def stub_account_api_forbidden_set_email_subscription(slug: nil, needed_level_of_authentication: "level1", **options)
270
+ stub_account_api_request(
271
+ :post,
272
+ "/api/transition-checker-email-subscription",
273
+ with: { body: hash_including({ slug: slug }.compact) },
274
+ response_status: 403,
275
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
276
+ **options,
277
+ )
278
+ end
279
+
280
+ #####################
281
+ # GET /api/attributes
282
+ #####################
283
+ def stub_account_api_has_attributes(attributes: [], values: {}, **options)
76
284
  querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
77
- if govuk_account_session
78
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/attributes?#{querystring}")
79
- .with(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
80
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, values: values }.compact.to_json)
81
- else
82
- stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/attributes?#{querystring}")
83
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, values: values }.compact.to_json)
84
- end
285
+ stub_account_api_request(
286
+ :get,
287
+ "/api/attributes?#{querystring}",
288
+ response_body: { values: values },
289
+ **options,
290
+ )
85
291
  end
86
292
 
87
- def stub_account_api_set_attributes(govuk_account_session: nil, attributes: nil, new_govuk_account_session: nil)
88
- if govuk_account_session
89
- stub_request(:patch, "#{ACCOUNT_API_ENDPOINT}/api/attributes")
90
- .with(body: hash_including({ attributes: attributes }.compact), headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
91
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
92
- else
93
- stub_request(:patch, "#{ACCOUNT_API_ENDPOINT}/api/attributes")
94
- .with(body: hash_including({ attributes: attributes }.compact))
95
- .to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
96
- end
293
+ def stub_account_api_unauthorized_has_attributes(attributes: [], **options)
294
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
295
+ stub_account_api_request(
296
+ :get,
297
+ "/api/attributes?#{querystring}",
298
+ response_status: 401,
299
+ **options,
300
+ )
301
+ end
302
+
303
+ def stub_account_api_forbidden_has_attributes(attributes: [], needed_level_of_authentication: "level1", **options)
304
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
305
+ stub_account_api_request(
306
+ :get,
307
+ "/api/attributes?#{querystring}",
308
+ response_status: 403,
309
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
310
+ **options,
311
+ )
312
+ end
313
+
314
+ #######################
315
+ # PATCH /api/attributes
316
+ #######################
317
+ def stub_account_api_set_attributes(attributes: nil, **options)
318
+ stub_account_api_request(
319
+ :patch,
320
+ "/api/attributes",
321
+ with: { body: hash_including({ attributes: attributes }.compact) },
322
+ **options,
323
+ )
324
+ end
325
+
326
+ def stub_account_api_unauthorized_set_attributes(attributes: nil, **options)
327
+ stub_account_api_request(
328
+ :patch,
329
+ "/api/attributes",
330
+ with: { body: hash_including({ attributes: attributes }.compact) },
331
+ response_status: 401,
332
+ **options,
333
+ )
334
+ end
335
+
336
+ def stub_account_api_forbidden_set_attributes(attributes: nil, needed_level_of_authentication: "level1", **options)
337
+ stub_account_api_request(
338
+ :patch,
339
+ "/api/attributes",
340
+ with: { body: hash_including({ attributes: attributes }.compact) },
341
+ response_status: 403,
342
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
343
+ **options,
344
+ )
345
+ end
346
+
347
+ ###########################
348
+ # GET /api/attributes/names
349
+ ###########################
350
+ def stub_account_api_get_attributes_names(attributes: [], **options)
351
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
352
+ stub_account_api_request(
353
+ :get,
354
+ "/api/attributes/names?#{querystring}",
355
+ response_body: { values: attributes },
356
+ **options,
357
+ )
358
+ end
359
+
360
+ def stub_account_api_unauthorized_get_attributes_names(attributes: [], **options)
361
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
362
+ stub_account_api_request(
363
+ :get,
364
+ "/api/attributes/names?#{querystring}",
365
+ response_status: 401,
366
+ **options,
367
+ )
368
+ end
369
+
370
+ def stub_account_api_forbidden_get_attributes_names(attributes: [], needed_level_of_authentication: "level1", **options)
371
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
372
+ stub_account_api_request(
373
+ :get,
374
+ "/api/attributes/names?#{querystring}",
375
+ response_status: 403,
376
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
377
+ **options,
378
+ )
379
+ end
380
+
381
+ ######################
382
+ # GET /api/saved-pages
383
+ ######################
384
+ def stub_account_api_returning_saved_pages(saved_pages: [], **options)
385
+ stub_account_api_request(
386
+ :get,
387
+ "/api/saved-pages",
388
+ response_body: { saved_pages: saved_pages },
389
+ **options,
390
+ )
391
+ end
392
+
393
+ def stub_account_api_unauthorized_get_saved_pages(**options)
394
+ stub_account_api_request(
395
+ :get,
396
+ "/api/saved-pages",
397
+ response_status: 401,
398
+ **options,
399
+ )
400
+ end
401
+
402
+ #################################
403
+ # GET /api/saved_pages/:page_path
404
+ #################################
405
+ def stub_account_api_get_saved_page(page_path:, content_id: "46163ed2-1777-4ee6-bdd4-6a2007e49d8f", title: "Ministry of Magic", **options)
406
+ stub_account_api_request(
407
+ :get,
408
+ "/api/saved-pages/#{CGI.escape(page_path)}",
409
+ response_body: {
410
+ saved_page: {
411
+ page_path: page_path,
412
+ content_id: content_id,
413
+ title: title,
414
+ },
415
+ },
416
+ **options,
417
+ )
418
+ end
419
+
420
+ def stub_account_api_does_not_have_saved_page(page_path:, **options)
421
+ stub_account_api_request(
422
+ :get,
423
+ "/api/saved-pages/#{CGI.escape(page_path)}",
424
+ response_status: 404,
425
+ **options,
426
+ )
427
+ end
428
+
429
+ def stub_account_api_unauthorized_get_saved_page(page_path:, **options)
430
+ stub_account_api_request(
431
+ :get,
432
+ "/api/saved-pages/#{CGI.escape(page_path)}",
433
+ response_status: 401,
434
+ **options,
435
+ )
436
+ end
437
+
438
+ #################################
439
+ # PUT /api/saved-pages/:page_path
440
+ #################################
441
+ def stub_account_api_save_page(page_path:, content_id: "c840bfa2-011a-42cc-ac7a-a6da990aff0b", title: "Ministry of Magic", **options)
442
+ stub_account_api_request(
443
+ :put,
444
+ "/api/saved-pages/#{CGI.escape(page_path)}",
445
+ response_body: {
446
+ saved_page: {
447
+ page_path: page_path,
448
+ content_id: content_id,
449
+ title: title,
450
+ },
451
+ },
452
+ **options,
453
+ )
454
+ end
455
+
456
+ def stub_account_api_save_page_already_exists(page_path:, **options)
457
+ stub_account_api_save_page(page_path: page_path, **options)
458
+ end
459
+
460
+ def stub_account_api_save_page_cannot_save_page(page_path:, **options)
461
+ stub_account_api_request(
462
+ :put,
463
+ "/api/saved-pages/#{CGI.escape(page_path)}",
464
+ response_status: 422,
465
+ response_body: cannot_save_page_problem_detail({ page_path: page_path }),
466
+ **options,
467
+ )
468
+ end
469
+
470
+ def stub_account_api_unauthorized_save_page(page_path:, **options)
471
+ stub_account_api_request(
472
+ :put,
473
+ "/api/saved-pages/#{CGI.escape(page_path)}",
474
+ response_status: 401,
475
+ **options,
476
+ )
477
+ end
478
+
479
+ def cannot_save_page_problem_detail(option = {})
480
+ {
481
+ title: "Cannot save page",
482
+ detail: "Cannot save page with path #{option['page_path']}, check it is not blank, and is a well formatted url path.",
483
+ type: "https://github.com/alphagov/account-api/blob/main/docs/api.md#cannot-save-page",
484
+ **option,
485
+ }
486
+ end
487
+
488
+ ####################################
489
+ # DELETE /api/saved-pages/:page_path
490
+ ####################################
491
+ def stub_account_api_delete_saved_page(page_path:, **options)
492
+ stub_account_api_request(
493
+ :delete,
494
+ "/api/saved-pages/#{CGI.escape(page_path)}",
495
+ response_status: 204,
496
+ **options,
497
+ )
498
+ end
499
+
500
+ def stub_account_api_delete_saved_page_does_not_exist(page_path:, **options)
501
+ stub_account_api_request(
502
+ :delete,
503
+ "/api/saved-pages/#{CGI.escape(page_path)}",
504
+ response_status: 404,
505
+ **options,
506
+ )
507
+ end
508
+
509
+ def stub_account_api_delete_saved_page_unauthorised(page_path:, **options)
510
+ stub_account_api_request(
511
+ :delete,
512
+ "/api/saved-pages/#{CGI.escape(page_path)}",
513
+ response_status: 401,
514
+ **options,
515
+ )
97
516
  end
98
517
  end
99
518
  end
@@ -72,23 +72,6 @@ module GdsApi
72
72
  headers: { "Content-Type" => "application/json" },
73
73
  )
74
74
  end
75
-
76
- def stub_link_checker_api_upsert_resource_monitor(app:, reference:, links:)
77
- response_body = { id: 1 }.to_json
78
-
79
- request_body = {
80
- links: links,
81
- app: app,
82
- reference: reference,
83
- }.to_json
84
-
85
- stub_request(:post, "#{LINK_CHECKER_API_ENDPOINT}/monitor")
86
- .with(body: request_body)
87
- .to_return(
88
- body: response_body,
89
- headers: { "Content-Type" => "application/json" },
90
- )
91
- end
92
75
  end
93
76
  end
94
77
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = "71.0.0".freeze
2
+ VERSION = "71.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 71.0.0
4
+ version: 71.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-22 00:00:00.000000000 Z
11
+ date: 2021-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -445,7 +445,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
445
445
  - !ruby/object:Gem::Version
446
446
  version: '0'
447
447
  requirements: []
448
- rubygems_version: 3.1.4
448
+ rubygems_version: 3.0.3
449
449
  signing_key:
450
450
  specification_version: 4
451
451
  summary: Adapters to work with GDS APIs