smime_spec.rb 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. require 'rails_helper'
  2. RSpec.describe SecureMailing::SMIME do
  3. before do
  4. Setting.set('smime_integration', true)
  5. end
  6. let(:raw_body) { 'Some text' }
  7. let(:system_email_address) { 'smime1@example.com' }
  8. let(:customer_email_address) { 'smime2@example.com' }
  9. let(:sender_certificate_subject) { "/emailAddress=#{sender_email_address}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com" }
  10. let(:recipient_certificate_subject) { "/emailAddress=#{recipient_email_address}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com" }
  11. let(:expired_email_address) { 'expiredsmime1@example.com' }
  12. let(:ca_certificate_subject) { '/emailAddress=RootCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com' }
  13. let(:content_type) { 'text/plain' }
  14. def build_mail
  15. Channel::EmailBuild.build(
  16. from: sender_email_address,
  17. to: recipient_email_address,
  18. body: raw_body,
  19. content_type: content_type,
  20. security: security_preferences
  21. )
  22. end
  23. describe '.outgoing' do
  24. shared_examples 'HttpLog writer' do |status|
  25. it "logs #{status}" do
  26. expect do
  27. build_mail
  28. rescue
  29. # allow failures
  30. end.to change(HttpLog, :count).by(1)
  31. expect(HttpLog.last.attributes).to include('direction' => 'out', 'status' => status)
  32. end
  33. end
  34. let(:sender_email_address) { system_email_address }
  35. let(:recipient_email_address) { customer_email_address }
  36. context 'without security' do
  37. let(:security_preferences) do
  38. nil
  39. end
  40. it 'builds mail' do
  41. expect(build_mail.body).not_to match(SecureMailing::SMIME::Incoming::EXPRESSION_SIGNATURE)
  42. expect(build_mail.body).not_to match(SecureMailing::SMIME::Incoming::EXPRESSION_MIME)
  43. expect(build_mail.body).to eq(raw_body)
  44. end
  45. end
  46. context 'signing' do
  47. let(:security_preferences) do
  48. {
  49. type: 'S/MIME',
  50. sign: {
  51. success: true,
  52. },
  53. encryption: {
  54. success: false,
  55. },
  56. }
  57. end
  58. context 'private key present' do
  59. let!(:sender_certificate) do
  60. create(:smime_certificate, :with_private, fixture: system_email_address)
  61. end
  62. it 'builds mail' do
  63. expect(build_mail.body).to match(SecureMailing::SMIME::Incoming::EXPRESSION_SIGNATURE)
  64. end
  65. it_behaves_like 'HttpLog writer', 'success'
  66. context 'expired certificate' do
  67. let(:system_email_address) { expired_email_address }
  68. it 'raises exception' do
  69. expect { build_mail }.to raise_error RuntimeError
  70. end
  71. it_behaves_like 'HttpLog writer', 'failed'
  72. end
  73. context 'when message is 7bit or 8bit encoded' do
  74. let(:mail) do
  75. smime_mail = build_mail
  76. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  77. SecureMailing.incoming(mail)
  78. mail
  79. end
  80. context 'when Content-Type is text/plain' do
  81. let(:raw_body) { "\r\n\r\n@john.doe, now known as John Dóe has accepted your invitation to join the Administrator / htmltest project.\r\n\r\nhttp://169.254.169.254:3000/root/htmltest\r\n\r\n-- \r\nYou're receiving this email because of your account on 169.254.169.254.\r\n\r\n\r\n\r\n" }
  82. it 'verifies' do
  83. expect(mail['x-zammad-article-preferences']['security']['sign']['success']).to be true
  84. end
  85. end
  86. context 'when Content-Type is text/html' do
  87. let(:content_type) { 'text/html' }
  88. let(:raw_body) { "<div><ul><li><p>an \nexample „Text“ with ümläütß. </p></li></ul></div>" }
  89. it 'verifies' do
  90. expect(mail['x-zammad-article-preferences']['security']['sign']['success']).to be true
  91. end
  92. end
  93. end
  94. context 'when certificate chain is present' do
  95. let(:system_email_address) { 'chain@example.com' }
  96. let!(:chain) do
  97. [
  98. sender_certificate,
  99. create(:smime_certificate, fixture: 'ChainCA'),
  100. create(:smime_certificate, fixture: 'IntermediateCA'),
  101. create(:smime_certificate, fixture: 'RootCA'),
  102. ]
  103. end
  104. let(:p7enc) do
  105. mail = Channel::EmailParser.new.parse(build_mail.to_s)
  106. OpenSSL::PKCS7.read_smime(mail[:raw])
  107. end
  108. it 'is included in the generated mail' do
  109. expect(p7enc.certificates).to eq(chain.map(&:parsed))
  110. end
  111. end
  112. end
  113. context 'no private key present' do
  114. before do
  115. create(:smime_certificate, fixture: system_email_address)
  116. end
  117. it 'raises exception' do
  118. expect { build_mail }.to raise_error RuntimeError
  119. end
  120. it_behaves_like 'HttpLog writer', 'failed'
  121. end
  122. end
  123. context 'encryption' do
  124. let(:security_preferences) do
  125. {
  126. type: 'S/MIME',
  127. sign: {
  128. success: false,
  129. },
  130. encryption: {
  131. success: true,
  132. },
  133. }
  134. end
  135. context 'public key present' do
  136. before do
  137. create(:smime_certificate, fixture: recipient_email_address)
  138. end
  139. it 'builds mail' do
  140. mail = build_mail
  141. expect(mail['Content-Type'].value).to match(SecureMailing::SMIME::Incoming::EXPRESSION_MIME)
  142. expect(mail.body).not_to include(raw_body)
  143. end
  144. it_behaves_like 'HttpLog writer', 'success'
  145. context 'expired certificate' do
  146. let(:recipient_email_address) { expired_email_address }
  147. it 'raises exception' do
  148. expect { build_mail }.to raise_error RuntimeError
  149. end
  150. it_behaves_like 'HttpLog writer', 'failed'
  151. end
  152. end
  153. context 'no public key present' do
  154. it 'raises exception' do
  155. expect { build_mail }.to raise_error ActiveRecord::RecordNotFound
  156. end
  157. it_behaves_like 'HttpLog writer', 'failed'
  158. end
  159. end
  160. end
  161. describe '.incoming' do
  162. shared_examples 'HttpLog writer' do |status|
  163. it "logs #{status}" do
  164. expect do
  165. mail
  166. rescue
  167. # allow failures
  168. end.to change(HttpLog, :count).by(2)
  169. expect(HttpLog.last.attributes).to include('direction' => 'in', 'status' => status)
  170. end
  171. end
  172. let(:sender_email_address) { customer_email_address }
  173. let(:recipient_email_address) { system_email_address }
  174. context 'signature verification' do
  175. let(:allow_expired) { false }
  176. let(:security_preferences) do
  177. {
  178. type: 'S/MIME',
  179. sign: {
  180. success: true,
  181. allow_expired: allow_expired,
  182. },
  183. encryption: {
  184. success: false,
  185. },
  186. }
  187. end
  188. context 'sender certificate present' do
  189. before do
  190. create(:smime_certificate, :with_private, fixture: sender_email_address)
  191. end
  192. let(:mail) do
  193. smime_mail = build_mail
  194. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  195. SecureMailing.incoming(mail)
  196. mail
  197. end
  198. it 'verifies' do
  199. expect(mail[:body]).to include(raw_body)
  200. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  201. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(sender_certificate_subject)
  202. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  203. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  204. end
  205. it_behaves_like 'HttpLog writer', 'success'
  206. context 'expired' do
  207. # required to build mail with expired certificate
  208. let(:allow_expired) { true }
  209. let(:sender_email_address) { expired_email_address }
  210. it 'verifies with comment' do
  211. expect(mail[:body]).to include(raw_body)
  212. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  213. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to include(expired_email_address).and include('expired')
  214. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  215. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  216. end
  217. it_behaves_like 'HttpLog writer', 'success'
  218. end
  219. context 'with wrapped mime-type S/MIME signature (e.g. for Microsoft Outlook)' do
  220. before do
  221. # We need to disable the open ssl detached flag, to force the smime-type with 'signed-data'.
  222. stub_const('OpenSSL::PKCS7::DETACHED', nil)
  223. end
  224. it 'check that mail was verified' do
  225. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  226. end
  227. it 'check that signe comment exists' do
  228. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(sender_certificate_subject)
  229. end
  230. it 'check that body was verified' do
  231. expect(mail[:body]).to include(raw_body)
  232. end
  233. end
  234. end
  235. context 'no sender certificate' do
  236. let!(:sender_certificate) { create(:smime_certificate, :with_private, fixture: sender_email_address) }
  237. let(:mail) do
  238. smime_mail = build_mail
  239. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  240. sender_certificate.destroy!
  241. SecureMailing.incoming(mail)
  242. mail
  243. end
  244. it 'fails' do
  245. expect(mail[:body]).to include(raw_body)
  246. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  247. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  248. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  249. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  250. end
  251. context 'public key present in signature' do
  252. let(:not_related_fixture) { 'smime3@example.com' }
  253. let!(:not_related_certificate) { create(:smime_certificate, fixture: not_related_fixture) }
  254. context 'not related certificate present' do
  255. it 'fails' do
  256. expect(mail[:body]).to include(raw_body)
  257. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  258. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  259. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  260. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  261. end
  262. it_behaves_like 'HttpLog writer', 'failed'
  263. context 'CA' do
  264. let(:not_related_fixture) { 'ExpiredCA' }
  265. it 'fails' do
  266. expect(mail[:body]).to include(raw_body)
  267. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  268. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  269. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  270. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  271. end
  272. it_behaves_like 'HttpLog writer', 'failed'
  273. end
  274. end
  275. context 'usage not prevented' do
  276. before do
  277. # remove OpenSSL::PKCS7::NOINTERN
  278. stub_const('SecureMailing::SMIME::Incoming::OPENSSL_PKCS7_VERIFY_FLAGS', OpenSSL::PKCS7::NOVERIFY)
  279. end
  280. it "won't perform verification" do
  281. expect(mail[:body]).to include(raw_body)
  282. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  283. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  284. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  285. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  286. end
  287. end
  288. end
  289. context 'root CA present' do
  290. before do
  291. create(:smime_certificate, fixture: ca_fixture)
  292. end
  293. let(:ca_fixture) { 'RootCA' }
  294. it 'verifies' do
  295. expect(mail[:body]).to include(raw_body)
  296. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  297. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(ca_certificate_subject)
  298. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  299. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  300. end
  301. it_behaves_like 'HttpLog writer', 'success'
  302. context 'expired' do
  303. let(:ca_fixture) { 'ExpiredCA' }
  304. it 'fails' do
  305. expect(mail[:body]).to include(raw_body)
  306. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  307. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  308. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  309. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  310. end
  311. it_behaves_like 'HttpLog writer', 'failed'
  312. context 'allowed' do
  313. let(:allow_expired) { true }
  314. # ATTENTION: expired CA is a special case where `allow_expired` does not count
  315. it 'fails' do
  316. expect(mail[:body]).to include(raw_body)
  317. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  318. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  319. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  320. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  321. end
  322. it_behaves_like 'HttpLog writer', 'failed'
  323. end
  324. end
  325. end
  326. context 'certificate chain' do
  327. let(:sender_email_address) { 'chain@example.com' }
  328. let(:ca_subject_chain) { ca_chain.reverse.map(&:subject).join(', ') }
  329. context 'incomplete certificate chain present' do
  330. before do
  331. create(:smime_certificate, fixture: 'RootCA')
  332. create(:smime_certificate, fixture: 'IntermediateCA')
  333. end
  334. it 'fails' do
  335. expect(mail[:body]).to include(raw_body)
  336. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  337. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq('Unable to find certificate for verification')
  338. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  339. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  340. end
  341. end
  342. context 'certificate chain only partly present' do
  343. let(:ca_certificate_subject) { subject_chain }
  344. let!(:ca_chain) do
  345. [
  346. create(:smime_certificate, fixture: 'IntermediateCA'),
  347. create(:smime_certificate, fixture: 'ChainCA'),
  348. ]
  349. end
  350. it 'verifies' do
  351. expect(mail[:body]).to include(raw_body)
  352. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  353. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(ca_subject_chain)
  354. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  355. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  356. end
  357. end
  358. context 'complete certificate chain present' do
  359. let!(:ca_chain) do
  360. [
  361. create(:smime_certificate, fixture: 'RootCA'),
  362. create(:smime_certificate, fixture: 'IntermediateCA'),
  363. create(:smime_certificate, fixture: 'ChainCA'),
  364. ]
  365. end
  366. it 'verifies' do
  367. allow(Rails.logger).to receive(:warn)
  368. expect(mail[:body]).to include(raw_body)
  369. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  370. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(ca_subject_chain)
  371. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  372. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to be nil
  373. expect(Rails.logger).not_to have_received(:warn).with(%r{#{Regexp.escape(ca_certificate_subject)}})
  374. end
  375. end
  376. end
  377. end
  378. end
  379. context 'decryption' do
  380. let(:allow_expired) { false }
  381. let(:security_preferences) do
  382. {
  383. type: 'S/MIME',
  384. sign: {
  385. success: false,
  386. },
  387. encryption: {
  388. success: true,
  389. allow_expired: allow_expired,
  390. },
  391. }
  392. end
  393. let!(:sender_certificate) { create(:smime_certificate, :with_private, fixture: sender_email_address) }
  394. let!(:recipient_certificate) { create(:smime_certificate, :with_private, fixture: recipient_email_address) }
  395. context 'private key present' do
  396. let(:mail) do
  397. smime_mail = build_mail
  398. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  399. SecureMailing.incoming(mail)
  400. mail
  401. end
  402. it 'decrypts' do
  403. expect(mail[:body]).to include(raw_body)
  404. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  405. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to be nil
  406. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be true
  407. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to eq(recipient_certificate_subject)
  408. end
  409. it_behaves_like 'HttpLog writer', 'success'
  410. context 'expired allowed' do
  411. let(:allow_expired) { true }
  412. let(:system_email_address) { expired_email_address }
  413. it 'decrypts with comment' do
  414. expect(mail[:body]).to include(raw_body)
  415. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  416. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to be nil
  417. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be true
  418. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to include(expired_email_address).and include('expired')
  419. end
  420. it_behaves_like 'HttpLog writer', 'success'
  421. end
  422. end
  423. context 'no private key present' do
  424. let(:mail) do
  425. smime_mail = build_mail
  426. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  427. sender_certificate.destroy!
  428. recipient_certificate.destroy!
  429. SecureMailing.incoming(mail)
  430. mail
  431. end
  432. it 'fails' do
  433. expect(mail[:body]).to include('no visible content')
  434. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be false
  435. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to be nil
  436. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be false
  437. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to eq('Unable to find private key to decrypt')
  438. end
  439. it_behaves_like 'HttpLog writer', 'failed'
  440. end
  441. end
  442. context 'with signature verification and decryption' do
  443. let!(:sender_certificate) { create(:smime_certificate, :with_private, fixture: sender_email_address) }
  444. let!(:recipient_certificate) { create(:smime_certificate, :with_private, fixture: recipient_email_address) }
  445. let(:security_preferences) do
  446. {
  447. type: 'S/MIME',
  448. sign: {
  449. success: true,
  450. },
  451. encryption: {
  452. success: true,
  453. },
  454. }
  455. end
  456. let(:mail) do
  457. smime_mail = build_mail
  458. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  459. SecureMailing.incoming(mail)
  460. mail
  461. end
  462. context 'with wrapped mime-type S/MIME signature (e.g. for Microsoft Outlook)' do
  463. before do
  464. # We need to disable the open ssl detached flag, to force the smime-type with 'signed-data'.
  465. stub_const('OpenSSL::PKCS7::DETACHED', nil)
  466. end
  467. it 'check that mail was decrypted' do
  468. expect(mail['x-zammad-article-preferences'][:security][:encryption][:success]).to be true
  469. end
  470. it 'check that encryption comment exists' do
  471. expect(mail['x-zammad-article-preferences'][:security][:encryption][:comment]).to eq(recipient_certificate_subject)
  472. end
  473. it 'check that mail was verified' do
  474. expect(mail['x-zammad-article-preferences'][:security][:sign][:success]).to be true
  475. end
  476. it 'check that signe comment exists' do
  477. expect(mail['x-zammad-article-preferences'][:security][:sign][:comment]).to eq(sender_certificate_subject)
  478. end
  479. it 'check that body was endcrypted and verified' do
  480. expect(mail[:body]).to include(raw_body)
  481. end
  482. end
  483. end
  484. end
  485. describe '.retry' do
  486. let(:sender_email_address) { customer_email_address }
  487. let(:recipient_email_address) { system_email_address }
  488. let(:security_preferences) do
  489. {
  490. type: 'S/MIME',
  491. sign: {
  492. success: true,
  493. },
  494. encryption: {
  495. success: true,
  496. },
  497. }
  498. end
  499. let(:mail) do
  500. sender_certificate = create(:smime_certificate, :with_private, fixture: sender_email_address)
  501. recipient_certificate = create(:smime_certificate, :with_private, fixture: system_email_address)
  502. smime_mail = Channel::EmailBuild.build(
  503. from: sender_email_address,
  504. to: recipient_email_address,
  505. body: raw_body,
  506. content_type: 'text/plain',
  507. security: security_preferences,
  508. attachments: [
  509. {
  510. content_type: 'text/plain',
  511. content: 'blub',
  512. filename: 'test-file1.txt',
  513. },
  514. ],
  515. )
  516. mail = Channel::EmailParser.new.parse(smime_mail.to_s)
  517. sender_certificate.destroy
  518. recipient_certificate.destroy
  519. mail
  520. end
  521. let!(:article) do
  522. _ticket, article, _user, _mail = Channel::EmailParser.new.process({}, mail['raw'] )
  523. article
  524. end
  525. context 'private key added' do
  526. before do
  527. create(:smime_certificate, :with_private, fixture: recipient_email_address)
  528. create(:smime_certificate, fixture: sender_email_address)
  529. end
  530. it 'succeeds' do
  531. SecureMailing.retry(article)
  532. expect(article.preferences[:security][:sign][:success]).to be true
  533. expect(article.preferences[:security][:sign][:comment]).to eq(sender_certificate_subject)
  534. expect(article.preferences[:security][:encryption][:success]).to be true
  535. expect(article.preferences[:security][:encryption][:comment]).to eq(recipient_certificate_subject)
  536. expect(article.body).to include(raw_body)
  537. expect(article.attachments.count).to eq(1)
  538. expect(article.attachments.first.filename).to eq('test-file1.txt')
  539. end
  540. context 'S/MIME activated' do
  541. before do
  542. Setting.set('smime_integration', false)
  543. end
  544. it 'succeeds' do
  545. Setting.set('smime_integration', true)
  546. SecureMailing.retry(article)
  547. expect(article.preferences[:security][:sign][:success]).to be true
  548. expect(article.preferences[:security][:sign][:comment]).to eq(sender_certificate_subject)
  549. expect(article.preferences[:security][:encryption][:success]).to be true
  550. expect(article.preferences[:security][:encryption][:comment]).to eq(recipient_certificate_subject)
  551. expect(article.body).to include(raw_body)
  552. expect(article.attachments.count).to eq(1)
  553. expect(article.attachments.first.filename).to eq('test-file1.txt')
  554. end
  555. end
  556. end
  557. end
  558. end