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