summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorAsh McKenzie <amckenzie@gitlab.com>2018-07-26 17:49:49 +1000
committerAsh McKenzie <amckenzie@gitlab.com>2018-08-01 00:24:10 +1000
commitaa4d2ba8c109948a13f58787a269205be1abd11d (patch)
tree1647c9017c3148d64cd491e5a56444f08c3b1040 /spec
parent28ff59405111209bbf5cd6cb59b4ffd648922a74 (diff)
downloadgitlab-shell-aa4d2ba8c109948a13f58787a269205be1abd11d.tar.gz
New Action classes
* Base - contains all common logic * Gitaly - performs interactions with Gitaly * API2FARecovery - 2FA recovery code generation * GitLFSAuthenticate - git-lfs authentication
Diffstat (limited to 'spec')
-rw-r--r--spec/action/api_2fa_recovery.rb_spec.rb73
-rw-r--r--spec/action/base_spec.rb12
-rw-r--r--spec/action/git_lfs_authenticate_spec.rb47
-rw-r--r--spec/action/gitaly_spec.rb133
4 files changed, 265 insertions, 0 deletions
diff --git a/spec/action/api_2fa_recovery.rb_spec.rb b/spec/action/api_2fa_recovery.rb_spec.rb
new file mode 100644
index 0000000..1f5219a
--- /dev/null
+++ b/spec/action/api_2fa_recovery.rb_spec.rb
@@ -0,0 +1,73 @@
+require_relative '../spec_helper'
+require_relative '../../lib/action/api_2fa_recovery'
+
+describe Action::API2FARecovery do
+ let(:key_id) { "key-#{rand(100) + 100}" }
+ let(:key) { Actor::Key.new(key_id) }
+ let(:username) { 'testuser' }
+ let(:discover_payload) { { 'username' => username } }
+ let(:api) { double(GitlabNet) }
+
+ before do
+ allow(GitlabNet).to receive(:new).and_return(api)
+ allow(api).to receive(:discover).with(key_id).and_return(discover_payload)
+ end
+
+ subject do
+ described_class.new(key_id)
+ end
+
+ describe '#execute' do
+ context 'with an invalid repsonse' do
+ it 'returns nil' do
+ expect($stdin).to receive(:gets).and_return("meh\n")
+
+ expect do
+ expect(subject.execute(nil, nil)).to be_nil
+ end.to output(/New recovery codes have \*not\* been generated/).to_stdout
+ end
+ end
+
+ context 'with a negative response' do
+ before do
+ expect(subject).to receive(:continue?).and_return(false)
+ end
+
+ it 'returns nil' do
+ expect do
+ expect(subject.execute(nil, nil)).to be_nil
+ end.to output(/New recovery codes have \*not\* been generated/).to_stdout
+ end
+ end
+
+
+ context 'with an affirmative response' do
+ let(:recovery_codes) { %w{ 8dfe0f433208f40b289904c6072e4a72 c33cee7fd0a73edb56e61b785e49af03 } }
+
+ before do
+ expect(subject).to receive(:continue?).and_return(true)
+ expect(api).to receive(:two_factor_recovery_codes).with(key_id).and_return(response)
+ end
+
+ context 'with a unsuccessful response' do
+ let(:response) { { 'success' => false } }
+
+ it 'puts error message to stdout' do
+ expect do
+ expect(subject.execute(nil, nil)).to be_falsey
+ end.to output(/An error occurred while trying to generate new recovery codes/).to_stdout
+ end
+ end
+
+ context 'with a successful response' do
+ let(:response) { { 'success' => true, 'recovery_codes' => recovery_codes } }
+
+ it 'puts information message including recovery codes to stdout' do
+ expect do
+ expect(subject.execute(nil, nil)).to be_truthy
+ end.to output(Regexp.new(recovery_codes.join("\n"))).to_stdout
+ end
+ end
+ end
+ end
+end
diff --git a/spec/action/base_spec.rb b/spec/action/base_spec.rb
new file mode 100644
index 0000000..e986378
--- /dev/null
+++ b/spec/action/base_spec.rb
@@ -0,0 +1,12 @@
+require_relative '../spec_helper'
+require_relative '../../lib/action/base'
+
+describe Action::Base do
+ describe '.create_from_json' do
+ it 'raises a NotImplementedError exeption' do
+ expect do
+ described_class.create_from_json('nomatter')
+ end.to raise_error(NotImplementedError)
+ end
+ end
+end
diff --git a/spec/action/git_lfs_authenticate_spec.rb b/spec/action/git_lfs_authenticate_spec.rb
new file mode 100644
index 0000000..f9a0791
--- /dev/null
+++ b/spec/action/git_lfs_authenticate_spec.rb
@@ -0,0 +1,47 @@
+require_relative '../spec_helper'
+require_relative '../../lib/action/git_lfs_authenticate'
+
+describe Action::GitLFSAuthenticate do
+ let(:key_id) { "key-#{rand(100) + 100}" }
+ let(:repo_name) { 'gitlab-ci.git' }
+ let(:username) { 'testuser' }
+ let(:discover_payload) { { 'username' => username } }
+ let(:api) { double(GitlabNet) }
+
+ before do
+ allow(GitlabNet).to receive(:new).and_return(api)
+ allow(api).to receive(:discover).with(key_id).and_return(discover_payload)
+ end
+
+ subject do
+ described_class.new(key_id, repo_name)
+ end
+
+ describe '#execute' do
+ context 'when response from API is not a success' do
+ before do
+ expect(api).to receive(:lfs_authenticate).with(key_id, repo_name).and_return(nil)
+ end
+
+ it 'returns nil' do
+ expect(subject.execute(nil, nil)).to be_nil
+ end
+ end
+
+ context 'when response from API is a success' do
+ let(:username) { 'testuser' }
+ let(:lfs_token) { '1234' }
+ let(:repository_http_path) { "/tmp/#{repo_name}" }
+ let(:gitlab_lfs_authentication) { GitlabLfsAuthentication.new(username, lfs_token, repository_http_path) }
+
+ before do
+ expect(api).to receive(:lfs_authenticate).with(key_id, repo_name).and_return(gitlab_lfs_authentication)
+ end
+
+ it 'puts payload to stdout' do
+ expect($stdout).to receive(:puts).with('{"header":{"Authorization":"Basic dGVzdHVzZXI6MTIzNA=="},"href":"/tmp/gitlab-ci.git/info/lfs/"}')
+ expect(subject.execute(nil, nil)).to be_truthy
+ end
+ end
+ end
+end
diff --git a/spec/action/gitaly_spec.rb b/spec/action/gitaly_spec.rb
new file mode 100644
index 0000000..9c35b49
--- /dev/null
+++ b/spec/action/gitaly_spec.rb
@@ -0,0 +1,133 @@
+require_relative '../spec_helper'
+require_relative '../../lib/action/gitaly'
+
+describe Action::Gitaly do
+ let(:git_trace_log_file_valid) { '/tmp/git_trace_performance.log' }
+ let(:git_trace_log_file_invalid) { "/bleep-bop#{git_trace_log_file_valid}" }
+ let(:git_trace_log_file_relative) { "..#{git_trace_log_file_valid}" }
+ let(:key_id) { "key-#{rand(100) + 100}" }
+ let(:gl_repository) { 'project-1' }
+ let(:gl_username) { 'testuser' }
+ let(:tmp_repos_path) { File.join(ROOT_PATH, 'tmp', 'repositories') }
+ let(:repo_name) { 'gitlab-ci.git' }
+ let(:repository_path) { File.join(tmp_repos_path, repo_name) }
+ let(:gitaly_address) { 'unix:gitaly.socket' }
+ let(:gitaly_token) { '123456' }
+ let(:gitaly) do
+ {
+ 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' },
+ 'address' => gitaly_address,
+ 'token' => gitaly_token
+ }
+ end
+
+ describe '.create_from_json' do
+ it 'returns an instance of Action::Gitaly' do
+ json = {
+ "gl_repository" => gl_repository,
+ "gl_username" => gl_username,
+ "repository_path" => repository_path,
+ "gitaly" => gitaly
+ }
+ expect(described_class.create_from_json(key_id, json)).to be_instance_of(Action::Gitaly)
+ end
+ end
+
+ subject do
+ described_class.new(key_id, gl_repository, gl_username, repository_path, gitaly)
+ end
+
+ describe '#execute' do
+ let(:args) { [ repository_path ] }
+ let(:base_exec_env) do
+ {
+ 'HOME' => ENV['HOME'],
+ 'PATH' => ENV['PATH'],
+ 'LD_LIBRARY_PATH' => ENV['LD_LIBRARY_PATH'],
+ 'LANG' => ENV['LANG'],
+ 'GL_ID' => key_id,
+ 'GL_PROTOCOL' => GitlabNet::GL_PROTOCOL,
+ 'GL_REPOSITORY' => gl_repository,
+ 'GL_USERNAME' => gl_username,
+ 'GITALY_TOKEN' => gitaly_token,
+ }
+ end
+ let(:with_trace_exec_env) do
+ base_exec_env.merge({
+ 'GIT_TRACE' => git_trace_log_file,
+ 'GIT_TRACE_PACKET' => git_trace_log_file,
+ 'GIT_TRACE_PERFORMANCE' => git_trace_log_file
+ })
+ end
+ let(:gitaly_request) do
+ {
+ 'repository' => gitaly['repository'],
+ 'gl_repository' => gl_repository,
+ 'gl_id' => key_id,
+ 'gl_username' => gl_username
+ }
+ end
+
+ context 'for migrated commands' do
+ context 'such as git-upload-pack' do
+ let(:git_trace_log_file) { nil }
+ let(:command) { 'git-upload-pack' }
+
+ before do
+ allow_any_instance_of(GitlabConfig).to receive(:git_trace_log_file).and_return(git_trace_log_file)
+ end
+
+ context 'with an invalid config.git_trace_log_file' do
+ let(:git_trace_log_file) { git_trace_log_file_invalid }
+
+ it 'returns true' do
+ expect(Kernel).to receive(:exec).with(
+ base_exec_env,
+ described_class::MIGRATED_COMMANDS[command],
+ gitaly_address,
+ JSON.dump(gitaly_request),
+ unsetenv_others: true,
+ chdir: ROOT_PATH
+ ).and_return(true)
+
+ expect(subject.execute(command, args)).to be_truthy
+ end
+ end
+
+ context 'with an relative config.git_trace_log_file' do
+ let(:git_trace_log_file) { git_trace_log_file_relative }
+
+ it 'returns true' do
+ expect(Kernel).to receive(:exec).with(
+ base_exec_env,
+ described_class::MIGRATED_COMMANDS[command],
+ gitaly_address,
+ JSON.dump(gitaly_request),
+ unsetenv_others: true,
+ chdir: ROOT_PATH
+ ).and_return(true)
+
+ expect(subject.execute(command, args)).to be_truthy
+ end
+ end
+
+ context 'with a valid config.git_trace_log_file' do
+ let(:git_trace_log_file) { git_trace_log_file_valid }
+
+ it 'returns true' do
+ expect(Kernel).to receive(:exec).with(
+ with_trace_exec_env,
+ described_class::MIGRATED_COMMANDS[command],
+ gitaly_address,
+ JSON.dump(gitaly_request),
+ unsetenv_others: true,
+ chdir: ROOT_PATH
+ ).and_return(true)
+
+ expect(subject.execute(command, args)).to be_truthy
+ end
+ end
+ end
+ end
+ end
+end