diff options
-rw-r--r-- | lib/gitlab_net.rb | 35 | ||||
-rw-r--r-- | spec/gitlab_config_spec.rb | 65 | ||||
-rw-r--r-- | spec/gitlab_keys_spec.rb | 20 | ||||
-rw-r--r-- | spec/gitlab_logger_spec.rb | 11 | ||||
-rw-r--r-- | spec/gitlab_net_spec.rb | 75 | ||||
-rw-r--r-- | spec/gitlab_projects_spec.rb | 6 | ||||
-rw-r--r-- | spec/gitlab_shell_spec.rb | 25 | ||||
-rw-r--r-- | spec/vcr_cassettes/denied-push-with-user.yml | 44 |
8 files changed, 265 insertions, 16 deletions
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 28971ef..6397106 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -53,27 +53,30 @@ class GitlabNet "#{config.gitlab_url}/api/v3/internal" end + def http_client_for(url) + Net::HTTP.new(url.host, url.port).tap do |http| + if URI::HTTPS === url + http.use_ssl = true + http.cert_store = cert_store + http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config.http_settings['self_signed_cert'] + end + end + end + + def http_request_for(url) + user = config.http_settings['user'] + password = config.http_settings['password'] + Net::HTTP::Get.new(url.request_uri).tap { |r| r.basic_auth(user, password) if user && password } + end + def get(url) $logger.debug "Performing GET #{url}" url = URI.parse(url) - http = Net::HTTP.new(url.host, url.port) - - if URI::HTTPS === url - http.use_ssl = true - http.cert_store = cert_store - - if config.http_settings['self_signed_cert'] - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - end - - request = Net::HTTP::Get.new(url.request_uri) - if config.http_settings['user'] && config.http_settings['password'] - request.basic_auth config.http_settings['user'], config.http_settings['password'] - end + http = http_client_for url + request = http_request_for url - http.start {|http| http.request(request) }.tap do |resp| + http.start { |http| http.request(request) }.tap do |resp| if resp.code == "200" $logger.debug { "Received response #{resp.code} => <#{resp.body}>." } else diff --git a/spec/gitlab_config_spec.rb b/spec/gitlab_config_spec.rb new file mode 100644 index 0000000..6c87224 --- /dev/null +++ b/spec/gitlab_config_spec.rb @@ -0,0 +1,65 @@ +require_relative 'spec_helper' +require_relative '../lib/gitlab_config' + +describe GitlabConfig do + let(:config) { GitlabConfig.new } + + describe :redis do + subject { config.redis } + + it { should be_a(Hash) } + it { should have_key('bin') } + it { should have_key('host') } + it { should have_key('port') } + it { should have_key('namespace') } + end + + describe :redis_namespace do + subject { config.redis_namespace } + + it { should eq('resque:gitlab') } + end + + describe :gitlab_url do + subject { config.gitlab_url } + + it { should_not be_empty } + it { should eq('http://localhost/') } + end + + describe :audit_usernames do + subject { config.audit_usernames } + + it("returns false by default") { should eq(false) } + end + + describe :redis_command do + subject { config.redis_command } + + it { should be_an(Array) } + it { should include(config.redis['host']) } + it { should include(config.redis['bin']) } + it { should include(config.redis['port'].to_s) } + + context "with empty redis config" do + before do + config.stub(:redis) { {} } + end + + it { should be_an(Array) } + it { should include('redis-cli') } + end + + context "with redis socket" do + let(:socket) { '/tmp/redis.socket' } + before do + config.stub(:redis) { {'bin' => '', 'socket' => socket } } + end + + it { should be_an(Array) } + it { should include(socket) } + it { should_not include('-p') } + it { should_not include('-h') } + end + end +end diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb index e742712..aaaee15 100644 --- a/spec/gitlab_keys_spec.rb +++ b/spec/gitlab_keys_spec.rb @@ -82,6 +82,14 @@ describe GitlabKeys do end end + describe :stdin do + let(:gitlab_keys) { build_gitlab_keys } + subject { gitlab_keys.send :stdin } + before { $stdin = 1 } + + it { should equal(1) } + end + describe :rm_key do let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } @@ -129,12 +137,24 @@ describe GitlabKeys do gitlab_keys.exec end + it 'batch-add-keys arg should execute batch_add_keys method' do + gitlab_keys = build_gitlab_keys('batch-add-keys') + gitlab_keys.should_receive(:batch_add_keys) + gitlab_keys.exec + end + it 'rm-key arg should execute rm_key method' do gitlab_keys = build_gitlab_keys('rm-key') gitlab_keys.should_receive(:rm_key) gitlab_keys.exec end + it 'clear arg should execute clear method' do + gitlab_keys = build_gitlab_keys('clear') + gitlab_keys.should_receive(:clear) + gitlab_keys.exec + end + it 'should puts message if unknown command arg' do gitlab_keys = build_gitlab_keys('change-key') gitlab_keys.should_receive(:puts).with('not allowed') diff --git a/spec/gitlab_logger_spec.rb b/spec/gitlab_logger_spec.rb new file mode 100644 index 0000000..7d8df76 --- /dev/null +++ b/spec/gitlab_logger_spec.rb @@ -0,0 +1,11 @@ +require_relative 'spec_helper' +require_relative '../lib/gitlab_logger' + +describe :convert_log_level do + subject { convert_log_level :extreme } + + it "converts invalid log level to Logger::INFO" do + $stderr.should_receive(:puts).at_least(:once) + should eq(Logger::INFO) + end +end diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index 0ceec23..1b7ef4d 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -58,6 +58,81 @@ describe GitlabNet, vcr: true do access.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.allowed?('git-upload-pack', 'gitlab/gitlabhq.git', 'user-1', 'master') + access.should be_false + end + end + end + end + + describe :host do + let(:net) { GitlabNet.new } + subject { net.send :host } + + it { should include(net.send(:config).gitlab_url) } + it("uses API version 3") { should include("api/v3") } + end + + describe :http_client_for do + subject { gitlab_net.send :http_client_for, URI('https://localhost/') } + before do + gitlab_net.stub! :cert_store + gitlab_net.send(:config).http_settings.stub(:[]).with('self_signed_cert') { true } + end + + its(:verify_mode) { should eq(OpenSSL::SSL::VERIFY_NONE) } + end + + describe :http_request_for do + let(:get) do + double(Net::HTTP::Get).tap do |get| + Net::HTTP::Get.stub(:new) { get } + end + end + let(:user) { 'user' } + let(:password) { 'password' } + let(:url) { URI 'http://localhost/' } + subject { gitlab_net.send :http_request_for, url } + + before 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 + end + + it { should_not be_nil } + end + + describe :cert_store do + let(:store) do + double(OpenSSL::X509::Store).tap do |store| + OpenSSL::X509::Store.stub(:new) { store } + end + end + + before :each do + store.should_receive(:set_default_paths).once + end + + after do + gitlab_net.send :cert_store + end + + it "calls add_file with http_settings['ca_file']" do + gitlab_net.send(:config).http_settings.stub(:[]).with('ca_file') { 'test_file' } + gitlab_net.send(:config).http_settings.stub(:[]).with('ca_path') { nil } + store.should_receive(:add_file).with('test_file') + store.should_not_receive(:add_path) + end + + it "calls add_path with http_settings['ca_path']" do + gitlab_net.send(:config).http_settings.stub(:[]).with('ca_file') { nil } + gitlab_net.send(:config).http_settings.stub(:[]).with('ca_path') { 'test_path' } + store.should_not_receive(:add_file) + store.should_receive(:add_path).with('test_path') end end end diff --git a/spec/gitlab_projects_spec.rb b/spec/gitlab_projects_spec.rb index 64cb63e..300029c 100644 --- a/spec/gitlab_projects_spec.rb +++ b/spec/gitlab_projects_spec.rb @@ -176,6 +176,7 @@ describe GitlabProjects do describe :update_head do let(:gl_projects) { build_gitlab_projects('update-head', repo_name, 'stable') } + let(:gl_projects_fail) { build_gitlab_projects 'update-head', repo_name } before do FileUtils.mkdir_p(tmp_repo_path) @@ -193,6 +194,11 @@ describe GitlabProjects do $logger.should_receive(:info).with("Update head in project #{repo_name} to <stable>.") gl_projects.exec end + + it "should failed and log an error" do + $logger.should_receive(:error).with("update-head failed: no branch provided.") + gl_projects_fail.exec.should be_false + end end describe :import_project do diff --git a/spec/gitlab_shell_spec.rb b/spec/gitlab_shell_spec.rb index 3243dd3..6a00638 100644 --- a/spec/gitlab_shell_spec.rb +++ b/spec/gitlab_shell_spec.rb @@ -145,6 +145,31 @@ describe GitlabShell do end end + describe :exec_cmd do + let(:shell) { GitlabShell.new } + before { Kernel.stub!(:exec) } + + it "uses Kernel::exec method" do + Kernel.should_receive(:exec).with(kind_of(Hash), 1, unsetenv_others: true).once + shell.send :exec_cmd, 1 + end + end + + describe :api do + let(:shell) { GitlabShell.new } + subject { shell.send :api } + + it { should be_a(GitlabNet) } + end + + describe :escape_path do + let(:shell) { GitlabShell.new } + before { File.stub(:absolute_path) { 'y' } } + subject { -> { shell.send(:escape_path, 'z') } } + + it { should raise_error(RuntimeError, "Wrong repository path") } + end + def ssh_cmd(cmd) ENV['SSH_ORIGINAL_COMMAND'] = cmd end diff --git a/spec/vcr_cassettes/denied-push-with-user.yml b/spec/vcr_cassettes/denied-push-with-user.yml new file mode 100644 index 0000000..c3e5b60 --- /dev/null +++ b/spec/vcr_cassettes/denied-push-with-user.yml @@ -0,0 +1,44 @@ +--- +http_interactions: +- request: + method: get + uri: https://dev.gitlab.org/api/v3/internal/allowed?action=git-upload-pack&forced_push=false&project=gitlab/gitlabhq&ref=master&user_id=1 + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 404 + message: Not Found + headers: + Server: + - nginx/1.1.19 + Date: + - Mon, 14 Apr 2014 18:25:54 GMT + Content-Type: + - application/json + Content-Length: + - '27' + Connection: + - keep-alive + Status: + - 404 Not Found + Cache-Control: + - no-cache + X-Request-Id: + - 2a2a3ef9-aaf1-4ffb-8b18-475d52ec5e09 + X-Runtime: + - '0.013223' + body: + encoding: UTF-8 + string: '{"message":"404 Not found"}' + http_version: + recorded_at: Mon, 14 Apr 2014 18:25:54 GMT +recorded_with: VCR 2.4.0 |