ssl_certificate_spec.rb 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe SSLCertificate, :aggregate_failures, type: :model do
  4. let(:fixture) { 'RootCA' }
  5. let(:certificate) { create(:ssl_certificate, fixture: fixture) }
  6. describe '.create' do
  7. context 'when certificate is RootCA' do
  8. it 'imports correctly' do
  9. expect(certificate)
  10. .to have_attributes(
  11. fingerprint: 'de4abd259187d7b5f2713ff7a97eb54dd5fe9d86',
  12. subject: '/emailAddress=RootCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com',
  13. not_before: Time.zone.parse('2023-08-01 09:47:39 UTC'),
  14. not_after: Time.zone.parse('2043-07-27 09:47:39 UTC'),
  15. )
  16. end
  17. end
  18. context 'when certificate is IntermediateCA' do
  19. let(:fixture) { 'IntermediateCA' }
  20. it 'imports correctly' do
  21. expect(certificate)
  22. .to have_attributes(
  23. fingerprint: 'd1badcd237d6d2c6f0c62b5ccb21c2130b24855e',
  24. subject: '/C=DE/ST=Berlin/O=Example Security/OU=IT Department/CN=example.com/emailAddress=IntermediateCA@example.com',
  25. not_before: Time.zone.parse('2023-08-01 09:47:39 UTC'),
  26. not_after: Time.zone.parse('2043-07-27 09:47:39 UTC'),
  27. )
  28. end
  29. end
  30. context 'when certificate is connection certificate' do
  31. let(:certificate_content) { File.read(Localhost::Authority.fetch('localhost').certificate_path) }
  32. let(:certificate) { create(:ssl_certificate, certificate: certificate_content) }
  33. it 'imports correctly' do
  34. expect(certificate)
  35. .to have_attributes(
  36. subject: 'DNS:localhost'
  37. )
  38. end
  39. end
  40. end
  41. describe '#certificate_parsed' do
  42. context 'when certificate is valid' do
  43. it 'returns certificate' do
  44. expect(certificate.certificate_parsed).to be_an_instance_of(Certificate::X509::SSL)
  45. end
  46. end
  47. context 'when certificate is not valid' do
  48. it 'raises an error' do
  49. certificate.update_columns certificate: 'blablabla'
  50. certificate.instance_variable_set('@certificate_parsed', nil) # rubocop:disable Performance/StringIdentifierArgument
  51. expect { certificate.reload.certificate_parsed }.to raise_error 'This is not a valid X509 certificate. Please check the certificate format.'
  52. end
  53. end
  54. end
  55. describe 'validations' do
  56. describe 'certificate validation' do
  57. context 'when certificate is not valid' do
  58. let(:fixture) { 'smime1@example.com' }
  59. let(:certificate) { build(:ssl_certificate, fixture: fixture) }
  60. it 'adds a base error' do
  61. certificate.save
  62. expect(certificate.errors[:base]).to be_present
  63. end
  64. end
  65. end
  66. end
  67. end