2015-08-20 19:17:59 +00:00
|
|
|
require "spec_helper"
|
|
|
|
|
2015-12-09 10:55:36 +00:00
|
|
|
describe Gitlab::Email::Receiver, lib: true do
|
2015-08-20 19:17:59 +00:00
|
|
|
before do
|
2015-09-21 07:46:47 +00:00
|
|
|
stub_incoming_email_setting(enabled: true, address: "reply+%{key}@appmail.adventuretime.ooo")
|
2016-03-17 19:03:51 +00:00
|
|
|
stub_config_setting(host: 'localhost')
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
let(:mail_key) { "59d8df8370b7e95c5a49fbf86aeb2c93" }
|
2015-08-20 19:17:59 +00:00
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml') }
|
|
|
|
|
|
|
|
let(:project) { create(:project, :public) }
|
|
|
|
let(:noteable) { create(:issue, project: project) }
|
|
|
|
let(:user) { create(:user) }
|
2016-05-20 18:05:57 +00:00
|
|
|
let!(:sent_notification) { SentNotification.record(noteable, user.id, mail_key) }
|
2015-08-20 19:17:59 +00:00
|
|
|
|
|
|
|
let(:receiver) { described_class.new(email_raw) }
|
2016-03-23 14:20:22 +00:00
|
|
|
let(:markdown) { "![image](uploads/image.png)" }
|
|
|
|
|
|
|
|
def setup_attachment
|
|
|
|
allow_any_instance_of(Gitlab::Email::AttachmentUploader).to receive(:execute).and_return(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
url: "uploads/image.png",
|
|
|
|
is_image: true,
|
|
|
|
alt: "image",
|
|
|
|
markdown: markdown
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2015-08-20 19:17:59 +00:00
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
context "when the recipient address doesn't include a mail key" do
|
|
|
|
let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "") }
|
2015-08-20 19:17:59 +00:00
|
|
|
|
|
|
|
it "raises a SentNotificationNotFoundError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::SentNotificationNotFoundError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
context "when no sent notification for the mail key could be found" do
|
|
|
|
let(:email_raw) { fixture_file('emails/wrong_mail_key.eml') }
|
2015-08-20 19:17:59 +00:00
|
|
|
|
|
|
|
it "raises a SentNotificationNotFoundError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::SentNotificationNotFoundError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the email is blank" do
|
|
|
|
let(:email_raw) { "" }
|
|
|
|
|
|
|
|
it "raises an EmptyEmailError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the email was auto generated" do
|
2016-05-20 18:05:57 +00:00
|
|
|
let!(:mail_key) { '636ca428858779856c226bb145ef4fad' }
|
2015-08-20 19:17:59 +00:00
|
|
|
let!(:email_raw) { fixture_file("emails/auto_reply.eml") }
|
2016-01-07 14:51:12 +00:00
|
|
|
|
2015-08-20 19:17:59 +00:00
|
|
|
it "raises an AutoGeneratedEmailError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::AutoGeneratedEmailError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user could not be found" do
|
|
|
|
before do
|
|
|
|
user.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a UserNotFoundError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::UserNotFoundError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-21 17:14:45 +00:00
|
|
|
context "when the user has been blocked" do
|
|
|
|
before do
|
|
|
|
user.block
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a UserBlockedError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::UserBlockedError)
|
2015-08-21 17:14:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-08-20 19:17:59 +00:00
|
|
|
context "when the user is not authorized to create a note" do
|
|
|
|
before do
|
|
|
|
project.update_attribute(:visibility_level, Project::PRIVATE)
|
|
|
|
end
|
|
|
|
|
2016-05-18 22:57:14 +00:00
|
|
|
it "raises a ProjectNotFound" do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the noteable could not be found" do
|
|
|
|
before do
|
|
|
|
noteable.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a NoteableNotFoundError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::NoteableNotFoundError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the reply is blank" do
|
|
|
|
let!(:email_raw) { fixture_file("emails/no_content_reply.eml") }
|
2016-01-07 14:51:12 +00:00
|
|
|
|
2015-08-20 19:17:59 +00:00
|
|
|
it "raises an EmptyEmailError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the note could not be saved" do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Note).to receive(:persisted?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an InvalidNoteError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when everything is fine" do
|
|
|
|
before do
|
2016-03-23 14:20:22 +00:00
|
|
|
setup_attachment
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates a comment" do
|
|
|
|
expect { receiver.execute }.to change { noteable.notes.count }.by(1)
|
|
|
|
note = noteable.notes.last
|
|
|
|
|
|
|
|
expect(note.author).to eq(sent_notification.recipient)
|
|
|
|
expect(note.note).to include("I could not disagree more.")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds all attachments" do
|
|
|
|
receiver.execute
|
|
|
|
|
|
|
|
note = noteable.notes.last
|
|
|
|
|
2016-01-07 14:51:12 +00:00
|
|
|
expect(note.note).to include(markdown)
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
2016-03-01 05:29:20 +00:00
|
|
|
|
2016-03-17 19:03:51 +00:00
|
|
|
context 'when sub-addressing is not supported' do
|
|
|
|
before do
|
|
|
|
stub_incoming_email_setting(enabled: true, address: nil)
|
|
|
|
end
|
2016-03-01 05:29:20 +00:00
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
shared_examples 'an email that contains a mail key' do |header|
|
|
|
|
it "fetches the mail key from the #{header} header and creates a comment" do
|
2016-03-17 19:03:51 +00:00
|
|
|
expect { receiver.execute }.to change { noteable.notes.count }.by(1)
|
|
|
|
note = noteable.notes.last
|
2016-03-01 05:29:20 +00:00
|
|
|
|
2016-03-17 19:03:51 +00:00
|
|
|
expect(note.author).to eq(sent_notification.recipient)
|
|
|
|
expect(note.note).to include('I could not disagree more.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
context 'mail key is in the References header' do
|
2016-03-17 19:03:51 +00:00
|
|
|
let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references.eml') }
|
|
|
|
|
2016-05-20 18:05:57 +00:00
|
|
|
it_behaves_like 'an email that contains a mail key', 'References'
|
2016-03-01 05:29:20 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|
2016-03-23 14:20:22 +00:00
|
|
|
|
|
|
|
context "when it's trying to create a new issue" do
|
|
|
|
before do
|
|
|
|
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.adventuretime.ooo")
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:sent_notification) {}
|
2016-04-06 20:39:45 +00:00
|
|
|
let!(:user) do
|
|
|
|
create(
|
|
|
|
:user,
|
|
|
|
email: 'jake@adventuretime.ooo',
|
|
|
|
authentication_token: 'auth_token'
|
|
|
|
)
|
|
|
|
end
|
2016-03-23 14:20:22 +00:00
|
|
|
let(:namespace) { create(:namespace, path: 'gitlabhq') }
|
|
|
|
let(:project) { create(:project, :public, namespace: namespace) }
|
|
|
|
let(:email_raw) { fixture_file('emails/valid_new_issue.eml') }
|
|
|
|
|
|
|
|
context "when everything is fine" do
|
|
|
|
it "creates a new issue" do
|
2016-03-31 13:56:37 +00:00
|
|
|
setup_attachment
|
|
|
|
|
2016-03-23 14:20:22 +00:00
|
|
|
expect { receiver.execute }.to change { project.issues.count }.by(1)
|
|
|
|
issue = project.issues.last
|
|
|
|
|
|
|
|
expect(issue.author).to eq(user)
|
|
|
|
expect(issue.title).to eq('New Issue by email')
|
|
|
|
expect(issue.description).to include('reply by email')
|
|
|
|
expect(issue.description).to include(markdown)
|
|
|
|
end
|
2016-03-31 13:56:37 +00:00
|
|
|
|
|
|
|
context "when the reply is blank" do
|
|
|
|
let!(:email_raw) { fixture_file("emails/valid_new_issue_empty.eml") }
|
|
|
|
|
|
|
|
it "creates a new issue" do
|
|
|
|
expect { receiver.execute }.to change { project.issues.count }.by(1)
|
|
|
|
issue = project.issues.last
|
|
|
|
|
|
|
|
expect(issue.author).to eq(user)
|
|
|
|
expect(issue.title).to eq('New Issue by email')
|
|
|
|
expect(issue.description).to eq('')
|
|
|
|
end
|
|
|
|
end
|
2016-03-23 14:20:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "something is wrong" do
|
2016-04-06 21:27:28 +00:00
|
|
|
before do
|
|
|
|
project
|
|
|
|
end
|
|
|
|
|
2016-03-23 14:20:22 +00:00
|
|
|
context "when the issue could not be saved" do
|
|
|
|
before do
|
|
|
|
allow_any_instance_of(Issue).to receive(:persisted?).and_return(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an InvalidIssueError" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidIssueError)
|
2016-03-23 14:20:22 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-06 20:39:45 +00:00
|
|
|
|
2016-05-20 22:38:08 +00:00
|
|
|
context "when we can't find the authentication_token" do
|
2016-04-06 20:39:45 +00:00
|
|
|
let!(:email_raw) { fixture_file("emails/wrong_authentication_token.eml") }
|
|
|
|
|
2016-05-20 22:38:08 +00:00
|
|
|
it "raises an UserNotFoundError" do
|
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::UserNotFoundError)
|
2016-04-06 20:39:45 +00:00
|
|
|
end
|
2016-04-06 21:27:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when project is private" do
|
|
|
|
let(:project) { create(:project, :private, namespace: namespace) }
|
|
|
|
|
|
|
|
it "raises a ProjectNotFound if the user is not a member" do
|
2016-05-18 22:19:25 +00:00
|
|
|
expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound)
|
2016-04-06 21:27:28 +00:00
|
|
|
end
|
2016-04-06 20:39:45 +00:00
|
|
|
end
|
2016-03-23 14:20:22 +00:00
|
|
|
end
|
|
|
|
end
|
2015-08-20 19:17:59 +00:00
|
|
|
end
|