2019-03-30 07:23:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-12 20:49:25 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe Discussion do
|
2016-08-12 20:49:25 +00:00
|
|
|
subject { described_class.new([first_note, second_note, third_note]) }
|
|
|
|
|
2017-04-04 22:27:23 +00:00
|
|
|
let(:first_note) { create(:diff_note_on_merge_request) }
|
2017-03-17 19:25:52 +00:00
|
|
|
let(:merge_request) { first_note.noteable }
|
2017-04-04 22:27:23 +00:00
|
|
|
let(:second_note) { create(:diff_note_on_merge_request, in_reply_to: first_note) }
|
|
|
|
let(:third_note) { create(:diff_note_on_merge_request) }
|
2017-03-10 01:29:11 +00:00
|
|
|
|
2019-07-04 03:33:14 +00:00
|
|
|
describe '.lazy_find' do
|
|
|
|
let!(:note1) { create(:discussion_note_on_merge_request).to_discussion }
|
|
|
|
let!(:note2) { create(:discussion_note_on_merge_request, in_reply_to: note1).to_discussion }
|
|
|
|
|
|
|
|
subject { [note1, note2].map { |note| described_class.lazy_find(note.discussion_id) } }
|
|
|
|
|
|
|
|
it 'batches requests' do
|
|
|
|
expect do
|
|
|
|
[described_class.lazy_find(note1.id),
|
|
|
|
described_class.lazy_find(note2.id)].map(&:__sync)
|
|
|
|
end.not_to exceed_query_limit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-10 01:29:11 +00:00
|
|
|
describe '.build' do
|
2017-03-17 19:25:52 +00:00
|
|
|
it 'returns a discussion of the right type' do
|
|
|
|
discussion = described_class.build([first_note, second_note], merge_request)
|
2017-04-04 22:27:23 +00:00
|
|
|
expect(discussion).to be_a(DiffDiscussion)
|
2017-03-17 19:25:52 +00:00
|
|
|
expect(discussion.notes.count).to be(2)
|
|
|
|
expect(discussion.first_note).to be(first_note)
|
|
|
|
expect(discussion.noteable).to be(merge_request)
|
|
|
|
end
|
2017-03-10 01:29:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.build_collection' do
|
2017-03-17 19:25:52 +00:00
|
|
|
it 'returns an array of discussions of the right type' do
|
|
|
|
discussions = described_class.build_collection([first_note, second_note, third_note], merge_request)
|
|
|
|
expect(discussions).to eq([
|
2017-04-04 22:27:23 +00:00
|
|
|
DiffDiscussion.new([first_note, second_note], merge_request),
|
|
|
|
DiffDiscussion.new([third_note], merge_request)
|
2017-03-17 19:25:52 +00:00
|
|
|
])
|
|
|
|
end
|
2017-03-10 01:29:11 +00:00
|
|
|
end
|
2019-06-07 17:13:26 +00:00
|
|
|
|
|
|
|
describe 'authorization' do
|
|
|
|
it 'delegates to the first note' do
|
|
|
|
policy = DeclarativePolicy.policy_for(instance_double(User, id: 1), subject)
|
|
|
|
|
|
|
|
expect(policy).to be_a(NotePolicy)
|
|
|
|
end
|
|
|
|
end
|
2021-07-08 09:09:33 +00:00
|
|
|
|
|
|
|
describe '#cache_key' do
|
2021-07-23 06:08:47 +00:00
|
|
|
let(:notes_sha) { Digest::SHA1.hexdigest("#{first_note.post_processed_cache_key}:#{second_note.post_processed_cache_key}:#{third_note.post_processed_cache_key}") }
|
2021-07-08 09:09:33 +00:00
|
|
|
|
2021-07-23 06:08:47 +00:00
|
|
|
it 'returns the cache key' do
|
|
|
|
expect(subject.cache_key).to eq("#{described_class::CACHE_VERSION}:#{subject.id}:#{notes_sha}:")
|
2021-07-08 09:09:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when discussion is resolved' do
|
|
|
|
before do
|
|
|
|
subject.resolve!(first_note.author)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the cache key with resolved at' do
|
2021-07-23 06:08:47 +00:00
|
|
|
expect(subject.cache_key).to eq("#{described_class::CACHE_VERSION}:#{subject.id}:#{notes_sha}:#{subject.resolved_at}")
|
2021-07-08 09:09:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-12 20:49:25 +00:00
|
|
|
end
|