summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-02-12 03:28:01 +0000
committerDmitriy Zaporozhets <dzaporozhets@gitlab.com>2015-02-12 03:28:01 +0000
commit63e16172e524d83e4fe04945e277bc21dda6d777 (patch)
tree032eb81a84675799500b5ecb54789fea90f3fcb6 /spec
parentf11e1bf914854e53217ece1ec6f8947f2894a1c7 (diff)
parent562d7eb4ecaa9ca35f970567c0f09cdb29d26521 (diff)
downloadgitlab-shell-63e16172e524d83e4fe04945e277bc21dda6d777.tar.gz
Merge branch 'internal-api-unavailable' into 'master'
Show nice error message when internal API is unreachable. Closes #27. Error message reads "Failed to authorize your Git request: internal API unreachable". See merge request !54
Diffstat (limited to 'spec')
-rw-r--r--spec/gitlab_access_spec.rb49
-rw-r--r--spec/gitlab_net_spec.rb22
-rw-r--r--spec/gitlab_shell_spec.rb21
3 files changed, 87 insertions, 5 deletions
diff --git a/spec/gitlab_access_spec.rb b/spec/gitlab_access_spec.rb
index 13db4bd..4768c71 100644
--- a/spec/gitlab_access_spec.rb
+++ b/spec/gitlab_access_spec.rb
@@ -5,15 +5,56 @@ 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(:gitlab_access) { GitlabAccess.new(repo_path, 'key-123', 'wow') }
+ let(:api) do
+ double(GitlabNet).tap do |api|
+ api.stub(check_access: GitAccessStatus.new(true))
+ end
+ end
+ subject do
+ GitlabAccess.new(repo_path, 'key-123', 'wow').tap do |access|
+ access.stub(exec_cmd: :exec_called)
+ access.stub(api: api)
+ end
+ end
before do
GitlabConfig.any_instance.stub(repos_path: repository_path)
end
describe :initialize do
- it { gitlab_access.repo_name.should == repo_name }
- it { gitlab_access.repo_path.should == repo_path }
- it { gitlab_access.changes.should == ['wow'] }
+ it { subject.repo_name.should == repo_name }
+ it { subject.repo_path.should == repo_path }
+ it { subject.changes.should == ['wow'] }
+ end
+
+ describe "#exec" do
+ context "access is granted" do
+
+ it "returns true" do
+ expect(subject.exec).to be_true
+ end
+ end
+
+ context "access is denied" do
+
+ before do
+ api.stub(check_access: GitAccessStatus.new(false))
+ end
+
+ it "returns false" do
+ expect(subject.exec).to be_false
+ end
+ end
+
+ context "API connection fails" do
+
+ before do
+ api.stub(:check_access).and_raise(GitlabNet::ApiUnreachableError)
+ end
+
+ it "returns false" do
+ expect(subject.exec).to be_false
+ end
+ end
end
end
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index 5e6e4de..17fb56d 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -26,6 +26,11 @@ describe GitlabNet, vcr: true do
gitlab_net.check
end
end
+
+ it "raises an exception if the connection fails" do
+ Net::HTTP.any_instance.stub(:request).and_raise(StandardError)
+ expect { gitlab_net.check }.to raise_error(GitlabNet::ApiUnreachableError)
+ end
end
describe :discover do
@@ -42,6 +47,13 @@ describe GitlabNet, vcr: true do
gitlab_net.discover('key-126')
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)
+ end
+ end
end
describe :broadcast_message do
@@ -110,6 +122,13 @@ describe GitlabNet, vcr: true do
end
end
end
+
+ 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', 'gitlab/gitlabhq.git', 'user-1', changes)
+ }.to raise_error(GitlabNet::ApiUnreachableError)
+ end
end
describe :host do
@@ -139,12 +158,13 @@ describe GitlabNet, vcr: true do
let(:user) { 'user' }
let(:password) { 'password' }
let(:url) { URI 'http://localhost/' }
- subject { gitlab_net.send :http_request_for, url }
+ subject { gitlab_net.send :http_request_for, :get, 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
+ get.should_receive(:set_form_data).with(hash_including(secret_token: 'a123')).once
end
it { should_not be_nil }
diff --git a/spec/gitlab_shell_spec.rb b/spec/gitlab_shell_spec.rb
index 5df2391..4ca7984 100644
--- a/spec/gitlab_shell_spec.rb
+++ b/spec/gitlab_shell_spec.rb
@@ -135,6 +135,27 @@ describe GitlabShell do
api.should_receive(:discover).with(key_id)
end
end
+
+ context "failed connection" do
+ before {
+ ssh_cmd 'git-upload-pack gitlab-ci.git'
+ api.stub(:check_access).and_raise(GitlabNet::ApiUnreachableError)
+ }
+ after { subject.exec }
+
+ it "should not process the command" do
+ subject.should_not_receive(:process_cmd)
+ end
+
+ it "should not execute the command" do
+ subject.should_not_receive(:exec_cmd)
+ end
+
+ it "should log the failed connection" do
+ message = "gitlab-shell: Failed to connect to internal API"
+ $logger.should_receive(:warn).with(message)
+ end
+ end
end
describe :validate_access do