2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-21 07:35:09 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Banzai::ObjectRenderer do
|
2017-08-23 12:53:29 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2016-06-21 07:35:09 -04:00
|
|
|
let(:user) { project.owner }
|
2018-04-03 09:45:17 -04:00
|
|
|
let(:renderer) do
|
|
|
|
described_class.new(
|
|
|
|
default_project: project,
|
|
|
|
user: user,
|
|
|
|
redaction_context: { custom_value: 'value' }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
let(:object) { Note.new(note: 'hello', note_html: '<p dir="auto">hello</p>', cached_markdown_version: Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16) }
|
2016-10-06 17:52:44 -04:00
|
|
|
|
2016-06-21 07:35:09 -04:00
|
|
|
describe '#render' do
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'with cache' do
|
|
|
|
it 'renders and redacts an Array of objects' do
|
|
|
|
renderer.render([object], :note)
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
expect(object.redacted_note_html).to eq '<p dir="auto">hello</p>'
|
|
|
|
expect(object.user_visible_reference_count).to eq 0
|
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2019-07-16 16:19:08 -04:00
|
|
|
it 'calls Banzai::ReferenceRedactor to perform redaction' do
|
2019-11-20 01:06:16 -05:00
|
|
|
expect_next_instance_of(Banzai::ReferenceRedactor) do |instance|
|
|
|
|
expect(instance).to receive(:redact).and_call_original
|
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
renderer.render([object], :note)
|
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'retrieves field content using Banzai::Renderer.render_field' do
|
2017-11-06 11:52:56 -05:00
|
|
|
expect(Banzai::Renderer).to receive(:render_field).with(object, :note, {}).and_call_original
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
renderer.render([object], :note)
|
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'passes context to PostProcessPipeline' do
|
|
|
|
another_user = create(:user)
|
|
|
|
another_project = create(:project)
|
|
|
|
object = Note.new(
|
|
|
|
note: 'hello',
|
|
|
|
note_html: 'hello',
|
2016-08-24 13:11:48 -04:00
|
|
|
author: another_user,
|
2017-08-23 12:53:29 -04:00
|
|
|
project: another_project
|
2016-08-24 13:11:48 -04:00
|
|
|
)
|
2016-06-21 07:35:09 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
expect(Banzai::Pipeline::PostProcessPipeline).to receive(:to_document).with(
|
|
|
|
anything,
|
|
|
|
hash_including(
|
|
|
|
skip_redaction: true,
|
|
|
|
current_user: user,
|
|
|
|
project: another_project,
|
|
|
|
author: another_user,
|
|
|
|
custom_value: 'value'
|
|
|
|
)
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
renderer.render([object], :note)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without cache' do
|
2019-06-05 00:59:48 -04:00
|
|
|
let(:cacheless_class) do
|
|
|
|
Class.new do
|
|
|
|
attr_accessor :title, :redacted_title_html, :project
|
|
|
|
|
|
|
|
def banzai_render_context(field)
|
|
|
|
{ project: project, pipeline: :single_line }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-10 23:11:00 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
let(:cacheless_thing) do
|
|
|
|
cacheless_class.new.tap do |thing|
|
|
|
|
thing.title = "Merge branch 'branch-merged' into 'master'"
|
|
|
|
thing.project = project
|
|
|
|
end
|
|
|
|
end
|
2017-08-23 12:53:29 -04:00
|
|
|
|
|
|
|
it 'renders and redacts an Array of objects' do
|
2019-06-05 00:59:48 -04:00
|
|
|
renderer.render([cacheless_thing], :title)
|
2017-08-23 12:53:29 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
expect(cacheless_thing.redacted_title_html).to eq("Merge branch 'branch-merged' into 'master'")
|
2017-08-23 12:53:29 -04:00
|
|
|
end
|
|
|
|
|
2019-07-16 16:19:08 -04:00
|
|
|
it 'calls Banzai::ReferenceRedactor to perform redaction' do
|
2019-11-20 01:06:16 -05:00
|
|
|
expect_next_instance_of(Banzai::ReferenceRedactor) do |instance|
|
|
|
|
expect(instance).to receive(:redact).and_call_original
|
|
|
|
end
|
2017-08-23 12:53:29 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
renderer.render([cacheless_thing], :title)
|
2017-08-23 12:53:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'retrieves field content using Banzai::Renderer.cacheless_render_field' do
|
2019-06-05 00:59:48 -04:00
|
|
|
expect(Banzai::Renderer).to receive(:cacheless_render_field).with(cacheless_thing, :title, {}).and_call_original
|
2017-08-23 12:53:29 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
renderer.render([cacheless_thing], :title)
|
2017-08-23 12:53:29 -04:00
|
|
|
end
|
2016-06-21 07:35:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|