|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +# rubocop:disable RSpec/InstanceVariable |
| 4 | +RSpec.describe SendSubmissionBatchJob, type: :job do |
| 5 | + include ActiveJob::TestHelper |
| 6 | + |
| 7 | + let(:mode_string) { "form" } |
| 8 | + let(:date) { Date.new(2022, 12, 14) } |
| 9 | + let(:delivery) { create(:delivery, batch_frequency: "daily") } |
| 10 | + |
| 11 | + let(:form_document) { create(:v2_form_document, :with_steps, name: "My Form", submission_email: "to@example.com") } |
| 12 | + let(:form_id) { form_document.form_id } |
| 13 | + let(:submissions) { [] } |
| 14 | + |
| 15 | + before do |
| 16 | + submissions |
| 17 | + ActionMailer::Base.deliveries.clear |
| 18 | + end |
| 19 | + |
| 20 | + context "when the job is processed" do |
| 21 | + before do |
| 22 | + described_class.perform_later(form_id:, mode_string:, date:, delivery:) |
| 23 | + travel 5.seconds do |
| 24 | + @job_ran_at = Time.zone.now |
| 25 | + perform_enqueued_jobs |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + context "when there are no submissions" do |
| 30 | + it "does not send an email" do |
| 31 | + expect(ActionMailer::Base.deliveries).to be_empty |
| 32 | + end |
| 33 | + |
| 34 | + it "does not update the delivery" do |
| 35 | + expect(delivery.reload.last_attempt_at).to be_nil |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + context "when there are submissions" do |
| 40 | + let(:submissions_to_include) do |
| 41 | + create_list( |
| 42 | + :submission, |
| 43 | + 3, |
| 44 | + form_document:, |
| 45 | + form_id:, |
| 46 | + mode: mode_string, |
| 47 | + created_at: date.beginning_of_day + 1.hour, |
| 48 | + ) |
| 49 | + end |
| 50 | + let(:submission_not_on_date) do |
| 51 | + create(:submission, form_document:, form_id:, mode: mode_string, created_at: date.beginning_of_day - 1.day) |
| 52 | + end |
| 53 | + let(:preview_submission) do |
| 54 | + create(:submission, :preview, form_document:, form_id:, created_at: date.beginning_of_day + 1.hour) |
| 55 | + end |
| 56 | + let(:submissions) { [submissions_to_include, submission_not_on_date, preview_submission] } |
| 57 | + |
| 58 | + it "sends an email" do |
| 59 | + expect(ActionMailer::Base.deliveries.count).to eq(1) |
| 60 | + |
| 61 | + mail = ActionMailer::Base.deliveries.last |
| 62 | + expect(mail.to).to include(form_document.submission_email) |
| 63 | + end |
| 64 | + |
| 65 | + it "updates the delivery" do |
| 66 | + mail = ActionMailer::Base.deliveries.last |
| 67 | + expect(delivery.reload.delivery_reference).to eq(mail.message_id) |
| 68 | + expect(delivery.reload.last_attempt_at).to be_within(1.second).of(@job_ran_at) |
| 69 | + end |
| 70 | + |
| 71 | + it "attaches a csv with the expected filename" do |
| 72 | + mail = ActionMailer::Base.deliveries.last |
| 73 | + expect(mail.attachments).not_to be_empty |
| 74 | + |
| 75 | + filenames = mail.attachments.map(&:filename) |
| 76 | + expect(filenames).to contain_exactly("govuk_forms_my_form_2022-12-14.csv") |
| 77 | + end |
| 78 | + |
| 79 | + it "attaches a csv containing header plus one line per submission" do |
| 80 | + mail = ActionMailer::Base.deliveries.last |
| 81 | + |
| 82 | + csv_content = mail.attachments.first.decoded |
| 83 | + expect(csv_content.lines.count).to eq(submissions_to_include.count + 1) |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | +end |
| 88 | +# rubocop:enable RSpec/InstanceVariable |
0 commit comments