create_spec.rb 10 KB

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