|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe AwsSesSubmissionBatchService do |
| 4 | + let(:service) { described_class.new(submissions:, form:, date:, mode:) } |
| 5 | + let(:form) { build(:form, id: form_latest_version.form_id, submission_email:) } |
| 6 | + let(:form_earlier_version) { create(:v2_form_document, :with_steps, form_id: form_latest_version.form_id, updated_at: form_earlier_version_updated_at) } |
| 7 | + let(:form_latest_version) { create(:v2_form_document, :with_steps, updated_at: form_latest_version_updated_at) } |
| 8 | + let(:submission_email) { "submit@email.gov.uk" } |
| 9 | + let(:form_earlier_version_updated_at) { Time.utc(2022, 9, 14, 7, 0, 0).iso8601(3) } |
| 10 | + let(:form_latest_version_updated_at) { Time.utc(2022, 9, 14, 8, 0, 0).iso8601(3) } |
| 11 | + let(:date) { Date.new(2024, 6, 1) } |
| 12 | + let(:mode) { instance_double(Mode, preview?: false) } |
| 13 | + let(:submissions) { earlier_version_submissions + latest_version_submissions } |
| 14 | + let(:earlier_version_submissions) { create_list(:submission, 2, form_document: form_earlier_version) } |
| 15 | + let(:latest_version_submissions) { create_list(:submission, 3, form_document: form_latest_version) } |
| 16 | + |
| 17 | + describe "#send_batch" do |
| 18 | + before do |
| 19 | + allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv", "filename-2.csv") |
| 20 | + allow(CsvGenerator).to receive(:generate_batched_submissions).and_return("csv-content", "csv-content-2") |
| 21 | + end |
| 22 | + |
| 23 | + context "when the form does not have a submission email address" do |
| 24 | + let(:submission_email) { nil } |
| 25 | + |
| 26 | + it "raises an error" do |
| 27 | + expect(AwsSesSubmissionBatchMailer).not_to receive(:batch_submission_email) |
| 28 | + expect { service.send_batch }.to raise_error(StandardError, "Form id: #{form.id} is missing a submission email address") |
| 29 | + end |
| 30 | + |
| 31 | + context "when the mode is preview" do |
| 32 | + let(:mode) { instance_double(Mode, preview?: true) } |
| 33 | + |
| 34 | + it "does not send an email" do |
| 35 | + expect(AwsSesSubmissionBatchMailer).not_to receive(:batch_submission_email) |
| 36 | + service.send_batch |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + it "calls the SubmissionFilenameGenerator to generate a filename for each form version" do |
| 42 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: 1) |
| 43 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: 2) |
| 44 | + service.send_batch |
| 45 | + end |
| 46 | + |
| 47 | + context "when there is only one form version" do |
| 48 | + let(:submissions) { latest_version_submissions } |
| 49 | + |
| 50 | + it "calls the SubmissionFilenameGenerator with a nil form version" do |
| 51 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date:, mode:, form_version: nil) |
| 52 | + service.send_batch |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + it "calls the CsvGenerator for each form version" do |
| 57 | + expect(CsvGenerator).to receive(:generate_batched_submissions).with(submissions: earlier_version_submissions, is_s3_submission: false) |
| 58 | + expect(CsvGenerator).to receive(:generate_batched_submissions).with(submissions: latest_version_submissions, is_s3_submission: false) |
| 59 | + service.send_batch |
| 60 | + end |
| 61 | + |
| 62 | + it "calls the AwsSesSubmissionBatchMailer to send the email with the generated files" do |
| 63 | + expect(AwsSesSubmissionBatchMailer).to receive(:batch_submission_email) |
| 64 | + .with( |
| 65 | + form:, |
| 66 | + date:, |
| 67 | + mode:, |
| 68 | + files: { "filename.csv" => "csv-content", "filename-2.csv" => "csv-content-2" }, |
| 69 | + ).and_call_original |
| 70 | + |
| 71 | + service.send_batch |
| 72 | + end |
| 73 | + |
| 74 | + it "returns the message id" do |
| 75 | + message_id = service.send_batch |
| 76 | + |
| 77 | + last_email = ActionMailer::Base.deliveries.last |
| 78 | + expect(message_id).to eq last_email.message_id |
| 79 | + end |
| 80 | + end |
| 81 | +end |
0 commit comments