|
12 | 12 | allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key)
|
13 | 13 | end
|
14 | 14 |
|
| 15 | + describe ProfileApiClient::CreateStudentError do |
| 16 | + subject { described_class.new(response) } |
| 17 | + |
| 18 | + let(:status) { '404' } |
| 19 | + let(:body) { '' } |
| 20 | + let(:response) { instance_double(Faraday::Response, status:, body:) } |
| 21 | + |
| 22 | + it 'includes status code in the message' do |
| 23 | + expect(subject.message).to eq('Student not created in Profile API (status code 404)') |
| 24 | + end |
| 25 | + |
| 26 | + describe '#duplicate_username?' do |
| 27 | + context 'when body includes duplicate username error code' do |
| 28 | + let(:body) { { 'errors' => [{ 'username' => 'jdoe', 'error' => 'ERR_USER_EXISTS' }] } } |
| 29 | + |
| 30 | + it 'returns true' do |
| 31 | + expect(subject.duplicate_username?).to be(true) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'when body does not include duplicate username error code' do |
| 36 | + it 'returns false' do |
| 37 | + expect(subject.duplicate_username?).to be(false) |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + |
15 | 43 | describe '.create_school' do
|
16 | 44 | let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
|
17 | 45 | let(:create_school_url) { "#{api_url}/api/v1/schools" }
|
@@ -331,25 +359,11 @@ def delete_safeguarding_flag
|
331 | 359 | expect(create_school_student).to eq(response)
|
332 | 360 | end
|
333 | 361 |
|
334 |
| - it 'raises exception with details of the error if 422 response indicates that the user already exists' do |
335 |
| - response = { 'errors' => [{ 'username' => 'jdoe', 'error' => 'ERR_USER_EXISTS' }] } |
336 |
| - stub_request(:post, create_students_url) |
337 |
| - .to_return(status: 422, body: response.to_json) |
338 |
| - expect { create_school_student }.to raise_error('Student not created in Profile API (status code 422). Username already exists') |
339 |
| - end |
340 |
| - |
341 |
| - it 'raises exception including the error code if 422 response indicates that some other error occurred' do |
342 |
| - response = { 'errors' => [{ 'username' => 'jdoe', 'error' => 'ERR_UNKNOWN' }] } |
343 |
| - stub_request(:post, create_students_url) |
344 |
| - .to_return(status: 422, body: response.to_json) |
345 |
| - expect { create_school_student }.to raise_error('Student not created in Profile API (status code 422). Unknown error code: ERR_UNKNOWN') |
346 |
| - end |
347 |
| - |
348 | 362 | it 'raises exception if anything other than a 201 status code is returned' do
|
349 | 363 | stub_request(:post, create_students_url)
|
350 | 364 | .to_return(status: 200)
|
351 | 365 |
|
352 |
| - expect { create_school_student }.to raise_error(RuntimeError) |
| 366 | + expect { create_school_student }.to raise_error(ProfileApiClient::CreateStudentError) |
353 | 367 | end
|
354 | 368 |
|
355 | 369 | it 'includes details of underlying response when exception is raised' do
|
|
0 commit comments