diff options
author | Imre Farkas <ifarkas@gitlab.com> | 2020-12-01 14:46:27 +0100 |
---|---|---|
committer | Imre Farkas <ifarkas@gitlab.com> | 2020-12-10 15:23:44 +0100 |
commit | 1293a33014c9cfc82b0bc1b9525987476b2aa857 (patch) | |
tree | c4d4057fcdc5753086493c71b10f9ea5ee7b84d5 /spec | |
parent | 384f3036e3d9c501e29a7ce24ece1e887a14d53a (diff) | |
download | gitlab-shell-1293a33014c9cfc82b0bc1b9525987476b2aa857.tar.gz |
Add 2fa_verify command
Diffstat (limited to 'spec')
-rw-r--r-- | spec/gitlab_shell_two_factor_verify_spec.rb | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/spec/gitlab_shell_two_factor_verify_spec.rb b/spec/gitlab_shell_two_factor_verify_spec.rb new file mode 100644 index 0000000..a9ce40d --- /dev/null +++ b/spec/gitlab_shell_two_factor_verify_spec.rb @@ -0,0 +1,81 @@ +require_relative 'spec_helper' + +require 'open3' +require 'json' + +describe 'bin/gitlab-shell 2fa_verify' do + include_context 'gitlab shell' + + let(:env) do + { 'SSH_CONNECTION' => 'fake', + 'SSH_ORIGINAL_COMMAND' => '2fa_verify' } + end + + before(:context) do + write_config('gitlab_url' => "http+unix://#{CGI.escape(tmp_socket_path)}") + end + + def mock_server(server) + server.mount_proc('/api/v4/internal/two_factor_otp_check') do |req, res| + res.content_type = 'application/json' + res.status = 200 + + params = JSON.parse(req.body) + key_id = params['key_id'] || params['user_id'].to_s + + if key_id == '100' + res.body = { success: true }.to_json + else + res.body = { success: false, message: 'boom!' }.to_json + end + end + + server.mount_proc('/api/v4/internal/discover') do |_, res| + res.status = 200 + res.content_type = 'application/json' + res.body = { id: 100, name: 'Some User', username: 'someuser' }.to_json + end + end + + describe 'command' do + context 'when key is provided' do + let(:cmd) { "#{gitlab_shell_path} key-100" } + + it 'prints a successful verification message' do + verify_successful_verification!(cmd) + end + end + + context 'when username is provided' do + let(:cmd) { "#{gitlab_shell_path} username-someone" } + + it 'prints a successful verification message' do + verify_successful_verification!(cmd) + end + end + + context 'when API error occurs' do + let(:cmd) { "#{gitlab_shell_path} key-101" } + + it 'prints the error message' do + Open3.popen2(env, cmd) do |stdin, stdout| + expect(stdout.gets(5)).to eq('OTP: ') + + stdin.puts('123456') + + expect(stdout.flush.read).to eq("\nOTP validation failed.\nboom!\n") + end + end + end + end + + def verify_successful_verification!(cmd) + Open3.popen2(env, cmd) do |stdin, stdout| + expect(stdout.gets(5)).to eq('OTP: ') + + stdin.puts('123456') + + expect(stdout.flush.read).to eq("\nOTP validation successful. Git operations are allowed for the next 15 minutes.\n") + end + end +end |