summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/url_blockers/domain_allowlist_entry_spec.rb
blob: ece0a018d536c91cfdc5f8cbf738917bb050860a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UrlBlockers::DomainAllowlistEntry do
  let(:domain) { 'www.example.com' }

  describe '#initialize' do
    it 'initializes without port' do
      domain_allowlist_entry = described_class.new(domain)

      expect(domain_allowlist_entry.domain).to eq(domain)
      expect(domain_allowlist_entry.port).to be(nil)
    end

    it 'initializes with port' do
      port = 8080
      domain_allowlist_entry = described_class.new(domain, port: port)

      expect(domain_allowlist_entry.domain).to eq(domain)
      expect(domain_allowlist_entry.port).to eq(port)
    end
  end

  describe '#match?' do
    it 'matches when domain and port are equal' do
      port = 8080
      domain_allowlist_entry = described_class.new(domain, port: port)

      expect(domain_allowlist_entry).to be_match(domain, port)
    end

    it 'matches any port when port is nil' do
      domain_allowlist_entry = described_class.new(domain)

      expect(domain_allowlist_entry).to be_match(domain, 8080)
      expect(domain_allowlist_entry).to be_match(domain, 9090)
    end

    it 'does not match when port is present but requested_port is nil' do
      domain_allowlist_entry = described_class.new(domain, port: 8080)

      expect(domain_allowlist_entry).not_to be_match(domain, nil)
    end

    it 'matches when port and requested_port are nil' do
      domain_allowlist_entry = described_class.new(domain)

      expect(domain_allowlist_entry).to be_match(domain)
    end

    it 'does not match if domain is not equal' do
      domain_allowlist_entry = described_class.new(domain)

      expect(domain_allowlist_entry).not_to be_match('www.gitlab.com', 8080)
    end
  end
end