diff options
author | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2021-01-20 22:34:23 +0300 |
commit | 6438df3a1e0fb944485cebf07976160184697d72 (patch) | |
tree | 00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /spec/lib/gitlab/email | |
parent | 42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff) |
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'spec/lib/gitlab/email')
4 files changed, 91 insertions, 219 deletions
diff --git a/spec/lib/gitlab/email/handler/create_note_handler_spec.rb b/spec/lib/gitlab/email/handler/create_note_handler_spec.rb index ef448ee96a4..8872800069a 100644 --- a/spec/lib/gitlab/email/handler/create_note_handler_spec.rb +++ b/spec/lib/gitlab/email/handler/create_note_handler_spec.rb @@ -4,146 +4,50 @@ require 'spec_helper' RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do include_context :email_shared_context - let!(:sent_notification) do - SentNotification.record_note(note, user.id, mail_key) - end + + let_it_be(:user) { create(:user) } + let_it_be(:project) { create(:project, :public, :repository) } let(:noteable) { note.noteable } let(:note) { create(:diff_note_on_merge_request, project: project) } - let(:user) { create(:user) } - let(:project) { create(:project, :public, :repository) } let(:email_raw) { fixture_file('emails/valid_reply.eml') } + let!(:sent_notification) do + SentNotification.record_note(note, user.id, mail_key) + end it_behaves_like :reply_processing_shared_examples + it_behaves_like :note_handler_shared_examples do + let(:recipient) { sent_notification.recipient } + + let(:update_commands_only) { fixture_file('emails/update_commands_only_reply.eml')} + let(:no_content) { fixture_file('emails/no_content_reply.eml') } + let(:commands_in_reply) { fixture_file('emails/commands_in_reply.eml') } + let(:with_quick_actions) { fixture_file('emails/valid_reply_with_quick_actions.eml') } + end + before do stub_incoming_email_setting(enabled: true, address: "reply+%{key}@appmail.adventuretime.ooo") stub_config_setting(host: 'localhost') end - context "when the recipient address doesn't include a mail key" do - let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, "") } + context 'when the recipient address does not include a mail key' do + let(:email_raw) { fixture_file('emails/valid_reply.eml').gsub(mail_key, '') } - it "raises a UnknownIncomingEmail" do + it 'raises a UnknownIncomingEmail' do expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail) end end - context "when no sent notification for the mail key could be found" do + context 'when no sent notification for the mail key could be found' do let(:email_raw) { fixture_file('emails/wrong_mail_key.eml') } - it "raises a SentNotificationNotFoundError" do + it 'raises a SentNotificationNotFoundError' do expect { receiver.execute }.to raise_error(Gitlab::Email::SentNotificationNotFoundError) end end - context "when the noteable could not be found" do - before do - noteable.destroy - end - - it "raises a NoteableNotFoundError" do - expect { receiver.execute }.to raise_error(Gitlab::Email::NoteableNotFoundError) - 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 - expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError) - end - - context 'because the note was update commands only' do - let!(:email_raw) { fixture_file("emails/update_commands_only_reply.eml") } - - context 'and current user cannot update noteable' do - it 'raises a CommandsOnlyNoteError' do - expect { receiver.execute }.to raise_error(Gitlab::Email::InvalidNoteError) - end - end - - context "and current user can update noteable" do - before do - project.add_developer(user) - end - - it 'does not raise an error' do - expect { receiver.execute }.to change { noteable.resource_state_events.count }.by(1) - - expect(noteable.reload).to be_closed - end - end - end - end - - context 'when the note contains quick actions' do - let!(:email_raw) { fixture_file("emails/commands_in_reply.eml") } - - context 'and current user cannot update the noteable' do - it 'only executes the commands that the user can perform' do - expect { receiver.execute } - .to change { noteable.notes.user.count }.by(1) - .and change { user.todos_pending_count }.from(0).to(1) - - expect(noteable.reload).to be_open - end - end - - context 'and current user can update noteable' do - before do - project.add_developer(user) - end - - it 'posts a note and updates the noteable' do - expect(TodoService.new.todo_exist?(noteable, user)).to be_falsy - - expect { receiver.execute } - .to change { noteable.notes.user.count }.by(1) - .and change { user.todos_pending_count }.from(0).to(1) - - expect(noteable.reload).to be_closed - end - end - end - - context "when the reply is blank" do - let!(:email_raw) { fixture_file("emails/no_content_reply.eml") } - - it "raises an EmptyEmailError" do - expect { receiver.execute }.to raise_error(Gitlab::Email::EmptyEmailError) - end - end - - shared_examples "checks permissions on noteable" do - context "when user has access" do - before do - project.add_reporter(user) - end - - it "creates a comment" do - expect { receiver.execute }.to change { noteable.notes.count }.by(1) - end - end - - context "when user does not have access" do - it "raises UserNotAuthorizedError" do - expect { receiver.execute }.to raise_error(Gitlab::Email::UserNotAuthorizedError) - end - end - end - - context "when discussion is locked" do - before do - noteable.update_attribute(:discussion_locked, true) - end - - it_behaves_like "checks permissions on noteable" - end - - context "when issue is confidential" do + context 'when issue is confidential' do let(:issue) { create(:issue, project: project) } let(:note) { create(:note, noteable: issue, project: project) } @@ -151,17 +55,17 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do issue.update_attribute(:confidential, true) end - it_behaves_like "checks permissions on noteable" + it_behaves_like :checks_permissions_on_noteable_examples end shared_examples 'a reply to existing comment' do - it "creates a comment" do + it 'creates a comment' do expect { receiver.execute }.to change { noteable.notes.count }.by(1) new_note = noteable.notes.last expect(new_note.author).to eq(sent_notification.recipient) expect(new_note.position).to eq(note.position) - expect(new_note.note).to include("I could not disagree more.") + expect(new_note.note).to include('I could not disagree more.') expect(new_note.in_reply_to?(note)).to be_truthy if note.part_of_discussion? @@ -172,32 +76,14 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do end end - context "when everything is fine" do + # additional shared tests in :reply_processing_shared_examples + context 'when everything is fine' do before do setup_attachment end it_behaves_like 'a reply to existing comment' - it "adds all attachments" do - expect_next_instance_of(Gitlab::Email::AttachmentUploader) do |uploader| - expect(uploader).to receive(:execute).with(upload_parent: project, uploader_class: FileUploader).and_return( - [ - { - url: "uploads/image.png", - alt: "image", - markdown: markdown - } - ] - ) - end - - receiver.execute - - note = noteable.notes.last - expect(note.note).to include(markdown) - end - context 'when sub-addressing is not supported' do before do stub_incoming_email_setting(enabled: true, address: nil) @@ -228,75 +114,9 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do end end - context "when note is not a discussion" do + context 'when note is not a discussion' do let(:note) { create(:note_on_merge_request, project: project) } it_behaves_like 'a reply to existing comment' end - - context 'when the service desk' do - let(:project) { create(:project, :public, service_desk_enabled: true) } - let(:support_bot) { User.support_bot } - let(:noteable) { create(:issue, project: project, author: support_bot, title: 'service desk issue') } - let(:note) { create(:note, project: project, noteable: noteable) } - let(:email_raw) { fixture_file('emails/valid_reply_with_quick_actions.eml') } - - let!(:sent_notification) do - SentNotification.record_note(note, support_bot.id, mail_key) - end - - context 'is enabled' do - before do - allow(Gitlab::ServiceDesk).to receive(:enabled?).with(project: project).and_return(true) - project.project_feature.update!(issues_access_level: issues_access_level) - end - - context 'when issues are enabled for everyone' do - let(:issues_access_level) { ProjectFeature::ENABLED } - - it 'creates a comment' do - expect { receiver.execute }.to change { noteable.notes.count }.by(1) - end - - context 'when quick actions are present' do - it 'encloses quick actions with code span markdown' do - receiver.execute - noteable.reload - - note = Note.last - expect(note.note).to include("Jake out\n\n`/close`\n`/title test`") - expect(noteable.title).to eq('service desk issue') - expect(noteable).to be_opened - end - end - end - - context 'when issues are protected members only' do - let(:issues_access_level) { ProjectFeature::PRIVATE } - - it 'creates a comment' do - expect { receiver.execute }.to change { noteable.notes.count }.by(1) - end - end - - context 'when issues are disabled' do - let(:issues_access_level) { ProjectFeature::DISABLED } - - it 'does not create a comment' do - expect { receiver.execute }.to raise_error(Gitlab::Email::UserNotAuthorizedError) - end - end - end - - context 'is disabled' do - before do - allow(Gitlab::ServiceDesk).to receive(:enabled?).and_return(false) - allow(Gitlab::ServiceDesk).to receive(:enabled?).with(project: project).and_return(false) - end - - it 'does not create a comment' do - expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound) - end - end - end end diff --git a/spec/lib/gitlab/email/handler/create_note_on_issuable_handler_spec.rb b/spec/lib/gitlab/email/handler/create_note_on_issuable_handler_spec.rb new file mode 100644 index 00000000000..94f28d3399a --- /dev/null +++ b/spec/lib/gitlab/email/handler/create_note_on_issuable_handler_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Gitlab::Email::Handler::CreateNoteOnIssuableHandler do + include_context :email_shared_context + + let_it_be(:user) { create(:user, email: 'jake@adventuretime.ooo', incoming_email_token: 'auth_token') } + let_it_be(:namespace) { create(:namespace, path: 'gitlabhq') } + let_it_be(:project) { create(:project, :public, namespace: namespace, path: 'gitlabhq') } + + let!(:noteable) { create(:issue, project: project) } + let(:email_raw) { email_fixture('emails/valid_note_on_issuable.eml') } + + before do + stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.adventuretime.ooo") + stub_config_setting(host: 'localhost') + end + + it_behaves_like :reply_processing_shared_examples + + it_behaves_like :note_handler_shared_examples, true do + let_it_be(:recipient) { user } + + let(:update_commands_only) { email_reply_fixture('emails/update_commands_only_reply.eml') } + let(:no_content) { email_reply_fixture('emails/no_content_reply.eml') } + let(:commands_in_reply) { email_reply_fixture('emails/commands_in_reply.eml') } + let(:with_quick_actions) { email_reply_fixture('emails/valid_reply_with_quick_actions.eml') } + end + + context 'when the recipient address does not include a mail key' do + let(:mail_key) { 'gitlabhq-gitlabhq-project_id-auth_token-issue-issue_iid' } + let(:email_raw) { fixture_file('emails/valid_note_on_issuable.eml').gsub(mail_key, '') } + + it 'raises an UnknownIncomingEmail' do + expect { receiver.execute }.to raise_error(Gitlab::Email::UnknownIncomingEmail) + end + end + + context 'when issue is confidential' do + before do + noteable.update_attribute(:confidential, true) + end + + it_behaves_like :checks_permissions_on_noteable_examples + end + + def email_fixture(path) + fixture_file(path) + .gsub('project_id', project.project_id.to_s) + .gsub('issue_iid', noteable.iid.to_s) + end + + def email_reply_fixture(path) + reply_address = 'reply+59d8df8370b7e95c5a49fbf86aeb2c93' + note_address = "incoming+#{project.full_path_slug}-#{project.project_id}-#{user.incoming_email_token}-issue-#{noteable.iid}" + + fixture_file(path) + .gsub(reply_address, note_address) + end +end diff --git a/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb b/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb index 32b451f8329..b1ffbedc7bf 100644 --- a/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb +++ b/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb @@ -191,16 +191,6 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler do expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound) end end - - context 'when service_desk_custom_address feature is disabled' do - before do - stub_feature_flags(service_desk_custom_address: false) - end - - it 'bounces the email' do - expect { receiver.execute }.to raise_error(Gitlab::Email::ProjectNotFound) - end - end end end diff --git a/spec/lib/gitlab/email/handler_spec.rb b/spec/lib/gitlab/email/handler_spec.rb index 2cd8c31e6b2..eff6fb63a5f 100644 --- a/spec/lib/gitlab/email/handler_spec.rb +++ b/spec/lib/gitlab/email/handler_spec.rb @@ -60,8 +60,9 @@ RSpec.describe Gitlab::Email::Handler do describe 'regexps are set properly' do let(:addresses) do - %W(sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX} sent_notification_key path-to-project-123-user_email_token-merge-request) + - %W(sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY} sent_notification_key path-to-project-123-user_email_token-issue) + + %W(sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX} sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY}) + + %w(sent_notification_key path-to-project-123-user_email_token-merge-request) + + %w(path-to-project-123-user_email_token-issue path-to-project-123-user_email_token-issue-123) + %w(path/to/project+user_email_token path/to/project+merge-request+user_email_token some/project) end |