Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-05-17 19:05:49 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2023-05-17 19:05:49 +0300
commit43a25d93ebdabea52f99b05e15b06250cd8f07d7 (patch)
treedceebdc68925362117480a5d672bcff122fb625b /spec/lib/gitlab/email
parent20c84b99005abd1c82101dfeff264ac50d2df211 (diff)
Add latest changes from gitlab-org/gitlab@16-0-stable-eev16.0.0-rc42
Diffstat (limited to 'spec/lib/gitlab/email')
-rw-r--r--spec/lib/gitlab/email/handler/create_issue_handler_spec.rb2
-rw-r--r--spec/lib/gitlab/email/handler/create_note_handler_spec.rb22
-rw-r--r--spec/lib/gitlab/email/hook/silent_mode_interceptor_spec.rb74
-rw-r--r--spec/lib/gitlab/email/hook/validate_addresses_interceptor_spec.rb52
-rw-r--r--spec/lib/gitlab/email/html_to_markdown_parser_spec.rb12
-rw-r--r--spec/lib/gitlab/email/incoming_email_spec.rb34
-rw-r--r--spec/lib/gitlab/email/message/build_ios_app_guide_spec.rb6
-rw-r--r--spec/lib/gitlab/email/message/in_product_marketing/helper_spec.rb6
-rw-r--r--spec/lib/gitlab/email/message/repository_push_spec.rb2
-rw-r--r--spec/lib/gitlab/email/receiver_spec.rb13
-rw-r--r--spec/lib/gitlab/email/reply_parser_spec.rb77
-rw-r--r--spec/lib/gitlab/email/service_desk_email_spec.rb53
12 files changed, 230 insertions, 123 deletions
diff --git a/spec/lib/gitlab/email/handler/create_issue_handler_spec.rb b/spec/lib/gitlab/email/handler/create_issue_handler_spec.rb
index 8ff8de2379a..369d7e994d2 100644
--- a/spec/lib/gitlab/email/handler/create_issue_handler_spec.rb
+++ b/spec/lib/gitlab/email/handler/create_issue_handler_spec.rb
@@ -116,7 +116,7 @@ RSpec.describe Gitlab::Email::Handler::CreateIssueHandler do
context "when the issue could not be saved" do
before do
allow_any_instance_of(Issue).to receive(:persisted?).and_return(false)
- allow_any_instance_of(Issue).to receive(:ensure_metrics).and_return(nil)
+ allow_any_instance_of(Issue).to receive(:ensure_metrics!).and_return(nil)
end
it "raises an InvalidIssueError" do
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 f70645a8272..e3b0e90bff9 100644
--- a/spec/lib/gitlab/email/handler/create_note_handler_spec.rb
+++ b/spec/lib/gitlab/email/handler/create_note_handler_spec.rb
@@ -206,4 +206,26 @@ RSpec.describe Gitlab::Email::Handler::CreateNoteHandler do
it_behaves_like 'a reply to existing comment'
end
+
+ context 'when note is authored from external author for service desk' do
+ before do
+ SentNotification.find_by(reply_key: mail_key).update!(recipient: User.support_bot)
+ end
+
+ context 'when email contains text, quoted text and quick commands' do
+ let(:email_raw) { fixture_file('emails/commands_in_reply.eml') }
+
+ it 'creates a discussion' do
+ expect { receiver.execute }.to change { noteable.notes.count }.by(1)
+ end
+
+ it 'links external participant' do
+ receiver.execute
+
+ new_note = noteable.notes.last
+
+ expect(new_note.note_metadata.external_author).to eq('jake@adventuretime.ooo')
+ end
+ end
+ end
end
diff --git a/spec/lib/gitlab/email/hook/silent_mode_interceptor_spec.rb b/spec/lib/gitlab/email/hook/silent_mode_interceptor_spec.rb
new file mode 100644
index 00000000000..cc371643bee
--- /dev/null
+++ b/spec/lib/gitlab/email/hook/silent_mode_interceptor_spec.rb
@@ -0,0 +1,74 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Email::Hook::SilentModeInterceptor, :mailer, feature_category: :geo_replication do
+ let_it_be(:user) { create(:user) }
+
+ before do
+ Mail.register_interceptor(described_class)
+ end
+
+ after do
+ Mail.unregister_interceptor(described_class)
+ end
+
+ context 'when silent mode is enabled' do
+ it 'prevents mail delivery' do
+ stub_application_setting(silent_mode_enabled: true)
+
+ deliver_mails(user)
+
+ should_not_email_anyone
+ end
+
+ it 'logs the suppression' do
+ stub_application_setting(silent_mode_enabled: true)
+
+ expect(Gitlab::AppJsonLogger).to receive(:info).with(
+ message: 'SilentModeInterceptor prevented sending mail',
+ mail_subject: 'Two-factor authentication disabled',
+ silent_mode_enabled: true
+ )
+ expect(Gitlab::AppJsonLogger).to receive(:info).with(
+ message: 'SilentModeInterceptor prevented sending mail',
+ mail_subject: 'Welcome to GitLab!',
+ silent_mode_enabled: true
+ )
+
+ deliver_mails(user)
+ end
+ end
+
+ context 'when silent mode is disabled' do
+ it 'does not prevent mail delivery' do
+ stub_application_setting(silent_mode_enabled: false)
+
+ deliver_mails(user)
+
+ should_email(user, times: 2)
+ end
+
+ it 'debug logs the no-op' do
+ stub_application_setting(silent_mode_enabled: false)
+
+ expect(Gitlab::AppJsonLogger).to receive(:debug).with(
+ message: 'SilentModeInterceptor did nothing',
+ mail_subject: 'Two-factor authentication disabled',
+ silent_mode_enabled: false
+ )
+ expect(Gitlab::AppJsonLogger).to receive(:debug).with(
+ message: 'SilentModeInterceptor did nothing',
+ mail_subject: 'Welcome to GitLab!',
+ silent_mode_enabled: false
+ )
+
+ deliver_mails(user)
+ end
+ end
+
+ def deliver_mails(user)
+ Notify.disabled_two_factor_email(user).deliver_now
+ DeviseMailer.user_admin_approval(user).deliver_now
+ end
+end
diff --git a/spec/lib/gitlab/email/hook/validate_addresses_interceptor_spec.rb b/spec/lib/gitlab/email/hook/validate_addresses_interceptor_spec.rb
deleted file mode 100644
index a3f0158db40..00000000000
--- a/spec/lib/gitlab/email/hook/validate_addresses_interceptor_spec.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-# frozen_string_literal: true
-
-require 'spec_helper'
-
-RSpec.describe Gitlab::Email::Hook::ValidateAddressesInterceptor do
- describe 'UNSAFE_CHARACTERS' do
- subject { described_class::UNSAFE_CHARACTERS }
-
- it { is_expected.to match('\\') }
- it { is_expected.to match("\x00") }
- it { is_expected.to match("\x01") }
- it { is_expected.not_to match('') }
- it { is_expected.not_to match('user@example.com') }
- it { is_expected.not_to match('foo-123+bar_456@example.com') }
- end
-
- describe '.delivering_email' do
- let(:mail) do
- ActionMailer::Base.mail(to: 'test@mail.com', from: 'info@mail.com', subject: 'title', body: 'hello')
- end
-
- let(:unsafe_email) { "evil+\x01$HOME@example.com" }
-
- it 'sends emails to normal addresses' do
- expect(Gitlab::AuthLogger).not_to receive(:info)
- expect { mail.deliver_now }.to change(ActionMailer::Base.deliveries, :count)
- end
-
- [:from, :to, :cc, :bcc].each do |header|
- it "does not send emails if the #{header.inspect} header contains unsafe characters" do
- mail[header] = unsafe_email
-
- expect(Gitlab::AuthLogger).to receive(:info).with(
- message: 'Skipping email with unsafe characters in address',
- address: unsafe_email,
- subject: mail.subject
- )
-
- expect { mail.deliver_now }.not_to change(ActionMailer::Base.deliveries, :count)
- end
- end
-
- [:reply_to].each do |header|
- it "sends emails if the #{header.inspect} header contains unsafe characters" do
- mail[header] = unsafe_email
-
- expect(Gitlab::AuthLogger).not_to receive(:info)
- expect { mail.deliver_now }.to change(ActionMailer::Base.deliveries, :count)
- end
- end
- end
-end
diff --git a/spec/lib/gitlab/email/html_to_markdown_parser_spec.rb b/spec/lib/gitlab/email/html_to_markdown_parser_spec.rb
index fe585d47d59..59c488739dc 100644
--- a/spec/lib/gitlab/email/html_to_markdown_parser_spec.rb
+++ b/spec/lib/gitlab/email/html_to_markdown_parser_spec.rb
@@ -1,17 +1,21 @@
# frozen_string_literal: true
-require 'spec_helper'
+require 'kramdown'
+require 'html2text'
+require 'fast_spec_helper'
+require 'support/helpers/fixture_helpers'
RSpec.describe Gitlab::Email::HtmlToMarkdownParser, feature_category: :service_desk do
+ include FixtureHelpers
+
subject { described_class.convert(html) }
describe '.convert' do
let(:html) { fixture_file("lib/gitlab/email/basic.html") }
it 'parses html correctly' do
- expect(subject)
- .to eq(
- <<-BODY.strip_heredoc.chomp
+ expect(subject).to eq(
+ <<~BODY.chomp
Hello, World!
This is some e-mail content. Even though it has whitespace and newlines, the e-mail converter will handle it correctly.
*Even* mismatched tags.
diff --git a/spec/lib/gitlab/email/incoming_email_spec.rb b/spec/lib/gitlab/email/incoming_email_spec.rb
new file mode 100644
index 00000000000..123b050aee7
--- /dev/null
+++ b/spec/lib/gitlab/email/incoming_email_spec.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Email::IncomingEmail, feature_category: :service_desk do
+ let(:setting_name) { :incoming_email }
+
+ it_behaves_like 'common email methods'
+
+ describe 'self.key_from_address' do
+ before do
+ stub_incoming_email_setting(address: 'replies+%{key}@example.com')
+ end
+
+ it "returns reply key" do
+ expect(described_class.key_from_address("replies+key@example.com")).to eq("key")
+ end
+
+ it 'does not match emails with extra bits' do
+ expect(described_class.key_from_address('somereplies+somekey@example.com.someotherdomain.com')).to be nil
+ end
+
+ context 'when a custom wildcard address is used' do
+ let(:wildcard_address) { 'custom.address+%{key}@example.com' }
+
+ it 'finds key if email matches address pattern' do
+ key = described_class.key_from_address(
+ 'custom.address+foo@example.com', wildcard_address: wildcard_address
+ )
+ expect(key).to eq('foo')
+ end
+ end
+ end
+end
diff --git a/spec/lib/gitlab/email/message/build_ios_app_guide_spec.rb b/spec/lib/gitlab/email/message/build_ios_app_guide_spec.rb
index 3089f955252..4b77b2f7192 100644
--- a/spec/lib/gitlab/email/message/build_ios_app_guide_spec.rb
+++ b/spec/lib/gitlab/email/message/build_ios_app_guide_spec.rb
@@ -2,13 +2,9 @@
require 'spec_helper'
-RSpec.describe Gitlab::Email::Message::BuildIosAppGuide do
+RSpec.describe Gitlab::Email::Message::BuildIosAppGuide, :saas do
subject(:message) { described_class.new }
- before do
- allow(Gitlab).to receive(:com?) { true }
- end
-
it 'contains the correct message', :aggregate_failures do
expect(message.subject_line).to eq 'Get set up to build for iOS'
expect(message.title).to eq "Building for iOS? We've got you covered."
diff --git a/spec/lib/gitlab/email/message/in_product_marketing/helper_spec.rb b/spec/lib/gitlab/email/message/in_product_marketing/helper_spec.rb
index 3c0d83d0f9e..a3c2d1b428e 100644
--- a/spec/lib/gitlab/email/message/in_product_marketing/helper_spec.rb
+++ b/spec/lib/gitlab/email/message/in_product_marketing/helper_spec.rb
@@ -27,11 +27,7 @@ RSpec.describe Gitlab::Email::Message::InProductMarketing::Helper do
subject(:class_with_helper) { dummy_class_with_helper.new(format) }
- context 'gitlab.com' do
- before do
- allow(Gitlab).to receive(:com?) { true }
- end
-
+ context 'for SaaS', :saas do
context 'format is HTML' do
it 'returns the correct HTML' do
message = "If you no longer wish to receive marketing emails from us, " \
diff --git a/spec/lib/gitlab/email/message/repository_push_spec.rb b/spec/lib/gitlab/email/message/repository_push_spec.rb
index f13d98ec9b9..bb68bca5dfa 100644
--- a/spec/lib/gitlab/email/message/repository_push_spec.rb
+++ b/spec/lib/gitlab/email/message/repository_push_spec.rb
@@ -45,7 +45,7 @@ RSpec.describe Gitlab::Email::Message::RepositoryPush do
describe '#project_name_with_namespace' do
subject { message.project_name_with_namespace }
- it { is_expected.to eq "#{group.name} / #{project.path}" }
+ it { is_expected.to eq "#{group.name} / #{project.name}" }
end
describe '#author' do
diff --git a/spec/lib/gitlab/email/receiver_spec.rb b/spec/lib/gitlab/email/receiver_spec.rb
index 865e40d4ecb..e58da2478bf 100644
--- a/spec/lib/gitlab/email/receiver_spec.rb
+++ b/spec/lib/gitlab/email/receiver_spec.rb
@@ -11,9 +11,10 @@ RSpec.describe Gitlab::Email::Receiver do
shared_examples 'successful receive' do
let(:handler) { double(:handler, project: project, execute: true, metrics_event: nil, metrics_params: nil) }
let(:client_id) { 'email/jake@example.com' }
+ let(:mail_key) { 'gitlabhq/gitlabhq+auth_token' }
it 'correctly finds the mail key' do
- expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler)
+ expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), mail_key).and_return(handler)
receiver.execute
end
@@ -92,6 +93,16 @@ RSpec.describe Gitlab::Email::Receiver do
it_behaves_like 'successful receive'
end
+ context 'when mail key is in the references header with a comma' do
+ let(:email_raw) { fixture_file('emails/valid_reply_with_references_in_comma.eml') }
+ let(:meta_key) { :references }
+ let(:meta_value) { ['"<reply-59d8df8370b7e95c5a49fbf86aeb2c93@localhost>,<issue_1@localhost>,<exchange@microsoft.com>"'] }
+
+ it_behaves_like 'successful receive' do
+ let(:mail_key) { '59d8df8370b7e95c5a49fbf86aeb2c93' }
+ end
+ end
+
context 'when all other headers are missing' do
let(:email_raw) { fixture_file('emails/missing_delivered_to_header.eml') }
let(:meta_key) { :received_recipients }
diff --git a/spec/lib/gitlab/email/reply_parser_spec.rb b/spec/lib/gitlab/email/reply_parser_spec.rb
index e4c68dbba92..35065b74eff 100644
--- a/spec/lib/gitlab/email/reply_parser_spec.rb
+++ b/spec/lib/gitlab/email/reply_parser_spec.rb
@@ -3,7 +3,7 @@
require "spec_helper"
# Inspired in great part by Discourse's Email::Receiver
-RSpec.describe Gitlab::Email::ReplyParser do
+RSpec.describe Gitlab::Email::ReplyParser, feature_category: :team_planning do
describe '#execute' do
def test_parse_body(mail_string, params = {})
described_class.new(Mail::Message.new(mail_string), **params).execute
@@ -188,67 +188,36 @@ RSpec.describe Gitlab::Email::ReplyParser do
)
end
- context 'properly renders email reply from gmail web client' do
- context 'when feature flag is enabled' do
- it do
- expect(test_parse_body(fixture_file("emails/html_only.eml")))
- .to eq(
- <<-BODY.strip_heredoc.chomp
- ### This is a reply from standard GMail in Google Chrome.
-
- The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
-
- Here's some **bold** text, **strong** text and *italic* in Markdown.
-
- Here's a link http://example.com
-
- Here's an img ![Miro](http://img.png)<details>
- <summary>
- One</summary>
- Some details</details>
-
- <details>
- <summary>
- Two</summary>
- Some details</details>
-
- Test reply.
-
- First paragraph.
-
- Second paragraph.
- BODY
- )
- end
- end
-
- context 'when feature flag is disabled' do
- before do
- stub_feature_flags(service_desk_html_to_text_email_handler: false)
- end
+ context 'properly renders email reply from gmail web client', feature_category: :service_desk do
+ it do
+ expect(test_parse_body(fixture_file("emails/html_only.eml")))
+ .to eq(
+ <<-BODY.strip_heredoc.chomp
+ ### This is a reply from standard GMail in Google Chrome.
- it do
- expect(test_parse_body(fixture_file("emails/html_only.eml")))
- .to eq(
- <<-BODY.strip_heredoc.chomp
- ### This is a reply from standard GMail in Google Chrome.
+ The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.
+ Here's some **bold** text, **strong** text and *italic* in Markdown.
- Here's some **bold** text, strong text and italic in Markdown.
+ Here's a link http://example.com
- Here's a link http://example.com
+ Here's an img ![Miro](http://img.png)<details>
+ <summary>
+ One</summary>
+ Some details</details>
- Here's an img [Miro]One Some details Two Some details
+ <details>
+ <summary>
+ Two</summary>
+ Some details</details>
- Test reply.
+ Test reply.
- First paragraph.
+ First paragraph.
- Second paragraph.
- BODY
- )
- end
+ Second paragraph.
+ BODY
+ )
end
end
diff --git a/spec/lib/gitlab/email/service_desk_email_spec.rb b/spec/lib/gitlab/email/service_desk_email_spec.rb
new file mode 100644
index 00000000000..d59b8aa2cf7
--- /dev/null
+++ b/spec/lib/gitlab/email/service_desk_email_spec.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::Email::ServiceDeskEmail, feature_category: :service_desk do
+ let(:setting_name) { :service_desk_email }
+
+ it_behaves_like 'common email methods'
+
+ describe '.key_from_address' do
+ context 'when service desk address is set' do
+ before do
+ stub_service_desk_email_setting(address: 'address+%{key}@example.com')
+ end
+
+ it 'returns key' do
+ expect(described_class.key_from_address('address+key@example.com')).to eq('key')
+ end
+ end
+
+ context 'when service desk address is not set' do
+ before do
+ stub_service_desk_email_setting(address: nil)
+ end
+
+ it 'returns nil' do
+ expect(described_class.key_from_address('address+key@example.com')).to be_nil
+ end
+ end
+ end
+
+ describe '.address_for_key' do
+ context 'when service desk address is set' do
+ before do
+ stub_service_desk_email_setting(address: 'address+%{key}@example.com')
+ end
+
+ it 'returns address' do
+ expect(described_class.address_for_key('foo')).to eq('address+foo@example.com')
+ end
+ end
+
+ context 'when service desk address is not set' do
+ before do
+ stub_service_desk_email_setting(address: nil)
+ end
+
+ it 'returns nil' do
+ expect(described_class.key_from_address('foo')).to be_nil
+ end
+ end
+ end
+end