-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathappointment_service_spec.rb
258 lines (213 loc) · 9.65 KB
/
appointment_service_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# frozen_string_literal: true
require 'rails_helper'
describe Eps::AppointmentService do
subject(:service) { described_class.new(user) }
let(:user) { double('User', account_uuid: '1234', icn:) }
let(:config) { instance_double(Eps::Configuration) }
let(:headers) { { 'Authorization' => 'Bearer token123' } }
let(:appointment_id) { 'appointment-123' }
let(:icn) { '123ICN' }
before do
allow(config).to receive(:base_path).and_return('api/v1')
allow_any_instance_of(Eps::BaseService).to receive_messages(config:, headers:)
end
describe '#get_appointment' do
let(:success_response) do
double('Response', status: 200, body: { 'id' => appointment_id,
'state' => 'submitted',
'patientId' => icn })
end
context 'when the request is successful' do
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_return(success_response)
end
it 'returns appointment details' do
response = service.get_appointment(appointment_id:)
expect(response.id).to eq(appointment_id)
expect(response.state).to eq('submitted')
expect(response.patientId).to eq(icn)
end
context 'when retrieve_latest_details is true' do
before do
expect_any_instance_of(VAOS::SessionService).to receive(:perform)
.with(:get, "/#{config.base_path}/appointments/#{appointment_id}?retrieveLatestDetails=true", {}, headers)
.and_return(success_response)
end
it 'includes the retrieveLatestDetails query parameter' do
response = service.get_appointment(appointment_id:, retrieve_latest_details: true)
expect(response.id).to eq(appointment_id)
end
end
end
context 'when the endpoint fails to return appointments' do
let(:failed_appt_response) do
double('Response', status: 500, body: 'Unknown service exception')
end
let(:exception) do
Common::Exceptions::BackendServiceException.new(nil, {}, failed_appt_response.status,
failed_appt_response.body)
end
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_raise(exception)
end
it 'throws exception' do
expect { service.get_appointment(appointment_id:) }.to raise_error(Common::Exceptions::BackendServiceException,
/VA900/)
end
end
end
describe 'get_appointments' do
context 'when requesting appointments for a logged in user' do
let(:successful_appt_response) do
double('Response', status: 200, body: { 'count' => 1,
'appointments' => [
{
'id' => appointment_id,
'state' => 'booked',
'patientId' => icn
}
] })
end
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_return(successful_appt_response)
end
end
context 'when the endpoint fails to return appointments' do
let(:failed_appt_response) do
double('Response', status: 500, body: 'Unknown service exception')
end
let(:exception) do
Common::Exceptions::BackendServiceException.new(nil, {}, failed_appt_response.status,
failed_appt_response.body)
end
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_raise(exception)
end
it 'throws exception' do
expect { service.get_appointments }.to raise_error(Common::Exceptions::BackendServiceException,
/VA900/)
end
end
end
describe 'create_draft_appointment' do
let(:referral_id) { 'test-referral-id' }
let(:successful_draft_appt_response) do
double('Response', status: 200, body: { 'id' => appointment_id,
'state' => 'draft',
'patientId' => icn })
end
context 'when creating draft appointment for a given referral_id' do
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_return(successful_draft_appt_response)
end
it 'returns the appointments scheduled' do
exp_response = OpenStruct.new(successful_draft_appt_response.body)
expect(service.create_draft_appointment(referral_id:)).to eq(exp_response)
end
end
context 'when the endpoint fails' do
let(:failed_response) do
double('Response', status: 500, body: 'Unknown service exception')
end
let(:exception) do
Common::Exceptions::BackendServiceException.new(nil, {}, failed_response.status,
failed_response.body)
end
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_raise(exception)
end
it 'throws exception' do
expect do
service.create_draft_appointment(referral_id:)
end.to raise_error(Common::Exceptions::BackendServiceException, /VA900/)
end
end
end
describe '#submit_appointment' do
let(:valid_params) do
{
network_id: 'network-123',
provider_service_id: 'provider-456',
slot_ids: ['slot-789'],
referral_number: 'REF-001'
}
end
context 'with valid parameters' do
let(:successful_response) do
double('Response', status: 200, body: { 'id' => appointment_id,
'state' => 'draft',
'patientId' => icn })
end
it 'submits the appointment successfully' do
expected_payload = {
network_id: valid_params[:network_id],
provider_service_id: valid_params[:provider_service_id],
slot_ids: valid_params[:slot_ids],
referral: {
referral_number: valid_params[:referral_number]
}
}
expect_any_instance_of(VAOS::SessionService).to receive(:perform)
.with(:post, "/#{config.base_path}/appointments/#{appointment_id}/submit", expected_payload, kind_of(Hash))
.and_return(successful_response)
exp_response = OpenStruct.new(successful_response.body)
expect(service.submit_appointment(appointment_id, valid_params)).to eq(exp_response)
end
it 'includes additional patient attributes when provided' do
params_with_attributes = valid_params.merge(additional_patient_attributes: patient_attributes)
expected_payload = {
network_id: valid_params[:network_id],
provider_service_id: valid_params[:provider_service_id],
slot_ids: valid_params[:slot_ids],
referral: {
referral_number: valid_params[:referral_number]
},
additional_patient_attributes: patient_attributes
}
expect_any_instance_of(VAOS::SessionService).to receive(:perform)
.with(:post, "/#{config.base_path}/appointments/#{appointment_id}/submit", expected_payload, kind_of(Hash))
.and_return(successful_response)
service.submit_appointment(appointment_id, params_with_attributes)
end
end
context 'with invalid parameters' do
it 'raises ArgumentError when appointment_id is nil' do
expect { service.submit_appointment(nil, valid_params) }
.to raise_error(ArgumentError, 'appointment_id is required and cannot be blank')
end
it 'raises ArgumentError when appointment_id is empty' do
expect { service.submit_appointment('', valid_params) }
.to raise_error(ArgumentError, 'appointment_id is required and cannot be blank')
end
it 'raises ArgumentError when appointment_id is blank' do
expect { service.submit_appointment(' ', valid_params) }
.to raise_error(ArgumentError, 'appointment_id is required and cannot be blank')
end
it 'raises ArgumentError when required parameters are missing' do
invalid_params = valid_params.except(:network_id)
expect { service.submit_appointment(appointment_id, invalid_params) }
.to raise_error(ArgumentError, /Missing required parameters: network_id/)
end
it 'raises ArgumentError when multiple required parameters are missing' do
invalid_params = valid_params.except(:network_id, :provider_service_id)
expect { service.submit_appointment(appointment_id, invalid_params) }
.to raise_error(ArgumentError, /Missing required parameters: network_id, provider_service_id/)
end
end
context 'when API returns an error' do
let(:response) { double('Response', status: 500, body: 'Unknown service exception') }
let(:exception) do
Common::Exceptions::BackendServiceException.new(nil, {}, response.status, response.body)
end
before do
allow_any_instance_of(VAOS::SessionService).to receive(:perform).and_raise(exception)
end
it 'returns the error response' do
expect do
service.submit_appointment(appointment_id, valid_params)
end.to raise_error(Common::Exceptions::BackendServiceException, /VA900/)
end
end
end
end