Commit 80422c66 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #158 from Aigeruth/improvement/coverage

Improve coverage.
parents ca425566 32f18932
...@@ -53,27 +53,30 @@ class GitlabNet ...@@ -53,27 +53,30 @@ class GitlabNet
"#{config.gitlab_url}/api/v3/internal" "#{config.gitlab_url}/api/v3/internal"
end 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) def get(url)
$logger.debug "Performing GET #{url}" $logger.debug "Performing GET #{url}"
url = URI.parse(url) url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port) http = http_client_for url
request = http_request_for url
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.start {|http| http.request(request) }.tap do |resp| http.start { |http| http.request(request) }.tap do |resp|
if resp.code == "200" if resp.code == "200"
$logger.debug { "Received response #{resp.code} => <#{resp.body}>." } $logger.debug { "Received response #{resp.code} => <#{resp.body}>." }
else else
......
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
...@@ -82,6 +82,14 @@ describe GitlabKeys do ...@@ -82,6 +82,14 @@ describe GitlabKeys do
end end
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 describe :rm_key do
let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
...@@ -129,12 +137,24 @@ describe GitlabKeys do ...@@ -129,12 +137,24 @@ describe GitlabKeys do
gitlab_keys.exec gitlab_keys.exec
end 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 it 'rm-key arg should execute rm_key method' do
gitlab_keys = build_gitlab_keys('rm-key') gitlab_keys = build_gitlab_keys('rm-key')
gitlab_keys.should_receive(:rm_key) gitlab_keys.should_receive(:rm_key)
gitlab_keys.exec gitlab_keys.exec
end 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 it 'should puts message if unknown command arg' do
gitlab_keys = build_gitlab_keys('change-key') gitlab_keys = build_gitlab_keys('change-key')
gitlab_keys.should_receive(:puts).with('not allowed') gitlab_keys.should_receive(:puts).with('not allowed')
......
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
...@@ -58,6 +58,81 @@ describe GitlabNet, vcr: true do ...@@ -58,6 +58,81 @@ describe GitlabNet, vcr: true do
access.should be_false access.should be_false
end end
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 end
end end
...@@ -176,6 +176,7 @@ describe GitlabProjects do ...@@ -176,6 +176,7 @@ describe GitlabProjects do
describe :update_head do describe :update_head do
let(:gl_projects) { build_gitlab_projects('update-head', repo_name, 'stable') } let(:gl_projects) { build_gitlab_projects('update-head', repo_name, 'stable') }
let(:gl_projects_fail) { build_gitlab_projects 'update-head', repo_name }
before do before do
FileUtils.mkdir_p(tmp_repo_path) FileUtils.mkdir_p(tmp_repo_path)
...@@ -193,6 +194,11 @@ describe GitlabProjects do ...@@ -193,6 +194,11 @@ describe GitlabProjects do
$logger.should_receive(:info).with("Update head in project #{repo_name} to <stable>.") $logger.should_receive(:info).with("Update head in project #{repo_name} to <stable>.")
gl_projects.exec gl_projects.exec
end 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 end
describe :import_project do describe :import_project do
......
...@@ -145,6 +145,31 @@ describe GitlabShell do ...@@ -145,6 +145,31 @@ describe GitlabShell do
end end
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) def ssh_cmd(cmd)
ENV['SSH_ORIGINAL_COMMAND'] = cmd ENV['SSH_ORIGINAL_COMMAND'] = cmd
end end
......
---
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment