PageRenderTime 38ms CodeModel.GetById 10ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/spec/finders/keys_finder_spec.rb

https://gitlab.com/tnir/gitlab-ce
Ruby | 168 lines | 137 code | 30 blank | 1 comment | 0 complexity | 89475f1141a841b3d5a5d8fcbe648f72 MD5 | raw file
  1# frozen_string_literal: true
  2
  3require 'spec_helper'
  4
  5describe KeysFinder do
  6  subject { described_class.new(params).execute }
  7
  8  let(:user) { create(:user) }
  9  let(:params) { {} }
 10
 11  let!(:key_1) do
 12    create(:personal_key,
 13      last_used_at: 7.days.ago,
 14      user: user,
 15      key: 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1016k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
 16      fingerprint: 'ba:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d1',
 17      fingerprint_sha256: 'nUhzNyftwADy8AH3wFY31tAKs7HufskYTte2aXo/lCg')
 18  end
 19
 20  let!(:key_2) { create(:personal_key, last_used_at: nil, user: user) }
 21  let!(:key_3) { create(:personal_key, last_used_at: 2.days.ago) }
 22
 23  context 'key_type' do
 24    let!(:deploy_key) { create(:deploy_key) }
 25
 26    context 'when `key_type` is `ssh`' do
 27      before do
 28        params[:key_type] = 'ssh'
 29      end
 30
 31      it 'returns only SSH keys' do
 32        expect(subject).to contain_exactly(key_1, key_2, key_3)
 33      end
 34    end
 35
 36    context 'when `key_type` is not specified' do
 37      it 'returns all types of keys' do
 38        expect(subject).to contain_exactly(key_1, key_2, key_3, deploy_key)
 39      end
 40    end
 41  end
 42
 43  context 'fingerprint' do
 44    context 'with invalid fingerprint' do
 45      context 'with invalid MD5 fingerprint' do
 46        before do
 47          params[:fingerprint] = '11:11:11:11'
 48        end
 49
 50        it 'raises InvalidFingerprint' do
 51          expect { subject }.to raise_error(KeysFinder::InvalidFingerprint)
 52        end
 53      end
 54
 55      context 'with invalid SHA fingerprint' do
 56        before do
 57          params[:fingerprint] = 'nUhzNyftwAAKs7HufskYTte2g'
 58        end
 59
 60        it 'raises InvalidFingerprint' do
 61          expect { subject }.to raise_error(KeysFinder::InvalidFingerprint)
 62        end
 63      end
 64    end
 65
 66    context 'with valid fingerprints' do
 67      let!(:deploy_key) do
 68        create(:deploy_key,
 69          user: user,
 70          key: 'ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt1017k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=',
 71          fingerprint: '8a:4a:12:92:0b:50:47:02:d4:5a:8e:a9:44:4e:08:b4',
 72          fingerprint_sha256: '4DPHOVNh53i9dHb5PpY2vjfyf5qniTx1/pBFPoZLDdk')
 73      end
 74
 75      context 'personal key with valid MD5 params' do
 76        context 'with an existent fingerprint' do
 77          before do
 78            params[:fingerprint] = 'ba:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d1'
 79          end
 80
 81          it 'returns the key' do
 82            expect(subject).to eq(key_1)
 83            expect(subject.user).to eq(user)
 84          end
 85        end
 86
 87        context 'deploy key with an existent fingerprint' do
 88          before do
 89            params[:fingerprint] = '8a:4a:12:92:0b:50:47:02:d4:5a:8e:a9:44:4e:08:b4'
 90          end
 91
 92          it 'returns the key' do
 93            expect(subject).to eq(deploy_key)
 94            expect(subject.user).to eq(user)
 95          end
 96        end
 97
 98        context 'with a non-existent fingerprint' do
 99          before do
100            params[:fingerprint] = 'bb:81:59:68:d7:6c:cd:02:02:bf:6a:9b:55:4e:af:d2'
101          end
102
103          it 'returns nil' do
104            expect(subject).to be_nil
105          end
106        end
107      end
108
109      context 'personal key with valid SHA256 params' do
110        context 'with an existent fingerprint' do
111          before do
112            params[:fingerprint] = 'SHA256:nUhzNyftwADy8AH3wFY31tAKs7HufskYTte2aXo/lCg'
113          end
114
115          it 'returns key' do
116            expect(subject).to eq(key_1)
117            expect(subject.user).to eq(user)
118          end
119        end
120
121        context 'deploy key with an existent fingerprint' do
122          before do
123            params[:fingerprint] = 'SHA256:4DPHOVNh53i9dHb5PpY2vjfyf5qniTx1/pBFPoZLDdk'
124          end
125
126          it 'returns key' do
127            expect(subject).to eq(deploy_key)
128            expect(subject.user).to eq(user)
129          end
130        end
131
132        context 'with a non-existent fingerprint' do
133          before do
134            params[:fingerprint] = 'SHA256:xTjuFqftwADy8AH3wFY31tAKs7HufskYTte2aXi/mNp'
135          end
136
137          it 'returns nil' do
138            expect(subject).to be_nil
139          end
140        end
141      end
142    end
143  end
144
145  context 'user' do
146    context 'without user' do
147      it 'contains ssh_keys of all users in the system' do
148        expect(subject).to contain_exactly(key_1, key_2, key_3)
149      end
150    end
151
152    context 'with user' do
153      before do
154        params[:users] = user
155      end
156
157      it 'contains ssh_keys of only the specified users' do
158        expect(subject).to contain_exactly(key_1, key_2)
159      end
160    end
161  end
162
163  context 'sort order' do
164    it 'sorts in last_used_at_desc order' do
165      expect(subject).to eq([key_3, key_1, key_2])
166    end
167  end
168end