Skip to content

Commit e6c1118

Browse files
committed
Add ProfileApiClient.safeguarding_flags
TODO: What to do with BYPASS_OAUTH? To list the Safeguarding flags for the user associated with the access token.
1 parent 5fbc6cb commit e6c1118

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

lib/profile_api_client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ def delete_school_student(token:, student_id:, organisation_id:)
198198
response.deep_symbolize_keys
199199
end
200200

201+
def safeguarding_flags(token:)
202+
response = connection.get('/api/v1/safeguarding-flags') do |request|
203+
apply_default_headers(request, token)
204+
end
205+
206+
unless response.status == 200
207+
raise "Safeguarding flags cannot be retrieved from Profile API. HTTP response code: #{response.status}"
208+
end
209+
210+
JSON.parse(response.body)
211+
end
212+
201213
private
202214

203215
def connection

spec/lib/profile_api_client_spec.rb

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
require 'rails_helper'
44

55
RSpec.describe ProfileApiClient do
6+
let(:api_url) { 'http://example.com' }
7+
let(:api_key) { 'api-key' }
8+
let(:token) { SecureRandom.uuid }
9+
10+
before do
11+
allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url)
12+
allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key)
13+
end
14+
615
describe '.create_school' do
7-
let(:api_url) { 'http://example.com' }
8-
let(:api_key) { 'api-key' }
9-
let(:token) { SecureRandom.uuid }
1016
let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
1117
let(:create_school_url) { "#{api_url}/api/v1/schools" }
1218

1319
before do
1420
stub_request(:post, create_school_url).to_return(status: 201, body: '{}')
15-
allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url)
16-
allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key)
1721
end
1822

1923
it 'makes a request to the profile api host' do
@@ -90,4 +94,75 @@ def create_school
9094
described_class.create_school(token:, school:)
9195
end
9296
end
97+
98+
describe '.safeguarding_flags' do
99+
let(:list_safeguarding_flags_url) { "#{api_url}/api/v1/safeguarding-flags" }
100+
101+
before do
102+
stub_request(:get, list_safeguarding_flags_url).to_return(status: 200, body: '[]')
103+
end
104+
105+
it 'makes a request to the profile api host' do
106+
list_safeguarding_flags
107+
expect(WebMock).to have_requested(:get, list_safeguarding_flags_url)
108+
end
109+
110+
it 'includes token in the authorization request header' do
111+
list_safeguarding_flags
112+
expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { authorization: "Bearer #{token}" })
113+
end
114+
115+
it 'includes the profile api key in the x-api-key request header' do
116+
list_safeguarding_flags
117+
expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'x-api-key' => api_key })
118+
end
119+
120+
it 'sets content-type of request to json' do
121+
list_safeguarding_flags
122+
expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'content-type' => 'application/json' })
123+
end
124+
125+
it 'sets accept header to json' do
126+
list_safeguarding_flags
127+
expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'accept' => 'application/json' })
128+
end
129+
130+
# rubocop:disable RSpec/ExampleLength
131+
it 'returns list of safeguarding flags if successful' do
132+
flags = [
133+
{
134+
'id' => '7ac79585-e187-4d2f-bf0c-a1cbe72ecc9a',
135+
'userId' => '583ba872-b16e-46e1-9f7d-df89d267550d',
136+
'flag' => 'school:owner',
137+
'isActive' => 'true',
138+
'createdAt' => '2024-07-01T12:49:18.926Z',
139+
'updatedAt' => '2024-07-01T12:49:18.926Z'
140+
}
141+
]
142+
stub_request(:get, list_safeguarding_flags_url)
143+
.to_return(status: 200, body: flags.to_json)
144+
expect(list_safeguarding_flags).to eq(flags)
145+
end
146+
# rubocop:enable RSpec/ExampleLength
147+
148+
it 'raises exception if anything other than a 200 status code is returned' do
149+
stub_request(:get, list_safeguarding_flags_url)
150+
.to_return(status: 201)
151+
152+
expect { list_safeguarding_flags }.to raise_error(RuntimeError)
153+
end
154+
155+
it 'includes details of underlying response when exception is raised' do
156+
stub_request(:get, list_safeguarding_flags_url)
157+
.to_return(status: 401)
158+
159+
expect { list_safeguarding_flags }.to raise_error('Safeguarding flags cannot be retrieved from Profile API. HTTP response code: 401')
160+
end
161+
162+
private
163+
164+
def list_safeguarding_flags
165+
described_class.safeguarding_flags(token:)
166+
end
167+
end
93168
end

0 commit comments

Comments
 (0)