|
5 | 5 | let(:form) { build(:form, submission_email:) } |
6 | 6 | let(:form_document) { build(:v2_form_document, :with_steps, form_id: form.id) } |
7 | 7 | let(:submission_email) { "submit@email.gov.uk" } |
8 | | - let(:date) { Date.new(2024, 6, 1) } |
9 | 8 | let(:mode) { instance_double(Mode, preview?: false) } |
10 | 9 | let(:submissions_query) { Submission.all } |
11 | 10 |
|
12 | 11 | describe "#send_daily_batch" do |
| 12 | + let(:date) { Date.new(2024, 6, 1) } |
| 13 | + |
13 | 14 | before do |
14 | 15 | allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv") |
15 | 16 | allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content]) |
|
28 | 29 | expect(message_id).to eq last_email.message_id |
29 | 30 | end |
30 | 31 |
|
| 32 | + context "when the csv generator returns a single csv version" do |
| 33 | + it "calls the AwsSesSubmissionBatchMailer to send the email with the generated file" do |
| 34 | + expect(AwsSesSubmissionBatchMailer).to receive(:daily_submission_batch_email) |
| 35 | + .with( |
| 36 | + form:, |
| 37 | + date:, |
| 38 | + mode:, |
| 39 | + files: { "filename.csv" => "csv-content" }, |
| 40 | + ).and_call_original |
| 41 | + |
| 42 | + service.send_daily_batch(date:) |
| 43 | + end |
| 44 | + end |
| 45 | + |
31 | 46 | context "when the csv generator returns multiple csv versions" do |
32 | 47 | before do |
33 | 48 | allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content csv-content-2]) |
|
42 | 57 |
|
43 | 58 | it "calls the AwsSesSubmissionBatchMailer to send the email with the generated files" do |
44 | 59 | expect(AwsSesSubmissionBatchMailer).to receive(:daily_submission_batch_email) |
45 | | - .with( |
46 | | - form:, |
47 | | - date:, |
48 | | - mode:, |
49 | | - files: { "filename.csv" => "csv-content", "filename-2.csv" => "csv-content-2" }, |
50 | | - ).and_call_original |
| 60 | + .with( |
| 61 | + form:, |
| 62 | + date:, |
| 63 | + mode:, |
| 64 | + files: { "filename.csv" => "csv-content", "filename-2.csv" => "csv-content-2" }, |
| 65 | + ).and_call_original |
51 | 66 |
|
52 | 67 | service.send_daily_batch(date:) |
53 | 68 | end |
54 | 69 | end |
55 | 70 | end |
| 71 | + |
| 72 | + describe "#send_weekly_batch" do |
| 73 | + let(:begin_date) { Date.new(2024, 6, 1) } |
| 74 | + let(:end_date) { Date.new(2024, 6, 7) } |
| 75 | + |
| 76 | + before do |
| 77 | + allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv") |
| 78 | + allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content]) |
| 79 | + create_list(:submission, 3, form_document:) |
| 80 | + end |
| 81 | + |
| 82 | + it "calls the SubmissionFilenameGenerator with a nil form version" do |
| 83 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: nil) |
| 84 | + service.send_weekly_batch(begin_date:, end_date:) |
| 85 | + end |
| 86 | + |
| 87 | + it "returns the message id" do |
| 88 | + message_id = service.send_weekly_batch(begin_date:, end_date:) |
| 89 | + |
| 90 | + last_email = ActionMailer::Base.deliveries.last |
| 91 | + expect(message_id).to eq last_email.message_id |
| 92 | + end |
| 93 | + |
| 94 | + context "when the csv generator returns a single csv version" do |
| 95 | + it "calls the AwsSesSubmissionBatchMailer to send the email with the generated file" do |
| 96 | + expect(AwsSesSubmissionBatchMailer).to receive(:weekly_submission_batch_email) |
| 97 | + .with( |
| 98 | + form:, |
| 99 | + begin_date:, |
| 100 | + end_date:, |
| 101 | + mode:, |
| 102 | + files: { "filename.csv" => "csv-content" }, |
| 103 | + ).and_call_original |
| 104 | + |
| 105 | + service.send_weekly_batch(begin_date:, end_date:) |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + context "when the csv generator returns multiple csv versions" do |
| 110 | + before do |
| 111 | + allow(CsvGenerator).to receive(:generate_batched_submissions).and_return(%w[csv-content csv-content-2]) |
| 112 | + allow(SubmissionFilenameGenerator).to receive(:batch_csv_filename).and_return("filename.csv", "filename-2.csv") |
| 113 | + end |
| 114 | + |
| 115 | + it "calls the SubmissionFilenameGenerator to generate a filename for each form version" do |
| 116 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: 1) |
| 117 | + expect(SubmissionFilenameGenerator).to receive(:batch_csv_filename).with(form_name: form.name, date: end_date, mode:, form_version: 2) |
| 118 | + service.send_weekly_batch(begin_date:, end_date:) |
| 119 | + end |
| 120 | + |
| 121 | + it "calls the AwsSesSubmissionBatchMailer to send the email with the generated files" do |
| 122 | + expect(AwsSesSubmissionBatchMailer).to receive(:weekly_submission_batch_email) |
| 123 | + .with( |
| 124 | + form:, |
| 125 | + begin_date:, |
| 126 | + end_date:, |
| 127 | + mode:, |
| 128 | + files: { "filename.csv" => "csv-content", "filename-2.csv" => "csv-content-2" }, |
| 129 | + ).and_call_original |
| 130 | + |
| 131 | + service.send_weekly_batch(begin_date:, end_date:) |
| 132 | + end |
| 133 | + end |
| 134 | + end |
56 | 135 | end |
0 commit comments