Welcome to mirror list, hosted at ThFree Co, Russian Federation.

url_builder_spec.rb « pages « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae94bbadffe618dbb837599117000a22cc341eb9 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Pages::UrlBuilder, feature_category: :pages do
  let(:pages_enabled) { true }
  let(:artifacts_server) { true }
  let(:access_control) { true }

  let(:port) { nil }
  let(:host) { 'example.com' }

  let(:full_path) { 'group/project' }
  let(:project_public) { true }
  let(:unique_domain) { 'unique-domain' }
  let(:unique_domain_enabled) { false }

  let(:project_setting) do
    instance_double(
      ProjectSetting,
      pages_unique_domain: unique_domain,
      pages_unique_domain_enabled?: unique_domain_enabled
    )
  end

  let(:project) do
    instance_double(
      Project,
      flipper_id: 'project:1', # required for the feature flag check
      public?: project_public,
      project_setting: project_setting,
      full_path: full_path
    )
  end

  subject(:builder) { described_class.new(project) }

  before do
    stub_pages_setting(
      enabled: pages_enabled,
      host: host,
      url: 'http://example.com',
      protocol: 'http',
      artifacts_server: artifacts_server,
      access_control: access_control,
      port: port
    )
  end

  describe '#pages_url' do
    subject(:pages_url) { builder.pages_url }

    it { is_expected.to eq('http://group.example.com/project') }

    context 'when namespace is upper cased' do
      let(:full_path) { 'Group/project' }

      it { is_expected.to eq('http://group.example.com/project') }
    end

    context 'when project is in a nested group page' do
      let(:full_path) { 'group/subgroup/project' }

      it { is_expected.to eq('http://group.example.com/subgroup/project') }
    end

    context 'when using domain pages' do
      let(:full_path) { 'group/group.example.com' }

      it { is_expected.to eq('http://group.example.com') }

      context 'in development mode' do
        let(:port) { 3010 }

        before do
          stub_rails_env('development')
        end

        it { is_expected.to eq('http://group.example.com:3010') }
      end
    end

    context 'when not using pages_unique_domain' do
      subject(:pages_url) { builder.pages_url(with_unique_domain: false) }

      context 'when pages_unique_domain_enabled is false' do
        let(:unique_domain_enabled) { false }

        it { is_expected.to eq('http://group.example.com/project') }
      end

      context 'when pages_unique_domain_enabled is true' do
        let(:unique_domain_enabled) { true }

        it { is_expected.to eq('http://group.example.com/project') }
      end
    end

    context 'when using pages_unique_domain' do
      subject(:pages_url) { builder.pages_url(with_unique_domain: true) }

      context 'when pages_unique_domain_enabled is false' do
        let(:unique_domain_enabled) { false }

        it { is_expected.to eq('http://group.example.com/project') }
      end

      context 'when pages_unique_domain_enabled is true' do
        let(:unique_domain_enabled) { true }

        it { is_expected.to eq('http://unique-domain.example.com') }
      end
    end
  end

  describe '#unique_host' do
    subject(:unique_host) { builder.unique_host }

    context 'when pages_unique_domain_enabled is false' do
      let(:unique_domain_enabled) { false }

      it { is_expected.to be_nil }
    end

    context 'when pages_unique_domain_enabled is true' do
      let(:unique_domain_enabled) { true }

      it { is_expected.to eq('unique-domain.example.com') }
    end
  end

  describe '#artifact_url' do
    let(:job) { instance_double(Ci::Build, id: 1) }
    let(:artifact) do
      instance_double(
        Gitlab::Ci::Build::Artifacts::Metadata::Entry,
        name: artifact_name,
        path: "path/#{artifact_name}")
    end

    subject(:artifact_url) { builder.artifact_url(artifact, job) }

    context 'with not allowed extension' do
      let(:artifact_name) { 'file.gif' }

      it { is_expected.to be_nil }
    end

    context 'with allowed extension' do
      let(:artifact_name) { 'file.txt' }

      it { is_expected.to eq("http://group.example.com/-/project/-/jobs/1/artifacts/path/file.txt") }

      context 'when port is configured' do
        let(:port) { 1234 }

        it { is_expected.to eq("http://group.example.com:1234/-/project/-/jobs/1/artifacts/path/file.txt") }
      end
    end
  end

  describe '#artifact_url_available?' do
    let(:job) { instance_double(Ci::Build, id: 1) }
    let(:artifact) do
      instance_double(
        Gitlab::Ci::Build::Artifacts::Metadata::Entry,
        name: artifact_name,
        path: "path/#{artifact_name}")
    end

    subject(:artifact_url_available) { builder.artifact_url_available?(artifact, job) }

    context 'with not allowed extensions' do
      let(:artifact_name) { 'file.gif' }

      it { is_expected.to be false }
    end

    context 'with allowed extensions' do
      let(:artifact_name) { 'file.txt' }

      it { is_expected.to be true }
    end
  end
end