2015-08-15 05:21:22 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-08-02 16:43:51 +00:00
|
|
|
describe EmailsOnPushWorker, :mailer do
|
2015-08-15 05:21:22 +00:00
|
|
|
include RepoHelpers
|
2016-08-19 12:07:39 +00:00
|
|
|
include EmailSpec::Matchers
|
2015-08-15 05:21:22 +00:00
|
|
|
|
2017-03-27 20:42:08 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2015-08-15 05:21:22 +00:00
|
|
|
let(:user) { create(:user) }
|
2016-08-12 08:09:29 +00:00
|
|
|
let(:data) { Gitlab::DataBuilder::Push.build_sample(project, user) }
|
2016-05-06 12:16:53 +00:00
|
|
|
let(:recipients) { user.email }
|
|
|
|
let(:perform) { subject.perform(project.id, recipients, data.stringify_keys) }
|
2016-08-19 12:07:39 +00:00
|
|
|
let(:email) { ActionMailer::Base.deliveries.last }
|
2015-08-15 05:21:22 +00:00
|
|
|
|
2017-05-01 15:13:33 +00:00
|
|
|
subject { described_class.new }
|
2015-08-15 05:21:22 +00:00
|
|
|
|
|
|
|
describe "#perform" do
|
2016-07-21 09:53:38 +00:00
|
|
|
context "when push is a new branch" do
|
|
|
|
before do
|
|
|
|
data_new_branch = data.stringify_keys.merge("before" => Gitlab::Git::BLANK_SHA)
|
|
|
|
|
|
|
|
subject.perform(project.id, recipients, data_new_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends a mail with the correct subject" do
|
|
|
|
expect(email.subject).to include("Pushed new branch")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the mail to the correct recipient" do
|
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when push is a deleted branch" do
|
|
|
|
before do
|
|
|
|
data_deleted_branch = data.stringify_keys.merge("after" => Gitlab::Git::BLANK_SHA)
|
|
|
|
|
|
|
|
subject.perform(project.id, recipients, data_deleted_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends a mail with the correct subject" do
|
|
|
|
expect(email.subject).to include("Deleted branch")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the mail to the correct recipient" do
|
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-19 12:07:39 +00:00
|
|
|
context "when push is a force push to delete commits" do
|
|
|
|
before do
|
|
|
|
data_force_push = data.stringify_keys.merge(
|
|
|
|
"after" => data[:before],
|
|
|
|
"before" => data[:after]
|
|
|
|
)
|
|
|
|
|
|
|
|
subject.perform(project.id, recipients, data_force_push)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends a mail with the correct subject" do
|
2016-10-03 12:11:16 +00:00
|
|
|
expect(email.subject).to include('adds bar folder and branch-test text file')
|
2016-08-19 12:07:39 +00:00
|
|
|
end
|
2016-05-06 12:16:53 +00:00
|
|
|
|
2016-08-19 12:07:39 +00:00
|
|
|
it "mentions force pushing in the body" do
|
|
|
|
expect(email).to have_body_text("force push")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the mail to the correct recipient" do
|
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are no errors in sending" do
|
2017-06-14 18:18:56 +00:00
|
|
|
before do
|
|
|
|
perform
|
|
|
|
end
|
2015-08-15 05:21:22 +00:00
|
|
|
|
2016-05-06 12:16:53 +00:00
|
|
|
it "sends a mail with the correct subject" do
|
2016-10-03 12:11:16 +00:00
|
|
|
expect(email.subject).to include('adds bar folder and branch-test text file')
|
2016-05-06 12:16:53 +00:00
|
|
|
end
|
|
|
|
|
2016-08-19 12:07:39 +00:00
|
|
|
it "does not mention force pushing in the body" do
|
|
|
|
expect(email).not_to have_body_text("force push")
|
|
|
|
end
|
|
|
|
|
2016-05-06 12:16:53 +00:00
|
|
|
it "sends the mail to the correct recipient" do
|
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
end
|
2015-08-15 05:21:22 +00:00
|
|
|
end
|
|
|
|
|
2016-05-06 12:16:53 +00:00
|
|
|
context "when there is an SMTP error" do
|
|
|
|
before do
|
|
|
|
allow(Notify).to receive(:repository_push_email).and_raise(Net::SMTPFatalError)
|
2016-08-19 12:08:21 +00:00
|
|
|
allow(subject).to receive_message_chain(:logger, :info)
|
2016-05-06 12:16:53 +00:00
|
|
|
perform
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gracefully handles an input SMTP error" do
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are multiple recipients" do
|
|
|
|
before do
|
2018-11-15 10:13:50 +00:00
|
|
|
# This is a hack because we modify the mail object before sending, for efficiency,
|
2016-05-06 12:16:53 +00:00
|
|
|
# but the TestMailer adapter just appends the objects to an array. To clone a mail
|
|
|
|
# object, create a new one!
|
|
|
|
# https://github.com/mikel/mail/issues/314#issuecomment-12750108
|
|
|
|
allow_any_instance_of(Mail::TestMailer).to receive(:deliver!).and_wrap_original do |original, mail|
|
|
|
|
original.call(Mail.new(mail.encoded))
|
|
|
|
end
|
|
|
|
end
|
2015-08-15 05:21:22 +00:00
|
|
|
|
2018-07-12 09:00:36 +00:00
|
|
|
context "when the recipient addresses are a list of email addresses" do
|
|
|
|
let(:recipients) do
|
|
|
|
1.upto(5).map { |i| user.email.sub('@', "+#{i}@") }.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the mail to each of the recipients" do
|
|
|
|
perform
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(5)
|
|
|
|
expect(email_recipients).to contain_exactly(*recipients.split)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "only generates the mail once" do
|
|
|
|
expect(Notify).to receive(:repository_push_email).once.and_call_original
|
|
|
|
expect(Premailer::Rails::CustomizedPremailer).to receive(:new).once.and_call_original
|
|
|
|
|
|
|
|
perform
|
|
|
|
end
|
2016-05-06 12:16:53 +00:00
|
|
|
end
|
2015-08-15 05:21:22 +00:00
|
|
|
|
2018-07-12 09:00:36 +00:00
|
|
|
context "when the recipient addresses contains angle brackets and are separated by spaces" do
|
|
|
|
let(:recipients) { "John Doe <johndoe@example.com> Jane Doe <janedoe@example.com>" }
|
|
|
|
|
|
|
|
it "accepts emails separated by whitespace" do
|
|
|
|
perform
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(2)
|
|
|
|
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the recipient addresses contain a mix of emails with and without angle brackets" do
|
|
|
|
let(:recipients) { "johndoe@example.com Jane Doe <janedoe@example.com>" }
|
|
|
|
|
|
|
|
it "accepts both kind of emails" do
|
|
|
|
perform
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(2)
|
|
|
|
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the recipient addresses contains angle brackets and are separated by newlines" do
|
|
|
|
let(:recipients) { "John Doe <johndoe@example.com>\nJane Doe <janedoe@example.com>" }
|
|
|
|
|
|
|
|
it "accepts emails separated by newlines" do
|
|
|
|
perform
|
|
|
|
|
|
|
|
expect(ActionMailer::Base.deliveries.count).to eq(2)
|
|
|
|
expect(email_recipients).to contain_exactly("johndoe@example.com", "janedoe@example.com")
|
|
|
|
end
|
2016-05-06 12:16:53 +00:00
|
|
|
end
|
2015-08-15 05:21:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|