summaryrefslogtreecommitdiff
path: root/spec/gitlab_net_spec.rb
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2018-07-26 19:06:20 +1000
committerAsh McKenzie <amckenzie@gitlab.com>2018-08-01 00:24:16 +1000
commitecba183b60eb0798ecf1736659ed7d0a995d0f01 (patch)
tree3606b2d25e914f69f6cc9aab6de7864331b49dbf /spec/gitlab_net_spec.rb
parent252a96d61f52d91486f92f980c2f0e4c7a1b9408 (diff)
downloadgitlab-shell-ecba183b60eb0798ecf1736659ed7d0a995d0f01.tar.gz
Utilise new Actions
* Move gitaly, git-lfs and 2FA logic out from gitlab_shell.rb * Streamline parsing of origin_cmd in GitlabShell * Utilise proper HTTP status codes sent from the API * Also support 200 OK with status of true/false (ideally get rid of this) * Use HTTP status constants * Use attr_reader definitions (var over @var) * Rspec deprecation fixes
Diffstat (limited to 'spec/gitlab_net_spec.rb')
-rw-r--r--spec/gitlab_net_spec.rb131
1 files changed, 99 insertions, 32 deletions
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index 291e895..a2760aa 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -57,7 +57,7 @@ describe GitlabNet, vcr: true do
it "raises an exception if the connection fails" do
VCR.use_cassette("discover-ok") do
allow_any_instance_of(Net::HTTP).to receive(:request).and_raise(StandardError)
- expect { gitlab_net.discover(key) }.to raise_error(GitlabNet::ApiUnreachableError)
+ expect(gitlab_net.discover(key)).to be_nil
end
end
end
@@ -263,11 +263,33 @@ describe GitlabNet, vcr: true do
end
describe '#check_access' do
+ context 'something is wrong with the API response' do
+ context 'but response is JSON parsable' do
+ it 'raises an UnknownError exception' do
+ VCR.use_cassette('failed-push') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
+ end.to raise_error(UnknownError, 'API is not accessible: An internal server error occurred')
+ end
+ end
+ end
+
+ context 'but response is not JSON parsable' do
+ it 'raises an UnknownError exception' do
+ VCR.use_cassette('failed-push-unparsable') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
+ end.to raise_error(UnknownError, 'API is not accessible')
+ end
+ end
+ end
+ end
+
context 'ssh key with access nil, to project' do
- it 'should allow pull access for host' do
- VCR.use_cassette("allowed-pull") do
- access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
- access.allowed?.should be_truthy
+ it 'should allow push access for host' do
+ VCR.use_cassette('allowed-push') do
+ action = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
+ expect(action).to be_instance_of(Action::Gitaly)
end
end
@@ -278,75 +300,120 @@ describe GitlabNet, vcr: true do
end
end
- it 'should allow push access for host' do
- VCR.use_cassette("allowed-push") do
- access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh')
- access.allowed?.should be_truthy
+ it 'should allow pull access for host' do
+ VCR.use_cassette("allowed-pull") do
+ action = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh')
+ expect(action).to be_instance_of(Action::Gitaly)
end
end
end
context 'ssh access has been disabled' do
it 'should deny pull access for host' do
+ VCR.use_cassette('ssh-pull-disabled-old') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
+ end
+
VCR.use_cassette('ssh-pull-disabled') do
- access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh')
- access.allowed?.should be_falsey
- access.message.should eq 'Git access over SSH is not allowed'
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
end
end
it 'should deny push access for host' do
+ VCR.use_cassette('ssh-push-disabled-old') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
+ end
+
VCR.use_cassette('ssh-push-disabled') do
- access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
- access.allowed?.should be_falsey
- access.message.should eq 'Git access over SSH is not allowed'
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
end
end
end
context 'http access has been disabled' do
it 'should deny pull access for host' do
+ VCR.use_cassette('http-pull-disabled-old') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Pulling over HTTP is not allowed.')
+ end
+
VCR.use_cassette('http-pull-disabled') do
- access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http')
- access.allowed?.should be_falsey
- access.message.should eq 'Pulling over HTTP is not allowed.'
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Pulling over HTTP is not allowed.')
end
end
it 'should deny push access for host' do
- VCR.use_cassette("http-push-disabled") do
- access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'http')
- access.allowed?.should be_falsey
- access.message.should eq 'Pushing over HTTP is not allowed.'
+ VCR.use_cassette('http-push-disabled-old') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Pushing over HTTP is not allowed.')
+ end
+
+ VCR.use_cassette('http-push-disabled') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'http')
+ end.to raise_error(AccessDeniedError, 'Pushing over HTTP is not allowed.')
end
end
end
context 'ssh key without access to project' do
it 'should deny pull access for host' do
- VCR.use_cassette("ssh-pull-project-denied") do
- access = gitlab_net.check_access('git-receive-pack', nil, project, key2, changes, 'ssh')
- access.allowed?.should be_falsey
+ VCR.use_cassette('ssh-pull-project-denied-old') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key2, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
+ end
+
+ VCR.use_cassette('ssh-pull-project-denied') do
+ expect do
+ gitlab_net.check_access('git-receive-pack', nil, project, key2, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
end
end
it 'should deny push access for host' do
- VCR.use_cassette("ssh-push-project-denied") do
- access = gitlab_net.check_access('git-upload-pack', nil, project, key2, changes, 'ssh')
- access.allowed?.should be_falsey
+ VCR.use_cassette('ssh-push-project-denied-old') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key2, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
+ end
+
+ VCR.use_cassette('ssh-push-project-denied') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, key2, changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
end
end
it 'should deny push access for host (with user)' do
- VCR.use_cassette("ssh-push-project-denied-with-user") do
- access = gitlab_net.check_access('git-upload-pack', nil, project, 'user-2', changes, 'ssh')
- access.allowed?.should be_falsey
+ VCR.use_cassette('ssh-push-project-denied-with-user-old') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, 'user-2', changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
+ end
+
+ VCR.use_cassette('ssh-push-project-denied-with-user') do
+ expect do
+ gitlab_net.check_access('git-upload-pack', nil, project, 'user-2', changes, 'ssh')
+ end.to raise_error(AccessDeniedError, 'Git access over SSH is not allowed')
end
end
end
it "raises an exception if the connection fails" do
- Net::HTTP.any_instance.stub(:request).and_raise(StandardError)
+ allow_any_instance_of(Net::HTTP).to receive(:request).and_raise(StandardError)
expect {
gitlab_net.check_access('git-upload-pack', nil, project, 'user-1', changes, 'ssh')
}.to raise_error(GitlabNet::ApiUnreachableError)