diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-20 17:22:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-12-20 17:22:11 +0300 |
commit | 0c872e02b2c822e3397515ec324051ff540f0cd5 (patch) | |
tree | ce2fb6ce7030e4dad0f4118d21ab6453e5938cdd /spec/lib/banzai | |
parent | f7e05a6853b12f02911494c4b3fe53d9540d74fc (diff) |
Add latest changes from gitlab-org/gitlab@15-7-stable-eev15.7.0-rc42
Diffstat (limited to 'spec/lib/banzai')
11 files changed, 169 insertions, 16 deletions
diff --git a/spec/lib/banzai/filter/attributes_filter_spec.rb b/spec/lib/banzai/filter/attributes_filter_spec.rb new file mode 100644 index 00000000000..cef5e24cdaa --- /dev/null +++ b/spec/lib/banzai/filter/attributes_filter_spec.rb @@ -0,0 +1,78 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Banzai::Filter::AttributesFilter, feature_category: :team_planning do + using RSpec::Parameterized::TableSyntax + include FilterSpecHelper + + def image + %(<img src="example.jpg">) + end + + describe 'attribute syntax' do + context 'when attribute syntax is valid' do + where(:text, :result) do + "#{image}{width=100}" | '<img src="example.jpg" width="100">' + "#{image}{ width=100 }" | '<img src="example.jpg" width="100">' + "#{image}{width=\"100\"}" | '<img src="example.jpg" width="100">' + "#{image}{width=100 width=200}" | '<img src="example.jpg" width="200">' + + "#{image}{.test_class width=100 style=\"width:400\"}" | '<img src="example.jpg" width="100">' + "<img src=\"example.jpg\" class=\"lazy\" />{width=100}" | '<img src="example.jpg" class="lazy" width="100">' + end + + with_them do + it 'adds them to the img' do + expect(filter(text).to_html).to eq result + end + end + end + + context 'when attribute syntax is invalid' do + where(:text, :result) do + "#{image} {width=100}" | '<img src="example.jpg"> {width=100}' + "#{image}{width=100\nheight=100}" | "<img src=\"example.jpg\">{width=100\nheight=100}" + "{width=100 height=100}\n#{image}" | "{width=100 height=100}\n<img src=\"example.jpg\">" + '<h1>header</h1>{width=100}' | '<h1>header</h1>{width=100}' + end + + with_them do + it 'does not recognize as attributes' do + expect(filter(text).to_html).to eq result + end + end + end + end + + describe 'height and width' do + context 'when size attributes are valid' do + where(:text, :result) do + "#{image}{width=100 height=200px}" | '<img src="example.jpg" width="100" height="200px">' + "#{image}{width=100}" | '<img src="example.jpg" width="100">' + "#{image}{width=100px}" | '<img src="example.jpg" width="100px">' + "#{image}{height=100%}" | '<img src="example.jpg" height="100%">' + "#{image}{width=\"100%\"}" | '<img src="example.jpg" width="100%">' + end + + with_them do + it 'adds them to the img' do + expect(filter(text).to_html).to eq result + end + end + end + + context 'when size attributes are invalid' do + where(:text, :result) do + "#{image}{width=100cs}" | '<img src="example.jpg">' + "#{image}{width=auto height=200}" | '<img src="example.jpg" height="200">' + end + + with_them do + it 'ignores them' do + expect(filter(text).to_html).to eq result + end + end + end + end +end diff --git a/spec/lib/banzai/filter/commit_trailers_filter_spec.rb b/spec/lib/banzai/filter/commit_trailers_filter_spec.rb index c22517621c1..3ebe0798972 100644 --- a/spec/lib/banzai/filter/commit_trailers_filter_spec.rb +++ b/spec/lib/banzai/filter/commit_trailers_filter_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' require 'ffaker' -RSpec.describe Banzai::Filter::CommitTrailersFilter do +RSpec.describe Banzai::Filter::CommitTrailersFilter, feature_category: :source_code_management do include FilterSpecHelper include CommitTrailersSpecHelper diff --git a/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb b/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb index d29af311ee5..db0c10a802b 100644 --- a/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb +++ b/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb @@ -29,12 +29,12 @@ RSpec.describe Banzai::Filter::InlineGrafanaMetricsFilter do ) end - it_behaves_like 'a metrics embed filter' - around do |example| travel_to(Time.utc(2019, 3, 17, 13, 10)) { example.run } end + it_behaves_like 'a metrics embed filter' + context 'when grafana is not configured' do before do allow(project).to receive(:grafana_integration).and_return(nil) diff --git a/spec/lib/banzai/filter/inline_observability_filter_spec.rb b/spec/lib/banzai/filter/inline_observability_filter_spec.rb new file mode 100644 index 00000000000..341ada6d2b5 --- /dev/null +++ b/spec/lib/banzai/filter/inline_observability_filter_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Banzai::Filter::InlineObservabilityFilter do + include FilterSpecHelper + + let(:input) { %(<a href="#{url}">example</a>) } + let(:doc) { filter(input) } + + context 'when the document has an external link' do + let(:url) { 'https://foo.com' } + + it 'leaves regular non-observability links unchanged' do + expect(doc.to_s).to eq(input) + end + end + + context 'when the document contains an embeddable observability link' do + let(:url) { 'https://observe.gitlab.com/12345' } + + it 'leaves the original link unchanged' do + expect(doc.at_css('a').to_s).to eq(input) + end + + it 'appends a observability charts placeholder' do + node = doc.at_css('.js-render-observability') + + expect(node).to be_present + expect(node.attribute('data-frame-url').to_s).to eq(url) + end + end +end diff --git a/spec/lib/banzai/filter/references/reference_filter_spec.rb b/spec/lib/banzai/filter/references/reference_filter_spec.rb index b14b9374364..6d7396ef216 100644 --- a/spec/lib/banzai/filter/references/reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/reference_filter_spec.rb @@ -148,7 +148,7 @@ RSpec.describe Banzai::Filter::References::ReferenceFilter do include_context 'document nodes' let(:node) { Nokogiri::HTML.fragment('text @reference') } - let(:ref_pattern) { %r{(?<!\w)@(?<user>[a-zA-Z0-9_\-\.]*)}x } + let(:ref_pattern) { %r{(?<!\w)@(?<user>[a-zA-Z0-9_\-.]*)}x } context 'when node has no reference pattern' do let(:node) { Nokogiri::HTML.fragment('random text') } diff --git a/spec/lib/banzai/filter/references/user_reference_filter_spec.rb b/spec/lib/banzai/filter/references/user_reference_filter_spec.rb index b153efd9655..d61b71c711d 100644 --- a/spec/lib/banzai/filter/references/user_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/references/user_reference_filter_spec.rb @@ -42,12 +42,12 @@ RSpec.describe Banzai::Filter::References::UserReferenceFilter do context 'mentioning @all' do let(:reference) { User.reference_prefix + 'all' } - it_behaves_like 'a reference containing an element node' - before do project.add_developer(project.creator) end + it_behaves_like 'a reference containing an element node' + it 'supports a special @all mention' do project.add_developer(user) doc = reference_filter("Hey #{reference}", author: user) diff --git a/spec/lib/banzai/filter/repository_link_filter_spec.rb b/spec/lib/banzai/filter/repository_link_filter_spec.rb index 4aeb6e2a722..0df680dc0c8 100644 --- a/spec/lib/banzai/filter/repository_link_filter_spec.rb +++ b/spec/lib/banzai/filter/repository_link_filter_spec.rb @@ -69,7 +69,7 @@ RSpec.describe Banzai::Filter::RepositoryLinkFilter do expect { filter(raw_doc) }.to change { Gitlab::GitalyClient.get_request_count }.by(2) end - shared_examples :preserve_unchanged do + shared_examples 'preserve unchanged' do it 'does not modify any relative URL in anchor' do doc = filter(link('README.md')) expect(doc.at_css('a')['href']).to eq 'README.md' @@ -96,25 +96,25 @@ RSpec.describe Banzai::Filter::RepositoryLinkFilter do context 'with a wiki' do let(:wiki) { double('ProjectWiki') } - include_examples :preserve_unchanged + include_examples 'preserve unchanged' end context 'without a repository' do let(:project) { create(:project) } - include_examples :preserve_unchanged + include_examples 'preserve unchanged' end context 'with an empty repository' do let(:project) { create(:project_empty_repo) } - include_examples :preserve_unchanged + include_examples 'preserve unchanged' end context 'without project repository access' do let(:project) { create(:project, :repository, repository_access_level: ProjectFeature::PRIVATE) } - include_examples :preserve_unchanged + include_examples 'preserve unchanged' end it 'does not raise an exception on invalid URIs' do @@ -147,7 +147,7 @@ RSpec.describe Banzai::Filter::RepositoryLinkFilter do .to eq "/#{project_path}/-/blob/#{ref}/non/existent.file" end - shared_examples :valid_repository do + shared_examples 'valid repository' do it 'handles Gitaly unavailable exceptions gracefully' do allow_next_instance_of(Gitlab::GitalyClient::BlobService) do |blob_service| allow(blob_service).to receive(:get_blob_types).and_raise(GRPC::Unavailable) @@ -364,13 +364,13 @@ RSpec.describe Banzai::Filter::RepositoryLinkFilter do end context 'with a valid commit' do - include_examples :valid_repository + include_examples 'valid repository' end context 'with a valid ref' do # force filter to use ref instead of commit let(:commit) { nil } - include_examples :valid_repository + include_examples 'valid repository' end end diff --git a/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb b/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb index a409c15533b..b4be26ef8d2 100644 --- a/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb +++ b/spec/lib/banzai/filter/syntax_highlight_filter_spec.rb @@ -12,7 +12,7 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do result = filter(%{<pre lang="#{lang}"><code><script>alert(1)</script></code></pre>}) # `(1)` symbols are wrapped by lexer tags. - expect(result.to_html).not_to match(%r{<script>alert.*<\/script>}) + expect(result.to_html).not_to match(%r{<script>alert.*</script>}) # `<>` stands for lexer tags like <span ...>, not <s above. expect(result.to_html).to match(%r{alert(<.*>)?\((<.*>)?1(<.*>)?\)}) @@ -192,4 +192,8 @@ RSpec.describe Banzai::Filter::SyntaxHighlightFilter do include_examples "XSS prevention", "ruby" end + + it_behaves_like "filter timeout" do + let(:text) { '<pre lang="ruby"><code>def fun end</code></pre>' } + end end diff --git a/spec/lib/banzai/filter/timeout_html_pipeline_filter_spec.rb b/spec/lib/banzai/filter/timeout_html_pipeline_filter_spec.rb new file mode 100644 index 00000000000..95d2e54459d --- /dev/null +++ b/spec/lib/banzai/filter/timeout_html_pipeline_filter_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Banzai::Filter::TimeoutHtmlPipelineFilter do + include FilterSpecHelper + + it_behaves_like 'filter timeout' do + let(:text) { '<p>some text</p>' } + end + + it 'raises NotImplementedError' do + expect { filter('test') }.to raise_error NotImplementedError + end +end diff --git a/spec/lib/banzai/pipeline/incident_management/timeline_event_pipeline_spec.rb b/spec/lib/banzai/pipeline/incident_management/timeline_event_pipeline_spec.rb index 4bccae04fda..8d15dbc8f2f 100644 --- a/spec/lib/banzai/pipeline/incident_management/timeline_event_pipeline_spec.rb +++ b/spec/lib/banzai/pipeline/incident_management/timeline_event_pipeline_spec.rb @@ -78,7 +78,7 @@ RSpec.describe Banzai::Pipeline::IncidentManagement::TimelineEventPipeline do it 'replaces existing label to a link' do # rubocop:disable Layout/LineLength is_expected.to match( - %r(<p>.+<a href=\"[\w/]+-/issues\?label_name=#{label.name}\".+style=\"background-color: #\d{6}\".*>#{label.name}</span></a></span> ~unknown</p>) + %r(<p>.+<a href="[\w/]+-/issues\?label_name=#{label.name}".+style="background-color: #\d{6}".*>#{label.name}</span></a></span> ~unknown</p>) ) # rubocop:enable Layout/LineLength end diff --git a/spec/lib/banzai/reference_parser/base_parser_spec.rb b/spec/lib/banzai/reference_parser/base_parser_spec.rb index 9e77137795a..61751b69842 100644 --- a/spec/lib/banzai/reference_parser/base_parser_spec.rb +++ b/spec/lib/banzai/reference_parser/base_parser_spec.rb @@ -18,6 +18,29 @@ RSpec.describe Banzai::ReferenceParser::BaseParser do parser_class.new(context) end + describe '.reference_class' do + context 'when the method is not defined' do + it 'build the reference class' do + dummy = Class.new(described_class) + dummy.reference_type = :issue + + expect(dummy.reference_class).to eq(Issue) + end + end + + context 'when the method is redefined' do + it 'uses specified reference class' do + dummy = Class.new(described_class) do + def self.reference_class + AlertManagement::Alert + end + end + + expect(dummy.reference_class).to eq(AlertManagement::Alert) + end + end + end + describe '.reference_type=' do it 'sets the reference type' do dummy = Class.new(described_class) |