|
12 | 12 | allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key)
|
13 | 13 | end
|
14 | 14 |
|
| 15 | + describe ProfileApiClient::CreateStudent422Error do |
| 16 | + subject(:exception) { described_class.new(error) } |
| 17 | + |
| 18 | + let(:error_code) { 'ERR_USER_EXISTS' } |
| 19 | + let(:error) { { 'username' => 'username', 'error' => error_code } } |
| 20 | + |
| 21 | + it 'includes status code, username and translated error code in the message' do |
| 22 | + expect(exception.message).to eq("Student not created in Profile API (status code 422, username 'username', error 'username has already been taken')") |
| 23 | + end |
| 24 | + |
| 25 | + context "when the error isn't recognised" do |
| 26 | + let(:error_code) { 'unrecognised-code' } |
| 27 | + |
| 28 | + it 'includes a default error message' do |
| 29 | + expect(exception.message).to match(/error 'unknown error'/) |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
15 | 34 | describe '.create_school' do
|
16 | 35 | let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
|
17 | 36 | let(:create_school_url) { "#{api_url}/api/v1/schools" }
|
@@ -282,4 +301,84 @@ def delete_safeguarding_flag
|
282 | 301 | described_class.delete_safeguarding_flag(token:, flag:)
|
283 | 302 | end
|
284 | 303 | end
|
| 304 | + |
| 305 | + describe '.create_school_student' do |
| 306 | + let(:username) { 'username' } |
| 307 | + let(:password) { 'password' } |
| 308 | + let(:name) { 'name' } |
| 309 | + let(:school) { build(:school, id: SecureRandom.uuid) } |
| 310 | + let(:create_students_url) { "#{api_url}/api/v1/schools/#{school.id}/students" } |
| 311 | + |
| 312 | + before do |
| 313 | + stub_request(:post, create_students_url).to_return(status: 201, body: '{}', headers: { 'content-type' => 'application/json' }) |
| 314 | + end |
| 315 | + |
| 316 | + it 'makes a request to the profile api host' do |
| 317 | + create_school_student |
| 318 | + expect(WebMock).to have_requested(:post, create_students_url) |
| 319 | + end |
| 320 | + |
| 321 | + it 'includes token in the authorization request header' do |
| 322 | + create_school_student |
| 323 | + expect(WebMock).to have_requested(:post, create_students_url).with(headers: { authorization: "Bearer #{token}" }) |
| 324 | + end |
| 325 | + |
| 326 | + it 'includes the profile api key in the x-api-key request header' do |
| 327 | + create_school_student |
| 328 | + expect(WebMock).to have_requested(:post, create_students_url).with(headers: { 'x-api-key' => api_key }) |
| 329 | + end |
| 330 | + |
| 331 | + it 'sets content-type of request to json' do |
| 332 | + create_school_student |
| 333 | + expect(WebMock).to have_requested(:post, create_students_url).with(headers: { 'content-type' => 'application/json' }) |
| 334 | + end |
| 335 | + |
| 336 | + it 'sets accept header to json' do |
| 337 | + create_school_student |
| 338 | + expect(WebMock).to have_requested(:post, create_students_url).with(headers: { 'accept' => 'application/json' }) |
| 339 | + end |
| 340 | + |
| 341 | + it 'sends the student details in the request body' do |
| 342 | + create_school_student |
| 343 | + expect(WebMock).to have_requested(:post, create_students_url).with(body: [{ name:, username:, password: }].to_json) |
| 344 | + end |
| 345 | + |
| 346 | + it 'returns the id of the created student(s) if successful' do |
| 347 | + response = { created: ['student-id'] } |
| 348 | + stub_request(:post, create_students_url) |
| 349 | + .to_return(status: 201, body: response.to_json, headers: { 'content-type' => 'application/json' }) |
| 350 | + expect(create_school_student).to eq(response) |
| 351 | + end |
| 352 | + |
| 353 | + it 'raises 422 exception if 422 status code is returned' do |
| 354 | + response = { errors: [username: 'username', error: 'ERR_USER_EXISTS'] } |
| 355 | + stub_request(:post, create_students_url) |
| 356 | + .to_return(status: 422, body: response.to_json, headers: { 'content-type' => 'application/json' }) |
| 357 | + |
| 358 | + expect { create_school_student }.to raise_error(ProfileApiClient::CreateStudent422Error) |
| 359 | + .with_message("Student not created in Profile API (status code 422, username 'username', error 'username has already been taken')") |
| 360 | + end |
| 361 | + |
| 362 | + it 'raises exception if anything other that 201 status code is returned' do |
| 363 | + stub_request(:post, create_students_url) |
| 364 | + .to_return(status: 200) |
| 365 | + |
| 366 | + expect { create_school_student }.to raise_error('Student not created in Profile API (status code 200)') |
| 367 | + end |
| 368 | + |
| 369 | + context 'when there are extraneous leading and trailing spaces in the student params' do |
| 370 | + let(:username) { ' username ' } |
| 371 | + let(:password) { ' password ' } |
| 372 | + let(:name) { ' name ' } |
| 373 | + |
| 374 | + it 'strips the extraneous spaces' do |
| 375 | + create_school_student |
| 376 | + expect(WebMock).to have_requested(:post, create_students_url).with(body: [{ name: 'name', username: 'username', password: 'password' }].to_json) |
| 377 | + end |
| 378 | + end |
| 379 | + |
| 380 | + def create_school_student |
| 381 | + described_class.create_school_student(token:, username:, password:, name:, school_id: school.id) |
| 382 | + end |
| 383 | + end |
285 | 384 | end
|
0 commit comments