summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/template/finders/global_template_finder_spec.rb
blob: e776284b3e8fdb263a804f395e732b911e483948 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Template::Finders::GlobalTemplateFinder do
  let(:base_dir) { Dir.mktmpdir }

  def create_template!(name_with_category)
    full_path = File.join(base_dir, name_with_category)
    FileUtils.mkdir_p(File.dirname(full_path))
    FileUtils.touch(full_path)
  end

  after do
    FileUtils.rm_rf(base_dir)
  end

  subject(:finder) { described_class.new(base_dir, '', { 'General' => '', 'Bar' => 'Bar' }, exclusions: exclusions) }

  let(:exclusions) { [] }

  describe '.find' do
    context 'with a non-prefixed General template' do
      before do
        create_template!('test-template')
      end

      it 'finds the template with no prefix' do
        expect(finder.find('test-template')).to be_present
      end

      it 'does not find a prefixed template' do
        expect(finder.find('Bar/test-template')).to be_nil
      end

      it 'does not permit path traversal requests' do
        expect { finder.find('../foo') }.to raise_error(/Invalid path/)
      end

      context 'while listed as an exclusion' do
        let(:exclusions) { %w[test-template] }

        it 'does not find the template without a prefix' do
          expect(finder.find('test-template')).to be_nil
        end

        it 'does not find the template with a prefix' do
          expect(finder.find('Bar/test-template')).to be_nil
        end

        it 'finds another prefixed template with the same name' do
          create_template!('Bar/test-template')

          expect(finder.find('test-template')).to be_nil
          expect(finder.find('Bar/test-template')).to be_present
        end
      end
    end

    context 'with a prefixed template' do
      before do
        create_template!('Bar/test-template')
      end

      it 'finds the template with a prefix' do
        expect(finder.find('Bar/test-template')).to be_present
      end

      # NOTE: This spec fails, the template Bar/test-template is found
      # See Gitlab issue: https://gitlab.com/gitlab-org/gitlab/issues/205719
      xit 'does not find the template without a prefix' do
        expect(finder.find('test-template')).to be_nil
      end

      it 'does not permit path traversal requests' do
        expect { finder.find('../foo') }.to raise_error(/Invalid path/)
      end

      context 'while listed as an exclusion' do
        let(:exclusions) { %w[Bar/test-template] }

        it 'does not find the template with a prefix' do
          expect(finder.find('Bar/test-template')).to be_nil
        end

        # NOTE: This spec fails, the template Bar/test-template is found
        # See Gitlab issue: https://gitlab.com/gitlab-org/gitlab/issues/205719
        xit 'does not find the template without a prefix' do
          expect(finder.find('test-template')).to be_nil
        end

        it 'finds another non-prefixed template with the same name' do
          create_template!('Bar/test-template')

          expect(finder.find('test-template')).to be_present
          expect(finder.find('Bar/test-template')).to be_nil
        end
      end
    end
  end
end