|
8 | 8 | let(:api_key) { 'api-key' } |
9 | 9 | let(:token) { SecureRandom.uuid } |
10 | 10 | let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) } |
| 11 | + let(:create_school_url) { "#{api_url}/api/v1/schools" } |
11 | 12 |
|
12 | 13 | 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: '{}') |
14 | 15 | allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url) |
15 | 16 | allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key) |
16 | 17 | end |
17 | 18 |
|
18 | 19 | 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) |
21 | 22 | end |
22 | 23 |
|
23 | 24 | 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}" }) |
26 | 27 | end |
27 | 28 |
|
28 | 29 | 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 }) |
31 | 32 | end |
32 | 33 |
|
33 | 34 | 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' }) |
36 | 37 | end |
37 | 38 |
|
38 | 39 | 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' }) |
41 | 42 | end |
42 | 43 |
|
43 | 44 | it 'sends the school id and code in the request body as json' do |
44 | | - described_class.create_school(token:, school:) |
| 45 | + create_school |
45 | 46 | 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) |
47 | 48 | end |
48 | 49 |
|
49 | 50 | it 'returns the created school if successful' do |
50 | 51 | data = { 'id' => 'school-id', 'schoolCode' => 'school-code' } |
51 | | - stub_request(:post, "#{api_url}/api/v1/schools") |
| 52 | + stub_request(:post, create_school_url) |
52 | 53 | .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) |
54 | 55 | end |
55 | 56 |
|
56 | 57 | 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) |
58 | 59 | .to_return(status: 200) |
59 | 60 |
|
60 | | - expect { described_class.create_school(token:, school:) }.to raise_error(RuntimeError) |
| 61 | + expect { create_school }.to raise_error(RuntimeError) |
61 | 62 | end |
62 | 63 |
|
63 | 64 | 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) |
65 | 66 | .to_return(status: 401) |
66 | 67 |
|
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') |
68 | 69 | end |
69 | 70 |
|
70 | 71 | describe 'when BYPASS_OAUTH is true' do |
|
73 | 74 | end |
74 | 75 |
|
75 | 76 | 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) |
78 | 79 | end |
79 | 80 |
|
80 | 81 | it 'returns the id and code of the school supplied' do |
81 | 82 | 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) |
83 | 84 | end |
84 | 85 | end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + def create_school |
| 90 | + described_class.create_school(token:, school:) |
| 91 | + end |
85 | 92 | end |
86 | 93 | end |
0 commit comments