diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-31 18:06:41 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2019-10-31 18:06:41 +0300 |
commit | 083d64c6468a070ae7b0b406ead8d87da27d1d22 (patch) | |
tree | ba92a9b5b6418f805ede9ef05b371d82d2c1d27d /spec/lib/banzai | |
parent | 0be510a49f6e4f8e27b19b707fd1dac61571f78f (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/lib/banzai')
-rw-r--r-- | spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb | 81 | ||||
-rw-r--r-- | spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb | 48 |
2 files changed, 114 insertions, 15 deletions
diff --git a/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb b/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb new file mode 100644 index 00000000000..81053469e7a --- /dev/null +++ b/spec/lib/banzai/filter/inline_grafana_metrics_filter_spec.rb @@ -0,0 +1,81 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Banzai::Filter::InlineGrafanaMetricsFilter do + include FilterSpecHelper + + let_it_be(:project) { create(:project) } + let_it_be(:grafana_integration) { create(:grafana_integration, project: project) } + + let(:input) { %(<a href="#{url}">example</a>) } + let(:doc) { filter(input) } + + let(:url) { grafana_integration.grafana_url + dashboard_path } + let(:dashboard_path) do + '/d/XDaNK6amz/gitlab-omnibus-redis' \ + '?from=1570397739557&to=1570484139557' \ + '&var-instance=All&panelId=14' + end + + context 'when feature flag is disabled' do + before do + stub_feature_flags(gfm_grafana_integration: false) + end + + it 'leaves the markdown unchanged' do + expect(unescape(doc.to_s)).to eq(input) + end + end + + it 'appends a metrics charts placeholder with dashboard url after metrics links' do + node = doc.at_css('.js-render-metrics') + expect(node).to be_present + + dashboard_url = urls.project_grafana_api_metrics_dashboard_url( + project, + embedded: true, + grafana_url: url, + start: "2019-10-06T21:35:39Z", + end: "2019-10-07T21:35:39Z" + ) + + expect(node.attribute('data-dashboard-url').to_s).to eq(dashboard_url) + end + + context 'when the dashboard link is part of a paragraph' do + let(:paragraph) { %(This is an <a href="#{url}">example</a> of metrics.) } + let(:input) { %(<p>#{paragraph}</p>) } + + it 'appends the charts placeholder after the enclosing paragraph' do + expect(unescape(doc.at_css('p').to_s)).to include(paragraph) + expect(doc.at_css('.js-render-metrics')).to be_present + end + end + + context 'when grafana is not configured' do + before do + allow(project).to receive(:grafana_integration).and_return(nil) + end + + it 'leaves the markdown unchanged' do + expect(unescape(doc.to_s)).to eq(input) + end + end + + context 'when parameters are missing' do + let(:dashboard_path) { '/d/XDaNK6amz/gitlab-omnibus-redis' } + + it 'leaves the markdown unchanged' do + expect(unescape(doc.to_s)).to eq(input) + end + end + + private + + # Nokogiri escapes the URLs, but we don't care about that + # distinction for the purposes of this filter + def unescape(html) + CGI.unescapeHTML(html) + end +end diff --git a/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb b/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb index a99cd7d6076..745b9133529 100644 --- a/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb +++ b/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb @@ -18,30 +18,48 @@ describe Banzai::Filter::InlineMetricsRedactorFilter do end context 'with a metrics charts placeholder' do - let(:input) { %(<div class="js-render-metrics" data-dashboard-url="#{url}"></div>) } + shared_examples_for 'a supported metrics dashboard url' do + context 'no user is logged in' do + it 'redacts the placeholder' do + expect(doc.to_s).to be_empty + end + end - context 'no user is logged in' do - it 'redacts the placeholder' do - expect(doc.to_s).to be_empty + context 'the user does not have permission do see charts' do + let(:doc) { filter(input, current_user: build(:user)) } + + it 'redacts the placeholder' do + expect(doc.to_s).to be_empty + end end - end - context 'the user does not have permission do see charts' do - let(:doc) { filter(input, current_user: build(:user)) } + context 'the user has requisite permissions' do + let(:user) { create(:user) } + let(:doc) { filter(input, current_user: user) } - it 'redacts the placeholder' do - expect(doc.to_s).to be_empty + it 'leaves the placeholder' do + project.add_maintainer(user) + + expect(doc.to_s).to eq input + end end end - context 'the user has requisite permissions' do - let(:user) { create(:user) } - let(:doc) { filter(input, current_user: user) } + let(:input) { %(<div class="js-render-metrics" data-dashboard-url="#{url}"></div>) } - it 'leaves the placeholder' do - project.add_maintainer(user) + it_behaves_like 'a supported metrics dashboard url' + + context 'for a grafana dashboard' do + let(:url) { urls.project_grafana_api_metrics_dashboard_url(project, embedded: true) } + + it_behaves_like 'a supported metrics dashboard url' + end - expect(doc.to_s).to eq input + context 'for an internal non-dashboard url' do + let(:url) { urls.project_url(project) } + + it 'leaves the placeholder' do + expect(doc.to_s).to be_empty end end end |