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

require 'spec_helper'

describe Gitlab::GitAccessSnippet do
  include GitHelpers

  let_it_be(:user) { create(:user) }
  let_it_be(:personal_snippet) { create(:personal_snippet, :private, :repository) }

  let(:protocol) { 'ssh' }
  let(:changes) { Gitlab::GitAccess::ANY }
  let(:push_access_check) { access.check('git-receive-pack', changes) }
  let(:pull_access_check) { access.check('git-upload-pack', changes) }
  let(:snippet) { personal_snippet }
  let(:actor) { personal_snippet.author }

  describe 'when feature flag :version_snippets is enabled' do
    it 'allows push and pull access' do
      aggregate_failures do
        expect { pull_access_check }.not_to raise_error
        expect { push_access_check }.not_to raise_error
      end
    end
  end

  describe 'when feature flag :version_snippets is disabled' do
    before do
      stub_feature_flags(version_snippets: false)
    end

    it 'does not allow push and pull access' do
      aggregate_failures do
        expect { push_access_check }.to raise_snippet_not_found
        expect { pull_access_check }.to raise_snippet_not_found
      end
    end
  end

  describe '#check_snippet_accessibility!' do
    context 'when the snippet exists' do
      it 'allows push and pull access' do
        aggregate_failures do
          expect { pull_access_check }.not_to raise_error
          expect { push_access_check }.not_to raise_error
        end
      end
    end

    context 'when the snippet is nil' do
      let(:snippet) { nil }

      it 'blocks push and pull with "not found"' do
        aggregate_failures do
          expect { pull_access_check }.to raise_snippet_not_found
          expect { push_access_check }.to raise_snippet_not_found
        end
      end
    end

    context 'when the snippet does not have a repository' do
      let(:snippet) { build_stubbed(:personal_snippet) }

      it 'blocks push and pull with "not found"' do
        aggregate_failures do
          expect { pull_access_check }.to raise_snippet_not_found
          expect { push_access_check }.to raise_snippet_not_found
        end
      end
    end
  end

  private

  def access
    described_class.new(actor, snippet, protocol,
                        authentication_abilities: [],
                        namespace_path: nil, project_path: nil,
                        redirected_path: nil, auth_result_type: nil)
  end

  def raise_snippet_not_found
    raise_error(Gitlab::GitAccess::NotFoundError, Gitlab::GitAccess::ERROR_MESSAGES[:snippet_not_found])
  end
end