debian-mirror-gitlab/spec/features/issues/gfm_autocomplete_spec.rb

822 lines
25 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
require 'spec_helper'
2017-08-17 22:00:37 +05:30
2020-06-23 00:09:42 +05:30
RSpec.describe 'GFM autocomplete', :js do
2020-07-28 23:09:34 +05:30
let_it_be(:user_xss_title) { 'eve <img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;' }
let_it_be(:user_xss) { create(:user, name: user_xss_title, username: 'xss.user') }
let_it_be(:user) { create(:user, name: '💃speciąl someone💃', username: 'someone.special') }
2021-04-17 20:07:23 +05:30
let_it_be(:user2) { create(:user, name: 'Marge Simpson', username: 'msimpson') }
2021-01-03 14:25:43 +05:30
let_it_be(:group) { create(:group, name: 'Ancestor') }
let_it_be(:child_group) { create(:group, parent: group, name: 'My group') }
let_it_be(:project) { create(:project, group: child_group) }
2020-07-28 23:09:34 +05:30
let_it_be(:label) { create(:label, project: project, title: 'special+') }
2019-09-30 21:07:59 +05:30
let(:issue) { create(:issue, project: project) }
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
before_all do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-11-20 20:47:30 +05:30
project.add_maintainer(user_xss)
2021-04-17 20:07:23 +05:30
project.add_maintainer(user2)
2020-07-28 23:09:34 +05:30
end
2018-11-20 20:47:30 +05:30
2020-07-28 23:09:34 +05:30
describe 'when tribute_autocomplete feature flag is off' do
before do
stub_feature_flags(tribute_autocomplete: false)
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
sign_in(user)
visit project_issue_path(project, issue)
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
end
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
it 'updates issue description with GFM reference' do
2021-04-17 20:07:23 +05:30
click_button 'Edit title and description'
2019-12-21 20:55:43 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-09-10 17:25:29 +05:30
2021-04-17 20:07:23 +05:30
fill_in 'Description', with: "@#{user.name[0...3]}"
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-09-10 17:25:29 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2017-09-10 17:25:29 +05:30
2020-07-28 23:09:34 +05:30
click_button 'Save changes'
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(find('.description')).to have_text(user.to_reference)
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens quick action autocomplete when updating description' do
2021-04-17 20:07:23 +05:30
click_button 'Edit title and description'
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
fill_in 'Description', with: '/'
2018-11-18 11:00:15 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to be_visible
2018-11-18 11:00:15 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens autocomplete menu when field starts with text' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@'
2018-11-18 11:00:15 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to be_visible
2018-11-20 20:47:30 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens autocomplete menu for Issues when field starts with text with item escaping HTML characters' do
issue_xss_title = 'This will execute alert<img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;'
create(:issue, project: project, title: issue_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '#'
2018-11-20 20:47:30 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2019-01-03 12:48:30 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text(issue_xss_title)
2018-11-18 11:00:15 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens autocomplete menu for Username when field starts with text with item escaping HTML characters' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@ev'
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_highlighted_autocomplete_item).to have_text(user_xss.username)
2019-01-03 12:48:30 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens autocomplete menu for Milestone when field starts with text with item escaping HTML characters' do
milestone_xss_title = 'alert milestone &lt;img src=x onerror="alert(\'Hello xss\');" a'
create(:milestone, project: project, title: milestone_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '%'
2020-07-28 23:09:34 +05:30
wait_for_requests
2019-01-03 12:48:30 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('alert milestone')
2019-01-03 12:48:30 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesnt open autocomplete menu character is prefixed with text' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'testing@'
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesnt select the first item for non-assignee dropdowns' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: ':'
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).not_to have_css('.cur')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'does not open autocomplete menu when ":" is prefixed by a number and letters' do
# Number.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '7:'
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
# ASCII letter.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'w:'
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
# Non-ASCII letter.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'Ё:'
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'selects the first item for assignee dropdowns' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@'
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_css('.cur:first-of-type')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'includes items for assignee dropdowns with non-ASCII characters in name' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "@#{user.name[0...8]}"
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text(user.name)
2017-08-17 22:00:37 +05:30
end
2021-03-11 19:13:27 +05:30
it 'searches across full name for assignees' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@speciąlsome'
2021-03-11 19:13:27 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_highlighted_autocomplete_item).to have_text(user.name)
2021-03-11 19:13:27 +05:30
end
2021-04-17 20:07:23 +05:30
it 'shows names that start with the query as the top result' do
fill_in 'Comment', with: '@mar'
wait_for_requests
expect(find_highlighted_autocomplete_item).to have_text(user2.name)
end
it 'shows usernames that start with the query as the top result' do
fill_in 'Comment', with: '@msi'
wait_for_requests
expect(find_highlighted_autocomplete_item).to have_text(user2.name)
end
# Regression test for https://gitlab.com/gitlab-org/gitlab/-/issues/321925
it 'shows username when pasting then pressing Enter' do
fill_in 'Comment', with: "@#{user.username}\n"
expect(find_field('Comment').value).to have_text "@#{user.username}"
end
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
it 'does not show `@undefined` when pressing `@` then Enter' do
fill_in 'Comment', with: "@\n"
expect(find_field('Comment').value).to have_text '@'
expect(find_field('Comment').value).not_to have_text '@undefined'
end
it 'selects the first item for non-assignee dropdowns if a query is entered' do
fill_in 'Comment', with: ':1'
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_css('.cur:first-of-type')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
context 'if a selected value has special characters' do
it 'wraps the result in double quotes' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "~#{label.title[0]}"
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text("~\"#{label.title}\"")
2020-07-28 23:09:34 +05:30
end
2017-08-17 22:00:37 +05:30
2020-07-28 23:09:34 +05:30
it "shows dropdown after a new line" do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "test\n\n@"
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to be_visible
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it "does not show dropdown when preceded with a special character" do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@@'
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesn\'t wrap for assignee values' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "@#{user.username[0]}"
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text("@#{user.username}")
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesn\'t wrap for emoji values' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: ':cartwheel_'
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text('cartwheel_tone1')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesn\'t open autocomplete after non-word character' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "@#{user.username[0..2]}!"
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.atwho-view')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesn\'t open autocomplete if there is no space before' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "hello:#{user.username[0..2]}"
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.atwho-view')
2020-07-28 23:09:34 +05:30
end
it 'triggers autocomplete after selecting a quick action' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '/as'
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text(user.username)
2020-07-28 23:09:34 +05:30
end
2021-03-08 18:12:59 +05:30
it 'does not limit quick actions autocomplete list to 5' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '/'
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_css('li', minimum: 6)
2021-03-08 18:12:59 +05:30
end
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
context 'assignees' do
let(:issue_assignee) { create(:issue, project: project) }
let(:unassigned_user) { create(:user) }
before do
issue_assignee.update(assignees: [user])
project.add_maintainer(unassigned_user)
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'lists users who are currently not assigned to the issue when using /assign' do
visit project_issue_path(project, issue_assignee)
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '/as'
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).not_to have_text(user.username)
expect(find_autocomplete_menu).to have_text(unassigned_user.username)
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'shows dropdown on new issue form' do
visit new_project_issue_path(project)
2021-04-17 20:07:23 +05:30
fill_in 'Description', with: '/ass'
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_autocomplete_item.click
expect(find_autocomplete_menu).to have_text(unassigned_user.username)
expect(find_autocomplete_menu).to have_text(user.username)
2020-07-28 23:09:34 +05:30
end
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
context 'labels' do
it 'opens autocomplete menu for Labels when field starts with text with item escaping HTML characters' do
label_xss_title = 'alert label &lt;img src=x onerror="alert(\'Hello xss\');" a'
create(:label, project: project, title: label_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~'
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('alert label')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'allows colons when autocompleting scoped labels' do
create(:label, project: project, title: 'scoped:label')
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~scoped:'
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('scoped:label')
2017-08-17 22:00:37 +05:30
end
2020-07-28 23:09:34 +05:30
it 'allows colons when autocompleting scoped labels with double colons' do
create(:label, project: project, title: 'scoped::label')
2017-08-17 22:00:37 +05:30
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~scoped::'
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2020-03-13 15:44:24 +05:30
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('scoped::label')
2020-07-28 23:09:34 +05:30
end
it 'allows spaces when autocompleting multi-word labels' do
create(:label, project: project, title: 'Accepting merge requests')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~Accepting merge'
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('Accepting merge requests')
2020-07-28 23:09:34 +05:30
end
it 'only autocompletes the latest label' do
create(:label, project: project, title: 'Accepting merge requests')
create(:label, project: project, title: 'Accepting job applicants')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~Accepting merge requests foo bar ~Accepting job'
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_autocomplete_menu).to have_text('Accepting job applicants')
2020-07-28 23:09:34 +05:30
end
it 'does not autocomplete labels if no tilde is typed' do
create(:label, project: project, title: 'Accepting merge requests')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'Accepting merge'
2020-07-28 23:09:34 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.atwho-view')
2020-07-28 23:09:34 +05:30
end
2020-03-13 15:44:24 +05:30
end
2021-02-22 17:27:13 +05:30
context 'when other notes are destroyed' do
let!(:discussion) { create(:discussion_note_on_issue, noteable: issue, project: issue.project) }
# This is meant to protect against this issue https://gitlab.com/gitlab-org/gitlab/-/issues/228729
it 'keeps autocomplete key listeners' do
visit project_issue_path(project, issue)
2021-04-17 20:07:23 +05:30
note = find_field('Comment')
2021-02-22 17:27:13 +05:30
2021-03-08 18:12:59 +05:30
start_comment_with_emoji(note, '.atwho-view li')
2021-02-22 17:27:13 +05:30
start_and_cancel_discussion
note.fill_in(with: '')
2021-03-08 18:12:59 +05:30
start_comment_with_emoji(note, '.atwho-view li')
2021-02-22 17:27:13 +05:30
note.native.send_keys(:enter)
expect(note.value).to eql('Hello :100: ')
end
end
2020-07-28 23:09:34 +05:30
shared_examples 'autocomplete suggestions' do
it 'suggests objects correctly' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: object.class.reference_prefix
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
find_autocomplete_menu.find('li').click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text(expected_body)
2020-03-13 15:44:24 +05:30
end
2020-07-28 23:09:34 +05:30
end
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
context 'issues' do
let(:object) { issue }
let(:expected_body) { object.to_reference }
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'autocomplete suggestions'
end
context 'merge requests' do
let(:object) { create(:merge_request, source_project: project) }
let(:expected_body) { object.to_reference }
2020-03-13 15:44:24 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'autocomplete suggestions'
2020-04-22 19:07:51 +05:30
end
2020-07-28 23:09:34 +05:30
context 'project snippets' do
let!(:object) { create(:project_snippet, project: project, title: 'code snippet') }
let(:expected_body) { object.to_reference }
2020-04-22 19:07:51 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'autocomplete suggestions'
end
2020-04-22 19:07:51 +05:30
2020-07-28 23:09:34 +05:30
context 'label' do
let!(:object) { label }
let(:expected_body) { object.title }
it_behaves_like 'autocomplete suggestions'
end
context 'milestone' do
2021-04-17 20:07:23 +05:30
let_it_be(:milestone_expired) { create(:milestone, project: project, due_date: 5.days.ago) }
let_it_be(:milestone_no_duedate) { create(:milestone, project: project, title: 'Foo - No due date') }
let_it_be(:milestone1) { create(:milestone, project: project, title: 'Milestone-1', due_date: 20.days.from_now) }
let_it_be(:milestone2) { create(:milestone, project: project, title: 'Milestone-2', due_date: 15.days.from_now) }
let_it_be(:milestone3) { create(:milestone, project: project, title: 'Milestone-3', due_date: 10.days.from_now) }
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
before do
fill_in 'Comment', with: '/milestone %'
wait_for_requests
end
it 'shows milestons list in the autocomplete menu' do
page.within(find_autocomplete_menu) do
expect(page).to have_selector('li', count: 5)
end
end
it 'shows expired milestone at the bottom of the list' do
page.within(find_autocomplete_menu) do
expect(page.find('li:last-child')).to have_content milestone_expired.title
end
end
it 'shows milestone due earliest at the top of the list' do
page.within(find_autocomplete_menu) do
aggregate_failures do
expect(page.all('li')[0]).to have_content milestone3.title
expect(page.all('li')[1]).to have_content milestone2.title
expect(page.all('li')[2]).to have_content milestone1.title
expect(page.all('li')[3]).to have_content milestone_no_duedate.title
end
end
end
2020-03-13 15:44:24 +05:30
end
end
2020-07-28 23:09:34 +05:30
describe 'when tribute_autocomplete feature flag is on' do
before do
stub_feature_flags(tribute_autocomplete: true)
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
sign_in(user)
visit project_issue_path(project, issue)
2019-01-03 12:48:30 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
end
it 'updates issue description with GFM reference' do
2021-04-17 20:07:23 +05:30
click_button 'Edit title and description'
2019-01-03 12:48:30 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
fill_in 'Description', with: "@#{user.name[0...3]}"
2019-09-30 21:07:59 +05:30
2020-07-28 23:09:34 +05:30
wait_for_requests
2019-09-30 21:07:59 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_tribute_autocomplete_menu.click
2020-07-28 23:09:34 +05:30
click_button 'Save changes'
2019-09-30 21:07:59 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find('.description')).to have_text(user.to_reference)
2019-09-30 21:07:59 +05:30
end
2020-07-28 23:09:34 +05:30
it 'opens autocomplete menu when field starts with text' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@'
2019-09-30 21:07:59 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to be_visible
2020-07-28 23:09:34 +05:30
end
2020-10-24 23:57:45 +05:30
it 'opens autocomplete menu for Issues when field starts with text with item escaping HTML characters' do
issue_xss_title = 'This will execute alert<img src=x onerror=alert(2)&lt;img src=x onerror=alert(1)&gt;'
create(:issue, project: project, title: issue_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '#'
2019-09-30 21:07:59 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text(issue_xss_title)
2019-09-30 21:07:59 +05:30
end
2020-10-24 23:57:45 +05:30
it 'opens autocomplete menu for Username when field starts with text with item escaping HTML characters' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@ev'
2019-09-30 21:07:59 +05:30
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text(user_xss.username)
2020-07-28 23:09:34 +05:30
end
2021-01-29 00:20:46 +05:30
it 'opens autocomplete menu for Milestone when field starts with text with item escaping HTML characters' do
milestone_xss_title = 'alert milestone &lt;img src=x onerror="alert(\'Hello xss\');" a'
create(:milestone, project: project, title: milestone_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '%'
2021-01-29 00:20:46 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('alert milestone')
2021-01-29 00:20:46 +05:30
end
2021-02-22 17:27:13 +05:30
it 'does not open autocomplete menu when trigger character is prefixed with text' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'testing@'
2021-02-22 17:27:13 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.tribute-container')
2021-02-22 17:27:13 +05:30
end
2021-03-08 18:12:59 +05:30
it 'does not open autocomplete menu when ":" is prefixed by a number and letters' do
# Number.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '7:'
expect(page).not_to have_css('.tribute-container')
2021-03-08 18:12:59 +05:30
# ASCII letter.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'w:'
expect(page).not_to have_css('.tribute-container')
2021-03-08 18:12:59 +05:30
# Non-ASCII letter.
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'Ё:'
expect(page).not_to have_css('.tribute-container')
2021-03-08 18:12:59 +05:30
end
2020-07-28 23:09:34 +05:30
it 'selects the first item for assignee dropdowns' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@'
2019-09-30 21:07:59 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_css('.highlight:first-of-type')
2019-09-30 21:07:59 +05:30
end
2020-07-28 23:09:34 +05:30
it 'includes items for assignee dropdowns with non-ASCII characters in name' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "@#{user.name[0...8]}"
2019-09-30 21:07:59 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text(user.name)
2019-09-30 21:07:59 +05:30
end
2021-03-08 18:12:59 +05:30
it 'selects the first item for non-assignee dropdowns if a query is entered' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: ':1'
2021-03-08 18:12:59 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_css('.highlight:first-of-type')
2021-03-08 18:12:59 +05:30
end
2021-01-03 14:25:43 +05:30
context 'when autocompleting for groups' do
it 'shows the group when searching for the name of the group' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@mygroup'
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('My group')
2021-01-03 14:25:43 +05:30
end
it 'does not show the group when searching for the name of the parent of the group' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '@ancestor'
2021-01-03 14:25:43 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).not_to have_text('My group')
2021-01-03 14:25:43 +05:30
end
end
2020-07-28 23:09:34 +05:30
context 'if a selected value has special characters' do
2020-10-24 23:57:45 +05:30
it 'wraps the result in double quotes' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "~#{label.title[0]}"
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_tribute_autocomplete_menu.click
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text("~\"#{label.title}\"")
2020-10-24 23:57:45 +05:30
end
2020-07-28 23:09:34 +05:30
it "shows dropdown after a new line" do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "test\n\n@"
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to be_visible
2019-02-15 15:39:39 +05:30
end
2020-07-28 23:09:34 +05:30
it 'doesn\'t wrap for assignee values' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "@#{user.username[0..2]}"
2018-12-23 12:14:25 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_tribute_autocomplete_menu.click
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text("@#{user.username}")
2019-02-15 15:39:39 +05:30
end
2021-03-08 18:12:59 +05:30
it 'does not wrap for emoji values' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: ':cartwheel_'
2019-02-15 15:39:39 +05:30
2021-04-17 20:07:23 +05:30
find_highlighted_tribute_autocomplete_menu.click
2019-02-15 15:39:39 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text('cartwheel_tone1')
2021-03-08 18:12:59 +05:30
end
2019-02-15 15:39:39 +05:30
2021-03-08 18:12:59 +05:30
it 'does not open autocomplete if there is no space before' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: "hello:#{user.username[0..2]}"
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.tribute-container')
2021-03-08 18:12:59 +05:30
end
it 'autocompletes for quick actions' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '/as'
find_highlighted_tribute_autocomplete_menu.click
2021-03-08 18:12:59 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text('/assign')
2020-07-28 23:09:34 +05:30
end
end
2019-02-15 15:39:39 +05:30
2020-07-28 23:09:34 +05:30
context 'assignees' do
let(:issue_assignee) { create(:issue, project: project) }
let(:unassigned_user) { create(:user) }
2019-02-15 15:39:39 +05:30
2020-07-28 23:09:34 +05:30
before do
issue_assignee.update(assignees: [user])
2019-02-15 15:39:39 +05:30
2020-07-28 23:09:34 +05:30
project.add_maintainer(unassigned_user)
end
it 'lists users who are currently not assigned to the issue when using /assign' do
visit project_issue_path(project, issue_assignee)
2021-04-17 20:07:23 +05:30
note = find_field('Comment')
note.native.send_keys('/assign ')
# The `/assign` ajax response might replace the one by `@` below causing a failed test
# so we need to wait for the `/assign` ajax request to finish first
wait_for_requests
note.native.send_keys('@')
wait_for_requests
2019-02-15 15:39:39 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).not_to have_text(user.username)
expect(find_tribute_autocomplete_menu).to have_text(unassigned_user.username)
2020-10-24 23:57:45 +05:30
end
it 'lists users who are currently not assigned to the issue when using /assign on the second line' do
visit project_issue_path(project, issue_assignee)
2021-04-17 20:07:23 +05:30
note = find_field('Comment')
note.native.send_keys('/assign @user2')
note.native.send_keys(:enter)
note.native.send_keys('/assign ')
# The `/assign` ajax response might replace the one by `@` below causing a failed test
# so we need to wait for the `/assign` ajax request to finish first
wait_for_requests
note.native.send_keys('@')
wait_for_requests
2020-10-24 23:57:45 +05:30
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).not_to have_text(user.username)
expect(find_tribute_autocomplete_menu).to have_text(unassigned_user.username)
2020-10-24 23:57:45 +05:30
end
end
context 'labels' do
it 'opens autocomplete menu for Labels when field starts with text with item escaping HTML characters' do
label_xss_title = 'alert label &lt;img src=x onerror="alert(\'Hello xss\');" a'
create(:label, project: project, title: label_xss_title)
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~'
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('alert label')
2020-10-24 23:57:45 +05:30
end
it 'allows colons when autocompleting scoped labels' do
create(:label, project: project, title: 'scoped:label')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~scoped:'
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('scoped:label')
2020-10-24 23:57:45 +05:30
end
it 'allows colons when autocompleting scoped labels with double colons' do
create(:label, project: project, title: 'scoped::label')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~scoped::'
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('scoped::label')
2020-10-24 23:57:45 +05:30
end
it 'autocompletes multi-word labels' do
create(:label, project: project, title: 'Accepting merge requests')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~Acceptingmerge'
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('Accepting merge requests')
2020-10-24 23:57:45 +05:30
end
it 'only autocompletes the latest label' do
create(:label, project: project, title: 'documentation')
create(:label, project: project, title: 'feature')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: '~documentation foo bar ~feat'
# Invoke autocompletion
find_field('Comment').native.send_keys(:right)
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(find_tribute_autocomplete_menu).to have_text('feature')
expect(find_tribute_autocomplete_menu).not_to have_text('documentation')
2020-10-24 23:57:45 +05:30
end
it 'does not autocomplete labels if no tilde is typed' do
create(:label, project: project, title: 'documentation')
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: 'document'
2020-10-24 23:57:45 +05:30
wait_for_requests
2021-04-17 20:07:23 +05:30
expect(page).not_to have_css('.tribute-container')
2020-10-24 23:57:45 +05:30
end
end
2021-03-08 18:12:59 +05:30
context 'when other notes are destroyed' do
let!(:discussion) { create(:discussion_note_on_issue, noteable: issue, project: issue.project) }
# This is meant to protect against this issue https://gitlab.com/gitlab-org/gitlab/-/issues/228729
it 'keeps autocomplete key listeners' do
visit project_issue_path(project, issue)
2021-04-17 20:07:23 +05:30
note = find_field('Comment')
2021-03-08 18:12:59 +05:30
start_comment_with_emoji(note, '.tribute-container li')
start_and_cancel_discussion
note.fill_in(with: '')
start_comment_with_emoji(note, '.tribute-container li')
note.native.send_keys(:enter)
expect(note.value).to eql('Hello :100: ')
end
end
2020-11-24 15:15:51 +05:30
shared_examples 'autocomplete suggestions' do
it 'suggests objects correctly' do
2021-04-17 20:07:23 +05:30
fill_in 'Comment', with: object.class.reference_prefix
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
find_tribute_autocomplete_menu.find('li').click
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
expect(find_field('Comment').value).to have_text(expected_body)
2020-11-24 15:15:51 +05:30
end
end
2021-01-29 00:20:46 +05:30
context 'issues' do
let(:object) { issue }
let(:expected_body) { object.to_reference }
it_behaves_like 'autocomplete suggestions'
end
2020-11-24 15:15:51 +05:30
context 'merge requests' do
let(:object) { create(:merge_request, source_project: project) }
let(:expected_body) { object.to_reference }
it_behaves_like 'autocomplete suggestions'
end
2021-01-29 00:20:46 +05:30
context 'project snippets' do
let!(:object) { create(:project_snippet, project: project, title: 'code snippet') }
let(:expected_body) { object.to_reference }
it_behaves_like 'autocomplete suggestions'
end
context 'label' do
let!(:object) { label }
let(:expected_body) { object.title }
it_behaves_like 'autocomplete suggestions'
end
context 'milestone' do
let!(:object) { create(:milestone, project: project) }
let(:expected_body) { object.to_reference }
it_behaves_like 'autocomplete suggestions'
end
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
private
2021-03-08 18:12:59 +05:30
def start_comment_with_emoji(note, selector)
note.native.send_keys('Hello :10')
wait_for_requests
find(selector, text: '100')
end
def start_and_cancel_discussion
2021-04-17 20:07:23 +05:30
fill_in('Reply to comment', with: 'Whoops!')
2021-03-08 18:12:59 +05:30
page.accept_alert 'Are you sure you want to cancel creating this comment?' do
click_button('Cancel')
end
wait_for_requests
end
2021-04-17 20:07:23 +05:30
def find_autocomplete_menu
find('.atwho-view ul', visible: true)
end
def find_highlighted_autocomplete_item
find('.atwho-view li.cur', visible: true)
end
def find_tribute_autocomplete_menu
find('.tribute-container ul', visible: true)
end
def find_highlighted_tribute_autocomplete_menu
find('.tribute-container li.highlight', visible: true)
end
2017-08-17 22:00:37 +05:30
end