smime_spec.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. require 'rails_helper'
  2. RSpec.describe 'Integration SMIME', type: :request do
  3. let(:admin) { create(:admin) }
  4. let(:email_address) { 'smime1@example.com' }
  5. before do
  6. authenticated_as(admin)
  7. end
  8. describe '/integration/smime/certificate' do
  9. let(:endpoint) { '/api/v1/integration/smime/certificate' }
  10. let(:certificate_path) do
  11. Rails.root.join("spec/fixtures/smime/#{email_address}.crt")
  12. end
  13. let(:certificate_string) do
  14. File.read(certificate_path)
  15. end
  16. context 'POST requests' do
  17. let(:parsed_certificate) { SMIMECertificate.parse(certificate_string) }
  18. it 'adds certificate by string' do
  19. expect do
  20. post endpoint, params: { data: certificate_string }, as: :json
  21. end.to change(SMIMECertificate, :count).by(1)
  22. expect(response).to have_http_status(:ok)
  23. expect(DateTime.parse(json_response['response'][0]['not_after_at'])).to eq(parsed_certificate.not_after)
  24. end
  25. it 'adds certificate by file' do
  26. expect do
  27. post endpoint, params: { file: Rack::Test::UploadedFile.new(certificate_path, 'text/plain', true) }
  28. end.to change(SMIMECertificate, :count).by(1)
  29. expect(response).to have_http_status(:ok)
  30. expect(DateTime.parse(json_response['response'][0]['not_after_at'])).to eq(parsed_certificate.not_after)
  31. end
  32. end
  33. context 'GET requests' do
  34. let!(:certificate) { create(:smime_certificate, fixture: email_address) }
  35. it 'lists certificates' do
  36. get endpoint, as: :json
  37. expect(response).to have_http_status(:ok)
  38. expect(json_response.any? { |e| e['id'] == certificate.id }).to be true
  39. end
  40. end
  41. context 'DELETE requests' do
  42. let!(:certificate) { create(:smime_certificate, fixture: email_address) }
  43. it 'deletes certificate' do
  44. expect do
  45. delete endpoint, params: { id: certificate.id }, as: :json
  46. end.to change(SMIMECertificate, :count).by(-1)
  47. expect(response).to have_http_status(:ok)
  48. end
  49. end
  50. end
  51. describe '/integration/smime/private_key' do
  52. let(:endpoint) { '/api/v1/integration/smime/private_key' }
  53. context 'POST requests' do
  54. let(:private_path) do
  55. Rails.root.join("spec/fixtures/smime/#{email_address}.key")
  56. end
  57. let(:private_string) { File.read(private_path) }
  58. let(:secret) do
  59. File.read(Rails.root.join("spec/fixtures/smime/#{email_address}.secret")).strip
  60. end
  61. let!(:certificate) { create(:smime_certificate, fixture: email_address) }
  62. it 'adds by string' do
  63. expect do
  64. post endpoint, params: { data: private_string, secret: secret }, as: :json
  65. end.to change {
  66. certificate.reload
  67. certificate.private_key
  68. }
  69. expect(response).to have_http_status(:ok)
  70. expect(json_response['result']).to eq('ok')
  71. end
  72. it 'adds by file' do
  73. expect do
  74. post endpoint, params: { file: Rack::Test::UploadedFile.new(private_path, 'text/plain', true), secret: secret }
  75. end.to change {
  76. certificate.reload
  77. certificate.private_key
  78. }
  79. expect(response).to have_http_status(:ok)
  80. expect(json_response['result']).to eq('ok')
  81. end
  82. end
  83. context 'DELETE requests' do
  84. let!(:certificate) { create(:smime_certificate, :with_private, fixture: email_address) }
  85. it 'deletes private key' do
  86. expect do
  87. delete endpoint, params: { id: certificate.id }, as: :json
  88. end.to change {
  89. certificate.reload
  90. certificate.private_key
  91. }.to(nil)
  92. expect(response).to have_http_status(:ok)
  93. end
  94. end
  95. end
  96. describe '/integration/smime' do
  97. let(:endpoint) { '/api/v1/integration/smime' }
  98. context 'POST requests' do
  99. let(:system_email_address) { create(:email_address, email: email_address) }
  100. let(:group) { create(:group, email_address: system_email_address) }
  101. let(:search_query) do
  102. {
  103. article: {
  104. to: email_address,
  105. },
  106. ticket: {
  107. group_id: group.id,
  108. },
  109. }
  110. end
  111. context 'certificate not present' do
  112. it 'does not find non existing certificates' do
  113. post endpoint, params: search_query, as: :json
  114. expect(response).to have_http_status(:ok)
  115. expect(json_response['encryption']['success']).to eq(false)
  116. expect(json_response['encryption']['comment']).to include(email_address)
  117. expect(json_response['sign']['success']).to eq(false)
  118. expect(json_response['sign']['comment']).to include(email_address)
  119. end
  120. end
  121. context 'certificate present' do
  122. before do
  123. create(:smime_certificate, :with_private, fixture: email_address)
  124. end
  125. it 'finds existing certificate' do
  126. post endpoint, params: search_query, as: :json
  127. expect(response).to have_http_status(:ok)
  128. expect(json_response['encryption']['success']).to eq(true)
  129. expect(json_response['encryption']['comment']).to include(email_address)
  130. expect(json_response['sign']['success']).to eq(true)
  131. expect(json_response['sign']['comment']).to include(email_address)
  132. end
  133. context 'but expired' do
  134. let(:email_address) { 'expiredsmime1@example.com' }
  135. it 'finds existing certificate with comment' do
  136. post endpoint, params: search_query, as: :json
  137. expect(response).to have_http_status(:ok)
  138. expect(json_response['encryption']['success']).to eq(false)
  139. expect(json_response['encryption']['comment']).to include(email_address).and include('expired')
  140. expect(json_response['sign']['success']).to eq(false)
  141. expect(json_response['sign']['comment']).to include(email_address).and include('expired')
  142. end
  143. end
  144. end
  145. end
  146. end
  147. end