summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/inline_metrics_redactor_filter_spec.rb
blob: 745b9133529d94d235da832f0f989ea86f6d0cb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

require 'spec_helper'

describe Banzai::Filter::InlineMetricsRedactorFilter do
  include FilterSpecHelper

  set(:project) { create(:project) }

  let(:url) { urls.metrics_dashboard_project_environment_url(project, 1, embedded: true) }
  let(:input) { %(<a href="#{url}">example</a>) }
  let(:doc) { filter(input) }

  context 'without a metrics charts placeholder' do
    it 'leaves regular non-metrics links unchanged' do
      expect(doc.to_s).to eq input
    end
  end

  context 'with a metrics charts placeholder' do
    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 '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

      context 'the user has requisite permissions' do
        let(:user) { create(:user) }
        let(:doc) { filter(input, current_user: user) }

        it 'leaves the placeholder' do
          project.add_maintainer(user)

          expect(doc.to_s).to eq input
        end
      end
    end

    let(:input) { %(<div class="js-render-metrics" data-dashboard-url="#{url}"></div>) }

    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

    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
end