Move notification model tests to the right files

These tests were testing model methods, but were inside files for system
tests.

We're moving them now because these tests use the `open_last_email`
method, and we're looking for places where using this method might
result in a flaky test when used inside a system test.
This commit is contained in:
Javi Martín
2025-03-12 00:43:54 +01:00
parent 3a9263f6e6
commit fb639a376d
4 changed files with 77 additions and 77 deletions

View File

@@ -168,4 +168,66 @@ describe Notification do
expect(notification.notifiable_action).to eq "replies_to" expect(notification.notifiable_action).to eq "replies_to"
end end
end end
describe ".send_pending", :delay_jobs do
let!(:user1) { create(:user) }
let!(:user2) { create(:user) }
let!(:user3) { create(:user) }
let!(:proposal_notification) { create(:proposal_notification) }
before do
create(:notification, notifiable: proposal_notification, user: user1)
create(:notification, notifiable: proposal_notification, user: user2)
create(:notification, notifiable: proposal_notification, user: user3)
reset_mailer
end
it "sends pending proposal notifications" do
Setting["org_name"] = "CONSUL"
Delayed::Worker.delay_jobs = false
Notification.send_pending
email = open_last_email
expect(email).to have_subject "Proposal notifications in CONSUL"
end
it "sends emails in batches" do
Notification.send_pending
expect(Delayed::Job.count).to eq 3
end
it "sends batches in time intervals" do
allow(Notification).to receive_messages(
batch_size: 1,
batch_interval: 1.second,
first_batch_run_at: Time.current
)
remove_users_without_pending_notifications
Notification.send_pending
now = Notification.first_batch_run_at
first_batch_run_at = now.change(usec: 0)
second_batch_run_at = (now + 1.second).change(usec: 0)
third_batch_run_at = (now + 2.seconds).change(usec: 0)
expect(Delayed::Job.count).to eq 3
expect(Delayed::Job.first.run_at.change(usec: 0)).to eq first_batch_run_at
expect(Delayed::Job.second.run_at.change(usec: 0)).to eq second_batch_run_at
expect(Delayed::Job.third.run_at.change(usec: 0)).to eq third_batch_run_at
end
end
def remove_users_without_pending_notifications
users_without_notifications.each(&:destroy)
end
def users_without_notifications
User.select do |user|
user.notifications.not_emailed.where(notifiable_type: "ProposalNotification").blank?
end
end
end end

View File

@@ -174,6 +174,21 @@ describe ProposalNotification do
proposal_notification.moderate_system_email(admin.user) proposal_notification.moderate_system_email(admin.user)
expect(Activity.last.actionable_type).to eq("ProposalNotification") expect(Activity.last.actionable_type).to eq("ProposalNotification")
end end
it "does not send emails for moderated notifications" do
user = create(:user, email_digest: true)
notification = create(:notification, :for_proposal_notification)
reset_mailer
notification.notifiable.moderate_system_email(create(:administrator).user)
email_digest = EmailDigest.new(user)
email_digest.deliver(Time.current)
email_digest.mark_as_emailed
expect { open_last_email }.to raise_error "No email has been sent!"
end
end end
end end
end end

View File

@@ -306,21 +306,6 @@ describe "Emails" do
expect(notification2.emailed_at).to be expect(notification2.emailed_at).to be
expect(email_digest.notifications).to be_empty expect(email_digest.notifications).to be_empty
end end
scenario "notifications moderated are not sent" do
user = create(:user, email_digest: true)
notification = create(:notification, :for_proposal_notification)
reset_mailer
notification.notifiable.moderate_system_email(create(:administrator).user)
email_digest = EmailDigest.new(user)
email_digest.deliver(Time.current)
email_digest.mark_as_emailed
expect { open_last_email }.to raise_error "No email has been sent!"
end
end end
context "User invites" do context "User invites" do

View File

@@ -185,66 +185,4 @@ describe "Notifications" do
expect(page).not_to have_link href: notification_path(notification), visible: :all expect(page).not_to have_link href: notification_path(notification), visible: :all
end end
end end
describe "#send_pending", :delay_jobs do
let!(:user1) { create(:user) }
let!(:user2) { create(:user) }
let!(:user3) { create(:user) }
let!(:proposal_notification) { create(:proposal_notification) }
before do
create(:notification, notifiable: proposal_notification, user: user1)
create(:notification, notifiable: proposal_notification, user: user2)
create(:notification, notifiable: proposal_notification, user: user3)
reset_mailer
end
it "sends pending proposal notifications" do
Setting["org_name"] = "CONSUL"
Delayed::Worker.delay_jobs = false
Notification.send_pending
email = open_last_email
expect(email).to have_subject("Proposal notifications in CONSUL")
end
it "sends emails in batches" do
Notification.send_pending
expect(Delayed::Job.count).to eq(3)
end
it "sends batches in time intervals" do
allow(Notification).to receive_messages(
batch_size: 1,
batch_interval: 1.second,
first_batch_run_at: Time.current
)
remove_users_without_pending_notifications
Notification.send_pending
now = Notification.first_batch_run_at
first_batch_run_at = now.change(usec: 0)
second_batch_run_at = (now + 1.second).change(usec: 0)
third_batch_run_at = (now + 2.seconds).change(usec: 0)
expect(Delayed::Job.count).to eq(3)
expect(Delayed::Job.first.run_at.change(usec: 0)).to eq(first_batch_run_at)
expect(Delayed::Job.second.run_at.change(usec: 0)).to eq(second_batch_run_at)
expect(Delayed::Job.third.run_at.change(usec: 0)).to eq(third_batch_run_at)
end
end
def remove_users_without_pending_notifications
users_without_notifications.each(&:destroy)
end
def users_without_notifications
User.select do |user|
user.notifications.not_emailed.where(notifiable_type: "ProposalNotification").blank?
end
end
end end