From fb639a376df4b9dece73c5e305b9720e82714811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 12 Mar 2025 00:43:54 +0100 Subject: [PATCH] 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. --- spec/models/notification_spec.rb | 62 +++++++++++++++++++++++ spec/models/proposal_notification_spec.rb | 15 ++++++ spec/system/emails_spec.rb | 15 ------ spec/system/notifications_spec.rb | 62 ----------------------- 4 files changed, 77 insertions(+), 77 deletions(-) diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index 9dc725f07..cd02c10fa 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -168,4 +168,66 @@ describe Notification do expect(notification.notifiable_action).to eq "replies_to" 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 diff --git a/spec/models/proposal_notification_spec.rb b/spec/models/proposal_notification_spec.rb index c92773c81..179ba2876 100644 --- a/spec/models/proposal_notification_spec.rb +++ b/spec/models/proposal_notification_spec.rb @@ -174,6 +174,21 @@ describe ProposalNotification do proposal_notification.moderate_system_email(admin.user) expect(Activity.last.actionable_type).to eq("ProposalNotification") 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 diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index f97b521b9..44b999252 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -306,21 +306,6 @@ describe "Emails" do expect(notification2.emailed_at).to be expect(email_digest.notifications).to be_empty 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 context "User invites" do diff --git a/spec/system/notifications_spec.rb b/spec/system/notifications_spec.rb index c79cf239e..f139f1d43 100644 --- a/spec/system/notifications_spec.rb +++ b/spec/system/notifications_spec.rb @@ -185,66 +185,4 @@ describe "Notifications" do expect(page).not_to have_link href: notification_path(notification), visible: :all 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