diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-15 09:10:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-06-15 09:10:13 +0300 |
commit | b4610c51192adc832d25192038a781d2aa1ebb98 (patch) | |
tree | 860bb889ee850e13a1f947489d768801556f9213 /spec/support | |
parent | a7b6d465ae75b497ac34cb43361edd0c8ce45fbd (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/matchers/markdown_matchers.rb | 12 | ||||
-rw-r--r-- | spec/support/shared_examples/models/mentionable_shared_examples.rb | 195 |
2 files changed, 187 insertions, 20 deletions
diff --git a/spec/support/matchers/markdown_matchers.rb b/spec/support/matchers/markdown_matchers.rb index 8fdece7b26d..7a82d7674d9 100644 --- a/spec/support/matchers/markdown_matchers.rb +++ b/spec/support/matchers/markdown_matchers.rb @@ -110,6 +110,18 @@ module MarkdownMatchers end end + # UserReferenceFilter + # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/18442 + # When `@all` is completely deprecated, this matcher should be renamed to + # `reference_users` and remove the original matcher `reference_users` + matcher :reference_users_excluding_all do + set_default_markdown_messages + + match do |actual| + expect(actual).to have_selector('a.gfm.gfm-project_member', count: 3) + end + end + # IssueReferenceFilter matcher :reference_issues do set_default_markdown_messages diff --git a/spec/support/shared_examples/models/mentionable_shared_examples.rb b/spec/support/shared_examples/models/mentionable_shared_examples.rb index f9612dd61be..9874db8dbd7 100644 --- a/spec/support/shared_examples/models/mentionable_shared_examples.rb +++ b/spec/support/shared_examples/models/mentionable_shared_examples.rb @@ -208,13 +208,13 @@ end RSpec.shared_examples 'mentions in description' do |mentionable_type| context 'when storing user mentions' do - before do - mentionable.store_mentions! - end - context 'when mentionable description has no mentions' do let(:mentionable) { create(mentionable_type, description: "just some description") } + before do + mentionable.store_mentions! + end + it 'stores no mentions' do expect(mentionable.user_mentions.count).to eq 0 end @@ -228,13 +228,49 @@ RSpec.shared_examples 'mentions in description' do |mentionable_type| let(:mentionable_desc) { "#{user.to_reference} #{user2.to_reference} #{user.to_reference} some description #{group.to_reference(full: true)} and #{user2.to_reference} @all" } let(:mentionable) { create(mentionable_type, description: mentionable_desc) } - it 'stores mentions' do - add_member(user) + context 'when `disable_all_mention` FF is disabled' do + before do + stub_feature_flags(disable_all_mention: false) - expect(mentionable.user_mentions.count).to eq 1 - expect(mentionable.referenced_users).to match_array([user, user2]) - expect(mentionable.referenced_projects(user)).to match_array([mentionable.project].compact) # epic.project is nil, and we want empty [] - expect(mentionable.referenced_groups(user)).to match_array([group]) + mentionable.store_mentions! + end + + it 'stores mentions' do + add_member(user) + + expect(mentionable.user_mentions.count).to eq 1 + expect(mentionable.referenced_users).to match_array([user, user2]) + expect(mentionable.referenced_groups(user)).to match_array([group]) + + # NOTE: https://gitlab.com/gitlab-org/gitlab/-/issues/18442 + # + # We created `Mentions` concern to track every note in which usernames are mentioned + # However, we never got to the point of utilizing the concern and its DB tables. + # See: https://gitlab.com/gitlab-org/gitlab/-/issues/21801 + # + # The following test is checking `@all`, a type of user mention, is recording + # the id of the project for the mentionable that has the `@all` mention. + # It's _surmised_ that the original intent was + # the project id would be useful to store so everyone (@all) in the project - + # could be notified using its mention record only. + expect(mentionable.referenced_projects(user)).to match_array([mentionable.project].compact) # epic.project is nil, and we want empty [] + end + end + + context 'when `disable_all_mention` FF is enabled' do + before do + stub_feature_flags(disable_all_mention: true) + + mentionable.store_mentions! + end + + it 'stores mentions' do + add_member(user) + + expect(mentionable.user_mentions.count).to eq 1 + expect(mentionable.referenced_users).to match_array([user, user2]) + expect(mentionable.referenced_groups(user)).to match_array([group]) + end end end end @@ -248,17 +284,37 @@ RSpec.shared_examples 'mentions in notes' do |mentionable_type| let(:note_desc) { "#{user.to_reference} #{user2.to_reference} #{user.to_reference} and #{group.to_reference(full: true)} and #{user2.to_reference} @all" } let!(:mentionable) { note.noteable } - before do - note.update!(note: note_desc) - note.store_mentions! - add_member(user) + context 'when `disable_all_mention` FF is enabled' do + before do + stub_feature_flags(disable_all_mention: true) + + note.update!(note: note_desc) + note.store_mentions! + add_member(user) + end + + it 'returns all mentionable mentions' do + expect(mentionable.user_mentions.count).to eq 1 + expect(mentionable.referenced_users).to match_array([user, user2]) + expect(mentionable.referenced_groups(user)).to eq [group] + end end - it 'returns all mentionable mentions' do - expect(mentionable.user_mentions.count).to eq 1 - expect(mentionable.referenced_users).to match_array([user, user2]) - expect(mentionable.referenced_projects(user)).to eq [mentionable.project].compact # epic.project is nil, and we want empty [] - expect(mentionable.referenced_groups(user)).to eq [group] + context 'when `disable_all_mention` FF is disabled' do + before do + stub_feature_flags(disable_all_mention: false) + + note.update!(note: note_desc) + note.store_mentions! + add_member(user) + end + + it 'returns all mentionable mentions' do + expect(mentionable.user_mentions.count).to eq 1 + expect(mentionable.referenced_users).to match_array([user, user2]) + expect(mentionable.referenced_groups(user)).to eq [group] + expect(mentionable.referenced_projects(user)).to eq [mentionable.project].compact # epic.project is nil, and we want empty [] + end end if [:epic, :issue].include?(mentionable_type) @@ -268,6 +324,9 @@ RSpec.shared_examples 'mentions in notes' do |mentionable_type| let(:note_desc) { "#{guest.to_reference} and #{user2.to_reference} and #{user.to_reference}" } before do + note.update!(note: note_desc) + note.store_mentions! + add_member(user) note.resource_parent.add_reporter(user2) note.resource_parent.add_guest(guest) # Bypass :confidential update model validation for testing purposes @@ -283,13 +342,15 @@ RSpec.shared_examples 'mentions in notes' do |mentionable_type| end RSpec.shared_examples 'load mentions from DB' do |mentionable_type| - context 'load stored mentions' do + context 'load stored mentions (when `disable_all_mention` is disabled)' do let_it_be(:user) { create(:user) } let_it_be(:mentioned_user) { create(:user) } let_it_be(:group) { create(:group) } let_it_be(:note_desc) { "#{mentioned_user.to_reference} and #{group.to_reference(full: true)} and @all" } before do + stub_feature_flags(disable_all_mention: false) + note.update!(note: note_desc) note.store_mentions! add_member(user) @@ -341,6 +402,7 @@ RSpec.shared_examples 'load mentions from DB' do |mentionable_type| let(:group_member) { create(:group_member, user: create(:user), group: private_group) } before do + stub_feature_flags(disable_all_mention: false) user_mention = note.user_mentions.first mention_ids = { mentioned_projects_ids: user_mention.mentioned_projects_ids.to_a << private_project.id, @@ -368,6 +430,99 @@ RSpec.shared_examples 'load mentions from DB' do |mentionable_type| end end end + + context 'when `disable_all_mention` is enabled' do + context 'load stored mentions' do + let_it_be(:user) { create(:user) } + let_it_be(:mentioned_user) { create(:user) } + let_it_be(:group) { create(:group) } + let_it_be(:note_desc) { "#{mentioned_user.to_reference} and #{group.to_reference(full: true)} and @all" } + + before do + stub_feature_flags(disable_all_mention: true) + + note.update!(note: note_desc) + note.store_mentions! + add_member(user) + end + + context 'when stored user mention contains ids of inexistent records' do + before do + user_mention = note.user_mentions.first + mention_ids = { + mentioned_users_ids: user_mention.mentioned_users_ids.to_a << non_existing_record_id, + mentioned_groups_ids: user_mention.mentioned_groups_ids.to_a << non_existing_record_id + } + user_mention.update!(mention_ids) + end + + it 'filters out inexistent mentions' do + expect(mentionable.referenced_users).to match_array([mentioned_user]) + expect(mentionable.referenced_projects(user)).to be_empty + expect(mentionable.referenced_groups(user)).to match_array([group]) + end + end + + if [:epic, :issue].include?(mentionable_type) + context 'and note is confidential' do + let_it_be(:guest) { create(:user) } + + let(:note_desc) { "#{guest.to_reference} and #{mentioned_user.to_reference}" } + + before do + note.resource_parent.add_reporter(mentioned_user) + note.resource_parent.add_guest(guest) + # Bypass :confidential update model validation for testing purposes + note.update_attribute(:confidential, true) + note.store_mentions! + end + + it 'stores only mentioned users that has permissions' do + expect(mentionable.referenced_users).to contain_exactly(mentioned_user) + end + end + end + + context 'when private projects and groups are mentioned' do + let(:mega_user) { create(:user) } + let(:private_project) { create(:project, :private) } + let(:project_member) { create(:project_member, user: create(:user), project: private_project) } + let(:private_group) { create(:group, :private) } + let(:group_member) { create(:group_member, user: create(:user), group: private_group) } + + before do + user_mention = note.user_mentions.first + mention_ids = { + mentioned_projects_ids: user_mention.mentioned_projects_ids.to_a << private_project.id, + mentioned_groups_ids: user_mention.mentioned_groups_ids.to_a << private_group.id + } + user_mention.update!(mention_ids) + end + + context 'when user has no access to some mentions' do + it 'filters out inaccessible mentions' do + expect(mentionable.referenced_projects(user)).to be_empty + expect(mentionable.referenced_groups(user)).to match_array([group]) + end + end + + context 'when user has access to the private project and group mentions' do + let(:user) { mega_user } + + before do + add_member(user) + private_project.add_developer(user) + private_group.add_developer(user) + end + + it 'returns all mentions' do + expect(mentionable.referenced_projects(user)).to match_array([private_project]) + expect(mentionable.referenced_groups(user)).to match_array([group, private_group]) + end + end + end + end + end end def add_member(user) |