|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | describe NHS::ImmunisationsAPI do |
| 4 | + before { Flipper.enable(:immunisations_fhir_api_integration) } |
| 5 | + |
4 | 6 | let(:organisation) { create(:organisation, ods_code: "A9A5A") } |
5 | 7 | let(:patient) do |
6 | 8 | create( |
|
48 | 50 | ) |
49 | 51 | end |
50 | 52 |
|
| 53 | + shared_examples "an immunisations_fhir_api_integration feature flag check" do |
| 54 | + context "the immunisations_fhir_api_integration feature flag is disabled" do |
| 55 | + before { Flipper.disable(:immunisations_fhir_api_integration) } |
| 56 | + |
| 57 | + it "does not make a request to the NHS API" do |
| 58 | + perform_request |
| 59 | + |
| 60 | + expect(request_stub).not_to have_been_made |
| 61 | + end |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + shared_examples "unexpected response status" do |unexpected_status, action| |
| 66 | + context "unexpected response status" do |
| 67 | + let(:status) { unexpected_status } |
| 68 | + let(:response) { "" } |
| 69 | + |
| 70 | + it "raises an error saying the response is unexpected" do |
| 71 | + expect { perform_request }.to raise_error( |
| 72 | + "Error #{action} vaccination record #{vaccination_record.id} to" \ |
| 73 | + " Immunisations API: unexpected response status #{status}" |
| 74 | + ) |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + shared_examples "client error (4XX) handling" do |action| |
| 80 | + context "4XX error" do |
| 81 | + let(:status) { 404 } |
| 82 | + let(:diagnostics) { "Invalid patient ID" } |
| 83 | + |
| 84 | + it "raises an error with the diagnostic message" do |
| 85 | + expect { perform_request }.to raise_error( |
| 86 | + StandardError, |
| 87 | + "Error #{action} vaccination record #{vaccination_record.id} to" \ |
| 88 | + " Immunisations API: Invalid patient ID" |
| 89 | + ) |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + shared_examples "generic error handling" do |
| 95 | + context "generic error" do |
| 96 | + let(:status) { 500 } |
| 97 | + |
| 98 | + it "raises an error with the diagnostic message" do |
| 99 | + expect { perform_request }.to raise_error(Faraday::Error) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + |
51 | 104 | describe "record_immunisation" do |
| 105 | + subject(:perform_request) do |
| 106 | + described_class.record_immunisation(vaccination_record) |
| 107 | + end |
| 108 | + |
52 | 109 | let!(:request_stub) do |
53 | 110 | stub_request( |
54 | 111 | :post, |
55 | 112 | "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
56 | | - ).to_return( |
57 | | - status: 201, |
58 | | - body: "", |
59 | | - headers: { |
60 | | - location: |
61 | | - "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444" |
62 | | - } |
63 | | - ) |
| 113 | + ).to_return(status:, body:, headers:) |
| 114 | + end |
| 115 | + |
| 116 | + let(:status) { 201 } |
| 117 | + let(:body) { "" } |
| 118 | + let(:headers) do |
| 119 | + { |
| 120 | + location: |
| 121 | + "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444" |
| 122 | + } |
64 | 123 | end |
65 | 124 |
|
66 | 125 | it "sends the correct JSON payload" do |
67 | 126 | expected_body = |
68 | | - File.read(Rails.root.join("spec/fixtures/fhir/immunisation.json")).chomp |
| 127 | + File.read( |
| 128 | + Rails.root.join("spec/fixtures/fhir/immunisation-create.json") |
| 129 | + ).chomp |
69 | 130 |
|
70 | 131 | request_stub.with do |request| |
71 | 132 | expect(request.headers).to include( |
|
78 | 139 | true |
79 | 140 | end |
80 | 141 |
|
81 | | - described_class.record_immunisation(vaccination_record) |
| 142 | + perform_request |
82 | 143 |
|
83 | 144 | expect(request_stub).to have_been_made |
84 | 145 | end |
85 | 146 |
|
86 | 147 | it "stores the id from the response" do |
87 | | - described_class.record_immunisation(vaccination_record) |
| 148 | + perform_request |
88 | 149 |
|
89 | 150 | expect( |
90 | 151 | vaccination_record.nhs_immunisations_api_id |
91 | 152 | ).to eq "ffff1111-eeee-2222-dddd-3333eeee4444" |
92 | 153 | end |
93 | 154 |
|
94 | | - it "stores the nhs_immunisations_api_synced_at from the response" do |
| 155 | + it "sets the nhs_immunisations_api_synced_at" do |
95 | 156 | freeze_time do |
96 | | - described_class.record_immunisation(vaccination_record) |
| 157 | + perform_request |
97 | 158 |
|
98 | 159 | expect( |
99 | 160 | vaccination_record.nhs_immunisations_api_synced_at |
|
102 | 163 | end |
103 | 164 |
|
104 | 165 | it "initialises the etag to 1" do |
105 | | - described_class.record_immunisation(vaccination_record) |
| 166 | + perform_request |
106 | 167 |
|
107 | 168 | expect(vaccination_record.nhs_immunisations_api_etag).to eq "1" |
108 | 169 | end |
109 | 170 |
|
110 | 171 | context "an error is returned by the api" do |
111 | | - before do |
112 | | - stub_request( |
113 | | - :post, |
114 | | - "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
115 | | - ).to_return(status: status, body: response, headers: {}) |
116 | | - end |
117 | | - |
118 | | - let(:status) { 201 } |
119 | 172 | let(:code) { nil } |
120 | 173 | let(:diagnostics) { nil } |
121 | | - |
122 | | - let(:response) do |
| 174 | + let(:headers) { {} } |
| 175 | + let(:body) do |
123 | 176 | { |
124 | 177 | resourceType: "OperationOutcome", |
125 | 178 | id: "bc2c3c82-4392-4314-9d6b-a7345f82d923", |
|
146 | 199 | }.to_json |
147 | 200 | end |
148 | 201 |
|
149 | | - context "unexpected response status" do |
150 | | - let(:status) { 200 } |
151 | | - let(:response) { "" } |
152 | | - |
153 | | - it "raises an error saying the response is unexpected" do |
154 | | - expect { |
155 | | - described_class.record_immunisation(vaccination_record) |
156 | | - }.to raise_error( |
157 | | - "Error recording vaccination record #{vaccination_record.id} to" \ |
158 | | - " Immunisations API: unexpected response status 200" |
159 | | - ) |
160 | | - end |
161 | | - end |
162 | | - |
163 | | - context "4XX error" do |
164 | | - let(:status) { 404 } |
165 | | - let(:diagnostics) { "Invalid patient ID" } |
166 | | - |
167 | | - it "raises an error with the diagnostic message" do |
168 | | - expect { |
169 | | - described_class.record_immunisation(vaccination_record) |
170 | | - }.to raise_error( |
171 | | - StandardError, |
172 | | - "Error recording vaccination record #{vaccination_record.id} to" \ |
173 | | - " Immunisations API: Invalid patient ID" |
174 | | - ) |
175 | | - end |
176 | | - end |
177 | | - |
178 | | - context "generic error" do |
179 | | - before do |
180 | | - stub_request( |
181 | | - :post, |
182 | | - "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
183 | | - ).to_return(status: 500, body: nil, headers: {}) |
184 | | - end |
185 | | - |
186 | | - it "raises an error with the diagnostic message" do |
187 | | - expect { |
188 | | - described_class.record_immunisation(vaccination_record) |
189 | | - }.to raise_error(Faraday::Error) |
190 | | - end |
191 | | - end |
| 202 | + include_examples "unexpected response status", 200, "recording" |
| 203 | + include_examples "client error (4XX) handling", "recording" |
| 204 | + include_examples "generic error handling" |
192 | 205 | end |
193 | 206 |
|
194 | | - context "the immunisations_fhir_api_integration feature flag is disabled" do |
195 | | - before { Flipper.disable(:immunisations_fhir_api_integration) } |
196 | | - |
197 | | - it "does not make a request to the NHS API" do |
198 | | - described_class.record_immunisation(vaccination_record) |
199 | | - |
200 | | - expect(stubbed_request).not_to have_been_made |
201 | | - end |
202 | | - end |
| 207 | + include_examples "an immunisations_fhir_api_integration feature flag check" |
203 | 208 | end |
204 | 209 | end |
0 commit comments