A platform for community discussion. Free, open, simple.
DEV: Remove the use of stubs and mocks in `Jobs::UserEmail` tests.
Guo Xiang Tan34b29f62db68
diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rbindex 4b0ae6b1f50..322d9c01d8d 100644--- a/app/jobs/regular/user_email.rb+++ b/app/jobs/regular/user_email.rb@@ -54,7 +54,10 @@ module Jobs)if message- Email::Sender.new(message, type, user).send+ Email::Sender.new(message, type, user).send(+ is_critical: self.class == Jobs::CriticalUserEmail+ )+if (b = user.user_stat.bounce_score) > SiteSetting.bounce_score_erode_on_send# erode bounce score each time we send an email# this means that we are punished a lot less for bouncesdiff --git a/lib/email/sender.rb b/lib/email/sender.rbindex 4cc6bcf25ea..40217f3d05e 100644--- a/lib/email/sender.rb+++ b/lib/email/sender.rb@@ -21,8 +21,13 @@ module Email@user = userend- def send- return if SiteSetting.disable_emails == "yes" && @email_type.to_s != "admin_login"+ def send(is_critical: false)+ if SiteSetting.disable_emails == "yes" &&+ @email_type.to_s != "admin_login" &&+ !is_critical++ return+ endreturn if ActionMailer::Base::NullMail === @messagereturn if ActionMailer::Base::NullMail === (@message.message rescue nil)diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rbindex 299aaba6c93..d35844bc895 100644--- a/spec/jobs/user_email_spec.rb+++ b/spec/jobs/user_email_spec.rb@@ -11,7 +11,6 @@ describe Jobs::UserEmail dolet(:staged) { Fabricate(:user, staged: true, last_seen_at: 11.minutes.ago) }let(:suspended) { Fabricate(:user, last_seen_at: 10.minutes.ago, suspended_at: 5.minutes.ago, suspended_till: 7.days.from_now) }let(:anonymous) { Fabricate(:anonymous, last_seen_at: 11.minutes.ago) }- let(:mailer) { Mail::Message.new(to: user.email) }it "raises an error when there is no user" doexpect { Jobs::UserEmail.new.execute(type: :digest) }.to raise_error(Discourse::InvalidParameters)@@ -26,13 +25,15 @@ describe Jobs::UserEmail doendit "doesn't call the mailer when the user is missing" do- UserNotifications.expects(:digest).neverJobs::UserEmail.new.execute(type: :digest, user_id: 1234)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't call the mailer when the user is staged" do- UserNotifications.expects(:digest).neverJobs::UserEmail.new.execute(type: :digest, user_id: staged.id)++ expect(ActionMailer::Base.deliveries).to eq([])endcontext "bounce score" do@@ -45,55 +46,48 @@ describe Jobs::UserEmail doemail_log = EmailLog.where(user_id: user.id).lastexpect(email_log.email_type).to eq("signup")++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )endendcontext 'to_address' doit 'overwrites a to_address when present' do- UserNotifications.expects(:confirm_new_email).returns(mailer)- Email::Sender.any_instance.expects(:send)Jobs::UserEmail.new.execute(type: :confirm_new_email, user_id: user.id, to_address: '[email protected]')- expect(mailer.to).to eq(['[email protected]'])++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ )endendcontext "disable_emails setting" doit "sends when no" doSiteSetting.disable_emails = 'no'- Email::Sender.any_instance.expects(:send).onceJobs::UserEmail.new.execute(type: :confirm_new_email, user_id: user.id)++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )endit "does not send an email when yes" doSiteSetting.disable_emails = 'yes'- Email::Sender.any_instance.expects(:send).neverJobs::UserEmail.new.execute(type: :confirm_new_email, user_id: user.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "sends when critical" doSiteSetting.disable_emails = 'yes'- Email::Sender.any_instance.expects(:send)Jobs::CriticalUserEmail.new.execute(type: :confirm_new_email, user_id: user.id)- end- end- context "recently seen" do- let(:post) { Fabricate(:post, user: user) }-- it "doesn't send an email to a user that's been recently seen" do- user.update_column(:last_seen_at, 9.minutes.ago)- Email::Sender.any_instance.expects(:send).never- Jobs::UserEmail.new.execute(type: :user_replied, user_id: user.id, post_id: post.id)- end-- it "does send an email to a user that's been recently seen but has email_always set" do- user.update_attributes(last_seen_at: 9.minutes.ago)- user.user_option.update_attributes(email_always: true)- PostTiming.create!(topic_id: post.topic_id, post_number: post.post_number, user_id: user.id, msecs: 100)-- Email::Sender.any_instance.expects(:send)- Jobs::UserEmail.new.execute(type: :user_replied, user_id: user.id, post_id: post.id)+ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )endend@@ -145,49 +139,65 @@ describe Jobs::UserEmail docontext 'args' doit 'passes a token as an argument when a token is present' do- UserNotifications.expects(:forgot_password).with(user, email_token: 'asdfasdf').returns(mailer)- Email::Sender.any_instance.expects(:send)Jobs::UserEmail.new.execute(type: :forgot_password, user_id: user.id, email_token: 'asdfasdf')++ mail = ActionMailer::Base.deliveries.first++ expect(mail.to).to contain_exactly(user.email)+ expect(mail.body).to include("asdfasdf")endcontext "post" dolet(:post) { Fabricate(:post, user: user) }- it 'passes a post as an argument when a post_id is present' do- UserNotifications.expects(:user_private_message).with(user, post: post).returns(mailer)- Email::Sender.any_instance.expects(:send)- Jobs::UserEmail.new.execute(type: :user_private_message, user_id: user.id, post_id: post.id)- end-it "doesn't send the email if you've seen the post" do- Email::Sender.any_instance.expects(:send).neverPostTiming.record_timing(topic_id: post.topic_id, user_id: user.id, post_number: post.post_number, msecs: 6666)Jobs::UserEmail.new.execute(type: :user_private_message, user_id: user.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send the email if the user deleted the post" do- Email::Sender.any_instance.expects(:send).neverpost.update_column(:user_deleted, true)Jobs::UserEmail.new.execute(type: :user_private_message, user_id: user.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send the email if user of the post has been deleted" do- Email::Sender.any_instance.expects(:send).neverpost.update_attributes!(user_id: nil)Jobs::UserEmail.new.execute(type: :user_replied, user_id: user.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endcontext 'user is suspended' doit "doesn't send email for a pm from a regular user" do- Email::Sender.any_instance.expects(:send).neverJobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "does send an email for a pm from a staff user" dopm_from_staff = Fabricate(:post, user: Fabricate(:moderator))pm_from_staff.topic.topic_allowed_users.create!(user_id: suspended.id)- Email::Sender.any_instance.expects(:send)- Jobs::UserEmail.new.execute(type: :user_private_message, user_id: suspended.id, post_id: pm_from_staff.id)++ pm_notification = Fabricate(:notification,+ user: suspended,+ topic: pm_from_staff.topic,+ post_number: pm_from_staff.post_number,+ data: { original_post_id: pm_from_staff.id }.to_json+ )++ Jobs::UserEmail.new.execute(type: :user_private_message,+ user_id: suspended.id,+ post_id: pm_from_staff.id,+ notification_id: pm_notification.id+ )++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ suspended.email+ )endend@@ -195,15 +205,17 @@ describe Jobs::UserEmail dobefore { SiteSetting.allow_anonymous_posting = true }it "doesn't send email for a pm from a regular user" do- Email::Sender.any_instance.expects(:send).neverJobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send email for a pm from a staff user" dopm_from_staff = Fabricate(:post, user: Fabricate(:moderator))pm_from_staff.topic.topic_allowed_users.create!(user_id: anonymous.id)- Email::Sender.any_instance.expects(:send).neverJobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, post_id: pm_from_staff.id)++ expect(ActionMailer::Base.deliveries).to eq([])endendend@@ -244,17 +256,61 @@ describe Jobs::UserEmail doendit "does send the email if the notification has been seen but the user is set for email_always" do- Email::Sender.any_instance.expects(:send)notification.update_column(:read, true)user.user_option.update_column(:email_always, true)- Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)++ Jobs::UserEmail.new.execute(type: :user_mentioned,+ user_id: user.id,+ post_id: post.id,+ notification_id: notification.id+ )++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )endit "does send the email if the user is using daily mailing list mode" do- Email::Sender.any_instance.expects(:send)user.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 0)- Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id)+ Jobs::UserEmail.new.execute(type: :user_mentioned,+ user_id: user.id,+ post_id: post.id,+ notification_id: notification.id+ )++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )+ end++ context "recently seen" do+ it "doesn't send an email to a user that's been recently seen" do+ user.update!(last_seen_at: 9.minutes.ago)++ Jobs::UserEmail.new.execute(type: :user_replied,+ user_id: user.id,+ post_id: post.id,+ notification_id: notification.id+ )++ expect(ActionMailer::Base.deliveries).to eq([])+ end++ it "does send an email to a user that's been recently seen but has email_always set" do+ user.update!(last_seen_at: 9.minutes.ago)+ user.user_option.update!(email_always: true)++ Jobs::UserEmail.new.execute(type: :user_replied,+ user_id: user.id,+ post_id: post.id,+ notification_id: notification.id+ )++ expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(+ user.email+ )+ endendcontext 'max_emails_per_day_per_user limit is reached' do@@ -361,7 +417,6 @@ describe Jobs::UserEmail doendit "doesn't send the mail if the user is using individual mailing list mode" do- Email::Sender.any_instance.expects(:send).neveruser.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 1)# sometimes, we pass the notification_idJobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id, post_id: post.id)@@ -373,10 +428,11 @@ describe Jobs::UserEmail dopost = Fabricate(:post)post.topic.destroyJobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_type: "posted", post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send the mail if the user is using individual mailing list mode with no echo" do- Email::Sender.any_instance.expects(:send).neveruser.user_option.update(mailing_list_mode: true, mailing_list_mode_frequency: 2)# sometimes, we pass the notification_idJobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id, post_id: post.id)@@ -388,12 +444,15 @@ describe Jobs::UserEmail dopost = Fabricate(:post)post.topic.destroyJobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_type: "posted", post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send the email if the post has been user deleted" do- Email::Sender.any_instance.expects(:send).neverpost.update_column(:user_deleted, true)Jobs::UserEmail.new.execute(type: :user_mentioned, user_id: user.id, notification_id: notification.id, post_id: post.id)++ expect(ActionMailer::Base.deliveries).to eq([])endcontext 'user is suspended' do@@ -450,8 +509,13 @@ describe Jobs::UserEmail dobefore { SiteSetting.allow_anonymous_posting = true }it "doesn't send email for a pm from a regular user" do- Email::Sender.any_instance.expects(:send).never- Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: notification.id)+ Jobs::UserEmail.new.execute(type: :user_private_message,+ user_id: anonymous.id,+ post_id: post.id,+ notification_id: notification.id+ )++ expect(ActionMailer::Base.deliveries).to eq([])endit "doesn't send email for a pm from staff" do@@ -463,8 +527,13 @@ describe Jobs::UserEmail dopost_number: pm_from_staff.post_number,data: { original_post_id: pm_from_staff.id }.to_json)- Email::Sender.any_instance.expects(:send).never- Jobs::UserEmail.new.execute(type: :user_private_message, user_id: anonymous.id, notification_id: pm_notification.id)+ Jobs::UserEmail.new.execute(type: :user_private_message,+ user_id: anonymous.id,+ post_id: pm_from_staff.id,+ notification_id: pm_notification.id+ )++ expect(ActionMailer::Base.deliveries).to eq([])endendend