summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/stages/url_validator_spec.rb
blob: 83cf161c4e2583f3f09d57e72dbdef50ead8ae8b (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Metrics::Dashboard::Stages::UrlValidator do
  let(:project) { build_stubbed(:project) }

  describe '#transform!' do
    context 'when the links contain a blocked url' do
      let(:dashboard) do
        {
          dashboard: "Test Dashboard",
          links: [
            { url: "http://1.1.1.1.1" },
            { url: "https://gitlab.com" },
            { url: "http://0.0.0.0" }
          ],
          panel_groups: [
            {
              group: "Group A",
              panels: [
                {
                  title: "Super Chart A1",
                  type: "area-chart",
                  y_label: "y_label",
                  metrics: [
                    {
                      id: "metric_a1",
                      query_range: "query",
                      unit: "unit",
                      label: "Legend Label"
                    }
                  ],
                  links: [
                    { url: "http://1.1.1.1.1" },
                    { url: "https://gitlab.com" },
                    { url: "http://0.0.0.0" }
                  ]
                }
              ]
            }
          ]
        }
      end

      let(:expected) do
        [{ url: '' }, { url: 'https://gitlab.com' }, { url: 'http://0.0.0.0' }]
      end

      let(:transform!) { described_class.new(project, dashboard, nil).transform! }

      before do
        stub_env('RSPEC_ALLOW_INVALID_URLS', 'false')
        stub_application_setting(allow_local_requests_from_web_hooks_and_services: true)
      end

      context 'dashboard related links' do
        it 'replaces the blocked url with an empty string' do
          transform!

          expect(dashboard[:links]).to eq(expected)
        end
      end

      context 'chart links' do
        it 'replaces the blocked url with an empty string' do
          transform!

          result = dashboard.dig(:panel_groups, 0, :panels, 0, :links)
          expect(result).to eq(expected)
        end
      end

      context 'when local requests are not allowed' do
        before do
          stub_application_setting(allow_local_requests_from_web_hooks_and_services: false)
        end

        let(:expected) do
          [{ url: '' }, { url: 'https://gitlab.com' }, { url: '' }]
        end

        it 'replaces the blocked url with an empty string' do
          transform!

          expect(dashboard[:links]).to eq(expected)
        end
      end

      context 'when the links are an array of strings instead of hashes' do
        before do
          dashboard[:links] = dashboard[:links].map(&:values)
        end

        it 'prevents an invalid link definition from erroring out' do
          expect { transform! }.not_to raise_error
        end
      end
    end
  end
end