Skip to content

Commit

Permalink
Merge pull request #1316 from alphagov/content-modelling/783-add-api-…
Browse files Browse the repository at this point in the history
…adapters-for-signon

(783) Add an API adapter for the signon users endpoint
  • Loading branch information
pezholio authored Dec 20, 2024
2 parents 3b7f6e0 + ef25e9b commit 1ee5bb6
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Add support for the the Signon API users endpoint [PR](https://github.com/alphagov/gds-api-adapters/pull/1316)

## 98.1.0

* Add method to get GraphQL responses in a similar format to Content Store content items [PR](https://github.com/alphagov/gds-api-adapters/pull/1314).
Expand Down
14 changes: 14 additions & 0 deletions lib/gds_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require "gds_api/publishing_api"
require "gds_api/search"
require "gds_api/search_api_v2"
require "gds_api/signon_api"
require "gds_api/support"
require "gds_api/support_api"
require "gds_api/worldwide"
Expand Down Expand Up @@ -154,6 +155,19 @@ def self.publishing_api(options = {})
)
end

# Creates a GdsApi::SignonApi adapter
#
# This will set a bearer token if a PUBLISHING_API_BEARER_TOKEN environment
# variable is set
#
# @return [GdsApi::SignonApi]
def self.signon_api(options = {})
GdsApi::SignonApi.new(
Plek.find("signon"),
{ bearer_token: ENV["SIGNON_API_BEARER_TOKEN"] }.merge(options),
)
end

# Creates a GdsApi::Search adapter to access via a search.* hostname
#
# @return [GdsApi::Search]
Expand Down
17 changes: 17 additions & 0 deletions lib/gds_api/signon_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative "base"

class GdsApi::SignonApi < GdsApi::Base
# Get users with specific UUIDs
#
# @param uuids [Array]
#
# signon_api.users(
# ["7ac47b33-c09c-4c1d-a9a7-0cfef99081ac"],
# )
#
# @return [GdsApi::Response] A response containing a list of users with the specified UUIDs
def get_users(uuids:)
query = query_string(uuids:)
get_json("#{endpoint}/api/users#{query}")
end
end
52 changes: 52 additions & 0 deletions test/pacts/signon_api_pact_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "test_helper"
require "gds_api/support_api"

describe "GdsApi::SignonApi pact tests" do
include PactTest

describe "#get_users" do
let(:bearer_token) { "SOME_BEARER_TOKEN" }
let(:api_client) { GdsApi::SignonApi.new(signon_api_host, { bearer_token: }) }

it "returns a list of users" do
uuids = %w[9ef9779f-3cba-481a-9a73-00d39e33eb7b b55873b4-bc83-4efe-bdc9-6b7d381a723e 64c7d994-17e0-44d9-97b0-87b43a581eb9]
signon_api
.given("users exist with the UUIDs #{uuids[0]}, #{uuids[1]} and #{uuids[2]}")
.upon_receiving("a raise ticket request")
.with(
method: :get,
path: "/api/users",
headers: GdsApi::JsonClient.default_request_headers.merge("Authorization" => "Bearer #{bearer_token}"),
query: "uuids%5B%5D=#{uuids[0]}&uuids%5B%5D=#{uuids[1]}&uuids%5B%5D=#{uuids[2]}",
)
.will_respond_with(
status: 200,
body: [
{
"uid": "9ef9779f-3cba-481a-9a73-00d39e33eb7b",
"name": Pact.like("Some user"),
"email": Pact.like("[email protected]"),
"organisation": nil,
},
{
"uid": "b55873b4-bc83-4efe-bdc9-6b7d381a723e",
"name": Pact.like("Some user"),
"email": Pact.like("[email protected]"),
"organisation": nil,
},
{
"uid": "64c7d994-17e0-44d9-97b0-87b43a581eb9",
"name": Pact.like("Some user"),
"email": Pact.like("[email protected]"),
"organisation": nil,
},
],
headers: {
"Content-Type" => "application/json; charset=utf-8",
},
)

api_client.get_users(uuids:)
end
end
end
11 changes: 11 additions & 0 deletions test/support/pact_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
ASSET_MANAGER_API_PORT = 3009
EMAIL_ALERT_API_PORT = 3010
SUPPORT_API_PORT = 3011
SIGNON_API_PORT = 3012

def publishing_api_host
"http://localhost:#{PUBLISHING_API_PORT}"
Expand Down Expand Up @@ -51,6 +52,10 @@ def support_api_host
"http://localhost:#{SUPPORT_API_PORT}"
end

def signon_api_host
"http://localhost:#{SIGNON_API_PORT}"
end

Pact.service_consumer "GDS API Adapters" do
has_pact_with "Publishing API" do
mock_service :publishing_api do
Expand Down Expand Up @@ -111,4 +116,10 @@ def support_api_host
port SUPPORT_API_PORT
end
end

has_pact_with "Signon API" do
mock_service :signon_api do
port SIGNON_API_PORT
end
end
end

0 comments on commit 1ee5bb6

Please sign in to comment.