summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/git_access_wiki_spec.rb
blob: a44426a95d5a26fe30aae1275daa112a2544003c (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
require 'spec_helper'

describe Gitlab::GitAccessWiki do
  let(:access) { described_class.new(user, project, 'web', authentication_abilities: authentication_abilities, redirected_path: redirected_path) }
  let(:project) { create(:project, :wiki_repo) }
  let(:user) { create(:user) }
  let(:changes) { ['6f6d7e7ed 570e7b2ab refs/heads/master'] }
  let(:redirected_path) { nil }
  let(:authentication_abilities) do
    [
      :read_project,
      :download_code,
      :push_code
    ]
  end

  describe '#push_access_check' do
    context 'when user can :create_wiki' do
      before do
        create(:protected_branch, name: 'master', project: project)
        project.add_developer(user)
      end

      subject { access.check('git-receive-pack', changes) }

      it { expect { subject }.not_to raise_error }

      context 'when in a read-only GitLab instance' do
        before do
          allow(Gitlab::Database).to receive(:read_only?) { true }
        end

        it 'does not give access to upload wiki code' do
          expect { subject }.to raise_error(Gitlab::GitAccess::UnauthorizedError, _("You can't push code to a read-only GitLab instance."))
        end
      end
    end
  end

  describe '#access_check_download!' do
    subject { access.check('git-upload-pack', Gitlab::GitAccess::ANY) }

    before do
      project.add_developer(user)
    end

    context 'when wiki feature is enabled' do
      it 'give access to download wiki code' do
        expect { subject }.not_to raise_error
      end

      context 'when the wiki repository does not exist' do
        it 'returns not found' do
          wiki_repo = project.wiki.repository
          Gitlab::GitalyClient::StorageSettings.allow_disk_access do
            FileUtils.rm_rf(wiki_repo.path)
          end

          # Sanity check for rm_rf
          expect(wiki_repo.exists?).to eq(false)

          expect { subject }.to raise_error(Gitlab::GitAccess::NotFoundError, _('A repository for this project does not exist yet.'))
        end
      end
    end

    context 'when wiki feature is disabled' do
      it 'does not give access to download wiki code' do
        project.project_feature.update_attribute(:wiki_access_level, ProjectFeature::DISABLED)

        expect { subject }.to raise_error(Gitlab::GitAccess::UnauthorizedError, _('You are not allowed to download code from this project.'))
      end
    end
  end
end