diff options
Diffstat (limited to 'spec/lib/banzai')
-rw-r--r-- | spec/lib/banzai/filter/issue_reference_filter_spec.rb | 55 | ||||
-rw-r--r-- | spec/lib/banzai/filter/upload_link_filter_spec.rb | 17 | ||||
-rw-r--r-- | spec/lib/banzai/filter/user_reference_filter_spec.rb | 2 | ||||
-rw-r--r-- | spec/lib/banzai/pipeline/wiki_pipeline_spec.rb | 8 | ||||
-rw-r--r-- | spec/lib/banzai/reference_parser/design_parser_spec.rb | 91 | ||||
-rw-r--r-- | spec/lib/banzai/renderer_spec.rb | 57 |
6 files changed, 223 insertions, 7 deletions
diff --git a/spec/lib/banzai/filter/issue_reference_filter_spec.rb b/spec/lib/banzai/filter/issue_reference_filter_spec.rb index 4a412da27a7..61c59162a30 100644 --- a/spec/lib/banzai/filter/issue_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/issue_reference_filter_spec.rb @@ -4,6 +4,7 @@ require 'spec_helper' describe Banzai::Filter::IssueReferenceFilter do include FilterSpecHelper + include DesignManagementTestHelpers def helper IssuesHelper @@ -358,6 +359,23 @@ describe Banzai::Filter::IssueReferenceFilter do end end + context 'when processing a link to the designs tab' do + let(:designs_tab_url) { url_for_designs(issue) } + let(:input_text) { "See #{designs_tab_url}" } + + subject(:link) { reference_filter(input_text).css('a').first } + + before do + enable_design_management + end + + it 'includes the word "designs" after the reference in the text content', :aggregate_failures do + expect(link.attr('title')).to eq(issue.title) + expect(link.attr('href')).to eq(designs_tab_url) + expect(link.text).to eq("#{issue.to_reference} (designs)") + end + end + context 'group context' do let(:group) { create(:group) } let(:context) { { project: nil, group: group } } @@ -467,4 +485,41 @@ describe Banzai::Filter::IssueReferenceFilter do end.not_to yield_control end end + + describe '#object_link_text_extras' do + before do + enable_design_management(enabled) + end + + let(:current_user) { project.owner } + let(:enabled) { true } + let(:matches) { Issue.link_reference_pattern.match(input_text) } + let(:extras) { subject.object_link_text_extras(issue, matches) } + + subject { filter_instance } + + context 'the link does not go to the designs tab' do + let(:input_text) { Gitlab::Routing.url_helpers.project_issue_url(issue.project, issue) } + + it 'does not include designs' do + expect(extras).not_to include('designs') + end + end + + context 'the link goes to the designs tab' do + let(:input_text) { url_for_designs(issue) } + + it 'includes designs' do + expect(extras).to include('designs') + end + + context 'design management is disabled' do + let(:enabled) { false } + + it 'does not include designs in the extras' do + expect(extras).not_to include('designs') + end + end + end + end end diff --git a/spec/lib/banzai/filter/upload_link_filter_spec.rb b/spec/lib/banzai/filter/upload_link_filter_spec.rb index 3f181dce7bc..c366f774895 100644 --- a/spec/lib/banzai/filter/upload_link_filter_spec.rb +++ b/spec/lib/banzai/filter/upload_link_filter_spec.rb @@ -51,6 +51,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(absolute_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end end @@ -59,11 +60,13 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') doc = filter(nested(link(upload_path))) expect(doc.at_css('a')['href']).to eq(relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end it 'rebuilds relative URL for an image' do @@ -71,11 +74,13 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('img')['src']).to eq(relative_path) expect(doc.at_css('img').classes).to include('gfm') + expect(doc.at_css('img')['data-link']).not_to eq('true') doc = filter(nested(image(upload_path))) expect(doc.at_css('img')['src']).to eq(relative_path) expect(doc.at_css('img').classes).to include('gfm') + expect(doc.at_css('img')['data-link']).not_to eq('true') end it 'does not modify absolute URL' do @@ -83,6 +88,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq 'http://example.com' expect(doc.at_css('a').classes).not_to include('gfm') + expect(doc.at_css('a')['data-link']).not_to eq('true') end it 'supports unescaped Unicode filenames' do @@ -91,6 +97,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq("/#{project.full_path}/uploads/%ED%95%9C%EA%B8%80.png") expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end it 'supports escaped Unicode filenames' do @@ -100,6 +107,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('img')['src']).to eq("/#{project.full_path}/uploads/%ED%95%9C%EA%B8%80.png") expect(doc.at_css('img').classes).to include('gfm') + expect(doc.at_css('img')['data-link']).not_to eq('true') end end @@ -118,6 +126,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(absolute_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end end @@ -126,6 +135,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end it 'rewrites the link correctly for subgroup' do @@ -135,6 +145,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end it 'does not modify absolute URL' do @@ -142,6 +153,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq 'http://example.com' expect(doc.at_css('a').classes).not_to include('gfm') + expect(doc.at_css('a')['data-link']).not_to eq('true') end end @@ -159,6 +171,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(absolute_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end end @@ -178,6 +191,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(absolute_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end end @@ -186,6 +200,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(gitlab_root + relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end end @@ -194,6 +209,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq(relative_path) expect(doc.at_css('a').classes).to include('gfm') + expect(doc.at_css('a')['data-link']).to eq('true') end it 'does not modify absolute URL' do @@ -201,6 +217,7 @@ describe Banzai::Filter::UploadLinkFilter do expect(doc.at_css('a')['href']).to eq 'http://example.com' expect(doc.at_css('a').classes).not_to include('gfm') + expect(doc.at_css('a')['data-link']).not_to eq('true') end end diff --git a/spec/lib/banzai/filter/user_reference_filter_spec.rb b/spec/lib/banzai/filter/user_reference_filter_spec.rb index a09aeb7d7f6..cd6b68343b5 100644 --- a/spec/lib/banzai/filter/user_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/user_reference_filter_spec.rb @@ -123,7 +123,7 @@ describe Banzai::Filter::UserReferenceFilter do it 'includes default classes' do doc = reference_filter("Hey #{reference}") - expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-project_member' + expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-project_member js-user-link' end context 'when a project is not specified' do diff --git a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb index 8c009bc409b..4d16c568c13 100644 --- a/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/wiki_pipeline_spec.rb @@ -57,10 +57,10 @@ describe Banzai::Pipeline::WikiPipeline do let(:namespace) { create(:namespace, name: "wiki_link_ns") } let(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } let(:project_wiki) { ProjectWiki.new(project, double(:user)) } - let(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } + let(:page) { build(:wiki_page, wiki: project_wiki, title: 'nested/twice/start-page') } - { "when GitLab is hosted at a root URL" => '/', - "when GitLab is hosted at a relative URL" => '/nested/relative/gitlab' }.each do |test_name, relative_url_root| + { 'when GitLab is hosted at a root URL' => '', + 'when GitLab is hosted at a relative URL' => '/nested/relative/gitlab' }.each do |test_name, relative_url_root| context test_name do before do allow(Gitlab.config.gitlab).to receive(:relative_url_root).and_return(relative_url_root) @@ -264,7 +264,7 @@ describe Banzai::Pipeline::WikiPipeline do let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") } let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) } - let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } + let_it_be(:page) { build(:wiki_page, wiki: project_wiki, title: 'nested/twice/start-page') } it 'generates video html structure' do markdown = "![video_file](video_file_name.mp4)" diff --git a/spec/lib/banzai/reference_parser/design_parser_spec.rb b/spec/lib/banzai/reference_parser/design_parser_spec.rb new file mode 100644 index 00000000000..76708acf887 --- /dev/null +++ b/spec/lib/banzai/reference_parser/design_parser_spec.rb @@ -0,0 +1,91 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::ReferenceParser::DesignParser do + include ReferenceParserHelpers + include DesignManagementTestHelpers + + let_it_be(:issue) { create(:issue) } + let_it_be(:design) { create(:design, :with_versions, issue: issue) } + let_it_be(:user) { create(:user, developer_projects: [issue.project]) } + + subject(:instance) { described_class.new(Banzai::RenderContext.new(issue.project, user)) } + + let(:link) { design_link(design) } + + before do + enable_design_management + end + + describe '#nodes_visible_to_user' do + it_behaves_like 'referenced feature visibility', 'issues' do + let(:project) { issue.project } + end + + describe 'specific states' do + let_it_be(:public_project) { create(:project, :public) } + + let_it_be(:other_project_link) do + design_link(create(:design, :with_versions)) + end + let_it_be(:public_link) do + design_link(create(:design, :with_versions, issue: create(:issue, project: public_project))) + end + let_it_be(:public_but_confidential_link) do + design_link(create(:design, :with_versions, issue: create(:issue, :confidential, project: public_project))) + end + + subject(:visible_nodes) do + nodes = [link, + other_project_link, + public_link, + public_but_confidential_link] + + instance.nodes_visible_to_user(user, nodes) + end + + it 'redacts links we should not have access to' do + expect(visible_nodes).to contain_exactly(link, public_link) + end + + context 'design management is not available' do + before do + enable_design_management(false) + end + + it 'redacts all nodes' do + expect(visible_nodes).to be_empty + end + end + end + end + + describe '#process' do + it 'returns the correct designs' do + frag = document([design, create(:design, :with_versions)]) + + expect(subject.process([frag])[:visible]).to contain_exactly(design) + end + end + + def design_link(design) + node = empty_html_link + node['class'] = 'gfm' + node['data-reference-type'] = 'design' + node['data-project'] = design.project.id.to_s + node['data-issue'] = design.issue.id.to_s + node['data-design'] = design.id.to_s + + node + end + + def document(designs) + frag = Nokogiri::HTML.fragment('') + designs.each do |design| + frag.add_child(design_link(design)) + end + + frag + end +end diff --git a/spec/lib/banzai/renderer_spec.rb b/spec/lib/banzai/renderer_spec.rb index 0d329b47aa3..b540a76face 100644 --- a/spec/lib/banzai/renderer_spec.rb +++ b/spec/lib/banzai/renderer_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' describe Banzai::Renderer do + let(:renderer) { described_class } + def fake_object(fresh:) object = double('object') @@ -40,8 +42,6 @@ describe Banzai::Renderer do end describe '#render_field' do - let(:renderer) { described_class } - context 'without cache' do let(:commit) { fake_cacheless_object } @@ -83,4 +83,57 @@ describe Banzai::Renderer do end end end + + 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 end |