diff options
Diffstat (limited to 'spec/lib/gitlab/email/handler')
-rw-r--r-- | spec/lib/gitlab/email/handler/create_note_handler_spec.rb | 120 | ||||
-rw-r--r-- | spec/lib/gitlab/email/handler/service_desk_handler_spec.rb | 119 |
2 files changed, 205 insertions, 34 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 c7b69f39951..30514b531dc 100644 --- a/spec/lib/gitlab/email/handler/create_note_handler_spec.rb +++ b/spec/lib/gitlab/email/handler/create_note_handler_spec.rb @@ -2,11 +2,11 @@ require 'spec_helper' -RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do +RSpec.describe Gitlab::Email::Handler::CreateNoteHandler, feature_category: :shared do include_context 'email shared context' - let_it_be(:user) { create(:user, email: 'jake@adventuretime.ooo') } - let_it_be(:project) { create(:project, :public, :repository) } + let_it_be_with_reload(:user) { create(:user, email: 'jake@adventuretime.ooo') } + let_it_be(:project) { create(:project, :public, :repository) } let(:noteable) { note.noteable } let(:note) { create(:diff_note_on_merge_request, project: project) } @@ -133,14 +133,16 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do end end - context 'mail key is in the References header' do + context 'when mail key is in the References header' do let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references.eml') } it_behaves_like 'an email that contains a mail key', 'References' end - context 'mail key is in the References header with a comma' do - let(:email_raw) { fixture_file('emails/reply_without_subaddressing_and_key_inside_references_with_a_comma.eml') } + context 'when mail key is in the References header with a comma' do + let(:email_raw) do + fixture_file('emails/reply_without_subaddressing_and_key_inside_references_with_a_comma.eml') + end it_behaves_like 'an email that contains a mail key', 'References' end @@ -228,4 +230,110 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do end end end + + context 'when issue is closed' do + let_it_be(:noteable) { create(:issue, :closed, :confidential, project: project) } + let_it_be(:note) { create(:note, noteable: noteable, project: project) } + + let!(:sent_notification) do + allow(Gitlab::ServiceDesk).to receive(:enabled?).with(project: project).and_return(true) + SentNotification.record_note(note, Users::Internal.support_bot.id) + end + + let(:reply_address) { "support+#{sent_notification.reply_key}@example.com" } + let(:reopen_note) { noteable.notes.last } + let(:email_raw) do + <<~EMAIL + From: from@example.com + To: #{reply_address} + Subject: Issue title + + Issue description + EMAIL + end + + before do + stub_incoming_email_setting(enabled: true, address: 'support+%{key}@example.com') + end + + it 'does not reopen issue but adds external participants comment' do + # Only 1 from received email + expect { receiver.execute }.to change { noteable.notes.count }.by(1) + expect(noteable).to be_closed + end + + context 'when noteable is a commit' do + let!(:note) { create(:note_on_commit, project: project) } + let!(:noteable) { note.noteable } + + let!(:sent_notification) do + allow(Gitlab::ServiceDesk).to receive(:enabled?).with(project: project).and_return(true) + SentNotification.record_note(note, Users::Internal.support_bot.id) + end + + it 'does not reopen issue but adds external participants comment' do + expect { receiver.execute }.to change { noteable.notes.count }.by(1) + end + end + + context 'when reopen_issue_on_external_participant_note is true' do + shared_examples 'an automatically reopened issue' do + it 'reopens issue, adds external participants comment and reopen comment' do + # 1 from received email and 1 reopen comment + expect { receiver.execute }.to change { noteable.notes.count }.by(2) + expect(noteable.reset).to be_open + + expect(reopen_note).to be_confidential + expect(reopen_note.author).to eq(Users::Internal.support_bot) + expect(reopen_note.note).to include(reopen_comment_body) + end + end + + let!(:settings) do + create(:service_desk_setting, project: project, reopen_issue_on_external_participant_note: true) + end + + let(:reopen_comment_body) do + s_( + "ServiceDesk|This issue has been reopened because it received a new comment from an external participant." + ) + end + + it_behaves_like 'an automatically reopened issue' + + it 'does not contain an assignee mention' do + receiver.execute + expect(reopen_note.note).not_to include("@") + end + + context 'when issue is assigned to a user' do + before do + noteable.update!(assignees: [user]) + end + + it_behaves_like 'an automatically reopened issue' + + it 'contains an assignee mention' do + receiver.execute + expect(reopen_note.note).to include(user.to_reference) + end + end + + context 'when issue is assigned to multiple users' do + let_it_be(:another_user) { create(:user) } + + before do + noteable.update!(assignees: [user, another_user]) + end + + it_behaves_like 'an automatically reopened issue' + + it 'contains two assignee mentions' do + receiver.execute + expect(reopen_note.note).to include(user.to_reference) + expect(reopen_note.note).to include(another_user.to_reference) + end + end + end + 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 f13fd0be4cd..9d484198cc0 100644 --- a/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb +++ b/spec/lib/gitlab/email/handler/service_desk_handler_spec.rb @@ -19,6 +19,7 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se let_it_be(:group) { create(:group, :private, :crm_enabled, name: "email") } + let(:expected_subject) { "The message subject! @all" } let(:expected_description) do "Service desk stuff!\n\n```\na = b\n```\n\n`/label ~label1`\n`/assign @user1`\n`/close`\n![image](uploads/image.png)" end @@ -43,7 +44,7 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se expect(new_issue.author).to eql(Users::Internal.support_bot) expect(new_issue.confidential?).to be true expect(new_issue.all_references.all).to be_empty - expect(new_issue.title).to eq("The message subject! @all") + expect(new_issue.title).to eq(expected_subject) expect(new_issue.description).to eq(expected_description.strip) expect(new_issue.email&.email_message_id).to eq(message_id) end @@ -115,6 +116,40 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se it_behaves_like 'a new issue request' + context 'when more than the defined limit of participants are in Cc header' do + before do + stub_const("IssueEmailParticipants::CreateService::MAX_NUMBER_OF_RECORDS", 6) + end + + let(:cc_addresses) { Array.new(6) { |i| "user#{i}@example.com" }.join(', ') } + let(:author_email) { 'from@example.com' } + let(:expected_subject) { "Issue title" } + let(:expected_description) do + <<~DESC + Issue description + + ![image](uploads/image.png) + DESC + end + + let(:email_raw) do + <<~EMAIL + From: #{author_email} + To: #{to_address} + Cc: #{cc_addresses} + Message-ID: <#{message_id}> + Subject: #{expected_subject} + + Issue description + EMAIL + end + + # Author email plus 5 from Cc + let(:issue_email_participants_count) { 6 } + + it_behaves_like 'a new issue request' + end + context 'when no CC header is present' do let(:email_raw) do <<~EMAIL @@ -462,7 +497,7 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se end end - shared_examples 'a handler that does not verify the custom email' do |error_identifier| + shared_examples 'a handler that does not verify the custom email' do it 'does not verify the custom email address' do # project has no owner, so only notify verification triggerer expect(Notify).to receive(:service_desk_verification_result_email).once @@ -477,20 +512,32 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se end end - shared_examples 'a handler that verifies Service Desk custom email verification emails' do + context 'when using incoming_email address' do + before do + stub_incoming_email_setting(enabled: true, address: 'support+%{key}@example.com') + end + it_behaves_like 'an early exiting handler' context 'with valid service desk settings' do let_it_be(:user) { create(:user) } + let_it_be(:credentials) { create(:service_desk_custom_email_credential, project: project) } - let!(:settings) { create(:service_desk_setting, project: project, custom_email: 'custom-support-email@example.com') } - let!(:verification) { create(:service_desk_custom_email_verification, project: project, token: 'ZROT4ZZXA-Y6', triggerer: user) } + let_it_be_with_reload(:settings) do + create(:service_desk_setting, project: project, custom_email: 'custom-support-email@example.com') + end + + let_it_be_with_reload(:verification) do + create(:service_desk_custom_email_verification, project: project, token: 'ZROT4ZZXA-Y6', triggerer: user) + end let(:message_delivery) { instance_double(ActionMailer::MessageDelivery) } - before do + before_all do project.add_maintainer(user) + end + before do allow(message_delivery).to receive(:deliver_later) allow(Notify).to receive(:service_desk_verification_result_email).and_return(message_delivery) end @@ -521,7 +568,9 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se verification.update!(token: 'XXXXXXXXXXXX') end - it_behaves_like 'a handler that does not verify the custom email', 'incorrect_token' + it_behaves_like 'a handler that does not verify the custom email' do + let(:error_identifier) { 'incorrect_token' } + end end context 'and verification email ingested too late' do @@ -529,7 +578,9 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se verification.update!(triggered_at: ServiceDesk::CustomEmailVerification::TIMEFRAME.ago) end - it_behaves_like 'a handler that does not verify the custom email', 'mail_not_received_within_timeframe' + it_behaves_like 'a handler that does not verify the custom email' do + let(:error_identifier) { 'mail_not_received_within_timeframe' } + end end context 'and from header differs from custom email address' do @@ -537,29 +588,13 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se settings.update!(custom_email: 'different-from@example.com') end - it_behaves_like 'a handler that does not verify the custom email', 'incorrect_from' - end - end - - context 'when service_desk_custom_email feature flag is disabled' do - before do - stub_feature_flags(service_desk_custom_email: false) - end - - it 'does not trigger the verification process and adds an issue instead' do - expect { receiver.execute }.to change { Issue.count }.by(1) + it_behaves_like 'a handler that does not verify the custom email' do + let(:error_identifier) { 'incorrect_from' } + end end end end - context 'when using incoming_email address' do - before do - stub_incoming_email_setting(enabled: true, address: 'support+%{key}@example.com') - end - - it_behaves_like 'a handler that verifies Service Desk custom email verification emails' - end - context 'when using service_desk_email address' do let(:receiver) { Gitlab::Email::ServiceDeskReceiver.new(email_raw) } @@ -567,7 +602,35 @@ RSpec.describe Gitlab::Email::Handler::ServiceDeskHandler, feature_category: :se stub_service_desk_email_setting(enabled: true, address: 'support+%{key}@example.com') end - it_behaves_like 'a handler that verifies Service Desk custom email verification emails' + it_behaves_like 'an early exiting handler' + + context 'with valid service desk settings' do + let_it_be(:user) { create(:user) } + let_it_be(:credentials) { create(:service_desk_custom_email_credential, project: project) } + + let_it_be_with_reload(:settings) do + create(:service_desk_setting, project: project, custom_email: 'custom-support-email@example.com') + end + + let_it_be_with_reload(:verification) do + create(:service_desk_custom_email_verification, project: project, token: 'ZROT4ZZXA-Y6', triggerer: user) + end + + let(:message_delivery) { instance_double(ActionMailer::MessageDelivery) } + + before_all do + project.add_maintainer(user) + end + + before do + allow(message_delivery).to receive(:deliver_later) + allow(Notify).to receive(:service_desk_verification_result_email).and_return(message_delivery) + end + + it_behaves_like 'a handler that does not verify the custom email' do + let(:error_identifier) { 'incorrect_forwarding_target' } + end + end end end end |