2019-04-11 08:17:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe PreviewMarkdownService do
|
2016-11-16 06:09:09 -05:00
|
|
|
let(:user) { create(:user) }
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-11-16 06:09:09 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'user references' do
|
|
|
|
let(:params) { { text: "Take a look #{user.to_reference}" } }
|
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
it 'returns users referenced in text' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:users]).to eq [user.username]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
describe 'suggestions' do
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, target_project: project, source_project: project)
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:text) { "```suggestion\nfoo\n```" }
|
|
|
|
let(:params) do
|
|
|
|
suggestion_params.merge(text: text,
|
|
|
|
target_type: 'MergeRequest',
|
|
|
|
target_id: merge_request.iid)
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
context 'when preview markdown param is present' do
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:path) { "files/ruby/popen.rb" }
|
|
|
|
let(:line) { 10 }
|
|
|
|
let(:diff_refs) { merge_request.diff_refs }
|
|
|
|
|
|
|
|
let(:suggestion_params) do
|
|
|
|
{
|
|
|
|
preview_suggestions: true,
|
|
|
|
file_path: path,
|
|
|
|
line: line,
|
|
|
|
base_sha: diff_refs.base_sha,
|
|
|
|
start_sha: diff_refs.start_sha,
|
|
|
|
head_sha: diff_refs.head_sha
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns suggestions referenced in text' do
|
|
|
|
position = Gitlab::Diff::Position.new(new_path: path,
|
|
|
|
new_line: line,
|
|
|
|
diff_refs: diff_refs)
|
|
|
|
|
|
|
|
expect(Gitlab::Diff::SuggestionsParser)
|
|
|
|
.to receive(:parse)
|
2019-06-04 11:43:29 -04:00
|
|
|
.with(text, position: position,
|
|
|
|
project: merge_request.project,
|
|
|
|
supports_suggestion: true)
|
2019-04-04 09:08:34 -04:00
|
|
|
.and_call_original
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
result = service.execute
|
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
expect(result[:suggestions]).to all(be_a(Gitlab::Diff::Suggestion))
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when user is not authorized' do
|
|
|
|
let(:another_user) { create(:user) }
|
|
|
|
let(:service) { described_class.new(project, another_user, params) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_guest(another_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no suggestions' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:suggestions]).to be_empty
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when preview markdown param is not present' do
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:suggestion_params) do
|
|
|
|
{
|
|
|
|
preview_suggestions: false
|
|
|
|
}
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
it 'returns suggestions referenced in text' do
|
2018-12-13 14:17:19 -05:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:suggestions]).to eq([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
context 'new note with quick actions' do
|
2016-11-16 06:09:09 -05:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
text: "Please do it\n/assign #{user.to_reference}",
|
2019-04-04 09:08:34 -04:00
|
|
|
target_type: 'Issue',
|
|
|
|
target_id: issue.id
|
2016-11-16 06:09:09 -05:00
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it 'removes quick actions from text' do
|
2016-11-16 06:09:09 -05:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:text]).to eq 'Please do it'
|
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it 'explains quick actions effect' do
|
2016-11-16 06:09:09 -05:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:commands]).to eq "Assigns #{user.to_reference}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge request description' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
text: "My work\n/estimate 2y",
|
2019-04-04 09:08:34 -04:00
|
|
|
target_type: 'MergeRequest'
|
2016-11-16 06:09:09 -05:00
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2016-11-16 06:09:09 -05:00
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it 'removes quick actions from text' do
|
2016-11-16 06:09:09 -05:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:text]).to eq 'My work'
|
|
|
|
end
|
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it 'explains quick actions effect' do
|
2016-11-16 06:09:09 -05:00
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:commands]).to eq 'Sets time estimate to 2y.'
|
2017-05-31 01:50:53 -04:00
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|
2018-07-06 14:49:33 -04:00
|
|
|
|
2018-07-28 17:43:21 -04:00
|
|
|
context 'commit description' do
|
|
|
|
let(:project) { create(:project, :repository) }
|
|
|
|
let(:commit) { project.commit }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
text: "My work\n/tag v1.2.3 Stable release",
|
2019-04-04 09:08:34 -04:00
|
|
|
target_type: 'Commit',
|
|
|
|
target_id: commit.id
|
2018-07-28 17:43:21 -04:00
|
|
|
}
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2018-07-28 17:43:21 -04:00
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
it 'removes quick actions from text' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:text]).to eq 'My work'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'explains quick actions effect' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:commands]).to eq 'Tags this commit to v1.2.3 with "Stable release".'
|
|
|
|
end
|
|
|
|
end
|
2022-07-15 05:10:09 -04:00
|
|
|
|
|
|
|
context 'note with multiple quick actions' do
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
text: "/confidential\n/due 2001-12-31\n/estimate 2y\n/assign #{user.to_reference}",
|
|
|
|
target_type: 'Issue',
|
|
|
|
target_id: issue.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:service) { described_class.new(project, user, params) }
|
|
|
|
|
|
|
|
it 'renders quick actions on multiple lines' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:commands]).to eq "Makes this issue confidential.<br>Sets the due date to Dec 31, 2001.<br>" \
|
|
|
|
"Sets time estimate to 2y.<br>Assigns #{user.to_reference}."
|
|
|
|
end
|
|
|
|
end
|
2016-11-16 06:09:09 -05:00
|
|
|
end
|