@@ -165,4 +165,68 @@ def list_safeguarding_flags
165
165
described_class . safeguarding_flags ( token :)
166
166
end
167
167
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
168
232
end
0 commit comments