ssl_certificate_spec.rb 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'SSL Certificate', :aggregate_failures, type: :request do
  4. let(:admin) { create(:admin) }
  5. before do
  6. authenticated_as(admin)
  7. end
  8. describe '/ssl_certificates' do
  9. let(:endpoint) { '/api/v1/ssl_certificates' }
  10. let(:certificate_path) do
  11. Rails.root.join('spec/fixtures/files/smime/RootCA.crt')
  12. end
  13. let(:certificate_string) do
  14. File.read(certificate_path)
  15. end
  16. describe 'POST requests' do
  17. let(:parsed_certificate) { Certificate::X509::SSL.new(certificate_string) }
  18. it 'adds certificate by string' do
  19. expect do
  20. post endpoint, params: { certificate: certificate_string }, as: :json
  21. end.to change(SSLCertificate, :count).by(1)
  22. expect(response).to have_http_status(:created)
  23. expect(json_response).to include(
  24. 'not_after' => parsed_certificate.not_after.as_json
  25. )
  26. end
  27. it 'adds certificate by file' do
  28. expect do
  29. post endpoint, params: { file: Rack::Test::UploadedFile.new(certificate_path, 'text/plain', true) }
  30. end.to change(SSLCertificate, :count).by(1)
  31. expect(response).to have_http_status(:created)
  32. expect(json_response).to include(
  33. 'not_after' => parsed_certificate.not_after.as_json
  34. )
  35. end
  36. end
  37. describe 'GET requests' do
  38. let!(:certificate) { create(:ssl_certificate, fixture: 'RootCA') }
  39. it 'lists certificates' do
  40. get endpoint, as: :json
  41. expect(response).to have_http_status(:ok)
  42. expect(json_response['SSLCertificate'].values.first.keys).to match_array %w[
  43. id
  44. subject
  45. fingerprint
  46. not_before
  47. not_after
  48. created_at
  49. updated_at
  50. ca
  51. ]
  52. expect(json_response).to include_assets_of certificate
  53. end
  54. end
  55. describe 'DELETE requests' do
  56. let!(:certificate) { create(:ssl_certificate, fixture: 'RootCA') }
  57. it 'deletes certificate' do
  58. expect do
  59. delete "#{endpoint}/#{certificate.id}", as: :json
  60. end.to change(SSLCertificate, :count).by(-1)
  61. expect(response).to have_http_status(:ok)
  62. end
  63. end
  64. end
  65. end