Skip to content

Commit 5fbc6cb

Browse files
committed
DRY up .create_school examples
1 parent 3f07206 commit 5fbc6cb

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

spec/lib/profile_api_client_spec.rb

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,63 +8,64 @@
88
let(:api_key) { 'api-key' }
99
let(:token) { SecureRandom.uuid }
1010
let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
11+
let(:create_school_url) { "#{api_url}/api/v1/schools" }
1112

1213
before do
13-
stub_request(:post, "#{api_url}/api/v1/schools").to_return(status: 201, body: '{}')
14+
stub_request(:post, create_school_url).to_return(status: 201, body: '{}')
1415
allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url)
1516
allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key)
1617
end
1718

1819
it 'makes a request to the profile api host' do
19-
described_class.create_school(token:, school:)
20-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools")
20+
create_school
21+
expect(WebMock).to have_requested(:post, create_school_url)
2122
end
2223

2324
it 'includes token in the authorization request header' do
24-
described_class.create_school(token:, school:)
25-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools").with(headers: { authorization: "Bearer #{token}" })
25+
create_school
26+
expect(WebMock).to have_requested(:post, create_school_url).with(headers: { authorization: "Bearer #{token}" })
2627
end
2728

2829
it 'includes the profile api key in the x-api-key request header' do
29-
described_class.create_school(token:, school:)
30-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools").with(headers: { 'x-api-key' => api_key })
30+
create_school
31+
expect(WebMock).to have_requested(:post, create_school_url).with(headers: { 'x-api-key' => api_key })
3132
end
3233

3334
it 'sets content-type of request to json' do
34-
described_class.create_school(token:, school:)
35-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools").with(headers: { 'content-type' => 'application/json' })
35+
create_school
36+
expect(WebMock).to have_requested(:post, create_school_url).with(headers: { 'content-type' => 'application/json' })
3637
end
3738

3839
it 'sets accept header to json' do
39-
described_class.create_school(token:, school:)
40-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools").with(headers: { 'accept' => 'application/json' })
40+
create_school
41+
expect(WebMock).to have_requested(:post, create_school_url).with(headers: { 'accept' => 'application/json' })
4142
end
4243

4344
it 'sends the school id and code in the request body as json' do
44-
described_class.create_school(token:, school:)
45+
create_school
4546
expected_body = { id: school.id, schoolCode: school.code }.to_json
46-
expect(WebMock).to have_requested(:post, "#{api_url}/api/v1/schools").with(body: expected_body)
47+
expect(WebMock).to have_requested(:post, create_school_url).with(body: expected_body)
4748
end
4849

4950
it 'returns the created school if successful' do
5051
data = { 'id' => 'school-id', 'schoolCode' => 'school-code' }
51-
stub_request(:post, "#{api_url}/api/v1/schools")
52+
stub_request(:post, create_school_url)
5253
.to_return(status: 201, body: data.to_json)
53-
expect(described_class.create_school(token:, school:)).to eq(data)
54+
expect(create_school).to eq(data)
5455
end
5556

5657
it 'raises exception if anything other than a 201 status code is returned' do
57-
stub_request(:post, "#{api_url}/api/v1/schools")
58+
stub_request(:post, create_school_url)
5859
.to_return(status: 200)
5960

60-
expect { described_class.create_school(token:, school:) }.to raise_error(RuntimeError)
61+
expect { create_school }.to raise_error(RuntimeError)
6162
end
6263

6364
it 'includes details of underlying response when exception is raised' do
64-
stub_request(:post, "#{api_url}/api/v1/schools")
65+
stub_request(:post, create_school_url)
6566
.to_return(status: 401)
6667

67-
expect { described_class.create_school(token:, school:) }.to raise_error('School not created in Profile API. HTTP response code: 401')
68+
expect { create_school }.to raise_error('School not created in Profile API. HTTP response code: 401')
6869
end
6970

7071
describe 'when BYPASS_OAUTH is true' do
@@ -73,14 +74,20 @@
7374
end
7475

7576
it 'does not make a request to Profile API' do
76-
described_class.create_school(token:, school:)
77-
expect(WebMock).not_to have_requested(:post, "#{api_url}/api/v1/schools")
77+
create_school
78+
expect(WebMock).not_to have_requested(:post, create_school_url)
7879
end
7980

8081
it 'returns the id and code of the school supplied' do
8182
expected = { 'id' => school.id, 'schoolCode' => school.code }
82-
expect(described_class.create_school(token:, school:)).to eq(expected)
83+
expect(create_school).to eq(expected)
8384
end
8485
end
86+
87+
private
88+
89+
def create_school
90+
described_class.create_school(token:, school:)
91+
end
8592
end
8693
end

0 commit comments

Comments
 (0)