|
49 | 49 | end |
50 | 50 |
|
51 | 51 | describe "record_immunisation" do |
| 52 | + before do |
| 53 | + stub_request( |
| 54 | + :post, |
| 55 | + "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 | + ) |
| 64 | + end |
| 65 | + |
52 | 66 | it "sends the correct JSON payload" do |
53 | 67 | expected_body = |
54 | 68 | File.read(Rails.root.join("spec/fixtures/fhir/immunisation.json")).chomp |
55 | 69 |
|
56 | 70 | # stree-ignore |
57 | 71 | stubbed_request = |
58 | 72 | stub_request( |
59 | | - :post, |
60 | | - "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
| 73 | + :post, "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
61 | 74 | ) |
62 | 75 | .with { |request| |
63 | | - expect(request.headers["Accept"]).to eq "application/fhir+json" |
64 | | - expect( |
65 | | - request.headers["Content-Type"] |
66 | | - ).to eq "application/fhir+json" |
67 | | - expect(request.body).to eq expected_body |
68 | | - true |
69 | | - } |
70 | | - .to_return(status: 200, body: "", headers: {}) |
| 76 | + expect(request.headers["Accept"]).to eq "application/fhir+json" |
| 77 | + expect( |
| 78 | + request.headers["Content-Type"] |
| 79 | + ).to eq "application/fhir+json" |
| 80 | + expect(request.body).to eq expected_body |
| 81 | + true |
| 82 | + } |
| 83 | + .to_return(status: 201, |
| 84 | + body: "", |
| 85 | + headers: { |
| 86 | + location: |
| 87 | + "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/Immunization/ffff1111-eeee-2222-dddd-3333eeee4444" |
| 88 | + }) |
71 | 89 |
|
72 | 90 | described_class.record_immunisation(vaccination_record) |
73 | 91 |
|
74 | 92 | expect(stubbed_request).to have_been_made |
75 | 93 | end |
76 | 94 |
|
| 95 | + it "stores the id from the response" do |
| 96 | + described_class.record_immunisation(vaccination_record) |
| 97 | + |
| 98 | + expect( |
| 99 | + vaccination_record.nhs_immunisations_api_id |
| 100 | + ).to eq "ffff1111-eeee-2222-dddd-3333eeee4444" |
| 101 | + end |
| 102 | + |
| 103 | + it "stores the nhs_immunisations_api_synced_at from the response" do |
| 104 | + freeze_time do |
| 105 | + described_class.record_immunisation(vaccination_record) |
| 106 | + |
| 107 | + expect( |
| 108 | + vaccination_record.nhs_immunisations_api_synced_at |
| 109 | + ).to eq Time.current |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + it "initialises the etag to 1" do |
| 114 | + described_class.record_immunisation(vaccination_record) |
| 115 | + |
| 116 | + expect(vaccination_record.nhs_immunisations_api_etag).to eq "1" |
| 117 | + end |
| 118 | + |
77 | 119 | context "an error is returned by the api" do |
78 | | - context "4XX error" do |
79 | | - let(:response) do |
80 | | - { |
81 | | - resourceType: "OperationOutcome", |
82 | | - id: "bc2c3c82-4392-4314-9d6b-a7345f82d923", |
83 | | - meta: { |
84 | | - profile: [ |
85 | | - "https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome" |
86 | | - ] |
87 | | - }, |
88 | | - issue: [ |
89 | | - { |
90 | | - severity: "error", |
91 | | - code: "invalid", |
92 | | - details: { |
93 | | - coding: [ |
94 | | - { |
95 | | - system: "https://fhir.nhs.uk/Codesystem/http-error-codes", |
96 | | - code: "NOT-FOUND" |
97 | | - } |
98 | | - ] |
99 | | - }, |
100 | | - diagnostics: "Invalid patient ID" |
101 | | - } |
| 120 | + before do |
| 121 | + stub_request( |
| 122 | + :post, |
| 123 | + "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
| 124 | + ).to_return(status: status, body: response, headers: {}) |
| 125 | + end |
| 126 | + |
| 127 | + let(:status) { 201 } |
| 128 | + let(:code) { nil } |
| 129 | + let(:diagnostics) { nil } |
| 130 | + |
| 131 | + let(:response) do |
| 132 | + { |
| 133 | + resourceType: "OperationOutcome", |
| 134 | + id: "bc2c3c82-4392-4314-9d6b-a7345f82d923", |
| 135 | + meta: { |
| 136 | + profile: [ |
| 137 | + "https://simplifier.net/guide/UKCoreDevelopment2/ProfileUKCore-OperationOutcome" |
102 | 138 | ] |
103 | | - }.to_json |
104 | | - end |
| 139 | + }, |
| 140 | + issue: [ |
| 141 | + { |
| 142 | + severity: "error", |
| 143 | + code: "invalid", |
| 144 | + details: { |
| 145 | + coding: [ |
| 146 | + { |
| 147 | + system: "https://fhir.nhs.uk/Codesystem/http-error-codes", |
| 148 | + code: |
| 149 | + } |
| 150 | + ] |
| 151 | + }, |
| 152 | + diagnostics: |
| 153 | + } |
| 154 | + ] |
| 155 | + }.to_json |
| 156 | + end |
105 | 157 |
|
106 | | - before do |
107 | | - stub_request( |
108 | | - :post, |
109 | | - "https://sandbox.api.service.nhs.uk/immunisation-fhir-api/FHIR/R4/Immunization" |
110 | | - ).to_return(status: 404, body: response, headers: {}) |
| 158 | + context "unexpected response status" do |
| 159 | + let(:status) { 200 } |
| 160 | + let(:response) { "" } |
| 161 | + |
| 162 | + it "raises an error saying the response is unexpected" do |
| 163 | + expect { |
| 164 | + described_class.record_immunisation(vaccination_record) |
| 165 | + }.to raise_error( |
| 166 | + "Error syncing vaccination record #{vaccination_record.id} to" \ |
| 167 | + " Immunisations API: unexpected response status 200" |
| 168 | + ) |
111 | 169 | end |
| 170 | + end |
| 171 | + |
| 172 | + context "4XX error" do |
| 173 | + let(:status) { 404 } |
112 | 174 |
|
113 | 175 | it "raises an error with the diagnostic message" do |
114 | 176 | expect { |
|
0 commit comments