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

virtual_host_finder_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: 49eee772f8dbb7251cbf85994da465ec7876f2f2 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Pages::VirtualHostFinder, feature_category: :pages do
  let_it_be(:project) { create(:project) }

  before_all do
    project.update_pages_deployment!(create(:pages_deployment, project: project))
  end

  before do
    stub_pages_setting(host: 'example.com')
  end

  it 'returns nil when host is empty' do
    expect(described_class.new(nil).execute).to be_nil
    expect(described_class.new('').execute).to be_nil
  end

  context 'when host is a pages custom domain host' do
    let_it_be(:pages_domain) { create(:pages_domain, project: project) }

    subject(:virtual_domain) { described_class.new(pages_domain.domain).execute }

    context 'when there are no pages deployed for the project' do
      before_all do
        project.mark_pages_as_not_deployed
      end

      it 'returns nil' do
        expect(virtual_domain).to be_nil
      end
    end

    context 'when there are pages deployed for the project' do
      before_all do
        project.mark_pages_as_deployed
      end

      it 'returns the virual domain when there are pages deployed for the project' do
        expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
        expect(virtual_domain.cache_key).to match(/pages_domain_for_domain_#{pages_domain.id}_/)
        expect(virtual_domain.lookup_paths.length).to eq(1)
        expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
      end

      context 'when :cache_pages_domain_api is disabled' do
        before do
          stub_feature_flags(cache_pages_domain_api: false)
        end

        it 'returns the virual domain when there are pages deployed for the project' do
          expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
          expect(virtual_domain.cache_key).to be_nil
          expect(virtual_domain.lookup_paths.length).to eq(1)
          expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
        end
      end
    end
  end

  context 'when host is a namespace domain' do
    context 'when there are no pages deployed for the project' do
      before_all do
        project.mark_pages_as_not_deployed
      end

      it 'returns no result if the provided host is not subdomain of the Pages host' do
        virtual_domain = described_class.new("#{project.namespace.path}.something.io").execute

        expect(virtual_domain).to eq(nil)
      end

      it 'returns the virual domain with no lookup_paths' do
        virtual_domain = described_class.new("#{project.namespace.path}.example.com").execute

        expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
        expect(virtual_domain.cache_key).to match(/pages_domain_for_namespace_#{project.namespace.id}_/)
        expect(virtual_domain.lookup_paths.length).to eq(0)
      end

      context 'when :cache_pages_domain_api is disabled' do
        before do
          stub_feature_flags(cache_pages_domain_api: false)
        end

        it 'returns the virual domain with no lookup_paths' do
          virtual_domain = described_class.new("#{project.namespace.path}.example.com".downcase).execute

          expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
          expect(virtual_domain.cache_key).to be_nil
          expect(virtual_domain.lookup_paths.length).to eq(0)
        end
      end
    end

    context 'when there are pages deployed for the project' do
      before_all do
        project.mark_pages_as_deployed
        project.namespace.update!(path: 'topNAMEspace')
      end

      it 'returns no result if the provided host is not subdomain of the Pages host' do
        virtual_domain = described_class.new("#{project.namespace.path}.something.io").execute

        expect(virtual_domain).to eq(nil)
      end

      it 'returns the virual domain when there are pages deployed for the project' do
        virtual_domain = described_class.new("#{project.namespace.path}.example.com").execute

        expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
        expect(virtual_domain.cache_key).to match(/pages_domain_for_namespace_#{project.namespace.id}_/)
        expect(virtual_domain.lookup_paths.length).to eq(1)
        expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
      end

      it 'finds domain with case-insensitive' do
        virtual_domain = described_class.new("#{project.namespace.path}.Example.com").execute

        expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
        expect(virtual_domain.cache_key).to match(/pages_domain_for_namespace_#{project.namespace.id}_/)
        expect(virtual_domain.lookup_paths.length).to eq(1)
        expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
      end

      context 'when :cache_pages_domain_api is disabled' do
        before_all do
          stub_feature_flags(cache_pages_domain_api: false)
        end

        it 'returns the virual domain when there are pages deployed for the project' do
          virtual_domain = described_class.new("#{project.namespace.path}.example.com").execute

          expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
          expect(virtual_domain.cache_key).to be_nil
          expect(virtual_domain.lookup_paths.length).to eq(1)
          expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
        end
      end
    end
  end

  context 'when host is a unique domain' do
    before_all do
      project.project_setting.update!(pages_unique_domain: 'unique-domain')
    end

    subject(:virtual_domain) { described_class.new('unique-domain.example.com').execute }

    context 'when pages unique domain is enabled' do
      before_all do
        project.project_setting.update!(pages_unique_domain_enabled: true)
      end

      context 'when there are no pages deployed for the project' do
        before_all do
          project.mark_pages_as_not_deployed
        end

        it 'returns nil' do
          expect(virtual_domain).to be_nil
        end
      end

      context 'when there are pages deployed for the project' do
        before_all do
          project.mark_pages_as_deployed
        end

        it 'returns the virual domain when there are pages deployed for the project' do
          expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
          expect(virtual_domain.lookup_paths.length).to eq(1)
          expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
        end

        context 'when a project path conflicts with a unique domain' do
          it 'prioritizes the unique domain project' do
            group = create(:group, path: 'unique-domain')
            other_project = build(:project, path: 'unique-domain.example.com', group: group)
            other_project.save!(validate: false)
            other_project.update_pages_deployment!(create(:pages_deployment, project: other_project))
            other_project.mark_pages_as_deployed

            expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
            expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
          end
        end

        context 'when :cache_pages_domain_api is disabled' do
          before do
            stub_feature_flags(cache_pages_domain_api: false)
          end

          it 'returns the virual domain when there are pages deployed for the project' do
            expect(virtual_domain).to be_an_instance_of(Pages::VirtualDomain)
            expect(virtual_domain.lookup_paths.length).to eq(1)
            expect(virtual_domain.lookup_paths.first.project_id).to eq(project.id)
          end
        end
      end
    end

    context 'when pages unique domain is disabled' do
      before_all do
        project.project_setting.update!(pages_unique_domain_enabled: false)
      end

      context 'when there are no pages deployed for the project' do
        before_all do
          project.mark_pages_as_not_deployed
        end

        it 'returns nil' do
          expect(virtual_domain).to be_nil
        end
      end

      context 'when there are pages deployed for the project' do
        before_all do
          project.mark_pages_as_deployed
        end

        it 'returns nil' do
          expect(virtual_domain).to be_nil
        end
      end
    end
  end
end