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