pgp_spec.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Integration PGP', :aggregate_failures, authenticated_as: :user, type: :request do
  4. before do
  5. PGPKey.destroy_all
  6. end
  7. shared_examples 'check authentication handling' do
  8. context 'with agent user' do
  9. let(:user) { 'agent' }
  10. it 'returns forbidden' do
  11. expect(response).to have_http_status(:forbidden)
  12. end
  13. end
  14. end
  15. describe 'request handling' do
  16. let(:user) { create(:admin) }
  17. context 'when calling GET / key_show' do
  18. let(:pgp_key) { create(:'pgp_key/zammad@localhost') }
  19. let(:fingerprint) { pgp_key.fingerprint }
  20. before do
  21. get "/api/v1/integration/pgp/key/#{pgp_key.id}"
  22. end
  23. context 'with admin user' do
  24. it 'fetches key info' do
  25. expect(response).to have_http_status(:ok)
  26. expect(json_response).to include(
  27. 'fingerprint' => pgp_key.fingerprint,
  28. 'name' => pgp_key.name,
  29. 'email_addresses' => pgp_key.email_addresses,
  30. 'expires_at' => pgp_key.expires_at,
  31. 'secret' => false
  32. )
  33. end
  34. end
  35. include_examples 'check authentication handling'
  36. end
  37. context 'when calling GET / key_download' do
  38. let(:pgp_key) { create(:'pgp_key/zammad@localhost') }
  39. let(:fingerprint) { pgp_key.fingerprint }
  40. let(:params) { '' }
  41. before do
  42. get "/api/v1/integration/pgp/key_download/#{pgp_key.id}?#{params}"
  43. end
  44. context 'with a public key' do
  45. it 'downloads public key' do
  46. expect(response).to have_http_status(:ok)
  47. expect(response.body).to eq(pgp_key.key)
  48. end
  49. context 'when requesting the private key' do
  50. let(:params) { 'secret=true' }
  51. it 'returns an error' do
  52. expect(response).to have_http_status(:unprocessable_entity)
  53. end
  54. end
  55. end
  56. context 'with a private key' do
  57. let(:pgp_key) { create(:'pgp_key/zammad@localhost', :with_private) }
  58. it 'downloads public key' do
  59. expect(response).to have_http_status(:ok)
  60. expect(response.body).to start_with('-----BEGIN PGP PUBLIC KEY BLOCK-----')
  61. end
  62. context 'when requesting the private key' do
  63. let(:params) { 'secret=true' }
  64. it 'downloads the private key' do
  65. expect(response).to have_http_status(:ok)
  66. expect(response.body).to eq(pgp_key.key)
  67. end
  68. end
  69. end
  70. end
  71. context 'when calling GET / key_list' do
  72. let(:pgp_key) { create(:'pgp_key/zammad@localhost') }
  73. let(:fingerprint) { pgp_key.fingerprint }
  74. before do
  75. pgp_key
  76. get '/api/v1/integration/pgp/key'
  77. end
  78. context 'with admin user' do
  79. it 'fetches key infos' do
  80. expect(response).to have_http_status(:ok)
  81. expect(json_response.last).to include(
  82. 'fingerprint' => fingerprint,
  83. 'name' => pgp_key.name,
  84. 'email_addresses' => pgp_key.email_addresses,
  85. 'expires_at' => pgp_key.expires_at,
  86. 'secret' => false
  87. )
  88. end
  89. end
  90. include_examples 'check authentication handling'
  91. end
  92. context 'when calling POST / create' do
  93. let(:public_key) { Rails.root.join('spec/fixtures/files/pgp/zammad@localhost.pub.asc').read }
  94. let(:fingerprint) { Rails.root.join('spec/fixtures/files/pgp/zammad@localhost.fingerprint').read }
  95. let(:private_key) { Rails.root.join('spec/fixtures/files/pgp/zammad@localhost.asc').read }
  96. let(:private_passphrase) { Rails.root.join('spec/fixtures/files/pgp/zammad@localhost.passphrase').read }
  97. context 'with admin user' do
  98. context 'when importing a public key' do
  99. before do
  100. post '/api/v1/integration/pgp/key', params: { key: public_key }
  101. end
  102. it 'creates a new public key' do
  103. expect(response).to have_http_status(:created)
  104. expect(json_response).to include(
  105. 'fingerprint' => fingerprint,
  106. 'name' => 'zammad@localhost',
  107. 'email_addresses' => ['zammad@localhost'],
  108. 'expires_at' => '2033-07-02T13:02:07.000Z',
  109. 'secret' => false
  110. )
  111. expect(PGPKey.last).to have_attributes(
  112. fingerprint: fingerprint,
  113. name: 'zammad@localhost',
  114. email_addresses: ['zammad@localhost'],
  115. expires_at: DateTime.parse('2033-07-02T13:02:07.000Z'),
  116. secret: false
  117. )
  118. expect(PGPKey.count).to eq 1
  119. end
  120. context 'when public key has leading whitespace' do
  121. let(:public_key) { " #{Rails.root.join('spec/fixtures/files/pgp/zammad@localhost.pub.asc').read}" }
  122. it 'creates a key if copy-pasted value has leading whitespace' do
  123. expect(response).to have_http_status(:created)
  124. end
  125. end
  126. context 'when adding the same key again' do
  127. before do
  128. post '/api/v1/integration/pgp/key', params: { key: public_key }
  129. end
  130. it 'returns an error' do
  131. expect(response).to have_http_status(:unprocessable_entity)
  132. end
  133. end
  134. context 'when importing a private key with the same fingerprint' do
  135. before do
  136. post '/api/v1/integration/pgp/key', params: { key: private_key, passphrase: private_passphrase }
  137. end
  138. it 'returns an error' do
  139. expect(response).to have_http_status(:unprocessable_entity)
  140. end
  141. end
  142. include_examples 'check authentication handling'
  143. end
  144. context 'when importing a private key' do
  145. before do
  146. post '/api/v1/integration/pgp/key', params: { key: private_key, passphrase: private_passphrase }
  147. end
  148. it 'creates only one key' do
  149. expect(response).to have_http_status(:created)
  150. expect(json_response).to include(
  151. 'fingerprint' => fingerprint,
  152. 'name' => 'zammad@localhost',
  153. 'email_addresses' => ['zammad@localhost'],
  154. 'expires_at' => '2033-07-02T13:02:07.000Z',
  155. 'secret' => true
  156. )
  157. expect(PGPKey.last).to have_attributes(
  158. fingerprint: fingerprint,
  159. name: 'zammad@localhost',
  160. email_addresses: ['zammad@localhost'],
  161. expires_at: DateTime.parse('2033-07-02T13:02:07.000Z'),
  162. secret: true
  163. )
  164. expect(PGPKey.count).to eq 1
  165. end
  166. end
  167. end
  168. end
  169. context 'when calling POST / search' do
  170. before do
  171. pgp_key
  172. post '/api/v1/integration/pgp', params: { ticket: ticket, article: article }
  173. end
  174. let(:email_address) { create(:email_address, email: 'zammad@localhost') }
  175. let(:group) { create(:group, email_address: email_address) }
  176. let(:ticket) { { 'group_id' => group.id } }
  177. let(:article) { { 'to' => 'zammad@localhost', 'from' => 'zammad@localhost' } }
  178. context 'without keys present' do
  179. let(:pgp_key) { nil }
  180. it 'returns no possible security options' do
  181. expect(response).to have_http_status(:ok)
  182. expect(json_response).to eq(
  183. {
  184. 'encryption' => {
  185. 'comment' => 'The PGP key for %s was not found.',
  186. 'commentPlaceholders' => ['zammad@localhost'],
  187. 'success' => false,
  188. },
  189. 'sign' => {
  190. 'comment' => 'The PGP key for %s was not found.',
  191. 'commentPlaceholders' => ['zammad@localhost'],
  192. 'success' => false,
  193. },
  194. 'type' => 'PGP',
  195. }
  196. )
  197. end
  198. end
  199. context 'with keys present' do
  200. let(:pgp_key) { create(:pgp_key, :with_private, fixture: 'zammad@localhost') }
  201. it 'returns possible security options' do
  202. expect(response).to have_http_status(:ok)
  203. expect(json_response).to eq(
  204. {
  205. 'encryption' => {
  206. 'comment' => 'The PGP keys for %s were found.',
  207. 'commentPlaceholders' => ['zammad@localhost'],
  208. 'success' => true,
  209. },
  210. 'sign' => {
  211. 'comment' => 'The PGP key for %s was found.',
  212. 'commentPlaceholders' => ['zammad@localhost'],
  213. 'success' => true,
  214. },
  215. 'type' => 'PGP',
  216. }
  217. )
  218. end
  219. end
  220. end
  221. context 'when calling DELETE' do
  222. let(:pgp_key) { create(:'pgp_key/zammad@localhost') }
  223. before do
  224. delete "/api/v1/integration/pgp/key/#{pgp_key.id}"
  225. end
  226. context 'with admin user' do
  227. it 'deletes the key' do
  228. expect(response).to have_http_status(:ok)
  229. end
  230. end
  231. include_examples 'check authentication handling'
  232. end
  233. context 'when calling GET status' do
  234. it 'returns empty JSON if all is OK' do
  235. get '/api/v1/integration/pgp/status'
  236. expect(json_response).to be_blank
  237. end
  238. it 'returns error message if GnuPG is not up to date' do
  239. allow(SecureMailing::PGP).to receive(:required_version?).and_return(false)
  240. get '/api/v1/integration/pgp/status'
  241. expect(json_response).to include('error' => be_present)
  242. end
  243. end
  244. end
  245. end