apply_ssl_certificates_spec.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Certificate::ApplySSLCertificates, :aggregate_failures, type: :model do
  4. describe '.ensure_fresh_ssl_context' do
  5. def current_store
  6. OpenSSL::SSL::SSLContext::DEFAULT_CERT_STORE
  7. end
  8. context 'with a custom certificate present' do
  9. it 'changes the context' do
  10. create(:ssl_certificate, fixture: 'RootCA')
  11. expect { described_class.ensure_fresh_ssl_context }.to change { current_store }
  12. expect { described_class.ensure_fresh_ssl_context }.not_to change { current_store }
  13. create(:ssl_certificate, fixture: 'ChainCA')
  14. expect { described_class.ensure_fresh_ssl_context }.to change { current_store }
  15. end
  16. end
  17. context 'without custom certificates present' do
  18. it 'changes the context' do
  19. expect { described_class.ensure_fresh_ssl_context }.to change { current_store }
  20. end
  21. end
  22. end
  23. describe '.extract_metadata' do
  24. it 'imports CA certificates correctly' do
  25. expect(create(:ssl_certificate, fixture: 'RootCA')).to have_attributes(
  26. fingerprint: 'de4abd259187d7b5f2713ff7a97eb54dd5fe9d86',
  27. subject: '/emailAddress=RootCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com',
  28. not_before: Time.zone.parse('2023-08-01 09:47:39 UTC'),
  29. not_after: Time.zone.parse('2043-07-27 09:47:39 UTC'),
  30. )
  31. expect(create(:ssl_certificate, fixture: 'IntermediateCA')).to have_attributes(
  32. fingerprint: 'd1badcd237d6d2c6f0c62b5ccb21c2130b24855e',
  33. subject: '/C=DE/ST=Berlin/O=Example Security/OU=IT Department/CN=example.com/emailAddress=IntermediateCA@example.com',
  34. not_before: Time.zone.parse('2023-08-01 09:47:39 UTC'),
  35. not_after: Time.zone.parse('2043-07-27 09:47:39 UTC'),
  36. )
  37. end
  38. it 'imports connection certificates correctly' do
  39. certificate = File.read(Localhost::Authority.fetch('localhost').certificate_path)
  40. expect(create(:ssl_certificate, certificate: certificate)).to have_attributes(
  41. subject: 'DNS:localhost',
  42. )
  43. end
  44. it 'rejects other certificates' do
  45. expect { create(:ssl_certificate, certificate: Rails.root.join('spec/fixtures/files/smime/smime1@example.com.crt').read) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: The certificate is not valid for SSL usage. Please check e.g. the validity period or the extensions.')
  46. end
  47. end
  48. end