@@ -602,4 +602,77 @@ def update_school_student
602602 end
603603 end
604604 # rubocop:enable RSpec/MultipleMemoizedHelpers
605+
606+ describe '.school_student' do
607+ let ( :school ) { build ( :school , id : SecureRandom . uuid ) }
608+ let ( :student_url ) { "#{ api_url } /api/v1/schools/#{ school . id } /students/#{ student_id } " }
609+ let ( :student_id ) { SecureRandom . uuid }
610+
611+ before do
612+ stub_request ( :get , student_url )
613+ . to_return (
614+ status : 200 ,
615+ body : '{"id":"","schoolId":"","name":"","username":"","createdAt":"","updatedAt":"","discardedAt":""}' ,
616+ headers : { 'content-type' => 'application/json' }
617+ )
618+ end
619+
620+ it 'makes a request to the profile api host' do
621+ school_student
622+ expect ( WebMock ) . to have_requested ( :get , student_url )
623+ end
624+
625+ it 'includes token in the authorization request header' do
626+ school_student
627+ expect ( WebMock ) . to have_requested ( :get , student_url ) . with ( headers : { authorization : "Bearer #{ token } " } )
628+ end
629+
630+ it 'includes the profile api key in the x-api-key request header' do
631+ school_student
632+ expect ( WebMock ) . to have_requested ( :get , student_url ) . with ( headers : { 'x-api-key' => api_key } )
633+ end
634+
635+ it 'sets accept header to json' do
636+ school_student
637+ expect ( WebMock ) . to have_requested ( :get , student_url ) . with ( headers : { 'accept' => 'application/json' } )
638+ end
639+
640+ # rubocop:disable RSpec/ExampleLength
641+ it 'returns the student(s) if successful' do
642+ student = {
643+ id : '549e4674-6ffd-4ac6-9a97-b4d7e5c0e5c5' ,
644+ schoolId : '132383f1-702a-46a0-9eb2-a40dd4f212e3' ,
645+ name : 'student-name' ,
646+ username : 'student-username' ,
647+ createdAt : '2024-07-03T13:00:40.041Z' ,
648+ updatedAt : '2024-07-03T13:00:40.041Z' ,
649+ discardedAt : nil
650+ }
651+ expected = ProfileApiClient ::Student . new ( **student )
652+ stub_request ( :get , student_url )
653+ . to_return ( status : 200 , body : student . to_json , headers : { 'content-type' => 'application/json' } )
654+ expect ( school_student ) . to eq ( expected )
655+ end
656+ # rubocop:enable RSpec/ExampleLength
657+
658+ it 'raises exception if anything other than a 200 status code is returned' do
659+ stub_request ( :get , student_url )
660+ . to_return ( status : 201 )
661+
662+ expect { school_student } . to raise_error ( ProfileApiClient ::UnexpectedResponse )
663+ end
664+
665+ it 'raises faraday exception for 4xx and 5xx responses' do
666+ stub_request ( :get , student_url )
667+ . to_return ( status : 401 )
668+
669+ expect { school_student } . to raise_error ( Faraday ::Error )
670+ end
671+
672+ private
673+
674+ def school_student
675+ described_class . school_student ( token :, school_id : school . id , student_id :)
676+ end
677+ end
605678end
0 commit comments