summaryrefslogtreecommitdiff
path: root/spec/gitlab_access_spec.rb
blob: 6c1d3485e72a242d0458ccaf16d2a6f88ff9278a (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
require 'spec_helper'
require 'gitlab_access'

describe GitlabAccess do
  let(:repository_path) { "/home/git/repositories" }
  let(:repo_name) { 'dzaporozhets/gitlab-ci' }
  let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
  let(:api) do
    double(GitlabNet).tap do |api|
      allow(api).to receive(:check_access).and_return(GitAccessStatus.new(true,
                                                '200',
                                                 'ok',
                                                 gl_repository: 'project-1',
                                                 gl_project_path: 'group/subgroup/project',
                                                 gl_id: 'user-123',
                                                 gl_username: 'testuser',
                                                 git_config_options: ['receive.MaxInputSize=10000'],
                                                 gitaly: nil,
                                                 git_protocol: 'version=2'))
    end
  end
  subject do
    GitlabAccess.new(nil, repo_path, 'key-123', 'wow', 'ssh').tap do |access|
      allow(access).to receive(:exec_cmd).and_return(:exec_called)
      allow(access).to receive(:api).and_return(api)
    end
  end

  before do
    allow_any_instance_of(GitlabConfig).to receive(:repos_path).and_return(repository_path)
  end

  describe :initialize do
    it { expect(subject.repo_path).to eq(repo_path) }
    it { expect(subject.changes).to eq(['wow']) }
    it { expect(subject.protocol).to eq('ssh') }
  end

  describe "#exec" do
    context "access is granted" do
      it "returns true" do
        expect(subject.exec).to be_truthy
      end
    end

    context "access is denied" do
      before do
        allow(api).to receive(:check_access).and_return(GitAccessStatus.new(
                  false,
                  '401',
                  'denied',
                  gl_repository: nil,
                  gl_project_path: nil,
                  gl_id: nil,
                  gl_username: nil,
                  git_config_options: nil,
                  gitaly: nil,
                  git_protocol: nil
                ))
      end

      it "returns false" do
        expect(subject.exec).to be_falsey
      end
    end

    context "API connection fails" do
      before do
        allow(api).to receive(:check_access).and_raise(GitlabNet::ApiUnreachableError)
      end

      it "returns false" do
        expect(subject.exec).to be_falsey
      end
    end
  end
end