summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2019-03-07 10:58:37 +0100
committerBob Van Landuyt <bob@vanlanduyt.co>2019-03-14 12:18:07 +0100
commit53511f3655a5eed9976164fbd88d14df3490000c (patch)
treeb46d8d7ccee0d21f9d5e8df3af02b6f37db5852d /spec
parent049beb74303a03d9fa598d23b150e0ccea3cd60d (diff)
downloadgitlab-shell-53511f3655a5eed9976164fbd88d14df3490000c.tar.gz
Detect user based on key, username or id
This allows gitlab-shell to be called with an argument of the format `key-123` or `username-name`. When called in this way, `gitlab-shell` will call the GitLab internal API. If the API responds with user information, it will print a welcome message including the username. If the API responds with a successful but empty response, gitlab-shell will print a welcome message for an anonymous user. If the API response includes an error message in JSON, this message will be printed to stderr. If the API call fails, an error message including the status code will be printed to stderr.
Diffstat (limited to 'spec')
-rw-r--r--spec/gitlab_shell_gitlab_shell_spec.rb33
1 files changed, 25 insertions, 8 deletions
diff --git a/spec/gitlab_shell_gitlab_shell_spec.rb b/spec/gitlab_shell_gitlab_shell_spec.rb
index 11692d3..cb3fd9c 100644
--- a/spec/gitlab_shell_gitlab_shell_spec.rb
+++ b/spec/gitlab_shell_gitlab_shell_spec.rb
@@ -30,12 +30,19 @@ describe 'bin/gitlab-shell' do
@server = HTTPUNIXServer.new(BindAddress: tmp_socket_path)
@server.mount_proc('/api/v4/internal/discover') do |req, res|
- if req.query['key_id'] == '100' ||
- req.query['user_id'] == '10' ||
- req.query['username'] == 'someuser'
+ identifier = req.query['key_id'] || req.query['username'] || req.query['user_id']
+ known_identifiers = %w(10 someuser 100)
+ if known_identifiers.include?(identifier)
res.status = 200
res.content_type = 'application/json'
res.body = '{"id":1, "name": "Some User", "username": "someuser"}'
+ elsif identifier == 'broken_message'
+ res.status = 401
+ res.body = '{"message": "Forbidden!"}'
+ elsif identifier && identifier != 'broken'
+ res.status = 200
+ res.content_type = 'application/json'
+ res.body = 'null'
else
res.status = 500
end
@@ -145,11 +152,7 @@ describe 'bin/gitlab-shell' do
)
end
- it_behaves_like 'results with keys' do
- before do
- pending
- end
- end
+ it_behaves_like 'results with keys'
it 'outputs "Only ssh allowed"' do
_, stderr, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser"], env: {})
@@ -157,6 +160,20 @@ describe 'bin/gitlab-shell' do
expect(stderr).to eq("Only ssh allowed\n")
expect(status).not_to be_success
end
+
+ it 'returns an error message when the API call fails with a message' do
+ _, stderr, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-broken_message"])
+
+ expect(stderr).to match(/Failed to get username: Forbidden!/)
+ expect(status).not_to be_success
+ end
+
+ it 'returns an error message when the API call fails without a message' do
+ _, stderr, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-broken"])
+
+ expect(stderr).to match(/Failed to get username: Internal API error \(500\)/)
+ expect(status).not_to be_success
+ end
end
def run!(args, env: {'SSH_CONNECTION' => 'fake'})