summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2018-08-01 13:46:11 +1000
committerAsh McKenzie <amckenzie@gitlab.com>2018-08-01 15:08:45 +1000
commit5709ea1a3435ca7e1308d2fa54bfbadc61682fc7 (patch)
treebdc20553ab9caea22c065202d8877dbbbe98da54
parent3f421a018e918865a3a36300a4dcefc5d1c8676a (diff)
downloadgitlab-shell-5709ea1a3435ca7e1308d2fa54bfbadc61682fc7.tar.gz
GitlatNet#discover only parse JSON if a 200
-rw-r--r--lib/gitlab_net.rb2
-rw-r--r--spec/gitlab_net_spec.rb8
-rw-r--r--spec/vcr_cassettes/discover-not-found.yml46
3 files changed, 54 insertions, 2 deletions
diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb
index 0a3e383..9ea18aa 100644
--- a/lib/gitlab_net.rb
+++ b/lib/gitlab_net.rb
@@ -35,7 +35,7 @@ class GitlabNet
def discover(actor)
resp = get("#{internal_api_endpoint}/discover?#{actor.identifier_key}=#{actor.id}")
- JSON.parse(resp.body)
+ JSON.parse(resp.body) if resp.code == HTTP_SUCCESS
rescue JSON::ParserError, ApiUnreachableError
nil
end
diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb
index a02744f..8df6fd3 100644
--- a/spec/gitlab_net_spec.rb
+++ b/spec/gitlab_net_spec.rb
@@ -42,7 +42,7 @@ describe GitlabNet, vcr: true do
end
describe '#discover' do
- it 'should return user has based on key id' do
+ it 'returns user has based on key id' do
VCR.use_cassette("discover-ok") do
user = gitlab_net.discover(actor1)
expect(user['name']).to eql 'Administrator'
@@ -50,6 +50,12 @@ describe GitlabNet, vcr: true do
end
end
+ it 'returns nil if the user cannot be found' do
+ VCR.use_cassette("discover-not-found") do
+ expect(gitlab_net.discover(actor1)).to be_nil
+ end
+ end
+
it 'adds the secret_token to request' do
VCR.use_cassette("discover-ok") do
allow_any_instance_of(Net::HTTP::Get).to receive(:set_form_data).with(hash_including(secret_token: secret))
diff --git a/spec/vcr_cassettes/discover-not-found.yml b/spec/vcr_cassettes/discover-not-found.yml
new file mode 100644
index 0000000..db03b02
--- /dev/null
+++ b/spec/vcr_cassettes/discover-not-found.yml
@@ -0,0 +1,46 @@
+---
+http_interactions:
+- request:
+ method: get
+ uri: http://localhost:3000/api/v4/internal/discover?key_id=1
+ body:
+ encoding: US-ASCII
+ string: secret_token=0a3938d9d95d807e94d937af3a4fbbea%0A
+ headers:
+ Accept-Encoding:
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
+ Accept:
+ - "*/*"
+ User-Agent:
+ - Ruby
+ Content-Type:
+ - application/x-www-form-urlencoded
+ response:
+ status:
+ code: 404
+ message: Not Found
+ headers:
+ Cache-Control:
+ - max-age=0, private, must-revalidate
+ Content-Length:
+ - '42'
+ Content-Type:
+ - application/json
+ Date:
+ - Wed, 21 Jun 2017 10:44:49 GMT
+ Etag:
+ - W/"63b4ab301951bea83c4fc398eba8e307"
+ Vary:
+ - Origin
+ X-Frame-Options:
+ - SAMEORIGIN
+ X-Request-Id:
+ - dc11b8d4-1972-417b-8305-2c35c849405c
+ X-Runtime:
+ - '0.230170'
+ body:
+ encoding: UTF-8
+ string: '{"message":"404 Not found"}'
+ http_version:
+ recorded_at: Wed, 21 Jun 2017 10:44:49 GMT
+recorded_with: VCR 2.4.0