Skip to content

Commit f060c69

Browse files
committed
Refactor immunisation api spec - shared examples
Part of the work to add immunisation api updates, this change has been separated out to simplify the overall change. Jira-Issue: MAV-1012
1 parent 7777220 commit f060c69

3 files changed

Lines changed: 82 additions & 77 deletions

File tree

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ terraform/.terraform
3939
*.tfstate
4040
*.tfstate.backup
4141
scratchpad
42-
spec/fixtures/fhir/immunisation.json
42+
spec/fixtures/fhir/immunisation-create.json
File renamed without changes.
Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
describe NHS::ImmunisationsAPI do
4+
before { Flipper.enable(:immunisations_fhir_api_integration) }
5+
46
let(:organisation) { create(:organisation, ods_code: "A9A5A") }
57
let(:patient) do
68
create(
@@ -48,24 +50,83 @@
4850
)
4951
end
5052

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+
51104
describe "record_immunisation" do
105+
subject(:perform_request) do
106+
described_class.record_immunisation(vaccination_record)
107+
end
108+
52109
let!(:request_stub) do
53110
stub_request(
54111
:post,
55112
"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+
}
64123
end
65124

66125
it "sends the correct JSON payload" do
67126
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
69130

70131
request_stub.with do |request|
71132
expect(request.headers).to include(
@@ -78,22 +139,22 @@
78139
true
79140
end
80141

81-
described_class.record_immunisation(vaccination_record)
142+
perform_request
82143

83144
expect(request_stub).to have_been_made
84145
end
85146

86147
it "stores the id from the response" do
87-
described_class.record_immunisation(vaccination_record)
148+
perform_request
88149

89150
expect(
90151
vaccination_record.nhs_immunisations_api_id
91152
).to eq "ffff1111-eeee-2222-dddd-3333eeee4444"
92153
end
93154

94-
it "stores the nhs_immunisations_api_synced_at from the response" do
155+
it "sets the nhs_immunisations_api_synced_at" do
95156
freeze_time do
96-
described_class.record_immunisation(vaccination_record)
157+
perform_request
97158

98159
expect(
99160
vaccination_record.nhs_immunisations_api_synced_at
@@ -102,24 +163,16 @@
102163
end
103164

104165
it "initialises the etag to 1" do
105-
described_class.record_immunisation(vaccination_record)
166+
perform_request
106167

107168
expect(vaccination_record.nhs_immunisations_api_etag).to eq "1"
108169
end
109170

110171
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 }
119172
let(:code) { nil }
120173
let(:diagnostics) { nil }
121-
122-
let(:response) do
174+
let(:headers) { {} }
175+
let(:body) do
123176
{
124177
resourceType: "OperationOutcome",
125178
id: "bc2c3c82-4392-4314-9d6b-a7345f82d923",
@@ -146,59 +199,11 @@
146199
}.to_json
147200
end
148201

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"
192205
end
193206

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"
203208
end
204209
end

0 commit comments

Comments
 (0)