gds-api-adapters 69.1.0 → 71.1.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: 30536128d763109b2965438ade68ca08a7bc60782f3ad29f4f874ee051d687ef
4
- data.tar.gz: 18db0bcda095594ec145e1ab54a6d80b68aa62df2d4348789931b2298d80b9ed
3
+ metadata.gz: 9324b0ac6cf23eff191016dbb952b2cc4507d1071a6d7d8e463ce389f1e38024
4
+ data.tar.gz: d5b157159178227c69d0d724f49f10e299937c7c855c9cc5b55019d9995e17f2
5
5
  SHA512:
6
- metadata.gz: f488b6b0512fd5a2ae41bad2116c225b2f39446f3d01fe4e493a1d4f0e4673176561dc0a1a79a68f349fc68f92c3b4d14e3cf8a506ffd81a014f22e3a6ad5480
7
- data.tar.gz: 8cb30f4f8c075c4674a792a9647112268be2a52a77c14c7d20c2ee65f5f2fbcf082e4c6211a6603984426e887a240b0910d4d894575a0f267ba156600e1ed2b5
6
+ metadata.gz: b24b9dec516dfbd48713c6bee798e1d4579def436e284077f89f649f3ec3dbd0fc88a3b59270c07f3d51f4e972058d6521168d571e263e037de785b4e48af9b7
7
+ data.tar.gz: 37dd29babe5be2b2f11d9fc26ea2d5b3d8e9414779b3d226f6282d533ad57ea9aed10cce07ee2725109ef30885e4707aec4d9c70e7ff818572f644a390bc3ce5
data/Rakefile CHANGED
@@ -30,5 +30,5 @@ end
30
30
 
31
31
  desc "Run the linter against changed files"
32
32
  task :lint do
33
- sh "bundle exec rubocop --format clang"
33
+ sh "bundle exec rubocop"
34
34
  end
data/lib/gds_api.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "addressable"
2
2
  require "plek"
3
3
  require "time"
4
+ require "gds_api/account_api"
4
5
  require "gds_api/asset_manager"
5
6
  require "gds_api/calendars"
6
7
  require "gds_api/content_store"
@@ -21,6 +22,19 @@ require "gds_api/worldwide"
21
22
 
22
23
  # @api documented
23
24
  module GdsApi
25
+ # Creates a GdsApi::AccountApi adapter
26
+ #
27
+ # This will set a bearer token if a ACCOUNT_API_BEARER_TOKEN environment
28
+ # variable is set
29
+ #
30
+ # @return [GdsApi::AccountApi]
31
+ def self.account_api(options = {})
32
+ GdsApi::AccountApi.new(
33
+ Plek.find("account-api"),
34
+ { bearer_token: ENV["ACCOUNT_API_BEARER_TOKEN"] }.merge(options),
35
+ )
36
+ end
37
+
24
38
  # Creates a GdsApi::AssetManager adapter
25
39
  #
26
40
  # This will set a bearer token if a ASSET_MANAGER_BEARER_TOKEN environment
@@ -0,0 +1,108 @@
1
+ require_relative "base"
2
+ require_relative "exceptions"
3
+
4
+ # Adapter for the Account API
5
+ #
6
+ # @see https://github.com/alphagov/account-api
7
+ # @api documented
8
+ class GdsApi::AccountApi < GdsApi::Base
9
+ AUTH_HEADER_NAME = "GOVUK-Account-Session".freeze
10
+
11
+ # Get an OAuth sign-in URL to redirect the user to
12
+ #
13
+ # @param [String, nil] redirect_path path on GOV.UK to send the user to after authentication
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)
16
+ #
17
+ # @return [Hash] An authentication URL and the OAuth state parameter (for CSRF protection)
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
+ )
26
+ get_json("#{endpoint}/api/oauth2/sign-in?#{querystring}")
27
+ end
28
+
29
+ # Validate an OAuth authentication response
30
+ #
31
+ # @param [String] code The OAuth code parameter, from the auth server.
32
+ # @param [String] state The OAuth state parameter, from the auth server.
33
+ #
34
+ # @return [Hash] The value for the govuk_account_session header, the path to redirect the user to, and the GA client ID (if there is one)
35
+ def validate_auth_response(code:, state:)
36
+ post_json("#{endpoint}/api/oauth2/callback", code: code, state: state)
37
+ end
38
+
39
+ # Register some initial state, to pass to get_sign_in_url, which is used to initialise the account if the user signs up
40
+ #
41
+ # @param [Hash, nil] attributes Initial attributes to store
42
+ #
43
+ # @return [Hash] The state ID to pass to get_sign_in_url
44
+ def create_registration_state(attributes:)
45
+ post_json("#{endpoint}/api/oauth2/state", attributes: attributes)
46
+ end
47
+
48
+ # Check if a user has an email subscription for the Transition Checker
49
+ #
50
+ # @param [String] govuk_account_session Value of the session header
51
+ #
52
+ # @return [Hash] Whether the user has a subscription, and a new session header
53
+ def check_for_email_subscription(govuk_account_session:)
54
+ get_json("#{endpoint}/api/transition-checker-email-subscription", auth_headers(govuk_account_session))
55
+ end
56
+
57
+ # Create or update a user's email subscription for the Transition Checker
58
+ #
59
+ # @param [String] govuk_account_session Value of the session header
60
+ # @param [String] slug The email topic slug
61
+ #
62
+ # @return [Hash] Whether the user has a subscription, and a new session header
63
+ def set_email_subscription(govuk_account_session:, slug:)
64
+ post_json("#{endpoint}/api/transition-checker-email-subscription", { slug: slug }, auth_headers(govuk_account_session))
65
+ end
66
+
67
+ # Look up the values of a user's attributes
68
+ #
69
+ # @param [String] attributes Names of the attributes to check
70
+ # @param [String] govuk_account_session Value of the session header
71
+ #
72
+ # @return [Hash] The attribute values (if present), and a new session header
73
+ def get_attributes(attributes:, govuk_account_session:)
74
+ querystring = nested_query_string({ attributes: attributes }.compact)
75
+ get_json("#{endpoint}/api/attributes?#{querystring}", auth_headers(govuk_account_session))
76
+ end
77
+
78
+ # Create or update attributes for a user
79
+ #
80
+ # @param [String] attributes Hash of new attribute values
81
+ # @param [String] govuk_account_session Value of the session header
82
+ #
83
+ # @return [Hash] A new session header
84
+ def set_attributes(attributes:, govuk_account_session:)
85
+ patch_json("#{endpoint}/api/attributes", { attributes: attributes }, auth_headers(govuk_account_session))
86
+ end
87
+
88
+ # Look up the names of a user's attributes
89
+ #
90
+ # @param [String] attributes Names of the attributes to check
91
+ # @param [String] govuk_account_session Value of the session header
92
+ #
93
+ # @return [Hash] The attribute names (if present), and a new session header
94
+ def get_attributes_names(attributes:, govuk_account_session:)
95
+ querystring = nested_query_string({ attributes: attributes }.compact)
96
+ get_json("#{endpoint}/api/attributes/names?#{querystring}", auth_headers(govuk_account_session))
97
+ end
98
+
99
+ private
100
+
101
+ def nested_query_string(params)
102
+ Rack::Utils.build_nested_query(params)
103
+ end
104
+
105
+ def auth_headers(govuk_account_session)
106
+ { AUTH_HEADER_NAME => govuk_account_session }
107
+ end
108
+ end
@@ -7,12 +7,13 @@ class GdsApi::AssetManager < GdsApi::Base
7
7
  #
8
8
  # Makes a +POST+ request to the asset manager api to create an asset.
9
9
  #
10
- # The asset must be provided as a +Hash+ with a single +file+ attribute that
10
+ # The asset must be provided as a +Hash+ with a +file+ attribute that
11
11
  # behaves like a +File+ object. The +content-type+ that the asset manager will
12
- # subsequently serve will be based *only* on the file's extension (derived
13
- # from +#path+). If you supply a +content-type+ via, for example
12
+ # subsequently serve will be based on the file's extension (derived from
13
+ # +#path+). If you supply a +content-type+ via, for example
14
14
  # +ActionDispatch::Http::UploadedFile+ or another multipart wrapper, it will
15
- # be ignored.
15
+ # be ignored. To provide a +content-type+ directly you must be specify it
16
+ # as a +content_type+ attribute of the hash.
16
17
  #
17
18
  # @param asset [Hash] The attributes for the asset to send to the api. Must
18
19
  # contain +file+, which behaves like a +File+. All other attributes will be
@@ -143,12 +144,10 @@ class GdsApi::AssetManager < GdsApi::Base
143
144
  #
144
145
  # Makes a +PUT+ request to the asset manager api to update an asset.
145
146
  #
146
- # The asset must be provided as a +Hash+ with a single +file+ attribute that
147
- # behaves like a +File+ object. The +content-type+ that the asset manager will
148
- # subsequently serve will be based *only* on the file's extension (derived
149
- # from +#path+). If you supply a +content-type+ via, for example
150
- # +ActionDispatch::Http::UploadedFile+ or another multipart wrapper, it will
151
- # be ignored.
147
+ # The asset must be provided as a +Hash+ with a +file+ attribute that
148
+ # behaves like a +File+ object. The +content-type+ of the file will be based
149
+ # on the files extension unless you specify a +content_type+ attribute of
150
+ # the hash to set it.
152
151
  #
153
152
  # @param id [String] The asset identifier (a UUID).
154
153
  # @param asset [Hash] The attributes for the asset to send to the api. Must
data/lib/gds_api/base.rb CHANGED
@@ -71,7 +71,7 @@ private
71
71
  case value
72
72
  when Array
73
73
  value.map do |v|
74
- "#{CGI.escape(key.to_s + '[]')}=#{CGI.escape(v.to_s)}"
74
+ "#{CGI.escape("#{key}[]")}=#{CGI.escape(v.to_s)}"
75
75
  end
76
76
  else
77
77
  "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
@@ -93,7 +93,7 @@ private
93
93
  def prefix_destination(redirect, path, query)
94
94
  uri = URI.parse(redirect["destination"])
95
95
  start_char = redirect["path"].length
96
- suffix = path[start_char..-1]
96
+ suffix = path[start_char..]
97
97
 
98
98
  if uri.path == "" && suffix[0] != "/"
99
99
  uri.path = "/#{suffix}"
@@ -40,17 +40,6 @@ class GdsApi::EmailAlertApi < GdsApi::Base
40
40
  post_json("#{endpoint}/messages", message, headers)
41
41
  end
42
42
 
43
- # Unpublishing alert
44
- #
45
- # @param message [Hash] content_id
46
- #
47
- # Used by email-alert-service to send a message to email-alert-api
48
- # when an unpublishing message is put on the Rabbitmq queue by
49
- # publishing-api
50
- def send_unpublish_message(message)
51
- post_json("#{endpoint}/unpublish-messages", message)
52
- end
53
-
54
43
  # Get topic matches
55
44
  #
56
45
  # @param attributes [Hash] tags, links, document_type,
@@ -14,49 +14,66 @@ module GdsApi
14
14
  end
15
15
 
16
16
  class EndpointNotFound < BaseError; end
17
+
17
18
  class TimedOutException < BaseError; end
19
+
18
20
  class InvalidUrl < BaseError; end
21
+
19
22
  class SocketErrorException < BaseError; end
20
23
 
21
24
  # Superclass for all 4XX and 5XX errors
22
25
  class HTTPErrorResponse < BaseError
23
- attr_accessor :code, :error_details
26
+ attr_accessor :code, :error_details, :http_body
24
27
 
25
- def initialize(code, message = nil, error_details = nil)
28
+ def initialize(code, message = nil, error_details = nil, http_body = nil)
26
29
  super(message)
27
30
  @code = code
28
31
  @error_details = error_details
32
+ @http_body = http_body
29
33
  end
30
34
  end
31
35
 
32
36
  # Superclass & fallback for all 4XX errors
33
37
  class HTTPClientError < HTTPErrorResponse; end
38
+
34
39
  class HTTPIntermittentClientError < HTTPClientError; end
35
40
 
36
41
  class HTTPNotFound < HTTPClientError; end
42
+
37
43
  class HTTPGone < HTTPClientError; end
44
+
38
45
  class HTTPPayloadTooLarge < HTTPClientError; end
46
+
39
47
  class HTTPUnauthorized < HTTPClientError; end
48
+
40
49
  class HTTPForbidden < HTTPClientError; end
50
+
41
51
  class HTTPConflict < HTTPClientError; end
52
+
42
53
  class HTTPUnprocessableEntity < HTTPClientError; end
54
+
43
55
  class HTTPBadRequest < HTTPClientError; end
56
+
44
57
  class HTTPTooManyRequests < HTTPIntermittentClientError; end
45
58
 
46
59
  # Superclass & fallback for all 5XX errors
47
60
  class HTTPServerError < HTTPErrorResponse; end
61
+
48
62
  class HTTPIntermittentServerError < HTTPServerError; end
49
63
 
50
64
  class HTTPInternalServerError < HTTPServerError; end
65
+
51
66
  class HTTPBadGateway < HTTPIntermittentServerError; end
67
+
52
68
  class HTTPUnavailable < HTTPIntermittentServerError; end
69
+
53
70
  class HTTPGatewayTimeout < HTTPIntermittentServerError; end
54
71
 
55
72
  module ExceptionHandling
56
73
  def build_specific_http_error(error, url, details = nil)
57
74
  message = "URL: #{url}\nResponse body:\n#{error.http_body}"
58
75
  code = error.http_code
59
- error_class_for_code(code).new(code, message, details)
76
+ error_class_for_code(code).new(code, message, details, error.http_body)
60
77
  end
61
78
 
62
79
  def error_class_for_code(code)
@@ -101,10 +101,10 @@ module GdsApi
101
101
  rescue RestClient::Exception => e
102
102
  # Attempt to parse the body as JSON if possible
103
103
  error_details = begin
104
- e.http_body ? JSON.parse(e.http_body) : nil
105
- rescue JSON::ParserError
106
- nil
107
- end
104
+ e.http_body ? JSON.parse(e.http_body) : nil
105
+ rescue JSON::ParserError
106
+ nil
107
+ end
108
108
  raise build_specific_http_error(e, url, error_details)
109
109
  end
110
110
 
@@ -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"]
@@ -24,6 +24,7 @@ module GdsApi
24
24
  PATTERN = /([-a-z]+)(?:\s*=\s*([^,\s]+))?,?+/i.freeze
25
25
 
26
26
  def initialize(value = nil)
27
+ super()
27
28
  parse(value)
28
29
  end
29
30
 
@@ -67,7 +67,7 @@ module GdsApi
67
67
  #
68
68
  # @param args [Hash] A valid search query. See search-api documentation for options.
69
69
  #
70
- # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
70
+ # @see https://github.com/alphagov/search-api/blob/master/docs/search-api.md
71
71
  def search(args, additional_headers = {})
72
72
  request_url = "#{base_url}/search.json?#{Rack::Utils.build_nested_query(args)}"
73
73
  get_json(request_url, additional_headers)
@@ -77,7 +77,7 @@ module GdsApi
77
77
  #
78
78
  # @param searches [Array] An array valid search queries. Maximum of 6. See search-api documentation for options.
79
79
  #
80
- # # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
80
+ # # @see https://github.com/alphagov/search-api/blob/master/docs/search-api.md
81
81
  def batch_search(searches, additional_headers = {})
82
82
  url_friendly_searches = searches.each_with_index.map do |search, index|
83
83
  { index => search }
@@ -95,7 +95,7 @@ module GdsApi
95
95
  # @param args [Hash] A valid search query. See search-api documentation for options.
96
96
  # @param page_size [Integer] Number of results in each page.
97
97
  #
98
- # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
98
+ # @see https://github.com/alphagov/search-api/blob/master/docs/search-api.md
99
99
  def search_enum(args, page_size: 100, additional_headers: {})
100
100
  Enumerator.new do |yielder|
101
101
  (0..Float::INFINITY).step(page_size).each do |index|
@@ -120,7 +120,7 @@ module GdsApi
120
120
  # GOV.UK - we only allow deletion from metasearch
121
121
  # @return [GdsApi::Response] A status code of 202 indicates the document has been successfully queued.
122
122
  #
123
- # @see https://github.com/alphagov/search-api/blob/master/doc/documents.md
123
+ # @see https://github.com/alphagov/search-api/blob/master/docs/documents.md
124
124
  def add_document(*args)
125
125
  @api.add_document(*args)
126
126
  end
@@ -132,7 +132,7 @@ module GdsApi
132
132
  # and contacts, which may be deleted with `delete_document`.
133
133
  #
134
134
  # @param base_path Base path of the page on GOV.UK.
135
- # @see https://github.com/alphagov/search-api/blob/master/doc/content-api.md
135
+ # @see https://github.com/alphagov/search-api/blob/master/docs/content-api.md
136
136
  def delete_content(base_path)
137
137
  request_url = "#{base_url}/content?link=#{base_path}"
138
138
  delete_json(request_url)
@@ -145,7 +145,7 @@ module GdsApi
145
145
  # and contacts.
146
146
  #
147
147
  # @param base_path [String] Base path of the page on GOV.UK.
148
- # @see https://github.com/alphagov/search-api/blob/master/doc/content-api.md
148
+ # @see https://github.com/alphagov/search-api/blob/master/docs/content-api.md
149
149
  def get_content(base_path)
150
150
  request_url = "#{base_url}/content?link=#{base_path}"
151
151
  get_json(request_url)
@@ -0,0 +1,212 @@
1
+ require "json"
2
+
3
+ module GdsApi
4
+ module TestHelpers
5
+ module AccountApi
6
+ ACCOUNT_API_ENDPOINT = Plek.find("account-api")
7
+
8
+ 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")
9
+ querystring = Rack::Utils.build_nested_query({ redirect_path: redirect_path, state_id: state_id, level_of_authentication: level_of_authentication }.compact)
10
+ stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/sign-in?#{querystring}")
11
+ .to_return(
12
+ status: 200,
13
+ body: { auth_uri: auth_uri, state: state }.to_json,
14
+ )
15
+ end
16
+
17
+ 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
+ stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/callback")
19
+ .with(body: hash_including({ code: code, state: state }.compact))
20
+ .to_return(
21
+ status: 200,
22
+ body: { govuk_account_session: govuk_account_session, redirect_path: redirect_path, ga_client_id: ga_client_id }.to_json,
23
+ )
24
+ end
25
+
26
+ def stub_account_api_rejects_auth_response(code: nil, state: nil)
27
+ stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/callback")
28
+ .with(body: hash_including({ code: code, state: state }.compact))
29
+ .to_return(status: 401)
30
+ end
31
+
32
+ def stub_account_api_create_registration_state(attributes: nil, state_id: "state-id")
33
+ stub_request(:post, "#{ACCOUNT_API_ENDPOINT}/api/oauth2/state")
34
+ .with(body: hash_including({ attributes: attributes }.compact))
35
+ .to_return(
36
+ status: 200,
37
+ body: { state_id: state_id }.to_json,
38
+ )
39
+ end
40
+
41
+ def stub_account_api_has_email_subscription(**options)
42
+ stub_account_api_request(
43
+ :get,
44
+ "/api/transition-checker-email-subscription",
45
+ response_body: { has_subscription: true },
46
+ **options,
47
+ )
48
+ end
49
+
50
+ def stub_account_api_does_not_have_email_subscription(**options)
51
+ stub_account_api_request(
52
+ :get,
53
+ "/api/transition-checker-email-subscription",
54
+ response_body: { has_subscription: false },
55
+ **options,
56
+ )
57
+ end
58
+
59
+ def stub_account_api_unauthorized_get_email_subscription(**options)
60
+ stub_account_api_request(
61
+ :get,
62
+ "/api/transition-checker-email-subscription",
63
+ response_status: 401,
64
+ **options,
65
+ )
66
+ end
67
+
68
+ def stub_account_api_forbidden_get_email_subscription(needed_level_of_authentication: "level1", **options)
69
+ stub_account_api_request(
70
+ :get,
71
+ "/api/transition-checker-email-subscription",
72
+ response_status: 403,
73
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
74
+ **options,
75
+ )
76
+ end
77
+
78
+ def stub_account_api_set_email_subscription(slug: nil, **options)
79
+ stub_account_api_request(
80
+ :post,
81
+ "/api/transition-checker-email-subscription",
82
+ with: { body: hash_including({ slug: slug }.compact) },
83
+ **options,
84
+ )
85
+ end
86
+
87
+ def stub_account_api_unauthorized_set_email_subscription(slug: nil, **options)
88
+ stub_account_api_request(
89
+ :post,
90
+ "/api/transition-checker-email-subscription",
91
+ with: { body: hash_including({ slug: slug }.compact) },
92
+ response_status: 401,
93
+ **options,
94
+ )
95
+ end
96
+
97
+ def stub_account_api_forbidden_set_email_subscription(slug: nil, needed_level_of_authentication: "level1", **options)
98
+ stub_account_api_request(
99
+ :post,
100
+ "/api/transition-checker-email-subscription",
101
+ with: { body: hash_including({ slug: slug }.compact) },
102
+ response_status: 403,
103
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
104
+ **options,
105
+ )
106
+ end
107
+
108
+ def stub_account_api_has_attributes(attributes: [], values: {}, **options)
109
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
110
+ stub_account_api_request(
111
+ :get,
112
+ "/api/attributes?#{querystring}",
113
+ response_body: { values: values },
114
+ **options,
115
+ )
116
+ end
117
+
118
+ def stub_account_api_unauthorized_has_attributes(attributes: [], **options)
119
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
120
+ stub_account_api_request(
121
+ :get,
122
+ "/api/attributes?#{querystring}",
123
+ response_status: 401,
124
+ **options,
125
+ )
126
+ end
127
+
128
+ def stub_account_api_forbidden_has_attributes(attributes: [], needed_level_of_authentication: "level1", **options)
129
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
130
+ stub_account_api_request(
131
+ :get,
132
+ "/api/attributes?#{querystring}",
133
+ response_status: 403,
134
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
135
+ **options,
136
+ )
137
+ end
138
+
139
+ def stub_account_api_set_attributes(attributes: nil, **options)
140
+ stub_account_api_request(
141
+ :patch,
142
+ "/api/attributes",
143
+ with: { body: hash_including({ attributes: attributes }.compact) },
144
+ **options,
145
+ )
146
+ end
147
+
148
+ def stub_account_api_unauthorized_set_attributes(attributes: nil, **options)
149
+ stub_account_api_request(
150
+ :patch,
151
+ "/api/attributes",
152
+ with: { body: hash_including({ attributes: attributes }.compact) },
153
+ response_status: 401,
154
+ **options,
155
+ )
156
+ end
157
+
158
+ def stub_account_api_forbidden_set_attributes(attributes: nil, needed_level_of_authentication: "level1", **options)
159
+ stub_account_api_request(
160
+ :patch,
161
+ "/api/attributes",
162
+ with: { body: hash_including({ attributes: attributes }.compact) },
163
+ response_status: 403,
164
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
165
+ **options,
166
+ )
167
+ end
168
+
169
+ def stub_account_api_get_attributes_names(attributes: [], **options)
170
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
171
+ stub_account_api_request(
172
+ :get,
173
+ "/api/attributes/names?#{querystring}",
174
+ response_body: { values: attributes },
175
+ **options,
176
+ )
177
+ end
178
+
179
+ def stub_account_api_unauthorized_get_attributes_names(attributes: [], **options)
180
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
181
+ stub_account_api_request(
182
+ :get,
183
+ "/api/attributes/names?#{querystring}",
184
+ response_status: 401,
185
+ **options,
186
+ )
187
+ end
188
+
189
+ def stub_account_api_forbidden_get_attributes_names(attributes: [], needed_level_of_authentication: "level1", **options)
190
+ querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
191
+ stub_account_api_request(
192
+ :get,
193
+ "/api/attributes/names?#{querystring}",
194
+ response_status: 403,
195
+ response_body: { needed_level_of_authentication: needed_level_of_authentication },
196
+ **options,
197
+ )
198
+ end
199
+
200
+ def stub_account_api_request(method, path, with: {}, response_status: 200, response_body: {}, govuk_account_session: nil, new_govuk_account_session: nil)
201
+ with.merge!(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session }) if govuk_account_session
202
+ new_govuk_account_session = nil if response_status >= 400
203
+ to_return = { status: response_status, body: response_body.merge(govuk_account_session: new_govuk_account_session).compact.to_json }
204
+ if with.empty?
205
+ stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").to_return(**to_return)
206
+ else
207
+ stub_request(method, "#{ACCOUNT_API_ENDPOINT}#{path}").with(**with).to_return(**to_return)
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end