2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-06 17:17:11 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Banzai::Renderer do
|
2020-05-05 17:09:42 -04:00
|
|
|
let(:renderer) { described_class }
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
def fake_object(fresh:)
|
|
|
|
object = double('object')
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(true)
|
2017-04-10 14:36:30 -04:00
|
|
|
allow(object).to receive(:cached_html_up_to_date?).with(:field).and_return(fresh)
|
|
|
|
allow(object).to receive(:cached_html_for).with(:field).and_return('field_html')
|
2016-10-06 17:17:11 -04:00
|
|
|
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
def fake_cacheless_object
|
|
|
|
object = double('cacheless object')
|
|
|
|
|
|
|
|
allow(object).to receive(:respond_to?).with(:cached_markdown_fields).and_return(false)
|
|
|
|
|
|
|
|
object
|
|
|
|
end
|
|
|
|
|
2019-07-03 19:12:02 -04:00
|
|
|
describe '#cache_collection_render' do
|
|
|
|
let(:merge_request) { fake_object(fresh: true) }
|
|
|
|
let(:context) { { cache_key: [merge_request, 'field'], rendered: merge_request.field_html } }
|
|
|
|
|
|
|
|
context 'when an item has a rendered field' do
|
|
|
|
before do
|
|
|
|
allow(merge_request).to receive(:field).and_return('This is the field')
|
|
|
|
allow(merge_request).to receive(:field_html).and_return('This is the field')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not touch redis if the field is in the cache' do
|
|
|
|
expect(Rails).not_to receive(:cache)
|
|
|
|
|
|
|
|
described_class.cache_collection_render([{ text: merge_request.field, context: context }])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-10 14:36:30 -04:00
|
|
|
describe '#render_field' do
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'without cache' do
|
2019-06-05 00:59:48 -04:00
|
|
|
let(:commit) { fake_cacheless_object }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'returns cacheless render field' do
|
2019-06-05 00:59:48 -04:00
|
|
|
expect(renderer).to receive(:cacheless_render_field).with(commit, :field, {})
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2019-06-05 00:59:48 -04:00
|
|
|
renderer.render_field(commit, :field)
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'with cache' do
|
|
|
|
subject { renderer.render_field(object, :field) }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
context 'with a stale cache' do
|
|
|
|
let(:object) { fake_object(fresh: false) }
|
2016-10-06 17:17:11 -04:00
|
|
|
|
2017-08-23 12:53:29 -04:00
|
|
|
it 'caches and returns the result' do
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(object).to receive(:refresh_markdown_cache!)
|
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "skips database caching on a GitLab read-only instance" do
|
2021-08-02 05:10:09 -04:00
|
|
|
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
|
2017-09-19 03:44:58 -04:00
|
|
|
expect(object).to receive(:refresh_markdown_cache!)
|
2017-08-23 12:53:29 -04:00
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an up-to-date cache' do
|
|
|
|
let(:object) { fake_object(fresh: true) }
|
|
|
|
|
|
|
|
it 'uses the cache' do
|
|
|
|
expect(object).to receive(:refresh_markdown_cache!).never
|
|
|
|
|
|
|
|
is_expected.to eq('field_html')
|
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-05-05 17:09:42 -04:00
|
|
|
|
2021-10-28 14:14:18 -04:00
|
|
|
describe '#cacheless_render' do
|
|
|
|
context 'without cache' do
|
|
|
|
let(:object) { fake_object(fresh: false) }
|
|
|
|
let(:histogram) { double('prometheus histogram') }
|
|
|
|
|
|
|
|
it 'returns cacheless render field' do
|
|
|
|
allow(renderer).to receive(:render_result).and_return(output: 'test')
|
|
|
|
allow(renderer).to receive(:real_duration_histogram).and_return(histogram)
|
|
|
|
allow(renderer).to receive(:cpu_duration_histogram).and_return(histogram)
|
|
|
|
|
|
|
|
expect(renderer).to receive(:render_result).with('test', {})
|
|
|
|
expect(histogram).to receive(:observe).twice
|
|
|
|
|
|
|
|
renderer.cacheless_render('test')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-05 17:09:42 -04:00
|
|
|
describe '#post_process' do
|
|
|
|
let(:context_options) { {} }
|
|
|
|
let(:html) { 'Consequatur aperiam et nesciunt modi aut assumenda quo id. '}
|
|
|
|
let(:post_processed_html) { double(html_safe: 'safe doc') }
|
|
|
|
let(:doc) { double(to_html: post_processed_html) }
|
|
|
|
|
|
|
|
subject { renderer.post_process(html, context_options) }
|
|
|
|
|
|
|
|
context 'when xhtml' do
|
|
|
|
let(:context_options) { { xhtml: ' ' } }
|
|
|
|
|
|
|
|
context 'without :post_process_pipeline key' do
|
|
|
|
it 'uses PostProcessPipeline' do
|
|
|
|
expect(::Banzai::Pipeline::PostProcessPipeline).to receive(:to_document).and_return(doc)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with :post_process_pipeline key' do
|
|
|
|
let(:context_options) { { post_process_pipeline: Object, xhtml: ' ' } }
|
|
|
|
|
|
|
|
it 'uses passed post process pipeline' do
|
|
|
|
expect(Object).to receive(:to_document).and_return(doc)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when not xhtml' do
|
|
|
|
context 'without :post_process_pipeline key' do
|
|
|
|
it 'uses PostProcessPipeline' do
|
|
|
|
expect(::Banzai::Pipeline::PostProcessPipeline).to receive(:to_html)
|
|
|
|
.with(html, { only_path: true, disable_asset_proxy: true })
|
|
|
|
.and_return(post_processed_html)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with :post_process_pipeline key' do
|
|
|
|
let(:context_options) { { post_process_pipeline: Object } }
|
|
|
|
|
|
|
|
it 'uses passed post process pipeline' do
|
|
|
|
expect(Object).to receive(:to_html).and_return(post_processed_html)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-10-06 17:17:11 -04:00
|
|
|
end
|