@@ -369,4 +369,87 @@ def create_school_student
369369 described_class . create_school_student ( token :, username :, password :, name :, school_id : school . id )
370370 end
371371 end
372+
373+ describe '.list_school_student' do
374+ let ( :school ) { build ( :school , id : SecureRandom . uuid ) }
375+ let ( :list_students_url ) { "#{ api_url } /api/v1/schools/#{ school . id } /students/list" }
376+ let ( :student_ids ) { [ SecureRandom . uuid ] }
377+
378+ before do
379+ stub_request ( :post , list_students_url ) . to_return ( status : 200 , body : '[]' , headers : { 'content-type' => 'application/json' } )
380+ end
381+
382+ it 'makes a request to the profile api host' do
383+ list_school_students
384+ expect ( WebMock ) . to have_requested ( :post , list_students_url )
385+ end
386+
387+ it 'includes token in the authorization request header' do
388+ list_school_students
389+ expect ( WebMock ) . to have_requested ( :post , list_students_url ) . with ( headers : { authorization : "Bearer #{ token } " } )
390+ end
391+
392+ it 'includes the profile api key in the x-api-key request header' do
393+ list_school_students
394+ expect ( WebMock ) . to have_requested ( :post , list_students_url ) . with ( headers : { 'x-api-key' => api_key } )
395+ end
396+
397+ it 'sets content-type of request to json' do
398+ list_school_students
399+ expect ( WebMock ) . to have_requested ( :post , list_students_url ) . with ( headers : { 'content-type' => 'application/json' } )
400+ end
401+
402+ it 'sets accept header to json' do
403+ list_school_students
404+ expect ( WebMock ) . to have_requested ( :post , list_students_url ) . with ( headers : { 'accept' => 'application/json' } )
405+ end
406+
407+ it 'sets body to the student IDs' do
408+ list_school_students
409+ expect ( WebMock ) . to have_requested ( :post , list_students_url ) . with ( body : student_ids )
410+ end
411+
412+ # rubocop:disable RSpec/ExampleLength
413+ it 'returns a hash representing the student(s) if successful' do
414+ response = [
415+ {
416+ id : '549e4674-6ffd-4ac6-9a97-b4d7e5c0e5c5' ,
417+ schoolId : '132383f1-702a-46a0-9eb2-a40dd4f212e3' ,
418+ name : 'student-name' ,
419+ username : 'student-username' ,
420+ createdAt : '2024-07-03T13:00:40.041Z' ,
421+ updatedAt : '2024-07-03T13:00:40.041Z' ,
422+ discardedAt : nil
423+ }
424+ ]
425+ stub_request ( :post , list_students_url )
426+ . to_return ( status : 200 , body : response . to_json )
427+ expect ( list_school_students ) . to eq ( response )
428+ end
429+ # rubocop:enable RSpec/ExampleLength
430+
431+ it "returns empty array if the API returns a 404 because one or more of the student IDs aren't found" do
432+ stub_request ( :post , list_students_url )
433+ . to_return ( status : 404 , body : '' )
434+ expect ( list_school_students ) . to eq ( [ ] )
435+ end
436+
437+ it 'raises exception if anything other than a 200 status code is returned' do
438+ stub_request ( :post , list_students_url )
439+ . to_return ( status : 500 )
440+
441+ expect { list_school_students } . to raise_error ( RuntimeError )
442+ end
443+
444+ it 'includes details of underlying response when exception is raised' do
445+ stub_request ( :post , list_students_url )
446+ . to_return ( status : 401 )
447+
448+ expect { list_school_students } . to raise_error ( 'Students cannot be listed in Profile API. HTTP response code: 401' )
449+ end
450+
451+ def list_school_students
452+ described_class . list_school_students ( token :, school_id : school . id , student_ids :)
453+ end
454+ end
372455end
0 commit comments