|
3 | 3 | require 'rails_helper'
|
4 | 4 |
|
5 | 5 | RSpec.describe ProfileApiClient do
|
| 6 | + let(:api_url) { 'http://example.com' } |
| 7 | + let(:api_key) { 'api-key' } |
| 8 | + let(:token) { SecureRandom.uuid } |
| 9 | + |
| 10 | + before do |
| 11 | + allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url) |
| 12 | + allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key) |
| 13 | + end |
| 14 | + |
6 | 15 | describe '.create_school' do
|
7 |
| - let(:api_url) { 'http://example.com' } |
8 |
| - let(:api_key) { 'api-key' } |
9 |
| - let(:token) { SecureRandom.uuid } |
10 | 16 | let(:school) { build(:school, id: SecureRandom.uuid, code: SecureRandom.uuid) }
|
11 | 17 | let(:create_school_url) { "#{api_url}/api/v1/schools" }
|
12 | 18 |
|
13 | 19 | before do
|
14 | 20 | stub_request(:post, create_school_url).to_return(status: 201, body: '{}')
|
15 |
| - allow(ENV).to receive(:fetch).with('IDENTITY_URL').and_return(api_url) |
16 |
| - allow(ENV).to receive(:fetch).with('PROFILE_API_KEY').and_return(api_key) |
17 | 21 | end
|
18 | 22 |
|
19 | 23 | it 'makes a request to the profile api host' do
|
@@ -90,4 +94,75 @@ def create_school
|
90 | 94 | described_class.create_school(token:, school:)
|
91 | 95 | end
|
92 | 96 | end
|
| 97 | + |
| 98 | + describe '.safeguarding_flags' do |
| 99 | + let(:list_safeguarding_flags_url) { "#{api_url}/api/v1/safeguarding-flags" } |
| 100 | + |
| 101 | + before do |
| 102 | + stub_request(:get, list_safeguarding_flags_url).to_return(status: 200, body: '[]') |
| 103 | + end |
| 104 | + |
| 105 | + it 'makes a request to the profile api host' do |
| 106 | + list_safeguarding_flags |
| 107 | + expect(WebMock).to have_requested(:get, list_safeguarding_flags_url) |
| 108 | + end |
| 109 | + |
| 110 | + it 'includes token in the authorization request header' do |
| 111 | + list_safeguarding_flags |
| 112 | + expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { authorization: "Bearer #{token}" }) |
| 113 | + end |
| 114 | + |
| 115 | + it 'includes the profile api key in the x-api-key request header' do |
| 116 | + list_safeguarding_flags |
| 117 | + expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'x-api-key' => api_key }) |
| 118 | + end |
| 119 | + |
| 120 | + it 'sets content-type of request to json' do |
| 121 | + list_safeguarding_flags |
| 122 | + expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'content-type' => 'application/json' }) |
| 123 | + end |
| 124 | + |
| 125 | + it 'sets accept header to json' do |
| 126 | + list_safeguarding_flags |
| 127 | + expect(WebMock).to have_requested(:get, list_safeguarding_flags_url).with(headers: { 'accept' => 'application/json' }) |
| 128 | + end |
| 129 | + |
| 130 | + # rubocop:disable RSpec/ExampleLength |
| 131 | + it 'returns list of safeguarding flags if successful' do |
| 132 | + flags = [ |
| 133 | + { |
| 134 | + 'id' => '7ac79585-e187-4d2f-bf0c-a1cbe72ecc9a', |
| 135 | + 'userId' => '583ba872-b16e-46e1-9f7d-df89d267550d', |
| 136 | + 'flag' => 'school:owner', |
| 137 | + 'isActive' => 'true', |
| 138 | + 'createdAt' => '2024-07-01T12:49:18.926Z', |
| 139 | + 'updatedAt' => '2024-07-01T12:49:18.926Z' |
| 140 | + } |
| 141 | + ] |
| 142 | + stub_request(:get, list_safeguarding_flags_url) |
| 143 | + .to_return(status: 200, body: flags.to_json) |
| 144 | + expect(list_safeguarding_flags).to eq(flags) |
| 145 | + end |
| 146 | + # rubocop:enable RSpec/ExampleLength |
| 147 | + |
| 148 | + it 'raises exception if anything other than a 200 status code is returned' do |
| 149 | + stub_request(:get, list_safeguarding_flags_url) |
| 150 | + .to_return(status: 201) |
| 151 | + |
| 152 | + expect { list_safeguarding_flags }.to raise_error(RuntimeError) |
| 153 | + end |
| 154 | + |
| 155 | + it 'includes details of underlying response when exception is raised' do |
| 156 | + stub_request(:get, list_safeguarding_flags_url) |
| 157 | + .to_return(status: 401) |
| 158 | + |
| 159 | + expect { list_safeguarding_flags }.to raise_error('Safeguarding flags cannot be retrieved from Profile API. HTTP response code: 401') |
| 160 | + end |
| 161 | + |
| 162 | + private |
| 163 | + |
| 164 | + def list_safeguarding_flags |
| 165 | + described_class.safeguarding_flags(token:) |
| 166 | + end |
| 167 | + end |
93 | 168 | end
|
0 commit comments