-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy patheps_appointment_worker_spec.rb
110 lines (96 loc) · 4 KB
/
eps_appointment_worker_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
# frozen_string_literal: true
require 'rails_helper'
require 'va_notify'
RSpec.describe Eps::EpsAppointmentWorker, type: :job do
subject(:worker) { described_class.new }
let(:user) { build(:user) }
let(:appointment_id) { '12345' }
let(:service) { instance_double(Eps::AppointmentService) }
let(:response) { OpenStruct.new(state: 'completed', appointmentDetails: OpenStruct.new(status: 'booked')) }
let(:unfinished_response) { OpenStruct.new(state: 'pending', appointmentDetails: OpenStruct.new(status: 'pending')) }
let(:va_notify_service) { instance_double(VaNotify::Service) }
before do
Sidekiq::Job.clear_all
allow(Eps::AppointmentService).to receive(:new).and_return(service)
allow(VaNotify::Service).to receive(:new)
.with(Settings.vanotify.services.va_gov.api_key)
.and_return(va_notify_service)
end
describe '.perform_async' do
it 'submits successfully' do
expect do
described_class.perform_async(appointment_id, user)
end.to change(described_class.jobs, :size).by(1)
end
it 'calls get_appointment with the appointment_id' do
allow(service).to receive(:get_appointment).with(appointment_id:).and_return(response)
expect(service).to receive(:get_appointment).with(appointment_id:)
worker.perform(appointment_id, user)
end
context 'when the appointment is not finished' do
before do
allow(service).to receive(:get_appointment).with(appointment_id:).and_return(unfinished_response)
end
it 'retries the job' do
expect(described_class).to receive(:perform_in).with(1.minute, appointment_id, user, 1)
worker.perform(appointment_id, user)
end
it 'sends failure message after max retries' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Could not complete booking'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
end
end
context 'when the appointment is not found' do
before do
allow(service).to receive(:get_appointment).with(appointment_id:).and_raise(
Common::Exceptions::BackendServiceException.new(nil, {}, 404, 'Appointment not found')
)
end
it 'sends failure message after max retries' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Service error, please contact support'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
end
end
context 'when the upstream service returns a 500 error' do
before do
allow(service).to receive(:get_appointment).with(appointment_id:).and_raise(
Common::Exceptions::BackendServiceException.new(nil, {}, 500, 'Internal server error')
)
end
it 'sends failure message after max retries' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => 'Service error, please contact support'
}
)
worker.perform(appointment_id, user, Eps::EpsAppointmentWorker::MAX_RETRIES)
end
end
describe '#send_vanotify_message' do
it 'sends email notification' do
expect(va_notify_service).to receive(:send_email).with(
email_address: user.va_profile_email,
template_id: Settings.vanotify.services.va_gov.template_id.va_appointment_failure,
parameters: {
'error' => nil
}
)
worker.send(:send_vanotify_message, user:)
end
end
end
end