debian-mirror-gitlab/spec/support/helpers/email_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
3.6 KiB
Ruby
Raw Permalink Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module EmailHelpers
2018-03-17 18:26:18 +05:30
def sent_to_user(user, recipients: email_recipients)
2021-11-11 11:23:49 +05:30
recipients.count { |to| to == user.notification_email_or_default }
2016-06-02 11:05:42 +05:30
end
2016-09-13 17:45:13 +05:30
def reset_delivered_emails!
2020-03-13 15:44:24 +05:30
# We shouldn't actually send the emails, but we keep the following line for
# back-compatibility until we only check the mailer jobs enqueued in Sidekiq
2016-09-13 17:45:13 +05:30
ActionMailer::Base.deliveries.clear
2020-03-13 15:44:24 +05:30
# We should only check that the mailer jobs are enqueued in Sidekiq, hence
# clearing the background jobs queue
ActiveJob::Base.queue_adapter.enqueued_jobs.clear
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
def should_only_email(*users, kind: :to)
recipients = email_recipients(kind: kind)
2018-03-17 18:26:18 +05:30
users.each { |user| should_email(user, recipients: recipients) }
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
expect(recipients.count).to eq(users.count)
end
2018-03-17 18:26:18 +05:30
def should_email(user, times: 1, recipients: email_recipients)
2019-07-31 22:56:46 +05:30
amount = sent_to_user(user, recipients: recipients)
failed_message = lambda { "User #{user.username} (#{user.id}): email test failed (expected #{times}, got #{amount})" }
expect(amount).to eq(times), failed_message
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
def should_not_email(user, recipients: email_recipients)
should_email(user, times: 0, recipients: recipients)
2017-08-17 22:00:37 +05:30
end
def should_not_email_anyone
expect(ActionMailer::Base.deliveries).to be_empty
2016-06-02 11:05:42 +05:30
end
2019-10-12 21:52:04 +05:30
def should_email_anyone
expect(ActionMailer::Base.deliveries).not_to be_empty
end
2017-08-17 22:00:37 +05:30
def email_recipients(kind: :to)
ActionMailer::Base.deliveries.flat_map(&kind)
2016-06-02 11:05:42 +05:30
end
2018-03-17 18:26:18 +05:30
def find_email_for(user)
2021-11-11 11:23:49 +05:30
ActionMailer::Base.deliveries.find { |d| d.to.include?(user.notification_email_or_default) }
2018-03-17 18:26:18 +05:30
end
2019-02-15 15:39:39 +05:30
2019-09-30 21:07:59 +05:30
def have_referable_subject(referable, include_project: true, reply: false)
prefix = (include_project && referable.project ? "#{referable.project.name} | " : '').freeze
2019-02-15 15:39:39 +05:30
prefix = "Re: #{prefix}" if reply
suffix = "#{referable.title} (#{referable.to_reference})"
have_subject [prefix, suffix].compact.join
end
2023-04-23 21:23:45 +05:30
def enqueue_mail_with(mailer_class, mail_method_name, *args)
args.map! { |arg| arg.is_a?(ActiveRecord::Base) ? arg.id : arg }
have_enqueued_mail(mailer_class, mail_method_name).with(*args)
end
def not_enqueue_mail_with(mailer_class, mail_method_name, *args)
args.map! { |arg| arg.is_a?(ActiveRecord::Base) ? arg.id : arg }
not_enqueue_mail(mailer_class, mail_method_name).with(*args)
end
def have_only_enqueued_mail_with_args(mailer_class, mailer_method, *args)
raise ArgumentError, 'You must provide at least one array of mailer arguments' if args.empty?
count_expectation = have_enqueued_mail(mailer_class, mailer_method).exactly(args.size).times
args.inject(count_expectation) do |composed_expectation, arguments|
composed_expectation.and(have_enqueued_mail(mailer_class, mailer_method).with(*arguments))
end
end
2023-06-20 00:43:36 +05:30
def expect_sender(user, sender_email: nil)
sender = subject.header[:from].addrs[0]
expect(sender.display_name).to eq("#{user.name} (@#{user.username})")
expect(sender.address).to eq(sender_email.presence || gitlab_sender)
end
def expect_service_desk_custom_email_delivery_options(service_desk_setting)
expect(subject.delivery_method).to be_a Mail::SMTP
expect(service_desk_setting.custom_email_credential).to be_present
credential = service_desk_setting.custom_email_credential
expect(subject.delivery_method.settings).to include(
address: credential.smtp_address,
port: credential.smtp_port,
user_name: credential.smtp_username,
password: credential.smtp_password,
domain: service_desk_setting.custom_email.split('@').last
)
end
2016-06-02 11:05:42 +05:30
end