2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Email::Handler do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:email) { Mail.new { body 'email' } }
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe '.for' do
|
2020-05-24 23:13:21 +05:30
|
|
|
context 'key matches the reply_key of a notification' do
|
|
|
|
it 'picks note handler' do
|
|
|
|
expect(described_class.for(email, '1234567890abcdef1234567890abcdef')).to be_an_instance_of(Gitlab::Email::Handler::CreateNoteHandler)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'key matches the reply_key of a notification, along with an unsubscribe suffix' do
|
|
|
|
it 'picks unsubscribe handler' do
|
|
|
|
expect(described_class.for(email, '1234567890abcdef1234567890abcdef-unsubscribe')).to be_an_instance_of(Gitlab::Email::Handler::UnsubscribeHandler)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
it 'picks issue handler if there is no merge request prefix' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(described_class.for(email, 'project+key')).to be_an_instance_of(Gitlab::Email::Handler::CreateIssueHandler)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'picks merge request handler if there is merge request key' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(described_class.for(email, 'project+merge-request+key')).to be_an_instance_of(Gitlab::Email::Handler::CreateMergeRequestHandler)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if no handler is found' do
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(described_class.for(email, '')).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns nil if provided email is nil' do
|
|
|
|
expect(described_class.for(nil, '')).to be_nil
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
|
|
|
|
describe 'regexps are set properly' do
|
|
|
|
let(:addresses) do
|
2019-02-15 15:39:39 +05:30
|
|
|
%W(sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX} sent_notification_key path-to-project-123-user_email_token-merge-request path-to-project-123-user_email_token-issue) +
|
|
|
|
%W(sent_notification_key#{Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY} sent_notification_key path/to/project+merge-request+user_email_token path/to/project+user_email_token)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'picks each handler at least once' do
|
|
|
|
matched_handlers = addresses.map do |address|
|
2020-03-13 15:44:24 +05:30
|
|
|
described_class.for(email, address).class
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
expect(matched_handlers.uniq).to match_array(ce_handlers)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can pick exactly one handler for each address' do
|
|
|
|
addresses.each do |address|
|
|
|
|
matched_handlers = ce_handlers.select do |handler|
|
2020-03-13 15:44:24 +05:30
|
|
|
handler.new(email, address).can_handle?
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
expect(matched_handlers.count).to eq(1), "#{address} matches #{matched_handlers.count} handlers: #{matched_handlers}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ce_handlers
|
2018-11-20 20:47:30 +05:30
|
|
|
@ce_handlers ||= Gitlab::Email::Handler.handlers.reject do |handler|
|
2018-10-15 14:42:47 +05:30
|
|
|
handler.name.start_with?('Gitlab::Email::Handler::EE::')
|
|
|
|
end
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|