|
1 | 1 | require "rails_helper" |
2 | 2 |
|
3 | | -RSpec.describe SendBounceNotificationsJob do |
| 3 | +RSpec.describe SendBounceNotificationsJob, :capture_logging do |
4 | 4 | include ActiveSupport::Testing::TimeHelpers |
5 | 5 | include ActiveJob::TestHelper |
6 | 6 |
|
7 | 7 | let(:bounced_on_date) { Date.new(2026, 5, 6) } |
| 8 | + let(:user_role) { :group_admin } |
8 | 9 |
|
9 | 10 | let(:form_with_bounces) { build :form } |
10 | 11 | let(:other_form_with_bounces) { build :form } |
|
24 | 25 | end |
25 | 26 | end |
26 | 27 |
|
27 | | - context "when there are multiple forms with bounces on the date", :capture_logging do |
| 28 | + context "when there are multiple forms with bounces on the date" do |
28 | 29 | before do |
29 | 30 | create_list :delivery, 2, :bounced, :daily_scheduled_delivery, form_id: other_form_with_bounces.id, failed_at: Time.zone.local(2026, 5, 6, 22, 59, 0) |
30 | | - described_class.perform_now(bounced_on_date:) |
| 31 | + described_class.perform_now(bounced_on_date:, user_role:) |
31 | 32 | end |
32 | 33 |
|
33 | 34 | it "sends an email per form with bounced submissions" do |
|
42 | 43 | expect(log_lines).to include( |
43 | 44 | hash_including( |
44 | 45 | "level" => "INFO", |
45 | | - "message" => "Sent bounce notifications to group admins for bounced deliveries on 6 May 2026 for form #{form_with_bounces.form_id}", |
| 46 | + "message" => "Sent bounce notifications to group admin users for bounced deliveries on 6 May 2026 for form #{form_with_bounces.form_id}", |
46 | 47 | ), |
47 | 48 | hash_including( |
48 | 49 | "level" => "INFO", |
49 | | - "message" => "Sent bounce notifications to group admins for bounced deliveries on 6 May 2026 for form #{other_form_with_bounces.form_id}", |
| 50 | + "message" => "Sent bounce notifications to group admin users for bounced deliveries on 6 May 2026 for form #{other_form_with_bounces.form_id}", |
50 | 51 | ), |
51 | 52 | ) |
52 | 53 | end |
53 | 54 | end |
54 | 55 |
|
55 | | - context "when the group has multiple group admins" do |
| 56 | + context "when notifying the group admins" do |
| 57 | + let(:user_role) { :group_admin } |
56 | 58 | let(:group) { build :group, group_admin_users_count: 2 } |
57 | 59 |
|
58 | 60 | before do |
59 | | - described_class.perform_now(bounced_on_date:) |
| 61 | + described_class.perform_now(bounced_on_date:, user_role:) |
60 | 62 | end |
61 | 63 |
|
62 | 64 | it "sends an email to each group admin" do |
|
66 | 68 | group.group_admin_users.second.email, |
67 | 69 | ) |
68 | 70 | end |
| 71 | + |
| 72 | + it "logs that it sent the notifications to the group admins" do |
| 73 | + expect(log_lines).to include( |
| 74 | + hash_including( |
| 75 | + "level" => "INFO", |
| 76 | + "message" => "Sent bounce notifications to group admin users for bounced deliveries on 6 May 2026 for form #{form_with_bounces.form_id}", |
| 77 | + ), |
| 78 | + ) |
| 79 | + end |
| 80 | + end |
| 81 | + |
| 82 | + context "when notifying the organisation admins" do |
| 83 | + let(:user_role) { :organisation_admin } |
| 84 | + let(:group) { build :group, group_admin_users_count: 1, organisation_admin_users_count: 2 } |
| 85 | + |
| 86 | + before do |
| 87 | + described_class.perform_now(bounced_on_date:, user_role:) |
| 88 | + end |
| 89 | + |
| 90 | + it "sends an email to each organisation admin" do |
| 91 | + expect(ActionMailer::Base.deliveries.size).to eq 2 |
| 92 | + expect(ActionMailer::Base.deliveries.map(&:to).flatten).to contain_exactly( |
| 93 | + group.organisation.organisation_admin_users.first.email, |
| 94 | + group.organisation.organisation_admin_users.second.email, |
| 95 | + ) |
| 96 | + end |
| 97 | + |
| 98 | + it "logs that it sent the notifications to the organisation admins" do |
| 99 | + expect(log_lines).to include( |
| 100 | + hash_including( |
| 101 | + "level" => "INFO", |
| 102 | + "message" => "Sent bounce notifications to organisation admin users for bounced deliveries on 6 May 2026 for form #{form_with_bounces.form_id}", |
| 103 | + ), |
| 104 | + ) |
| 105 | + end |
69 | 106 | end |
70 | 107 | end |
0 commit comments