summaryrefslogtreecommitdiff
path: root/spec/models/pages/lookup_path_spec.rb
blob: cb1938a0113b671ab930fdec517038a10b73db66 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Pages::LookupPath do
  let_it_be(:project) do
    create(:project, :pages_private, pages_https_only: true)
  end

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

  before do
    stub_pages_setting(access_control: true, external_https: ["1.1.1.1:443"])
    stub_artifacts_object_storage
  end

  describe '#project_id' do
    it 'delegates to Project#id' do
      expect(lookup_path.project_id).to eq(project.id)
    end
  end

  describe '#access_control' do
    it 'delegates to Project#private_pages?' do
      expect(lookup_path.access_control).to eq(true)
    end
  end

  describe '#https_only' do
    subject(:lookup_path) { described_class.new(project, domain: domain) }

    context 'when no domain provided' do
      let(:domain) { nil }

      it 'delegates to Project#pages_https_only?' do
        expect(lookup_path.https_only).to eq(true)
      end
    end

    context 'when there is domain provided' do
      let(:domain) { instance_double(PagesDomain, https?: false) }

      it 'takes into account the https setting of the domain' do
        expect(lookup_path.https_only).to eq(false)
      end
    end
  end

  describe '#source' do
    shared_examples 'uses disk storage' do
      it 'sets the source type to "file"' do
        expect(lookup_path.source[:type]).to eq('file')
      end

      it 'sets the source path to the project full path suffixed with "public/' do
        expect(lookup_path.source[:path]).to eq(project.full_path + "/public/")
      end
    end

    include_examples 'uses disk storage'

    context 'when artifact_id from build job is present in pages metadata' do
      let(:artifacts_archive) { create(:ci_job_artifact, :zip, :remote_store, project: project) }

      before do
        project.mark_pages_as_deployed(artifacts_archive: artifacts_archive)
      end

      it 'sets the source type to "zip"' do
        expect(lookup_path.source[:type]).to eq('zip')
      end

      it 'sets the source path to the artifacts archive URL' do
        Timecop.freeze do
          expect(lookup_path.source[:path]).to eq(artifacts_archive.file.url(expire_at: 1.day.from_now))
          expect(lookup_path.source[:path]).to include("Expires=86400")
        end
      end

      context 'when artifact is not uploaded to object storage' do
        let(:artifacts_archive) { create(:ci_job_artifact, :zip) }

        include_examples 'uses disk storage'
      end

      context 'when feature flag is disabled' do
        before do
          stub_feature_flags(pages_artifacts_archive: false)
        end

        include_examples 'uses disk storage'
      end
    end
  end

  describe '#prefix' do
    it 'returns "/" for pages group root projects' do
      project = instance_double(Project, pages_group_root?: true)
      lookup_path = described_class.new(project, trim_prefix: 'mygroup')

      expect(lookup_path.prefix).to eq('/')
    end

    it 'returns the project full path with the provided prefix removed' do
      project = instance_double(Project, pages_group_root?: false, full_path: 'mygroup/myproject')
      lookup_path = described_class.new(project, trim_prefix: 'mygroup')

      expect(lookup_path.prefix).to eq('/myproject/')
    end
  end
end