Skip to content

Commit

Permalink
Add account-api attribute endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
barrucadu committed Mar 12, 2021
1 parent 15764a0 commit d449899
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* BREAKING: Minimum ruby version supported is updated to 2.6
* BREAKING: `content_store_endpoint` helper now accepts a keyword argument instead of a boolean
* Add new account-api adapter with auth and transition checker email subscription endpoints
* Add new account-api adapter with auth, transition checker email subscription, and attribute endpoints

# 69.3.0

Expand Down
21 changes: 21 additions & 0 deletions lib/gds_api/account_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ def set_email_subscription(govuk_account_session:, slug:)
post_json("#{endpoint}/api/transition-checker-email-subscription", { slug: slug }, auth_headers(govuk_account_session))
end

# Look up the values of a user's attributes
#
# @param [String] attributes Names of the attributes to check
# @param [String] govuk_account_session Value of the session header
#
# @return [Hash] The attribute values (if present), and a new session header
def get_attributes(attributes:, govuk_account_session:)
querystring = nested_query_string({ attributes: attributes }.compact)
get_json("#{endpoint}/api/attributes?#{querystring}", auth_headers(govuk_account_session))
end

# Create or update attributes for a user
#
# @param [String] attributes Hash of new attribute values
# @param [String] govuk_account_session Value of the session header
#
# @return [Hash] A new session header
def set_attributes(attributes:, govuk_account_session:)
patch_json("#{endpoint}/api/attributes", { attributes: attributes.transform_values(&:to_json) }, auth_headers(govuk_account_session))
end

private

def nested_query_string(params)
Expand Down
24 changes: 24 additions & 0 deletions lib/gds_api/test_helpers/account_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ def stub_account_api_set_email_subscription(govuk_account_session: nil, slug: "s
.to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
end
end

def stub_account_api_has_attributes(govuk_account_session: nil, attributes: [], values: {}, new_govuk_account_session: nil)
querystring = Rack::Utils.build_nested_query({ attributes: attributes }.compact)
if govuk_account_session
stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/attributes?#{querystring}")
.with(headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
.to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, values: values }.compact.to_json)
else
stub_request(:get, "#{ACCOUNT_API_ENDPOINT}/api/attributes?#{querystring}")
.to_return(status: 200, body: { govuk_account_session: new_govuk_account_session, values: values }.compact.to_json)
end
end

def stub_account_api_set_attributes(govuk_account_session: nil, attributes: nil, new_govuk_account_session: nil)
if govuk_account_session
stub_request(:patch, "#{ACCOUNT_API_ENDPOINT}/api/attributes")
.with(body: hash_including({ attributes: attributes&.transform_values(&:to_json) }.compact), headers: { GdsApi::AccountApi::AUTH_HEADER_NAME => govuk_account_session })
.to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
else
stub_request(:patch, "#{ACCOUNT_API_ENDPOINT}/api/attributes")
.with(body: hash_including({ attributes: attributes&.transform_values(&:to_json) }.compact))
.to_return(status: 200, body: { govuk_account_session: new_govuk_account_session }.compact.to_json)
end
end
end
end
end
19 changes: 19 additions & 0 deletions test/account_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@
stub_account_api_set_email_subscription(new_govuk_account_session: "new-session")
assert_equal("new-session", api_client.set_email_subscription(govuk_account_session: "foo", slug: "slug").to_hash["govuk_account_session"])
end

describe "attributes exist" do
before { stub_account_api_has_attributes(attributes: attributes.keys, values: attributes, new_govuk_account_session: "new-session") }

let(:attributes) { { "foo" => { "bar" => %w[baz] } } }

it "returns the attribute values" do
assert(api_client.get_attributes(attributes: attributes.keys, govuk_account_session: "foo")["values"] == attributes)
end

it "returns the new session value" do
assert_equal("new-session", api_client.get_attributes(attributes: attributes.keys, govuk_account_session: "foo")["govuk_account_session"])
end
end

it "returns a new session when setting attributes" do
stub_account_api_set_attributes(attributes: { foo: %w[bar] }, new_govuk_account_session: "new-session")
assert_equal("new-session", api_client.set_attributes(govuk_account_session: "foo", attributes: { foo: %w[bar] }).to_hash["govuk_account_session"])
end
end

0 comments on commit d449899

Please sign in to comment.