debian-mirror-gitlab/spec/lib/banzai/object_renderer_spec.rb

105 lines
3.2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2016-08-24 12:49:21 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Banzai::ObjectRenderer do
2018-03-17 18:26:18 +05:30
let(:project) { create(:project, :repository) }
2016-08-24 12:49:21 +05:30
let(:user) { project.owner }
2018-05-09 12:01:36 +05:30
let(:renderer) do
described_class.new(
default_project: project,
user: user,
redaction_context: { custom_value: 'value' }
)
end
2019-09-04 21:01:54 +05:30
let(:object) { Note.new(note: 'hello', note_html: '<p dir="auto">hello</p>', cached_markdown_version: Gitlab::MarkdownCache::CACHE_COMMONMARK_VERSION << 16) }
2016-11-03 12:29:30 +05:30
2016-08-24 12:49:21 +05:30
describe '#render' do
2018-03-17 18:26:18 +05:30
context 'with cache' do
it 'renders and redacts an Array of objects' do
renderer.render([object], :note)
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
expect(object.redacted_note_html).to eq '<p dir="auto">hello</p>'
expect(object.user_visible_reference_count).to eq 0
end
2016-08-24 12:49:21 +05:30
2019-09-30 21:07:59 +05:30
it 'calls Banzai::ReferenceRedactor to perform redaction' do
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Banzai::ReferenceRedactor) do |instance|
expect(instance).to receive(:redact).and_call_original
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
renderer.render([object], :note)
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
it 'retrieves field content using Banzai::Renderer.render_field' do
expect(Banzai::Renderer).to receive(:render_field).with(object, :note, {}).and_call_original
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
renderer.render([object], :note)
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
it 'passes context to PostProcessPipeline' do
another_user = create(:user)
another_project = create(:project)
object = Note.new(
note: 'hello',
note_html: 'hello',
2017-08-17 22:00:37 +05:30
author: another_user,
2018-03-17 18:26:18 +05:30
project: another_project
2017-08-17 22:00:37 +05:30
)
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
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-09-04 21:01:54 +05:30
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
let(:cacheless_thing) do
cacheless_class.new.tap do |thing|
thing.title = "Merge branch 'branch-merged' into 'master'"
thing.project = project
end
end
2018-03-17 18:26:18 +05:30
it 'renders and redacts an Array of objects' do
2019-09-04 21:01:54 +05:30
renderer.render([cacheless_thing], :title)
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
expect(cacheless_thing.redacted_title_html).to eq("Merge branch 'branch-merged' into 'master'")
2018-03-17 18:26:18 +05:30
end
2019-09-30 21:07:59 +05:30
it 'calls Banzai::ReferenceRedactor to perform redaction' do
2020-01-01 13:55:28 +05:30
expect_next_instance_of(Banzai::ReferenceRedactor) do |instance|
expect(instance).to receive(:redact).and_call_original
end
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
renderer.render([cacheless_thing], :title)
2018-03-17 18:26:18 +05:30
end
it 'retrieves field content using Banzai::Renderer.cacheless_render_field' do
2019-09-04 21:01:54 +05:30
expect(Banzai::Renderer).to receive(:cacheless_render_field).with(cacheless_thing, :title, {}).and_call_original
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
renderer.render([cacheless_thing], :title)
2018-03-17 18:26:18 +05:30
end
2016-08-24 12:49:21 +05:30
end
end
end