article_security_examples.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. RSpec.shared_examples 'mobile app: article security' do |integration:, ticket_create: false|
  3. let(:security_name) { integration == :pgp ? 'pgp' : 'smime' }
  4. let(:certificate) { integration == :pgp ? :pgp_key : :smime_certificate }
  5. def authenticate
  6. Setting.set("#{security_name}_integration", true)
  7. Setting.set("#{security_name}_config", security_config) if defined?(security_config)
  8. agent
  9. end
  10. shared_examples 'having available security options' do |encrypt:, sign:|
  11. it "available security options - encrypt: #{encrypt}, sign: #{sign}" do
  12. prepare_email_article
  13. expect { find_outer('Security') }.not_to raise_error
  14. expect(find_button('Encrypt', disabled: !encrypt).disabled?).to be(!encrypt)
  15. expect(find_button('Sign', disabled: !sign).disabled?).to be(!sign)
  16. click('button[aria-describedby="tooltip-security-security"]')
  17. expect(page).to have_css('[aria-label="Security Information"]')
  18. end
  19. end
  20. shared_examples 'saving article' do |encrypt:, sign:|
  21. it "can create a ticket - encrypt: #{encrypt}, sign: #{sign}" do
  22. prepare_email_article with_body: true
  23. submit_form
  24. find('[role=alert]', text: 'Ticket has been created successfully.') if ticket_create
  25. expect(Ticket.last.articles.last.preferences['security']['encryption']['success']).to be(encrypt)
  26. expect(Ticket.last.articles.last.preferences['security']['sign']['success']).to be(sign)
  27. end
  28. end
  29. context 'without certificates present' do
  30. it_behaves_like 'having available security options', encrypt: false, sign: false
  31. it_behaves_like 'saving article', encrypt: false, sign: false
  32. end
  33. context 'with sender certificate present' do
  34. let(:system_email_address) { "#{security_name}1@example.com" }
  35. let(:email_address) { create(:email_address, email: system_email_address) }
  36. let(:group) { create(:group, email_address: email_address) }
  37. before do
  38. create(certificate, :with_private, fixture: system_email_address)
  39. end
  40. it_behaves_like 'having available security options', encrypt: false, sign: true
  41. it_behaves_like 'saving article', encrypt: false, sign: true
  42. context 'with recipient certificate present' do
  43. let(:recipient_email_address) { "#{security_name}2@example.com" }
  44. let(:customer) { create(:customer, email: recipient_email_address) }
  45. before do
  46. create(certificate, fixture: recipient_email_address)
  47. end
  48. it_behaves_like 'having available security options', encrypt: true, sign: true
  49. it_behaves_like 'saving article', encrypt: true, sign: true
  50. it 'hides the security field for phone tickets' do
  51. prepare_phone_article
  52. expect(page).to have_no_css('label', text: 'Security')
  53. end
  54. context 'with default group configuration' do
  55. let(:security_config) do
  56. {
  57. 'group_id' => group_defaults
  58. }
  59. end
  60. let(:group_defaults) do
  61. {
  62. 'default_encryption' => {
  63. group.id.to_s => default_encryption,
  64. },
  65. 'default_sign' => {
  66. group.id.to_s => default_sign,
  67. }
  68. }
  69. end
  70. let(:default_sign) { true }
  71. let(:default_encryption) { true }
  72. shared_examples 'having default security options' do |encrypt:, sign:|
  73. it "default security options - encrypt: #{encrypt}, sign: #{sign}" do
  74. prepare_email_article
  75. expect(find_button('Encrypt')['aria-selected']).to eq(encrypt.to_s)
  76. expect(find_button('Sign')['aria-selected']).to eq(sign.to_s)
  77. end
  78. end
  79. it_behaves_like 'having default security options', encrypt: true, sign: true
  80. context 'when it has no value' do
  81. let(:group_defaults) { {} }
  82. it_behaves_like 'having default security options', encrypt: true, sign: true
  83. end
  84. context 'when signing is disabled' do
  85. let(:default_sign) { false }
  86. it_behaves_like 'having default security options', encrypt: true, sign: false
  87. end
  88. context 'when encryption is disabled' do
  89. let(:default_encryption) { false }
  90. it_behaves_like 'having default security options', encrypt: false, sign: true
  91. end
  92. end
  93. end
  94. end
  95. context 'with recipient certificate present' do
  96. let(:recipient_email_address) { "#{security_name}2@example.com" }
  97. let(:customer) { create(:customer, email: recipient_email_address) }
  98. before do
  99. create(certificate, fixture: recipient_email_address)
  100. end
  101. it_behaves_like 'having available security options', encrypt: true, sign: false
  102. it_behaves_like 'saving article', encrypt: true, sign: false
  103. end
  104. end