@@ -287,4 +287,80 @@ def delete_safeguarding_flag
287
287
described_class . delete_safeguarding_flag ( token :, flag :)
288
288
end
289
289
end
290
+
291
+ describe '.create_school_student' do
292
+ let ( :username ) { 'username' }
293
+ let ( :password ) { 'password' }
294
+ let ( :name ) { 'name' }
295
+ let ( :school ) { build ( :school , id : SecureRandom . uuid ) }
296
+ let ( :create_students_url ) { "#{ api_url } /api/v1/schools/#{ school . id } /students" }
297
+
298
+ before do
299
+ stub_request ( :post , create_students_url ) . to_return ( status : 201 , body : '{}' )
300
+ end
301
+
302
+ it 'makes a request to the profile api host' do
303
+ create_school_student
304
+ expect ( WebMock ) . to have_requested ( :post , create_students_url )
305
+ end
306
+
307
+ it 'includes token in the authorization request header' do
308
+ create_school_student
309
+ expect ( WebMock ) . to have_requested ( :post , create_students_url ) . with ( headers : { authorization : "Bearer #{ token } " } )
310
+ end
311
+
312
+ it 'includes the profile api key in the x-api-key request header' do
313
+ create_school_student
314
+ expect ( WebMock ) . to have_requested ( :post , create_students_url ) . with ( headers : { 'x-api-key' => api_key } )
315
+ end
316
+
317
+ it 'sets content-type of request to json' do
318
+ create_school_student
319
+ expect ( WebMock ) . to have_requested ( :post , create_students_url ) . with ( headers : { 'content-type' => 'application/json' } )
320
+ end
321
+
322
+ it 'sets accept header to json' do
323
+ create_school_student
324
+ expect ( WebMock ) . to have_requested ( :post , create_students_url ) . with ( headers : { 'accept' => 'application/json' } )
325
+ end
326
+
327
+ it 'returns the id of the created student(s) if successful' do
328
+ data = { 'created' => [ 'student-id' ] }
329
+ stub_request ( :post , create_students_url )
330
+ . to_return ( status : 201 , body : data . to_json )
331
+ expect ( create_school_student ) . to eq ( data )
332
+ end
333
+
334
+ it 'raises exception with details of the error if 422 response indicates that the user already exists' do
335
+ response = { 'errors' => [ { 'username' => 'jdoe' , 'error' => 'ERR_USER_EXISTS' } ] }
336
+ stub_request ( :post , create_students_url )
337
+ . to_return ( status : 422 , body : response . to_json )
338
+ expect { create_school_student } . to raise_error ( 'Student not created in Profile API. HTTP response code 422. Username already exists' )
339
+ end
340
+
341
+ it 'raises exception including the error code if 422 response indicates that some other error occurred' do
342
+ response = { 'errors' => [ { 'username' => 'jdoe' , 'error' => 'ERR_UNKNOWN' } ] }
343
+ stub_request ( :post , create_students_url )
344
+ . to_return ( status : 422 , body : response . to_json )
345
+ expect { create_school_student } . to raise_error ( 'Student not created in Profile API. HTTP response code 422. Unknown error code: ERR_UNKNOWN' )
346
+ end
347
+
348
+ it 'raises exception if anything other than a 201 status code is returned' do
349
+ stub_request ( :post , create_students_url )
350
+ . to_return ( status : 200 )
351
+
352
+ expect { create_school_student } . to raise_error ( RuntimeError )
353
+ end
354
+
355
+ it 'includes details of underlying response when exception is raised' do
356
+ stub_request ( :post , create_students_url )
357
+ . to_return ( status : 401 )
358
+
359
+ expect { create_school_student } . to raise_error ( 'Student not created in Profile API. HTTP response code: 401' )
360
+ end
361
+
362
+ def create_school_student
363
+ described_class . create_school_student ( token :, username :, password :, name :, school :)
364
+ end
365
+ end
290
366
end
0 commit comments