debian-mirror-gitlab/spec/lib/banzai/filter/references/abstract_reference_filter_spec.rb

103 lines
3.2 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-04-29 21:17:54 +05:30
RSpec.describe Banzai::Filter::References::AbstractReferenceFilter do
2020-03-13 15:44:24 +05:30
let_it_be(:project) { create(:project) }
let(:doc) { Nokogiri::HTML.fragment('') }
let(:filter) { described_class.new(doc, project: project) }
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
describe '#references_per_parent' do
2020-03-13 15:44:24 +05:30
let(:doc) { Nokogiri::HTML.fragment("#1 #{project.full_path}#2 #2") }
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
it 'returns a Hash containing references grouped per parent paths' do
expect(described_class).to receive(:object_class).exactly(6).times.and_return(Issue)
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
refs = filter.references_per_parent
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
expect(refs).to match(a_hash_including(project.full_path => contain_exactly(1, 2)))
2017-08-17 22:00:37 +05:30
end
end
2020-07-02 01:45:43 +05:30
describe '#data_attributes_for' do
let_it_be(:issue) { create(:issue, project: project) }
it 'is not an XSS vector' do
allow(described_class).to receive(:object_class).and_return(Issue)
data_attributes = filter.data_attributes_for('xss <img onerror=alert(1) src=x>', project, issue, link_content: true)
expect(data_attributes[:original]).to eq('xss <img onerror=alert(1) src=x>')
end
end
2018-03-17 18:26:18 +05:30
describe '#parent_per_reference' do
it 'returns a Hash containing projects grouped per parent paths' do
expect(filter).to receive(:references_per_parent)
2020-03-13 15:44:24 +05:30
.and_return({ project.full_path => Set.new([1]) })
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expect(filter.parent_per_reference)
2017-09-10 17:25:29 +05:30
.to eq({ project.full_path => project })
2017-08-17 22:00:37 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe '#find_for_paths' do
2017-08-17 22:00:37 +05:30
context 'with RequestStore disabled' do
it 'returns a list of Projects for a list of paths' do
2018-03-17 18:26:18 +05:30
expect(filter.find_for_paths([project.full_path]))
2017-09-10 17:25:29 +05:30
.to eq([project])
2017-08-17 22:00:37 +05:30
end
it "return an empty array for paths that don't exist" do
2018-03-17 18:26:18 +05:30
expect(filter.find_for_paths(['nonexistent/project']))
2017-09-10 17:25:29 +05:30
.to eq([])
2017-08-17 22:00:37 +05:30
end
end
2017-09-10 17:25:29 +05:30
context 'with RequestStore enabled', :request_store do
2017-08-17 22:00:37 +05:30
it 'returns a list of Projects for a list of paths' do
2018-03-17 18:26:18 +05:30
expect(filter.find_for_paths([project.full_path]))
2017-09-10 17:25:29 +05:30
.to eq([project])
2017-08-17 22:00:37 +05:30
end
context "when no project with that path exists" do
it "returns no value" do
2018-03-17 18:26:18 +05:30
expect(filter.find_for_paths(['nonexistent/project']))
2017-09-10 17:25:29 +05:30
.to eq([])
2017-08-17 22:00:37 +05:30
end
it "adds the ref to the project refs cache" do
project_refs_cache = {}
2018-03-17 18:26:18 +05:30
allow(filter).to receive(:refs_cache).and_return(project_refs_cache)
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
filter.find_for_paths(['nonexistent/project'])
2017-08-17 22:00:37 +05:30
expect(project_refs_cache).to eq({ 'nonexistent/project' => nil })
end
context 'when the project refs cache includes nil values' do
before do
# adds { 'nonexistent/project' => nil } to cache
2018-03-17 18:26:18 +05:30
filter.from_ref_cached('nonexistent/project')
2017-08-17 22:00:37 +05:30
end
it "return an empty array for paths that don't exist" do
2018-03-17 18:26:18 +05:30
expect(filter.find_for_paths(['nonexistent/project']))
2017-09-10 17:25:29 +05:30
.to eq([])
2017-08-17 22:00:37 +05:30
end
end
end
end
end
2018-03-17 18:26:18 +05:30
describe '#current_parent_path' do
it 'returns the path of the current parent' do
2017-08-17 22:00:37 +05:30
doc = Nokogiri::HTML.fragment('')
filter = described_class.new(doc, project: project)
2018-03-17 18:26:18 +05:30
expect(filter.current_parent_path).to eq(project.full_path)
2017-08-17 22:00:37 +05:30
end
end
end