1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
# frozen_string_literal: true
# Specifications for behavior common to all Mentionable implementations.
# Requires a shared context containing:
# - subject { "the mentionable implementation" }
# - let(:backref_text) { "the way that +subject+ should refer to itself in backreferences " }
# - let(:set_mentionable_text) { lambda { |txt| "block that assigns txt to the subject's mentionable_text" } }
RSpec.shared_context 'mentionable context' do
let(:project) { subject.project }
let(:author) { subject.author }
let(:mentioned_issue) { create(:issue, project: project) }
let!(:mentioned_mr) { create(:merge_request, source_project: project) }
let(:mentioned_commit) { project.commit("HEAD~1") }
let(:ext_proj) { create(:project, :public, :repository) }
let(:ext_issue) { create(:issue, project: ext_proj) }
let(:ext_mr) { create(:merge_request, :simple, source_project: ext_proj) }
let(:ext_commit) { ext_proj.commit("HEAD~2") }
# Override to add known commits to the repository stub.
let(:extra_commits) { [] }
# A string that mentions each of the +mentioned_.*+ objects above. Mentionables should add a self-reference
# to this string and place it in their +mentionable_text+.
let(:ref_string) do
<<-MSG.strip_heredoc
These references are new:
Issue: #{mentioned_issue.to_reference}
Merge: #{mentioned_mr.to_reference}
Commit: #{mentioned_commit.to_reference}
This reference is a repeat and should only be mentioned once:
Repeat: #{mentioned_issue.to_reference}
These references are cross-referenced:
Issue: #{ext_issue.to_reference(project)}
Merge: #{ext_mr.to_reference(project)}
Commit: #{ext_commit.to_reference(project)}
This is a self-reference and should not be mentioned at all:
Self: #{backref_text}
MSG
end
before do
# Wire the project's repository to return the mentioned commit, and +nil+
# for any unrecognized commits.
allow_any_instance_of(::Repository).to receive(:commit).and_call_original
allow_any_instance_of(::Repository).to receive(:commit).with(mentioned_commit.short_id).and_return(mentioned_commit)
extra_commits.each do |commit|
allow_any_instance_of(::Repository).to receive(:commit).with(commit.short_id).and_return(commit)
end
set_mentionable_text.call(ref_string)
project.add_developer(author)
end
end
RSpec.shared_examples 'a mentionable' do
include_context 'mentionable context'
it 'generates a descriptive back-reference' do
expect(subject.gfm_reference).to eq(backref_text)
end
it "extracts references from its reference property", :clean_gitlab_redis_cache do
# De-duplicate and omit itself
refs = subject.referenced_mentionables
expect(refs.size).to eq(6)
expect(refs).to include(mentioned_issue)
expect(refs).to include(mentioned_mr)
expect(refs).to include(mentioned_commit)
expect(refs).to include(ext_issue)
expect(refs).to include(ext_mr)
expect(refs).to include(ext_commit)
end
context 'when there are cached markdown fields' do
before do
skip unless subject.is_a?(CacheMarkdownField)
end
it 'sends in cached markdown fields when appropriate' do
subject.extractors[author] = nil
expect_next_instance_of(Gitlab::ReferenceExtractor) do |ext|
attrs = subject.class.mentionable_attrs.collect(&:first) & subject.cached_markdown_fields.markdown_fields
attrs.each do |field|
expect(ext).to receive(:analyze).with(subject.send(field), hash_including(rendered: anything))
end
end
expect(subject).to receive(:cached_markdown_fields).at_least(1).and_call_original
subject.all_references(author)
end
end
it 'creates cross-reference notes', :clean_gitlab_redis_cache do
mentioned_objects = [mentioned_issue, mentioned_mr, mentioned_commit,
ext_issue, ext_mr, ext_commit]
mentioned_objects.each do |referenced|
expect(SystemNoteService).to receive(:cross_reference)
.with(referenced, subject.local_reference, author)
end
subject.create_cross_references!
end
end
RSpec.shared_examples 'an editable mentionable' do
include_context 'mentionable context'
it_behaves_like 'a mentionable'
let(:new_issues) do
[create(:issue, project: project), create(:issue, project: ext_proj)]
end
context 'when there are cached markdown fields' do
before do
skip unless subject.is_a?(CacheMarkdownField)
subject.save!
end
it 'refreshes markdown cache if necessary' do
set_mentionable_text.call('This is a text')
subject.extractors[author] = nil
expect_next_instance_of(Gitlab::ReferenceExtractor) do |ext|
subject.cached_markdown_fields.markdown_fields.each do |field|
expect(ext).to receive(:analyze).with(subject.send(field), hash_including(rendered: anything))
end
end
expect(subject).to receive(:refresh_markdown_cache).and_call_original
expect(subject).to receive(:cached_markdown_fields).at_least(:once).and_call_original
subject.all_references(author)
end
context 'when the markdown cache is stale' do
before do
expect(subject).to receive(:latest_cached_markdown_version).at_least(:once) do
Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION_SHIFTED + 1
end
end
it 'persists the refreshed cache so that it does not have to be refreshed every time' do
expect(subject).to receive(:refresh_markdown_cache).at_least(1).and_call_original
subject.all_references(author)
subject.reload
subject.all_references(author)
end
end
end
it 'creates new cross-reference notes when the mentionable text is edited' do
subject.save!
subject.create_cross_references!
new_text = <<-MSG.strip_heredoc
These references already existed:
Issue: #{mentioned_issue.to_reference}
Commit: #{mentioned_commit.to_reference}
---
This cross-project reference already existed:
Issue: #{ext_issue.to_reference(project)}
---
These two references are introduced in an edit:
Issue: #{new_issues[0].to_reference}
Cross: #{new_issues[1].to_reference(project)}
MSG
# These three objects were already referenced, and should not receive new
# notes
[mentioned_issue, mentioned_commit, ext_issue].each do |oldref|
expect(SystemNoteService).not_to receive(:cross_reference)
.with(oldref, any_args)
end
# These two issues are new and should receive reference notes
# In the case of MergeRequests remember that cannot mention commits included in the MergeRequest
new_issues.each do |newref|
expect(SystemNoteService).to receive(:cross_reference)
.with(newref, subject.local_reference, author)
end
set_mentionable_text.call(new_text)
subject.create_new_cross_references!(author)
end
end
RSpec.shared_examples 'mentions in description' do |mentionable_type|
context 'when storing user mentions' do
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
end
context 'when mentionable description contains mentions' do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:group) { create(:group) }
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) }
context 'when `disable_all_mention` FF is disabled' do
before do
stub_feature_flags(disable_all_mention: false)
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
end
RSpec.shared_examples 'mentions in notes' do |mentionable_type|
context 'when mentionable notes contain mentions' do
let(:user) { create(:user) }
let(:user2) { create(:user) }
let(:group) { create(:group) }
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 }
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
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)
context 'and note is confidential' do
let_it_be(:guest) { create(:user) }
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
note.update_attribute(:confidential, true)
end
it 'returns only mentioned users that has permissions' do
expect(note.mentioned_users).to contain_exactly(user, user2)
end
end
end
end
end
RSpec.shared_examples 'load mentions from DB' do |mentionable_type|
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)
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_projects_ids: user_mention.mentioned_projects_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 match_array([mentionable.project].compact) # epic.project is nil, and we want 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
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,
mentioned_groups_ids: user_mention.mentioned_groups_ids.to_a << private_group.id
}
user_mention.update!(mention_ids)
add_member(mega_user)
private_project.add_developer(mega_user)
private_group.add_developer(mega_user)
end
context 'when user has no access to some mentions' do
it 'filters out inaccessible mentions' do
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])
end
end
context 'when user has access to all mentions' do
it 'returns all mentions' do
expect(mentionable.referenced_projects(mega_user)).to match_array([mentionable.project, private_project].compact) # epic.project is nil, and we want empty []
expect(mentionable.referenced_groups(mega_user)).to match_array([group, private_group])
end
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)
issuable_parent = if mentionable.is_a?(Epic)
mentionable.group
else
mentionable.project
end
issuable_parent&.add_developer(user)
end
|