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