|
64 | 64 |
|
65 | 65 | let!(:bounced_submission) { create :submission, :bounced, form_id: } |
66 | 66 | let!(:another_bounced_submission) { create :submission, :bounced, form_id: } |
| 67 | + let!(:bounced_daily_delivery) { create :delivery, :failed, :daily_scheduled_delivery, submissions: [bounced_submission, another_bounced_submission] } |
67 | 68 |
|
68 | 69 | before do |
69 | 70 | # create some submissions that won't be matched |
|
72 | 73 | end |
73 | 74 |
|
74 | 75 | it "logs the bounced submissions" do |
75 | | - expect(Rails.logger).to receive(:info).with("Found 2 bounced submission deliveries for form with ID #{form_id}") |
76 | | - expect(Rails.logger).to receive(:info).with "Submission reference: #{bounced_submission.reference}, created_at: #{bounced_submission.created_at}, last_attempt_at: #{bounced_submission.single_submission_delivery.last_attempt_at}" |
77 | | - expect(Rails.logger).to receive(:info).with "Submission reference: #{another_bounced_submission.reference}, created_at: #{another_bounced_submission.created_at}, last_attempt_at: #{another_bounced_submission.single_submission_delivery.last_attempt_at}" |
| 76 | + expect(Rails.logger).to receive(:info).with("Found 3 bounced submission deliveries for form with ID #{form_id}") |
| 77 | + expect(Rails.logger).to receive(:info).with "Immediate delivery - submission reference: #{bounced_submission.reference}, created_at: #{bounced_submission.created_at}, last_attempt_at: #{bounced_submission.single_submission_delivery.last_attempt_at}" |
| 78 | + expect(Rails.logger).to receive(:info).with "Immediate delivery - submission reference: #{another_bounced_submission.reference}, created_at: #{another_bounced_submission.created_at}, last_attempt_at: #{another_bounced_submission.single_submission_delivery.last_attempt_at}" |
| 79 | + expect(Rails.logger).to receive(:info).with "Daily batch delivery - delivery_reference: #{bounced_daily_delivery.delivery_reference}, created_at: #{bounced_daily_delivery.created_at}, last_attempt_at: #{bounced_daily_delivery.last_attempt_at}" |
78 | 80 | task.invoke(form_id) |
79 | 81 | end |
80 | 82 | end |
|
89 | 91 | let(:other_form_id) { 2 } |
90 | 92 | let!(:bounced_submission) { create :submission, :bounced, form_id: } |
91 | 93 | let!(:pending_submission) { create :submission, :sent, form_id: } |
| 94 | + let!(:bounced_delivery) { create :delivery, :daily_scheduled_delivery, :failed, submissions: [bounced_submission] } |
92 | 95 |
|
93 | 96 | before do |
94 | 97 | create :submission, :sent, form_id: other_form_id |
|
99 | 102 |
|
100 | 103 | it "logs how many deliveries to retry" do |
101 | 104 | allow(Rails.logger).to receive(:info) |
102 | | - expect(Rails.logger).to receive(:info).with("1 submission deliveries to retry for form with ID: #{form_id}") |
| 105 | + expect(Rails.logger).to receive(:info).with("2 deliveries to retry for form with ID: #{form_id}") |
103 | 106 |
|
104 | 107 | task.invoke(*valid_args) |
105 | 108 | end |
|
108 | 111 | it "logs submissions that are being retried" do |
109 | 112 | allow(Rails.logger).to receive(:info) |
110 | 113 | expect(Rails.logger).to receive(:info).with("Retrying submission with reference #{bounced_submission.reference} for form with ID: #{form_id}") |
| 114 | + expect(Rails.logger).to receive(:info).with("Retrying daily batch delivery with delivery_id: #{bounced_delivery.id} for date: #{bounced_submission.submission_time.to_date} for form with ID: #{form_id}") |
111 | 115 |
|
112 | 116 | task.invoke(*valid_args) |
113 | 117 | end |
|
118 | 122 | }.to have_enqueued_job.with(bounced_submission) |
119 | 123 | end |
120 | 124 |
|
| 125 | + it "enqueues bounced daily deliveries for retrying" do |
| 126 | + expect { |
| 127 | + task.invoke(*valid_args) |
| 128 | + }.to have_enqueued_job.with(delivery: bounced_delivery) |
| 129 | + end |
| 130 | + |
121 | 131 | it "does not enqueue pending submissions for retrying" do |
122 | 132 | expect { |
123 | 133 | task.invoke(*valid_args) |
|
254 | 264 | create :delivery, :failed, submissions: [submission], created_at: Time.parse("2024-01-01T18:00:00Z") |
255 | 265 | end |
256 | 266 |
|
| 267 | + let!(:matching_batch_delivery) do |
| 268 | + submission = create(:submission, form_id:) |
| 269 | + create :delivery, :failed, :daily_scheduled_delivery, submissions: [submission], created_at: Time.parse("2024-01-01T12:00:00Z") |
| 270 | + end |
| 271 | + |
257 | 272 | let!(:not_bounced_delivery) do |
258 | 273 | submission = create(:submission, form_id:) |
259 | 274 | create :delivery, submissions: [submission], created_at: Time.parse("2024-01-01T12:00:00Z") |
|
291 | 306 |
|
292 | 307 | it "logs the deliveries to disregard" do |
293 | 308 | allow(Rails.logger).to receive(:info) |
294 | | - expect(Rails.logger).to receive(:info).with("Found 2 bounced submission deliveries to disregard for form ID #{form_id} in time range: #{Time.zone.parse(start_time)} to #{Time.zone.parse(end_time)}").once |
| 309 | + expect(Rails.logger).to receive(:info).with("Found 3 bounced submission deliveries to disregard for form ID #{form_id} in time range: #{Time.zone.parse(start_time)} to #{Time.zone.parse(end_time)}").once |
295 | 310 | expect(Rails.logger).to receive(:info).with("Disregarded bounce of delivery with delivery_reference #{early_matching_delivery.delivery_reference}") |
296 | 311 | expect(Rails.logger).to receive(:info).with("Disregarded bounce of delivery with delivery_reference #{late_matching_delivery.delivery_reference}") |
| 312 | + expect(Rails.logger).to receive(:info).with("Disregarded bounce of delivery with delivery_reference #{matching_batch_delivery.delivery_reference}") |
297 | 313 | task.invoke(*valid_args) |
298 | 314 | end |
299 | 315 |
|
|
310 | 326 | allow(Rails.logger).to receive(:info) |
311 | 327 | expect(Rails.logger).to receive(:info).with("Would disregard bounce of delivery with delivery_reference #{early_matching_delivery.delivery_reference} which was created at #{early_matching_delivery.created_at}") |
312 | 328 | expect(Rails.logger).to receive(:info).with("Would disregard bounce of delivery with delivery_reference #{late_matching_delivery.delivery_reference} which was created at #{late_matching_delivery.created_at}") |
| 329 | + expect(Rails.logger).to receive(:info).with("Would disregard bounce of delivery with delivery_reference #{matching_batch_delivery.delivery_reference} which was created at #{matching_batch_delivery.created_at}") |
313 | 330 | task.invoke(*valid_args) |
314 | 331 | end |
315 | 332 | end |
|
518 | 535 | end |
519 | 536 | end |
520 | 537 |
|
| 538 | + describe "submissions:redeliver_daily_batches_by_date" do |
| 539 | + subject(:task) do |
| 540 | + Rake::Task["submissions:redeliver_daily_batches_by_date"] |
| 541 | + .tap(&:reenable) |
| 542 | + end |
| 543 | + |
| 544 | + let(:form_id) { 1 } |
| 545 | + let(:other_form_id) { 2 } |
| 546 | + let(:start_time) { "2024-01-01T00:00:00Z" } |
| 547 | + let(:end_time) { "2024-01-02T00:00:00Z" } |
| 548 | + let(:dry_run) { "false" } |
| 549 | + |
| 550 | + let!(:batched_submission) do |
| 551 | + create :submission, |
| 552 | + :sent, |
| 553 | + form_id:, |
| 554 | + created_at: Time.parse("2024-01-01T12:00:00Z"), |
| 555 | + reference: "ref1" |
| 556 | + end |
| 557 | + |
| 558 | + let!(:batch_delivery) do |
| 559 | + create :delivery, |
| 560 | + :daily_scheduled_delivery, |
| 561 | + :failed, |
| 562 | + submissions: [batched_submission], |
| 563 | + created_at: Time.parse("2024-01-02T02:00:00Z"), |
| 564 | + delivery_reference: "batch1" |
| 565 | + end |
| 566 | + |
| 567 | + let!(:next_day_batch_delivery) do |
| 568 | + submission = create :submission, |
| 569 | + :sent, |
| 570 | + form_id:, |
| 571 | + created_at: Time.parse("2024-01-02T12:00:00Z"), |
| 572 | + reference: "ref1" |
| 573 | + create :delivery, |
| 574 | + :daily_scheduled_delivery, |
| 575 | + :failed, |
| 576 | + submissions: [submission], |
| 577 | + created_at: Time.parse("2024-01-03T02:00:00Z"), |
| 578 | + delivery_reference: "batch-next-day" |
| 579 | + end |
| 580 | + |
| 581 | + let!(:other_form_batch_delivery) do |
| 582 | + submission = create :submission, |
| 583 | + :sent, |
| 584 | + form_id: other_form_id, |
| 585 | + created_at: Time.parse("2024-01-01T12:00:00Z"), |
| 586 | + reference: "ref2" |
| 587 | + create :delivery, |
| 588 | + :daily_scheduled_delivery, |
| 589 | + :failed, |
| 590 | + submissions: [submission], |
| 591 | + created_at: Time.parse("2024-01-02T02:00:00Z"), |
| 592 | + delivery_reference: "batch2" |
| 593 | + end |
| 594 | + |
| 595 | + context "with valid arguments" do |
| 596 | + let(:valid_args) { [form_id, start_time, end_time, dry_run] } |
| 597 | + |
| 598 | + it "enqueues matching batch delivery for re-delivery" do |
| 599 | + expect { |
| 600 | + task.invoke(*valid_args) |
| 601 | + }.to have_enqueued_job(SendSubmissionBatchJob).with(delivery: batch_delivery) |
| 602 | + end |
| 603 | + |
| 604 | + it "does not enqueue a batch delivery outside the time range" do |
| 605 | + expect { |
| 606 | + task.invoke(*valid_args) |
| 607 | + }.not_to have_enqueued_job(SendSubmissionBatchJob).with(delivery: next_day_batch_delivery) |
| 608 | + end |
| 609 | + |
| 610 | + it "does not enqueue another form's batch deliveries" do |
| 611 | + expect { |
| 612 | + task.invoke(*valid_args) |
| 613 | + }.not_to have_enqueued_job(SendSubmissionBatchJob).with(delivery: other_form_batch_delivery) |
| 614 | + end |
| 615 | + |
| 616 | + context "when dry_run is true" do |
| 617 | + let(:dry_run) { "true" } |
| 618 | + |
| 619 | + it "does not enqueue any jobs" do |
| 620 | + expect { |
| 621 | + task.invoke(*valid_args) |
| 622 | + }.not_to have_enqueued_job(SendSubmissionJob) |
| 623 | + end |
| 624 | + end |
| 625 | + |
| 626 | + context "when no submissions took place between the given times" do |
| 627 | + let(:valid_args) { [form_id, "2025-01-01T00:00:00Z", "2025-01-02T00:00:00Z", dry_run] } |
| 628 | + |
| 629 | + it "does not enqueue any jobs" do |
| 630 | + expect { |
| 631 | + task.invoke(*valid_args) |
| 632 | + }.not_to have_enqueued_job(SendSubmissionBatchJob) |
| 633 | + end |
| 634 | + end |
| 635 | + end |
| 636 | + |
| 637 | + context "with invalid arguments" do |
| 638 | + it "aborts when form_id is missing" do |
| 639 | + expect { |
| 640 | + task.invoke("", start_time, end_time, dry_run) |
| 641 | + }.to raise_error(SystemExit) |
| 642 | + .and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr |
| 643 | + end |
| 644 | + |
| 645 | + it "aborts when start_timestamp is missing" do |
| 646 | + expect { |
| 647 | + task.invoke(form_id, "", end_time, dry_run) |
| 648 | + }.to raise_error(SystemExit) |
| 649 | + .and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr |
| 650 | + end |
| 651 | + |
| 652 | + it "aborts when end_timestamp is missing" do |
| 653 | + expect { |
| 654 | + task.invoke(form_id, start_time, "", dry_run) |
| 655 | + }.to raise_error(SystemExit) |
| 656 | + .and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr |
| 657 | + end |
| 658 | + |
| 659 | + it "aborts when start_timestamp is invalid" do |
| 660 | + expect { |
| 661 | + task.invoke(form_id, "invalid-date", end_time, dry_run) |
| 662 | + }.to raise_error(SystemExit) |
| 663 | + .and output("Error: Invalid timestamp format. Use ISO 8601 format (e.g. '2024-01-01T00:00:00Z')\n").to_stderr |
| 664 | + end |
| 665 | + |
| 666 | + it "aborts when end_timestamp is invalid" do |
| 667 | + expect { |
| 668 | + task.invoke(form_id, start_time, "invalid-date", dry_run) |
| 669 | + }.to raise_error(SystemExit) |
| 670 | + .and output("Error: Invalid timestamp format. Use ISO 8601 format (e.g. '2024-01-01T00:00:00Z')\n").to_stderr |
| 671 | + end |
| 672 | + |
| 673 | + it "aborts when start_timestamp is after end_timestamp" do |
| 674 | + expect { |
| 675 | + task.invoke(form_id, "2024-01-02T00:00:00Z", "2024-01-01T00:00:00Z", dry_run) |
| 676 | + }.to raise_error(SystemExit) |
| 677 | + .and output("Error: Start timestamp must be before end timestamp\n").to_stderr |
| 678 | + end |
| 679 | + |
| 680 | + it "aborts when start_timestamp equals end_timestamp" do |
| 681 | + expect { |
| 682 | + task.invoke(form_id, start_time, start_time, dry_run) |
| 683 | + }.to raise_error(SystemExit) |
| 684 | + .and output("Error: Start timestamp must be before end timestamp\n").to_stderr |
| 685 | + end |
| 686 | + |
| 687 | + it "aborts when dry_run is an invalid value" do |
| 688 | + expect { |
| 689 | + task.invoke(form_id, start_time, start_time, "foo") |
| 690 | + }.to raise_error(SystemExit) |
| 691 | + .and output("usage: rake submissions:redeliver_daily_batches_by_date[<form_id>,<start_timestamp>,<end_timestamp>,<dry_run>]\n").to_stderr |
| 692 | + end |
| 693 | + end |
| 694 | + end |
| 695 | + |
521 | 696 | describe "submissions:file_answers:fix_missing_original_filenames" do |
522 | 697 | subject(:task) do |
523 | 698 | Rake::Task["submissions:file_answers:fix_missing_original_filenames"] |
|
0 commit comments