Skip to content

Commit 528a786

Browse files
committed
1 parent b8dc482 commit 528a786

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lib/profile_api_client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ def remove_school_teacher(*)
7979
{}
8080
end
8181

82+
def school_student(token:, school_id:, student_id:)
83+
response = connection(token).get("/api/v1/schools/#{school_id}/students/#{student_id}")
84+
85+
raise UnexpectedResponse, response unless response.status == 200
86+
87+
Student.new(**response.body)
88+
end
89+
8290
def list_school_students(token:, school_id:, student_ids:)
8391
return [] if token.blank?
8492

spec/lib/profile_api_client_spec.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
605678
end

0 commit comments

Comments
 (0)