create_spec.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. require 'rails_helper'
  2. require 'system/examples/text_modules_examples'
  3. RSpec.describe 'Ticket Create', type: :system do
  4. context 'when applying ticket templates' do
  5. let(:agent) { create(:agent_user, groups: [permitted_group]) }
  6. let(:permitted_group) { create(:group) }
  7. let(:unpermitted_group) { create(:group) }
  8. let!(:template) { create(:template, :dummy_data, group: unpermitted_group, owner: agent) }
  9. # Regression test for issue #2424 - Unavailable ticket template attributes get applied
  10. it 'unavailable attributes do not get applied', authenticated: -> { agent } do
  11. visit 'ticket/create'
  12. use_template(template)
  13. expect(page).not_to have_selector 'select[name="group_id"]'
  14. end
  15. end
  16. context 'when using text modules' do
  17. include_examples 'text modules', path: 'ticket/create'
  18. end
  19. context 'S/MIME' do
  20. prepend_before do
  21. Setting.set('smime_integration', true)
  22. end
  23. context 'no certificate present' do
  24. let!(:template) { create(:template, :dummy_data) }
  25. it 'has no security selections' do
  26. visit 'ticket/create'
  27. within(:active_content) do
  28. use_template(template)
  29. expect(page).not_to have_css('div.js-securityEncrypt.btn--active', wait: 5)
  30. expect(page).not_to have_css('div.js-securitySign.btn--active', wait: 5)
  31. click '.js-submit'
  32. expect(page).to have_css('.ticket-article-item', count: 1)
  33. open_article_meta
  34. expect(page).not_to have_css('span', text: 'Signed')
  35. expect(page).not_to have_css('span', text: 'Encrypted')
  36. security_result = Ticket::Article.last.preferences['security']
  37. expect(security_result['encryption']['success']).to be nil
  38. expect(security_result['sign']['success']).to be nil
  39. end
  40. end
  41. end
  42. context 'private key configured', authenticated: -> { agent } do
  43. let!(:template) { create(:template, :dummy_data, group: group, owner: agent, customer: customer) }
  44. let(:system_email_address) { 'smime1@example.com' }
  45. let(:email_address) { create(:email_address, email: system_email_address) }
  46. let(:group) { create(:group, email_address: email_address) }
  47. let(:agent_groups) { [group] }
  48. let(:agent) { create(:agent_user, groups: agent_groups) }
  49. before do
  50. create(:smime_certificate, :with_private, fixture: system_email_address)
  51. end
  52. context 'recipient certificate present' do
  53. let(:recipient_email_address) { 'smime2@example.com' }
  54. let(:customer) { create(:customer_user, email: recipient_email_address) }
  55. before do
  56. create(:smime_certificate, fixture: recipient_email_address)
  57. end
  58. it 'plain' do
  59. visit 'ticket/create'
  60. within(:active_content) do
  61. use_template(template)
  62. # wait till S/MIME check AJAX call is ready
  63. expect(page).to have_css('div.js-securityEncrypt.btn--active', wait: 5)
  64. expect(page).to have_css('div.js-securitySign.btn--active', wait: 5)
  65. # deactivate encryption and signing
  66. click '.js-securityEncrypt'
  67. click '.js-securitySign'
  68. click '.js-submit'
  69. expect(page).to have_css('.ticket-article-item', count: 1)
  70. open_article_meta
  71. expect(page).not_to have_css('span', text: 'Signed')
  72. expect(page).not_to have_css('span', text: 'Encrypted')
  73. security_result = Ticket::Article.last.preferences['security']
  74. expect(security_result['encryption']['success']).to be nil
  75. expect(security_result['sign']['success']).to be nil
  76. end
  77. end
  78. it 'signed' do
  79. visit 'ticket/create'
  80. within(:active_content) do
  81. use_template(template)
  82. # wait till S/MIME check AJAX call is ready
  83. expect(page).to have_css('div.js-securityEncrypt.btn--active', wait: 5)
  84. expect(page).to have_css('div.js-securitySign.btn--active', wait: 5)
  85. # deactivate encryption
  86. click '.js-securityEncrypt'
  87. click '.js-submit'
  88. expect(page).to have_css('.ticket-article-item', count: 1)
  89. open_article_meta
  90. expect(page).to have_css('span', text: 'Signed')
  91. expect(page).not_to have_css('span', text: 'Encrypted')
  92. security_result = Ticket::Article.last.preferences['security']
  93. expect(security_result['encryption']['success']).to be nil
  94. expect(security_result['sign']['success']).to be true
  95. end
  96. end
  97. it 'encrypted' do
  98. visit 'ticket/create'
  99. within(:active_content) do
  100. use_template(template)
  101. # wait till S/MIME check AJAX call is ready
  102. expect(page).to have_css('div.js-securityEncrypt.btn--active', wait: 5)
  103. expect(page).to have_css('div.js-securitySign.btn--active', wait: 5)
  104. # deactivate signing
  105. click '.js-securitySign'
  106. click '.js-submit'
  107. expect(page).to have_css('.ticket-article-item', count: 1)
  108. open_article_meta
  109. expect(page).not_to have_css('span', text: 'Signed')
  110. expect(page).to have_css('span', text: 'Encrypted')
  111. security_result = Ticket::Article.last.preferences['security']
  112. expect(security_result['encryption']['success']).to be true
  113. expect(security_result['sign']['success']).to be nil
  114. end
  115. end
  116. it 'signed and encrypted' do
  117. visit 'ticket/create'
  118. within(:active_content) do
  119. use_template(template)
  120. # wait till S/MIME check AJAX call is ready
  121. expect(page).to have_css('div.js-securityEncrypt.btn--active', wait: 5)
  122. expect(page).to have_css('div.js-securitySign.btn--active', wait: 5)
  123. click '.js-submit'
  124. expect(page).to have_css('.ticket-article-item', count: 1)
  125. open_article_meta
  126. expect(page).to have_css('span', text: 'Signed')
  127. expect(page).to have_css('span', text: 'Encrypted')
  128. security_result = Ticket::Article.last.preferences['security']
  129. expect(security_result['encryption']['success']).to be true
  130. expect(security_result['sign']['success']).to be true
  131. end
  132. end
  133. context 'Group default behavior' do
  134. let(:smime_config) { {} }
  135. before do
  136. Setting.set('smime_config', smime_config)
  137. end
  138. shared_examples 'security defaults example' do |sign:, encrypt:|
  139. it "security defaults sign: #{sign}, encrypt: #{encrypt}" do
  140. within(:active_content) do
  141. encrypt_button = find('.js-securityEncrypt', wait: 5)
  142. sign_button = find('.js-securitySign', wait: 5)
  143. await_empty_ajax_queue
  144. active_button_class = '.btn--active'
  145. expect(encrypt_button.matches_css?(active_button_class, wait: 2)).to be(encrypt)
  146. expect(sign_button.matches_css?(active_button_class, wait: 2)).to be(sign)
  147. end
  148. end
  149. end
  150. shared_examples 'security defaults' do |sign:, encrypt:|
  151. before do
  152. visit 'ticket/create'
  153. within(:active_content) do
  154. use_template(template)
  155. end
  156. end
  157. include_examples 'security defaults example', sign: sign, encrypt: encrypt
  158. end
  159. shared_examples 'security defaults group change' do |sign:, encrypt:|
  160. before do
  161. visit 'ticket/create'
  162. within(:active_content) do
  163. use_template(template)
  164. await_empty_ajax_queue
  165. select new_group.name, from: 'group_id'
  166. end
  167. end
  168. include_examples 'security defaults example', sign: sign, encrypt: encrypt
  169. end
  170. context 'not configured' do
  171. it_behaves_like 'security defaults', sign: true, encrypt: true
  172. end
  173. context 'configuration present' do
  174. let(:smime_config) do
  175. {
  176. 'group_id' => group_defaults
  177. }
  178. end
  179. let(:group_defaults) do
  180. {
  181. 'default_encryption' => {
  182. group.id.to_s => default_encryption,
  183. },
  184. 'default_sign' => {
  185. group.id.to_s => default_sign,
  186. }
  187. }
  188. end
  189. let(:default_sign) { true }
  190. let(:default_encryption) { true }
  191. shared_examples 'sign and encrypt variations' do |check_examples_name|
  192. it_behaves_like check_examples_name, sign: true, encrypt: true
  193. context 'no value' do
  194. let(:group_defaults) { {} }
  195. it_behaves_like check_examples_name, sign: true, encrypt: true
  196. end
  197. context 'signing disabled' do
  198. let(:default_sign) { false }
  199. it_behaves_like check_examples_name, sign: false, encrypt: true
  200. end
  201. context 'encryption disabled' do
  202. let(:default_encryption) { false }
  203. it_behaves_like check_examples_name, sign: true, encrypt: false
  204. end
  205. end
  206. context 'same Group' do
  207. it_behaves_like 'sign and encrypt variations', 'security defaults'
  208. end
  209. context 'Group change' do
  210. let(:new_group) { create(:group, email_address: email_address) }
  211. let(:agent_groups) { [group, new_group] }
  212. let(:group_defaults) do
  213. {
  214. 'default_encryption' => {
  215. new_group.id.to_s => default_encryption,
  216. },
  217. 'default_sign' => {
  218. new_group.id.to_s => default_sign,
  219. }
  220. }
  221. end
  222. it_behaves_like 'sign and encrypt variations', 'security defaults group change'
  223. end
  224. end
  225. end
  226. end
  227. end
  228. end
  229. end