Skip to content

Commit 31a3d5b

Browse files
committed
Add ProfileApiClient.create_safeguarding_flag
1 parent e6c1118 commit 31a3d5b

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

lib/profile_api_client.rb

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# frozen_string_literal: true
22

33
class ProfileApiClient
4+
SAFEGUARDING_FLAGS = {
5+
teacher: 'school:teacher',
6+
owner: 'school:owner'
7+
}.freeze
8+
49
class << self
510
# TODO: Replace with HTTP requests once the profile API has been built.
611

@@ -210,6 +215,17 @@ def safeguarding_flags(token:)
210215
JSON.parse(response.body)
211216
end
212217

218+
def create_safeguarding_flag(token:, flag:)
219+
response = connection.post('/api/v1/safeguarding-flags') do |request|
220+
apply_default_headers(request, token)
221+
request.body = { flag: }.to_json
222+
end
223+
224+
return if response.status == 201 || response.status == 303
225+
226+
raise "Safeguarding flag not created in Profile API. HTTP response code: #{response.status}"
227+
end
228+
213229
private
214230

215231
def connection

spec/lib/profile_api_client_spec.rb

+64
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,68 @@ def list_safeguarding_flags
165165
described_class.safeguarding_flags(token:)
166166
end
167167
end
168+
169+
describe '.create_safeguarding_flag' do
170+
let(:flag) { 'school:owner' }
171+
let(:create_safeguarding_flag_url) { "#{api_url}/api/v1/safeguarding-flags" }
172+
173+
before do
174+
stub_request(:post, create_safeguarding_flag_url).to_return(status: 201, body: '{}')
175+
end
176+
177+
it 'makes a request to the profile api host' do
178+
create_safeguarding_flag
179+
expect(WebMock).to have_requested(:post, create_safeguarding_flag_url)
180+
end
181+
182+
it 'includes token in the authorization request header' do
183+
create_safeguarding_flag
184+
expect(WebMock).to have_requested(:post, create_safeguarding_flag_url).with(headers: { authorization: "Bearer #{token}" })
185+
end
186+
187+
it 'includes the profile api key in the x-api-key request header' do
188+
create_safeguarding_flag
189+
expect(WebMock).to have_requested(:post, create_safeguarding_flag_url).with(headers: { 'x-api-key' => api_key })
190+
end
191+
192+
it 'sets content-type of request to json' do
193+
create_safeguarding_flag
194+
expect(WebMock).to have_requested(:post, create_safeguarding_flag_url).with(headers: { 'content-type' => 'application/json' })
195+
end
196+
197+
it 'sets accept header to json' do
198+
create_safeguarding_flag
199+
expect(WebMock).to have_requested(:post, create_safeguarding_flag_url).with(headers: { 'accept' => 'application/json' })
200+
end
201+
202+
it 'returns empty body if created successfully' do
203+
stub_request(:post, create_safeguarding_flag_url)
204+
.to_return(status: 201)
205+
expect(create_safeguarding_flag).to be_nil
206+
end
207+
208+
it 'returns empty body if 303 response returned to indicate that the flag already exists' do
209+
stub_request(:post, create_safeguarding_flag_url)
210+
.to_return(status: 303)
211+
expect(create_safeguarding_flag).to be_nil
212+
end
213+
214+
it 'raises exception if anything other than a 201 status code is returned' do
215+
stub_request(:post, create_safeguarding_flag_url)
216+
.to_return(status: 200)
217+
218+
expect { create_safeguarding_flag }.to raise_error(RuntimeError)
219+
end
220+
221+
it 'includes details of underlying response when exception is raised' do
222+
stub_request(:post, create_safeguarding_flag_url)
223+
.to_return(status: 401)
224+
225+
expect { create_safeguarding_flag }.to raise_error('Safeguarding flag not created in Profile API. HTTP response code: 401')
226+
end
227+
228+
def create_safeguarding_flag
229+
described_class.create_safeguarding_flag(token:, flag:)
230+
end
231+
end
168232
end

0 commit comments

Comments
 (0)