diff options
Diffstat (limited to 'spec/gitlab_net_spec.rb')
-rw-r--r-- | spec/gitlab_net_spec.rb | 108 |
1 files changed, 56 insertions, 52 deletions
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index 0b9bee7..9c9e6a1 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -6,11 +6,14 @@ require_relative '../lib/gitlab_access_status' describe GitlabNet, vcr: true do let(:gitlab_net) { GitlabNet.new } let(:changes) { ['0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4'] } - let(:host) { 'https://dev.gitlab.org/api/v4/internal' } + let(:host) { 'http://localhost:3000/api/v4/internal' } + let(:project) { 'gitlab-org/gitlab-test.git' } + let(:key) { 'key-1' } + let(:key2) { 'key-2' } + let(:secret) { gitlab_net.send(:secret_token) } before do gitlab_net.stub(:host).and_return(host) - gitlab_net.stub(:secret_token).and_return('a123') end describe :check do @@ -23,7 +26,7 @@ describe GitlabNet, vcr: true do it 'adds the secret_token to request' do VCR.use_cassette("check-ok") do - Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) + Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: secret)) gitlab_net.check end end @@ -37,23 +40,23 @@ describe GitlabNet, vcr: true do describe :discover do it 'should return user has based on key id' do VCR.use_cassette("discover-ok") do - user = gitlab_net.discover('key-126') - user['name'].should == 'Dmitriy Zaporozhets' - user['username'].should == 'dzaporozhets' + user = gitlab_net.discover(key) + user['name'].should == 'Administrator' + user['username'].should == 'root' end end it 'adds the secret_token to request' do VCR.use_cassette("discover-ok") do - Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) - gitlab_net.discover('key-126') + Net::HTTP::Get.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: secret)) + gitlab_net.discover(key) end end it "raises an exception if the connection fails" do VCR.use_cassette("discover-ok") do Net::HTTP.any_instance.stub(:request).and_raise(StandardError) - expect { gitlab_net.discover('key-126') }.to raise_error(GitlabNet::ApiUnreachableError) + expect { gitlab_net.discover(key) }.to raise_error(GitlabNet::ApiUnreachableError) end end end @@ -62,10 +65,10 @@ describe GitlabNet, vcr: true do context 'lfs authentication succeeded' do it 'should return the correct data' do VCR.use_cassette('lfs-authenticate-ok') do - lfs_access = gitlab_net.lfs_authenticate('key-126', 'gitlab/gitlabhq.git') - lfs_access.username.should == 'dzaporozhets' - lfs_access.lfs_token.should == 'wsnys8Zm8Jn7zyhHTAAK' - lfs_access.repository_http_path.should == 'http://gitlab.dev/gitlab/gitlabhq.git' + lfs_access = gitlab_net.lfs_authenticate(key, project) + lfs_access.username.should == 'root' + lfs_access.lfs_token.should == 'Hyzhyde_wLUeyUQsR3tHGTG8eNocVQm4ssioTEsBSdb6KwCSzQ' + lfs_access.repository_http_path.should == URI.join(host.sub('api/v4', ''), project).to_s end end end @@ -111,7 +114,7 @@ describe GitlabNet, vcr: true do end describe :authorized_key do - let (:ssh_key) { "AAAAB3NzaC1yc2EAAAADAQABAAACAQDPKPqqnqQ9PDFw65cO7iHXrKw6ucSZg8Bd2CZ150Yy1YRDPJOWeRNCnddS+M/Lk" } + let (:ssh_key) { "rsa-key" } it "should return nil when the resource is not implemented" do VCR.use_cassette("ssh-key-not-implemented") do @@ -131,10 +134,11 @@ describe GitlabNet, vcr: true do VCR.use_cassette("ssh-key-ok") do result = gitlab_net.authorized_key(ssh_key) result.should eq({ - "created_at" => "2016-03-04T18:27:36.959Z", - "id" => 2, - "key" => "ssh-rsa a-made=up-rsa-key dummy@gitlab.com", - "title" => "some key title" + "can_push" => false, + "created_at" => "2017-06-21T09:50:07.150Z", + "id" => 99, + "key" => "ssh-rsa rsa-key dummy@gitlab.com", + "title" => "untitled" }) end end @@ -143,7 +147,7 @@ describe GitlabNet, vcr: true do describe '#two_factor_recovery_codes' do it 'returns two factor recovery codes' do VCR.use_cassette('two-factor-recovery-codes') do - result = gitlab_net.two_factor_recovery_codes('key-1') + result = gitlab_net.two_factor_recovery_codes(key) expect(result['success']).to be_true expect(result['recovery_codes']).to eq(['f67c514de60c4953','41278385fc00c1e0']) end @@ -151,7 +155,7 @@ describe GitlabNet, vcr: true do it 'returns false when recovery codes cannot be generated' do VCR.use_cassette('two-factor-recovery-codes-fail') do - result = gitlab_net.two_factor_recovery_codes('key-1') + result = gitlab_net.two_factor_recovery_codes('key-777') expect(result['success']).to be_false expect(result['message']).to eq('Could not find the given key') end @@ -181,40 +185,40 @@ describe GitlabNet, vcr: true do describe :check_access do context 'ssh key with access nil, to project' do - it 'should allow pull access for dev.gitlab.org' do + it 'should allow pull access for host' do VCR.use_cassette("allowed-pull") do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-126', changes, 'ssh') + access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh') access.allowed?.should be_true end end it 'adds the secret_token to the request' do VCR.use_cassette("allowed-pull") do - Net::HTTP::Post.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')) - gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-126', changes, 'ssh') + Net::HTTP::Post.any_instance.should_receive(:set_form_data).with(hash_including(secret_token: secret)) + gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh') end end - it 'should allow push access for dev.gitlab.org' do + it 'should allow push access for host' do VCR.use_cassette("allowed-push") do - access = gitlab_net.check_access('git-upload-pack', nil, 'gitlab/gitlabhq.git', 'key-126', changes, 'ssh') + access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh') access.allowed?.should be_true end end end context 'ssh access has been disabled' do - it 'should deny pull access for dev.gitlab.org' do - VCR.use_cassette('ssh-access-disabled') do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'ssh') + it 'should deny pull access for host' do + VCR.use_cassette('ssh-pull-disabled') do + access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh') access.allowed?.should be_false access.message.should eq 'Git access over SSH is not allowed' end end - it 'should deny pull access for dev.gitlab.org' do - VCR.use_cassette('ssh-access-disabled') do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'ssh') + it 'should deny push access for host' do + VCR.use_cassette('ssh-push-disabled') do + access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh') access.allowed?.should be_false access.message.should eq 'Git access over SSH is not allowed' end @@ -222,41 +226,41 @@ describe GitlabNet, vcr: true do end context 'http access has been disabled' do - it 'should deny pull access for dev.gitlab.org' do - VCR.use_cassette('http-access-disabled') do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'http') + it 'should deny pull access for host' do + VCR.use_cassette('http-pull-disabled') do + access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'http') access.allowed?.should be_false - access.message.should eq 'Git access over HTTP is not allowed' + access.message.should eq 'Pulling over HTTP is not allowed.' end end - it 'should deny pull access for dev.gitlab.org' do - VCR.use_cassette('http-access-disabled') do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'http') + 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_false - access.message.should eq 'Git access over HTTP is not allowed' + access.message.should eq 'Pushing over HTTP is not allowed.' end end end context 'ssh key without access to project' do - it 'should deny pull access for dev.gitlab.org' do - VCR.use_cassette("denied-pull") do - access = gitlab_net.check_access('git-receive-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'ssh') + 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_false end end - it 'should deny push access for dev.gitlab.org' do - VCR.use_cassette("denied-push") do - access = gitlab_net.check_access('git-upload-pack', nil, 'gitlab/gitlabhq.git', 'key-2', changes, 'ssh') + 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_false end end - it 'should deny push access for dev.gitlab.org (with user)' do - VCR.use_cassette("denied-push-with-user") do - access = gitlab_net.check_access('git-upload-pack', nil, 'gitlab/gitlabhq.git', 'user-1', changes, 'ssh') + 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_false end end @@ -265,7 +269,7 @@ describe GitlabNet, vcr: true do it "raises an exception if the connection fails" do Net::HTTP.any_instance.stub(:request).and_raise(StandardError) expect { - gitlab_net.check_access('git-upload-pack', nil, 'gitlab/gitlabhq.git', 'user-1', changes, 'ssh') + gitlab_net.check_access('git-upload-pack', nil, project, 'user-1', changes, 'ssh') }.to raise_error(GitlabNet::ApiUnreachableError) end end @@ -304,7 +308,7 @@ describe GitlabNet, vcr: true do gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user } gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password } get.should_receive(:basic_auth).with(user, password).once - get.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')).once + get.should_receive(:set_form_data).with(hash_including(secret_token: secret)).once end it { should_not be_nil } @@ -313,7 +317,7 @@ describe GitlabNet, vcr: true do context 'Unix socket' do it 'sets the Host header to "localhost"' do gitlab_net = described_class.new - gitlab_net.should_receive(:secret_token).and_return('a123') + gitlab_net.should_receive(:secret_token).and_return(secret) request = gitlab_net.send(:http_request_for, :get, URI('http+unix://%2Ffoo')) |