2016-02-12 09:58:39 -05:00
|
|
|
module EmailHelpers
|
2017-10-10 14:45:43 -04:00
|
|
|
def sent_to_user(user, recipients: email_recipients)
|
|
|
|
recipients.count { |to| to == user.notification_email }
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
|
2016-08-16 12:08:43 -04:00
|
|
|
def reset_delivered_emails!
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
end
|
|
|
|
|
2016-10-18 08:02:35 -04:00
|
|
|
def should_only_email(*users, kind: :to)
|
|
|
|
recipients = email_recipients(kind: kind)
|
2016-09-19 02:01:58 -04:00
|
|
|
|
2017-10-10 14:45:43 -04:00
|
|
|
users.each { |user| should_email(user, recipients: recipients) }
|
2016-09-19 02:01:58 -04:00
|
|
|
|
2016-08-16 12:08:43 -04:00
|
|
|
expect(recipients.count).to eq(users.count)
|
|
|
|
end
|
|
|
|
|
2017-10-10 14:45:43 -04:00
|
|
|
def should_email(user, times: 1, recipients: email_recipients)
|
2019-04-17 05:10:42 -04:00
|
|
|
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
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
|
|
|
|
2017-10-10 14:45:43 -04:00
|
|
|
def should_not_email(user, recipients: email_recipients)
|
2017-10-12 09:18:15 -04:00
|
|
|
should_email(user, times: 0, recipients: recipients)
|
2016-02-12 09:58:39 -05:00
|
|
|
end
|
2016-09-19 01:54:16 -04:00
|
|
|
|
2016-10-17 06:03:53 -04:00
|
|
|
def should_not_email_anyone
|
2016-09-19 01:54:16 -04:00
|
|
|
expect(ActionMailer::Base.deliveries).to be_empty
|
|
|
|
end
|
2016-09-28 03:39:54 -04:00
|
|
|
|
2016-10-18 08:02:35 -04:00
|
|
|
def email_recipients(kind: :to)
|
|
|
|
ActionMailer::Base.deliveries.flat_map(&kind)
|
2016-09-28 03:39:54 -04:00
|
|
|
end
|
2017-12-28 12:25:02 -05:00
|
|
|
|
|
|
|
def find_email_for(user)
|
|
|
|
ActionMailer::Base.deliveries.find { |d| d.to.include?(user.notification_email) }
|
|
|
|
end
|
2018-12-11 07:33:27 -05:00
|
|
|
|
|
|
|
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-02-12 09:58:39 -05:00
|
|
|
end
|