smime_certificate_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. require 'rails_helper'
  2. RSpec.describe SMIMECertificate, type: :model do
  3. describe '.for_sender_email_address' do
  4. let(:lookup_address) { 'smime1@example.com' }
  5. context 'no certificate present' do
  6. it 'returns nil' do
  7. expect(described_class.for_sender_email_address(lookup_address)).to be nil
  8. end
  9. end
  10. context 'certificate present' do
  11. context 'with private key' do
  12. let!(:certificate) { create(:smime_certificate, :with_private, fixture: lookup_address) }
  13. it 'returns certificate' do
  14. expect(described_class.for_sender_email_address(lookup_address)).to eq(certificate)
  15. end
  16. end
  17. context 'without private key' do
  18. before do
  19. create(:smime_certificate, fixture: lookup_address)
  20. end
  21. it 'returns nil' do
  22. expect(described_class.for_sender_email_address(lookup_address)).to be nil
  23. end
  24. end
  25. context 'different letter case' do
  26. let(:fixture) { 'CaseInsenstive@eXample.COM' }
  27. let(:lookup_address) { 'CaseInsenStive@Example.coM' }
  28. context 'with private key' do
  29. let!(:certificate) { create(:smime_certificate, :with_private, fixture: fixture) }
  30. it 'returns certificate' do
  31. expect(described_class.for_sender_email_address(lookup_address)).to eq(certificate)
  32. end
  33. end
  34. end
  35. end
  36. end
  37. describe 'for_recipipent_email_addresses!' do
  38. context 'no certificate present' do
  39. let(:lookup_addresses) { ['smime1@example.com', 'smime2@example.com'] }
  40. it 'raises ActiveRecord::RecordNotFound' do
  41. expect { described_class.for_recipipent_email_addresses!(lookup_addresses) }.to raise_error(ActiveRecord::RecordNotFound)
  42. end
  43. end
  44. context 'not all certificates present' do
  45. let(:existing_address) { 'smime1@example.com' }
  46. let(:not_existing_address) { 'smime2@example.com' }
  47. let(:lookup_addresses) { [existing_address, not_existing_address] }
  48. before do
  49. create(:smime_certificate, fixture: existing_address)
  50. end
  51. it 'raises ActiveRecord::RecordNotFound' do
  52. expect { described_class.for_recipipent_email_addresses!(lookup_addresses) }.to raise_error(ActiveRecord::RecordNotFound)
  53. end
  54. context 'exception message' do
  55. let(:message) do
  56. described_class.for_recipipent_email_addresses!(lookup_addresses)
  57. rescue => e
  58. e.message
  59. end
  60. it 'does not contain found address' do
  61. expect(message).not_to include(existing_address)
  62. end
  63. it 'contains address not found' do
  64. expect(message).to include(not_existing_address)
  65. end
  66. end
  67. end
  68. context 'all certificates present' do
  69. let(:lookup_addresses) { ['smime1@example.com', 'smime2@example.com'] }
  70. let!(:certificates) do
  71. lookup_addresses.map do |existing_address|
  72. create(:smime_certificate, fixture: existing_address)
  73. end
  74. end
  75. it 'returns certificates' do
  76. expect(described_class.for_recipipent_email_addresses!(lookup_addresses)).to eq(certificates)
  77. end
  78. end
  79. context 'different letter case' do
  80. let(:fixture) { 'CaseInsenstive@eXample.COM' }
  81. let(:lookup_addresses) { ['CaseInsenStive@Example.coM'] }
  82. let!(:certificates) do
  83. [ create(:smime_certificate, fixture: fixture) ]
  84. end
  85. it 'returns certificates' do
  86. expect(described_class.for_recipipent_email_addresses!(lookup_addresses)).to eq(certificates)
  87. end
  88. end
  89. end
  90. describe '#email_addresses' do
  91. context 'certificate with single email address' do
  92. let(:email_address) { 'smime1@example.com' }
  93. let(:certificate) { create(:smime_certificate, fixture: email_address) }
  94. it 'returns the mail address' do
  95. expect(certificate.email_addresses).to eq([email_address])
  96. end
  97. end
  98. context 'certificate with multiple email addresses' do
  99. let(:email_addresses) { ['smimedouble@example.com', 'smimedouble@example.de'] }
  100. let(:certificate) { create(:smime_certificate, fixture: 'smimedouble@example.com') }
  101. it 'returns all mail addresses' do
  102. expect(certificate.email_addresses).to eq(email_addresses)
  103. end
  104. end
  105. end
  106. describe '#expired?' do
  107. let(:certificate) { create(:smime_certificate, fixture: fixture) }
  108. context 'expired' do
  109. let(:fixture) { 'expiredsmime1@example.com' }
  110. it 'returns true' do
  111. expect(certificate.expired?).to be true
  112. end
  113. end
  114. context 'valid' do
  115. let(:fixture) { 'smime1@example.com' }
  116. it 'returns false' do
  117. expect(certificate.expired?).to be false
  118. end
  119. end
  120. end
  121. context 'certificate parsing' do
  122. context 'expiration dates' do
  123. shared_examples 'correctly parsed' do |fixture|
  124. let(:certificate) { create(:smime_certificate, fixture: fixture) }
  125. it "handles '#{fixture}' fixture" do
  126. expect(certificate.not_before_at).to a_kind_of(ActiveSupport::TimeWithZone)
  127. expect(certificate.not_after_at).to a_kind_of(ActiveSupport::TimeWithZone)
  128. end
  129. end
  130. it_behaves_like 'correctly parsed', 'smime1@example.com'
  131. it_behaves_like 'correctly parsed', 'smime2@example.com'
  132. it_behaves_like 'correctly parsed', 'smime3@example.com'
  133. it_behaves_like 'correctly parsed', 'CaseInsenstive@eXample.COM'
  134. it_behaves_like 'correctly parsed', 'ca'
  135. end
  136. end
  137. it 'ensures uniqueness of records' do
  138. expect { create_list(:smime_certificate, 2, fixture: 'smime1@example.com') }.to raise_error(ActiveRecord::RecordInvalid, /Validation failed/)
  139. end
  140. end