@@ -165,4 +165,73 @@ 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 'sends the safeguarding flag in the request body' do
203
+ create_safeguarding_flag
204
+ expect ( WebMock ) . to have_requested ( :post , create_safeguarding_flag_url ) . with ( body : { flag : } . to_json )
205
+ end
206
+
207
+ it 'returns empty body if created successfully' do
208
+ stub_request ( :post , create_safeguarding_flag_url )
209
+ . to_return ( status : 201 )
210
+ expect ( create_safeguarding_flag ) . to be_nil
211
+ end
212
+
213
+ it 'returns empty body if 303 response returned to indicate that the flag already exists' do
214
+ stub_request ( :post , create_safeguarding_flag_url )
215
+ . to_return ( status : 303 )
216
+ expect ( create_safeguarding_flag ) . to be_nil
217
+ end
218
+
219
+ it 'raises exception if anything other than a 201 status code is returned' do
220
+ stub_request ( :post , create_safeguarding_flag_url )
221
+ . to_return ( status : 200 )
222
+
223
+ expect { create_safeguarding_flag } . to raise_error ( RuntimeError )
224
+ end
225
+
226
+ it 'includes details of underlying response when exception is raised' do
227
+ stub_request ( :post , create_safeguarding_flag_url )
228
+ . to_return ( status : 401 )
229
+
230
+ expect { create_safeguarding_flag } . to raise_error ( 'Safeguarding flag not created in Profile API. HTTP response code: 401' )
231
+ end
232
+
233
+ def create_safeguarding_flag
234
+ described_class . create_safeguarding_flag ( token :, flag :)
235
+ end
236
+ end
168
237
end
0 commit comments