2019-12-11 13:08:10 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe KeysFinder do
|
2020-02-10 04:08:56 -05:00
|
|
|
subject { described_class.new(params).execute }
|
2019-12-11 13:08:10 -05:00
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
2019-12-17 04:07:48 -05:00
|
|
|
let(:params) { {} }
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
let!(:key_1) do
|
|
|
|
create(:personal_key,
|
|
|
|
last_used_at: 7.days.ago,
|
|
|
|
user: user,
|
2019-12-11 13:08:10 -05:00
|
|
|
key: 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1016k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
|
|
|
|
fingerprint: 'ba:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d1',
|
2019-12-17 04:07:48 -05:00
|
|
|
fingerprint_sha256: 'nUhzNyftwADy8AH3wFY31tAKs7HufskYTte2aXo/lCg')
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
let!(:key_2) { create(:personal_key, last_used_at: nil, user: user) }
|
|
|
|
let!(:key_3) { create(:personal_key, last_used_at: 2.days.ago) }
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'key_type' do
|
|
|
|
let!(:deploy_key) { create(:deploy_key) }
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'when `key_type` is `ssh`' do
|
|
|
|
before do
|
|
|
|
params[:key_type] = 'ssh'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only SSH keys' do
|
|
|
|
expect(subject).to contain_exactly(key_1, key_2, key_3)
|
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'when `key_type` is not specified' do
|
|
|
|
it 'returns all types of keys' do
|
|
|
|
expect(subject).to contain_exactly(key_1, key_2, key_3, deploy_key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'fingerprint' do
|
|
|
|
context 'with invalid fingerprint' do
|
|
|
|
context 'with invalid MD5 fingerprint' do
|
2019-12-17 04:07:48 -05:00
|
|
|
before do
|
2020-02-10 04:08:56 -05:00
|
|
|
params[:fingerprint] = '11:11:11:11'
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'raises InvalidFingerprint' do
|
|
|
|
expect { subject }.to raise_error(KeysFinder::InvalidFingerprint)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'with invalid SHA fingerprint' do
|
|
|
|
before do
|
|
|
|
params[:fingerprint] = 'nUhzNyftwAAKs7HufskYTte2g'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises InvalidFingerprint' do
|
|
|
|
expect { subject }.to raise_error(KeysFinder::InvalidFingerprint)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'with valid fingerprints' do
|
|
|
|
let!(:deploy_key) do
|
|
|
|
create(:deploy_key,
|
|
|
|
user: user,
|
|
|
|
key: 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1017k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
|
|
|
|
fingerprint: '8a:4a:12:92:0b:50:47:02:d4:5a:8e:a9:44:4e:08:b4',
|
|
|
|
fingerprint_sha256: '4DPHOVNh53i9dHb5PpY2vjfyf5qniTx1/pBFPoZLDdk')
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'personal key with valid MD5 params' do
|
|
|
|
context 'with an existent fingerprint' do
|
2019-12-17 04:07:48 -05:00
|
|
|
before do
|
2020-02-10 04:08:56 -05:00
|
|
|
params[:fingerprint] = 'ba:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d1'
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns the key' do
|
|
|
|
expect(subject).to eq(key_1)
|
|
|
|
expect(subject.user).to eq(user)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'deploy key with an existent fingerprint' do
|
2019-12-17 04:07:48 -05:00
|
|
|
before do
|
2020-02-10 04:08:56 -05:00
|
|
|
params[:fingerprint] = '8a:4a:12:92:0b:50:47:02:d4:5a:8e:a9:44:4e:08:b4'
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns the key' do
|
|
|
|
expect(subject).to eq(deploy_key)
|
|
|
|
expect(subject.user).to eq(user)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'with a non-existent fingerprint' do
|
|
|
|
before do
|
|
|
|
params[:fingerprint] = 'bb:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d2'
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be_nil
|
2020-01-13 19:08:14 -05:00
|
|
|
end
|
2020-02-10 04:08:56 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-13 19:08:14 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'personal key with valid SHA256 params' do
|
|
|
|
context 'with an existent fingerprint' do
|
|
|
|
before do
|
|
|
|
params[:fingerprint] = 'SHA256:nUhzNyftwADy8AH3wFY31tAKs7HufskYTte2aXo/lCg'
|
|
|
|
end
|
2019-12-17 04:07:48 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns key' do
|
|
|
|
expect(subject).to eq(key_1)
|
|
|
|
expect(subject.user).to eq(user)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'deploy key with an existent fingerprint' do
|
|
|
|
before do
|
|
|
|
params[:fingerprint] = 'SHA256:4DPHOVNh53i9dHb5PpY2vjfyf5qniTx1/pBFPoZLDdk'
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns key' do
|
|
|
|
expect(subject).to eq(deploy_key)
|
|
|
|
expect(subject.user).to eq(user)
|
2020-01-13 19:08:14 -05:00
|
|
|
end
|
2020-02-10 04:08:56 -05:00
|
|
|
end
|
2020-01-13 19:08:14 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'with a non-existent fingerprint' do
|
|
|
|
before do
|
|
|
|
params[:fingerprint] = 'SHA256:xTjuFqftwADy8AH3wFY31tAKs7HufskYTte2aXi/mNp'
|
|
|
|
end
|
2019-12-17 04:07:48 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'returns nil' do
|
|
|
|
expect(subject).to be_nil
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-10 04:08:56 -05:00
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'user' do
|
|
|
|
context 'without user' do
|
|
|
|
it 'contains ssh_keys of all users in the system' do
|
|
|
|
expect(subject).to contain_exactly(key_1, key_2, key_3)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
2020-02-10 04:08:56 -05:00
|
|
|
end
|
2019-12-17 04:07:48 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'with user' do
|
|
|
|
before do
|
|
|
|
params[:users] = user
|
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
it 'contains ssh_keys of only the specified users' do
|
|
|
|
expect(subject).to contain_exactly(key_1, key_2)
|
2019-12-17 04:07:48 -05:00
|
|
|
end
|
|
|
|
end
|
2020-02-10 04:08:56 -05:00
|
|
|
end
|
2019-12-11 13:08:10 -05:00
|
|
|
|
2020-02-10 04:08:56 -05:00
|
|
|
context 'sort order' do
|
|
|
|
it 'sorts in last_used_at_desc order' do
|
|
|
|
expect(subject).to eq([key_3, key_1, key_2])
|
2019-12-11 13:08:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|