11require_relative 'spec_helper'
22
3+ require 'open3'
4+
35describe 'bin/gitlab-shell' do
46 def original_root_path
57 ROOT_PATH
@@ -60,67 +62,66 @@ def tmp_socket_path
6062 shared_examples 'results with keys' do
6163 # Basic valid input
6264 it 'succeeds and prints username when a valid known key id is given' do
63- output , status = run! ( [ "key-100" ] )
65+ output , _ , status = run! ( [ "key-100" ] )
6466
6567 expect ( output ) . to eq ( "Welcome to GitLab, @someuser!\n " )
6668 expect ( status ) . to be_success
6769 end
6870
6971 it 'succeeds and prints username when a valid known username is given' do
70- output , status = run! ( [ "username-someuser" ] )
72+ output , _ , status = run! ( [ "username-someuser" ] )
7173
7274 expect ( output ) . to eq ( "Welcome to GitLab, @someuser!\n " )
7375 expect ( status ) . to be_success
7476 end
7577
7678 # Valid but unknown input
7779 it 'succeeds and prints Anonymous when a valid unknown key id is given' do
78- output , status = run! ( [ "key-12345" ] )
80+ output , _ , status = run! ( [ "key-12345" ] )
7981
8082 expect ( output ) . to eq ( "Welcome to GitLab, Anonymous!\n " )
8183 expect ( status ) . to be_success
8284 end
8385
8486 it 'succeeds and prints Anonymous when a valid unknown username is given' do
85- output , status = run! ( [ "username-unknown" ] )
87+ output , _ , status = run! ( [ "username-unknown" ] )
8688
8789 expect ( output ) . to eq ( "Welcome to GitLab, Anonymous!\n " )
8890 expect ( status ) . to be_success
8991 end
9092
91- # Invalid input. TODO: capture stderr & compare
9293 it 'gets an ArgumentError on invalid input (empty)' do
93- output , status = run! ( [ ] )
94+ _ , stderr , status = run! ( [ ] )
9495
95- expect ( output ) . to eq ( "" )
96+ expect ( stderr ) . to match ( /who='' is invalid/ )
9697 expect ( status ) . not_to be_success
9798 end
9899
99100 it 'gets an ArgumentError on invalid input (unknown)' do
100- output , status = run! ( [ "whatever" ] )
101+ _ , stderr , status = run! ( [ "whatever" ] )
101102
102- expect ( output ) . to eq ( "" )
103+ expect ( stderr ) . to match ( /who='' is invalid/ )
103104 expect ( status ) . not_to be_success
104105 end
105106
106107 it 'gets an ArgumentError on invalid input (multiple unknown)' do
107- output , status = run! ( [ "this" , "is" , "all" , "invalid" ] )
108+ _ , stderr , status = run! ( [ "this" , "is" , "all" , "invalid" ] )
108109
109- expect ( output ) . to eq ( "" )
110+ expect ( stderr ) . to match ( /who='' is invalid/ )
110111 expect ( status ) . not_to be_success
111112 end
112113
113114 # Not so basic valid input
114115 # (https://gitlab.com/gitlab-org/gitlab-shell/issues/145)
115116 it 'succeeds and prints username when a valid known key id is given in the middle of other input' do
116- output , status = run! ( [ "-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell" , "key-100" , "2foo" ] )
117+ output , _ , status = run! ( [ "-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell" , "key-100" , "2foo" ] )
117118
118119 expect ( output ) . to eq ( "Welcome to GitLab, @someuser!\n " )
119120 expect ( status ) . to be_success
120121 end
121122
122123 it 'succeeds and prints username when a valid known username is given in the middle of other input' do
123- output , status = run! ( [ "-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell" , "username-someuser" , "foo" ] )
124+ output , _ , status = run! ( [ "-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell" , "username-someuser" , "foo" ] )
124125
125126 expect ( output ) . to eq ( "Welcome to GitLab, @someuser!\n " )
126127 expect ( status ) . to be_success
@@ -144,24 +145,27 @@ def tmp_socket_path
144145 )
145146 end
146147
147-
148-
149148 it_behaves_like 'results with keys' do
150149 before do
151150 pending
152151 end
153152 end
153+
154+ it 'outputs "Only ssh allowed"' do
155+ _ , stderr , status = run! ( [ "-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell" , "username-someuser" ] , env : { } )
156+
157+ expect ( stderr ) . to eq ( "Only ssh allowed\n " )
158+ expect ( status ) . not_to be_success
159+ end
154160 end
155161
156- def run! ( args )
162+ def run! ( args , env : { 'SSH_CONNECTION' => 'fake' } )
157163 cmd = [
158164 gitlab_shell_path ,
159165 args
160- ] . flatten . compact
161-
162- output = IO . popen ( { 'SSH_CONNECTION' => 'fake' } , cmd , &:read )
166+ ] . flatten . compact . join ( ' ' )
163167
164- [ output , $? ]
168+ Open3 . capture3 ( env , cmd )
165169 end
166170
167171 def write_config ( config )
0 commit comments