summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/metrics/dashboard/url_spec.rb
blob: 34bc635941456d8792164fa6aa104d3e5e678bf4 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Metrics::Dashboard::Url do
  describe '#regex' do
    it 'returns a regular expression' do
      expect(described_class.regex).to be_a Regexp
    end

    it 'matches a metrics dashboard link with named params' do
      url = Gitlab::Routing.url_helpers.metrics_namespace_project_environment_url('foo', 'bar', 1, start: 123345456, anchor: 'title')

      expected_params = {
        'url' => url,
        'namespace' => 'foo',
        'project' => 'bar',
        'environment' => '1',
        'query' => '?start=123345456',
        'anchor' => '#title'
      }

      expect(described_class.regex).to match url

      described_class.regex.match(url) do |m|
        expect(m.named_captures).to eq expected_params
      end
    end

    it 'does not match other gitlab urls that contain the term metrics' do
      url = Gitlab::Routing.url_helpers.active_common_namespace_project_prometheus_metrics_url('foo', 'bar', :json)

      expect(described_class.regex).not_to match url
    end

    it 'does not match other gitlab urls' do
      url = Gitlab.config.gitlab.url

      expect(described_class.regex).not_to match url
    end

    it 'does not match non-gitlab urls' do
      url = 'https://www.super_awesome_site.com/'

      expect(described_class.regex).not_to match url
    end
  end

  describe '#build_dashboard_url' do
    it 'builds the url for the dashboard endpoint' do
      url = described_class.build_dashboard_url('foo', 'bar', 1)

      expect(url).to match described_class.regex
    end
  end
end