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)
|
|
|
|
recipients.count { |to| to == user.notification_email }
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def reset_delivered_emails!
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
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
|
|
|
|
|
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)
|
|
|
|
ActionMailer::Base.deliveries.find { |d| d.to.include?(user.notification_email) }
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
def have_referable_subject(referable, include_project: true, reply: false)
|
|
|
|
prefix = (include_project && referable.project ? "#{referable.project.name} | " : '').freeze
|
|
|
|
prefix = "Re: #{prefix}" if reply
|
|
|
|
|
|
|
|
suffix = "#{referable.title} (#{referable.to_reference})"
|
|
|
|
|
|
|
|
have_subject [prefix, suffix].compact.join
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|