From 324ff19571cada7e148c53bb70e70f823eff4335 Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Wed, 24 Oct 2018 16:03:00 +0100 Subject: Backport SSH host key detection code to CE This functionality is needed for SSH push mirroring support, which is a CE feature. --- .../projects/mirrors_controller_spec.rb | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'spec/controllers/projects/mirrors_controller_spec.rb') diff --git a/spec/controllers/projects/mirrors_controller_spec.rb b/spec/controllers/projects/mirrors_controller_spec.rb index 6114eef7003..00c1e617e3a 100644 --- a/spec/controllers/projects/mirrors_controller_spec.rb +++ b/spec/controllers/projects/mirrors_controller_spec.rb @@ -63,6 +63,69 @@ describe Projects::MirrorsController do end end + describe '#ssh_host_keys', :use_clean_rails_memory_store_caching do + let(:project) { create(:project) } + let(:cache) { SshHostKey.new(project: project, url: "ssh://example.com:22") } + + before do + sign_in(project.owner) + end + + context 'invalid URLs' do + %w[ + INVALID + git@example.com:foo/bar.git + ssh://git@example.com:foo/bar.git + ].each do |url| + it "returns an error with a 400 response for URL #{url.inspect}" do + do_get(project, url) + + expect(response).to have_gitlab_http_status(400) + expect(json_response).to eq('message' => 'Invalid URL') + end + end + end + + context 'no data in cache' do + it 'requests the cache to be filled and returns a 204 response' do + expect(ReactiveCachingWorker).to receive(:perform_async).with(cache.class, cache.id).at_least(:once) + + do_get(project) + + expect(response).to have_gitlab_http_status(204) + end + end + + context 'error in the cache' do + it 'returns the error with a 400 response' do + stub_reactive_cache(cache, error: 'An error') + + do_get(project) + + expect(response).to have_gitlab_http_status(400) + expect(json_response).to eq('message' => 'An error') + end + end + + context 'data in the cache' do + let(:ssh_key) { 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAfuCHKVTjquxvt6CM6tdG4SLp1Btn/nOeHHE5UOzRdf' } + let(:ssh_fp) { { type: 'ed25519', bits: 256, fingerprint: '2e:65:6a:c8:cf:bf:b2:8b:9a:bd:6d:9f:11:5c:12:16', index: 0 } } + + it 'returns the data with a 200 response' do + stub_reactive_cache(cache, known_hosts: ssh_key) + + do_get(project) + + expect(response).to have_gitlab_http_status(200) + expect(json_response).to eq('known_hosts' => ssh_key, 'fingerprints' => [ssh_fp.stringify_keys], 'host_keys_changed' => true) + end + end + + def do_get(project, url = 'ssh://example.com') + get :ssh_host_keys, namespace_id: project.namespace, project_id: project, ssh_url: url + end + end + def do_put(project, options, extra_attrs = {}) attrs = extra_attrs.merge(namespace_id: project.namespace.to_param, project_id: project.to_param) attrs[:project] = options -- cgit v1.2.3