debian-mirror-gitlab/spec/requests/projects/merge_requests_discussions_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

280 lines
8.8 KiB
Ruby
Raw Permalink Normal View History

2019-12-04 20:38:33 +05:30
# frozen_string_literal: true
require 'spec_helper'
2023-03-04 22:38:38 +05:30
RSpec.describe 'merge requests discussions', feature_category: :source_code_management do
2019-12-04 20:38:33 +05:30
# Further tests can be found at merge_requests_controller_spec.rb
2020-03-13 15:44:24 +05:30
describe 'GET /:namespace/:project/-/merge_requests/:iid/discussions' do
2021-11-18 22:05:49 +05:30
let(:project) { create(:project, :repository, :public) }
2022-04-04 11:22:00 +05:30
let(:owner) { project.first_owner }
2021-11-18 22:05:49 +05:30
let(:user) { create(:user) }
2019-12-04 20:38:33 +05:30
let(:merge_request) { create(:merge_request_with_diffs, target_project: project, source_project: project) }
before do
2021-11-18 22:05:49 +05:30
project.add_maintainer(owner)
2019-12-04 20:38:33 +05:30
project.add_developer(user)
login_as(user)
end
2022-08-27 11:52:29 +05:30
# rubocop:disable RSpec/InstanceVariable
2019-12-04 20:38:33 +05:30
def send_request
2022-08-27 11:52:29 +05:30
get(
discussions_namespace_project_merge_request_path(namespace_id: project.namespace, project_id: project, id: merge_request.iid),
headers: { 'If-None-Match' => @etag }
)
@etag = response.etag
2019-12-04 20:38:33 +05:30
end
2022-08-27 11:52:29 +05:30
# rubocop:enable RSpec/InstanceVariable
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
shared_examples 'N+1 queries' do
it 'avoids N+1 DB queries', :request_store do
send_request # warm up
create(:diff_note_on_merge_request, noteable: merge_request, project: merge_request.project)
control = ActiveRecord::QueryRecorder.new { send_request }
create(:diff_note_on_merge_request, noteable: merge_request, project: merge_request.project)
expect do
send_request
end.not_to exceed_query_limit(control).with_threshold(notes_metadata_threshold)
end
end
2019-12-04 20:38:33 +05:30
it 'returns 200' do
send_request
2020-04-22 19:07:51 +05:30
expect(response).to have_gitlab_http_status(:ok)
2019-12-04 20:38:33 +05:30
end
# https://docs.gitlab.com/ee/development/query_recorder.html#use-request-specs-instead-of-controller-specs
2023-07-09 08:55:56 +05:30
context 'with notes_metadata_threshold' do
let(:notes_metadata_threshold) { 1 }
2021-04-29 21:17:54 +05:30
2023-07-09 08:55:56 +05:30
it_behaves_like 'N+1 queries'
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
context 'when external_note_author_service_desk feature flag is disabled' do
let(:notes_metadata_threshold) { 0 }
2019-12-04 20:38:33 +05:30
2023-07-09 08:55:56 +05:30
before do
stub_feature_flags(external_note_author_service_desk: false)
end
it_behaves_like 'N+1 queries'
end
2019-12-04 20:38:33 +05:30
end
it 'limits Gitaly queries', :request_store do
Gitlab::GitalyClient.allow_n_plus_1_calls do
2022-10-11 01:57:18 +05:30
create_list(:diff_note_on_merge_request, 7, noteable: merge_request, project: merge_request.project)
2019-12-04 20:38:33 +05:30
end
# The creations above write into the Gitaly counts
Gitlab::GitalyClient.reset_counts
expect { send_request }
.to change { Gitlab::GitalyClient.get_request_count }.by_at_most(4)
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'caching' do
2021-10-27 15:23:28 +05:30
let(:reference) { create(:issue, project: project) }
let(:author) { create(:user) }
let!(:first_note) { create(:diff_note_on_merge_request, author: author, noteable: merge_request, project: project, note: "reference: #{reference.to_reference}") }
2021-09-30 23:02:18 +05:30
let!(:second_note) { create(:diff_note_on_merge_request, in_reply_to: first_note, noteable: merge_request, project: project) }
let!(:award_emoji) { create(:award_emoji, awardable: first_note) }
2021-11-11 11:23:49 +05:30
let!(:author_membership) { project.add_maintainer(author) }
2021-09-30 23:02:18 +05:30
shared_examples 'cache miss' do
it 'does not hit a warm cache' do
expect_next_instance_of(DiscussionSerializer) do |serializer|
expect(serializer).to receive(:represent) do |arg|
expect(arg.notes).to contain_exactly(*changed_notes)
end.and_call_original
end
send_request
end
end
2022-08-27 11:52:29 +05:30
shared_examples 'cache hit' do
it 'gets cached on subsequent requests' do
2023-05-27 22:25:52 +05:30
expect(DiscussionSerializer).not_to receive(:new)
2021-09-30 23:02:18 +05:30
2022-08-27 11:52:29 +05:30
send_request
end
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
before do
send_request
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache hit'
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
context 'when a note in a discussion got updated' do
before do
first_note.update!(updated_at: 1.minute.from_now)
2021-10-27 15:23:28 +05:30
end
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
end
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when a note in a discussion got its reference state updated' do
before do
reference.close!
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
end
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when a note in a discussion got resolved' do
before do
travel_to(1.minute.from_now) do
first_note.resolve!(user)
2022-08-27 11:52:29 +05:30
end
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when a note is added to a discussion' do
let!(:third_note) { create(:diff_note_on_merge_request, in_reply_to: first_note, noteable: merge_request, project: project) }
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note, third_note] }
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when a note is removed from a discussion' do
before do
second_note.destroy!
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note] }
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when an emoji is awarded to a note in discussion' do
before do
travel_to(1.minute.from_now) do
create(:award_emoji, awardable: first_note)
2022-08-27 11:52:29 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-09-30 23:02:18 +05:30
2023-05-27 22:25:52 +05:30
context 'when an award emoji is removed from a note in discussion' do
before do
travel_to(1.minute.from_now) do
award_emoji.destroy!
2022-08-27 11:52:29 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-10-27 15:23:28 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
context 'when the diff note position changes' do
before do
# This replicates a position change wherein timestamps aren't updated
2023-07-09 08:55:56 +05:30
# which is why `save(touch: false)` is utilized. This is the same
# approach being used in Discussions::UpdateDiffPositionService which
# is responsible for updating the positions of diff discussions when
# MR updates.
2023-05-27 22:25:52 +05:30
first_note.position = Gitlab::Diff::Position.new(
old_path: first_note.position.old_path,
new_path: first_note.position.new_path,
old_line: first_note.position.old_line,
new_line: first_note.position.new_line + 1,
diff_refs: first_note.position.diff_refs
)
2023-07-09 08:55:56 +05:30
first_note.save!(touch: false)
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-10-27 15:23:28 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
context 'when the HEAD diff note position changes' do
before do
# This replicates a DiffNotePosition change. This is the same approach
# being used in Discussions::CaptureDiffNotePositionService which is
# responsible for updating/creating DiffNotePosition of a diff discussions
# in relation to HEAD diff.
new_position = Gitlab::Diff::Position.new(
old_path: first_note.position.old_path,
new_path: first_note.position.new_path,
old_line: first_note.position.old_line,
new_line: first_note.position.new_line + 1,
diff_refs: first_note.position.diff_refs
)
DiffNotePosition.create_or_update_for(
first_note,
diff_type: :head,
position: new_position,
line_code: 'bd4b7bfff3a247ccf6e3371c41ec018a55230bcc_534_521'
)
end
2022-08-27 11:52:29 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-10-27 15:23:28 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
context 'when author detail changes' do
before do
author.update!(name: "#{author.name} (Updated)")
end
2022-08-27 11:52:29 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-10-27 15:23:28 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-10-27 15:23:28 +05:30
2023-05-27 22:25:52 +05:30
context 'when author status changes' do
before do
Users::SetStatusService.new(author, message: "updated status").execute
end
2022-08-27 11:52:29 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-11-11 11:23:49 +05:30
end
2023-05-27 22:25:52 +05:30
end
2021-11-11 11:23:49 +05:30
2023-05-27 22:25:52 +05:30
context 'when author role changes' do
before do
Members::UpdateService.new(owner, access_level: Gitlab::Access::GUEST).execute(author_membership)
end
2022-08-27 11:52:29 +05:30
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
2021-11-11 11:23:49 +05:30
end
end
2023-05-27 22:25:52 +05:30
context 'when current_user role changes' do
2021-09-30 23:02:18 +05:30
before do
2023-05-27 22:25:52 +05:30
Members::UpdateService.new(owner, access_level: Gitlab::Access::GUEST).execute(project.member(user))
2021-09-30 23:02:18 +05:30
end
2023-05-27 22:25:52 +05:30
it_behaves_like 'cache miss' do
let(:changed_notes) { [first_note, second_note] }
end
2021-09-30 23:02:18 +05:30
end
end
2019-12-04 20:38:33 +05:30
end
end