|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe Dependents::Form686c674FailureEmailJob, type: :job do |
| 6 | + let(:job) { described_class.new } |
| 7 | + let(:claim_id) { 123 } |
| 8 | + let(:email) { '[email protected]' } |
| 9 | + let(:template_id) { 'test-template-id' } |
| 10 | + let(:claim) do |
| 11 | + instance_double( |
| 12 | + SavedClaim::DependencyClaim, |
| 13 | + form_id: described_class::FORM_ID, |
| 14 | + confirmation_number: 'ABCD1234', |
| 15 | + parsed_form: { |
| 16 | + 'veteran_information' => { |
| 17 | + 'full_name' => { |
| 18 | + 'first' => 'John' |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + ) |
| 23 | + end |
| 24 | + let(:personalisation) do |
| 25 | + { |
| 26 | + 'first_name' => 'JOHN', |
| 27 | + 'date_submitted' => 'January 01, 2023', |
| 28 | + 'confirmation_number' => 'ABCD1234' |
| 29 | + } |
| 30 | + end |
| 31 | + let(:va_notify_client) { instance_double(VaNotify::Service) } |
| 32 | + |
| 33 | + describe '#perform' do |
| 34 | + before do |
| 35 | + allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) |
| 36 | + allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) |
| 37 | + allow(va_notify_client).to receive(:send_email) |
| 38 | + allow(Rails.logger).to receive(:warn) |
| 39 | + end |
| 40 | + |
| 41 | + it 'sends an email with the correct parameters' do |
| 42 | + expect(va_notify_client).to receive(:send_email).with( |
| 43 | + email_address: email, |
| 44 | + template_id:, |
| 45 | + personalisation: { |
| 46 | + 'first_name' => 'JOHN', |
| 47 | + 'date_submitted' => 'January 01, 2023', |
| 48 | + 'confirmation_number' => 'ABCD1234' |
| 49 | + } |
| 50 | + ) |
| 51 | + |
| 52 | + job.perform(claim_id, email, template_id, personalisation) |
| 53 | + end |
| 54 | + |
| 55 | + context 'when an error occurs' do |
| 56 | + before do |
| 57 | + allow(va_notify_client).to receive(:send_email).and_raise(StandardError.new('Test error')) |
| 58 | + end |
| 59 | + |
| 60 | + it 'logs the error and allows retry' do |
| 61 | + expect(Rails.logger).to receive(:warn).with( |
| 62 | + 'Form686c674FailureEmailJob failed, retrying send...', |
| 63 | + { claim_id:, error: instance_of(StandardError) } |
| 64 | + ) |
| 65 | + |
| 66 | + # Should not raise error |
| 67 | + expect { job.perform(claim_id, email, template_id, personalisation) }.not_to raise_error |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + describe 'sidekiq_retries_exhausted' do |
| 73 | + it 'logs an error when retries are exhausted' do |
| 74 | + msg = { 'args' => [claim_id] } |
| 75 | + ex = StandardError.new('Test exhausted error') |
| 76 | + |
| 77 | + expect(Rails.logger).to receive(:error).with( |
| 78 | + 'Form686c674FailureEmailJob exhausted all retries', |
| 79 | + { |
| 80 | + saved_claim_id: claim_id, |
| 81 | + error_message: 'Test exhausted error' |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + described_class.sidekiq_retries_exhausted_block.call(msg, ex) |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe '#va_notify_client' do |
| 90 | + before do |
| 91 | + allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) |
| 92 | + allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) |
| 93 | + |
| 94 | + vanotify = double('vanotify') |
| 95 | + services = double('services') |
| 96 | + va_gov = double('va_gov') |
| 97 | + |
| 98 | + allow(Settings).to receive(:vanotify).and_return(vanotify) |
| 99 | + allow(vanotify).to receive(:services).and_return(services) |
| 100 | + allow(services).to receive(:va_gov).and_return(va_gov) |
| 101 | + allow(va_gov).to receive(:api_key).and_return('test-api-key') |
| 102 | + end |
| 103 | + |
| 104 | + it 'initializes VaNotify::Service with correct parameters' do |
| 105 | + expected_callback_options = { |
| 106 | + callback_metadata: { |
| 107 | + notification_type: 'error', |
| 108 | + form_id: described_class::FORM_ID, |
| 109 | + statsd_tags: { service: 'dependent-change', function: described_class::ZSF_DD_TAG_FUNCTION } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + expect(VaNotify::Service).to receive(:new).with('test-api-key', expected_callback_options) |
| 114 | + |
| 115 | + # Just allow the job to execute, which should create the client |
| 116 | + allow(va_notify_client).to receive(:send_email) |
| 117 | + job.perform(claim_id, email, template_id, personalisation) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + describe '#personalisation' do |
| 122 | + before do |
| 123 | + allow(SavedClaim::DependencyClaim).to receive(:find).with(claim_id).and_return(claim) |
| 124 | + today = double('today') |
| 125 | + allow(Time.zone).to receive(:today).and_return(today) |
| 126 | + allow(today).to receive(:strftime).with('%B %d, %Y').and_return('January 01, 2023') |
| 127 | + # Create the service but don't set expectations on send_email yet |
| 128 | + allow(VaNotify::Service).to receive(:new).and_return(va_notify_client) |
| 129 | + end |
| 130 | + |
| 131 | + it 'sends correct personalisation data in the email' do |
| 132 | + # Instead of directly calling the private method, test what gets sent to va_notify_client |
| 133 | + expect(va_notify_client).to receive(:send_email).with( |
| 134 | + email_address: email, |
| 135 | + template_id:, |
| 136 | + personalisation: |
| 137 | + ) |
| 138 | + |
| 139 | + job.perform(claim_id, email, template_id, { |
| 140 | + 'first_name' => 'JOHN', |
| 141 | + 'date_submitted' => 'January 01, 2023', |
| 142 | + 'confirmation_number' => 'ABCD1234' |
| 143 | + }) |
| 144 | + end |
| 145 | + |
| 146 | + context 'when first name is nil' do |
| 147 | + let(:claim) do |
| 148 | + instance_double( |
| 149 | + SavedClaim::DependencyClaim, |
| 150 | + form_id: described_class::FORM_ID, |
| 151 | + confirmation_number: 'ABCD1234', |
| 152 | + parsed_form: { |
| 153 | + 'veteran_information' => { |
| 154 | + 'full_name' => { |
| 155 | + 'first' => nil |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + ) |
| 160 | + end |
| 161 | + |
| 162 | + it 'handles nil first_name gracefully' do |
| 163 | + expect(va_notify_client).to receive(:send_email).with( |
| 164 | + email_address: email, |
| 165 | + template_id:, |
| 166 | + personalisation: hash_including('first_name' => nil) |
| 167 | + ) |
| 168 | + personalisation['first_name'] = nil |
| 169 | + job.perform(claim_id, email, template_id, personalisation) |
| 170 | + end |
| 171 | + end |
| 172 | + end |
| 173 | +end |
0 commit comments