shared_draft_start_spec.rb 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket Shared Draft Start', authenticated_as: :authenticate, type: :system do
  4. let(:group) { create(:group, shared_drafts: group_shared_drafts) }
  5. let(:group_access) { :full }
  6. let(:group_shared_drafts) { true }
  7. let(:draft) { create(:ticket_shared_draft_start, group: group, content: draft_content) }
  8. let(:draft_body) { 'draft body' }
  9. let(:draft_options) { { priority_id: '3' } }
  10. let(:draft_content) do
  11. {
  12. body: draft_body
  13. }.merge draft_options
  14. end
  15. let(:user) do
  16. user = create(:agent)
  17. user.user_groups.create! group: group, access: group_access
  18. user
  19. end
  20. def authenticate
  21. draft
  22. user
  23. end
  24. before do
  25. visit '/'
  26. click '.settings.add'
  27. end
  28. shared_examples 'shared draft ID is present' do
  29. it 'sets shared draft ID' do
  30. within :active_content do
  31. elem = find('.ticket-create input[name=shared_draft_id]', visible: :all)
  32. expect(Ticket::SharedDraftStart).to exist(elem.value)
  33. end
  34. end
  35. end
  36. context 'sidebar' do
  37. context 'given multiple groups' do
  38. let(:another_group) { create(:group, shared_drafts: false) }
  39. def authenticate
  40. user.user_groups.create! group: another_group, access: :full
  41. user
  42. end
  43. it 'not visible without group selected' do
  44. expect(page).to have_no_selector :draft_sidebar_button
  45. end
  46. it 'not visible when group with disabled draft selected' do
  47. within(:active_content) do
  48. set_tree_select_value('group_id', another_group.name)
  49. end
  50. expect(page).to have_no_selector :draft_sidebar_button
  51. end
  52. it 'visible when group with active draft selected' do
  53. within(:active_content) do
  54. set_tree_select_value('group_id', group.name)
  55. end
  56. expect(page).to have_selector :draft_sidebar_button
  57. end
  58. end
  59. context 'when single group' do
  60. it 'visible' do
  61. expect(page).to have_selector :draft_sidebar_button
  62. end
  63. context 'when drafts disabled' do
  64. let(:group_shared_drafts) { false }
  65. it 'not visible' do
  66. expect(page).to have_no_selector :draft_sidebar_button
  67. end
  68. end
  69. end
  70. end
  71. context 'create' do
  72. before { click :draft_sidebar_button }
  73. it 'prevents a draft creation without name' do
  74. within :draft_sidebar do
  75. expect { click '.js-create' }
  76. .to change { has_css? '.has-error', wait: false }
  77. .to true
  78. end
  79. end
  80. it 'create a draft with name' do
  81. within :draft_sidebar do
  82. find('.js-name').fill_in with: 'Draft Name'
  83. expect { click '.js-create' }
  84. .to change(Ticket::SharedDraftStart, :count)
  85. .by 1
  86. end
  87. end
  88. context 'with a signature' do
  89. let(:signature) { create(:signature) }
  90. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  91. # https://github.com/zammad/zammad/issues/4042
  92. it 'creates a draft without signature' do
  93. within :active_content do
  94. find('div[data-name=body]').send_keys draft_body
  95. find('[data-type=email-out]').click
  96. end
  97. within :draft_sidebar do
  98. find('.js-name').fill_in with: 'Draft Name'
  99. click '.js-create'
  100. end
  101. wait.until do
  102. draft = Ticket::SharedDraftStart.last
  103. next false if draft.nil?
  104. expect(draft.content).to include(body: draft_body)
  105. end
  106. end
  107. end
  108. context 'draft saved' do
  109. before do
  110. within :draft_sidebar do
  111. find('.js-name').fill_in with: 'Draft Name'
  112. click '.js-create'
  113. end
  114. end
  115. include_examples 'shared draft ID is present'
  116. end
  117. end
  118. context 'update' do
  119. before do
  120. create(:store, :image, o_id: draft.id, object: draft.class.name)
  121. click :draft_sidebar_button
  122. within :draft_sidebar do
  123. click '.text-muted'
  124. end
  125. in_modal do
  126. click '.js-submit'
  127. end
  128. end
  129. it 'changes content' do
  130. within :active_content do
  131. find(:richtext).send_keys('add update')
  132. click '.js-update'
  133. end
  134. expect(draft.reload.content['body']).to match %r{add update}
  135. end
  136. it 'changes name' do
  137. within :active_content do
  138. find('.js-name').fill_in with: 'new name'
  139. click '.js-update'
  140. end
  141. expect(draft.reload.name).to eq 'new name'
  142. end
  143. it 'requires name' do
  144. within :draft_sidebar do
  145. find('.js-name').fill_in with: ''
  146. expect { click '.js-update' }
  147. .to change { has_css? '.has-error', wait: false }
  148. .to true
  149. end
  150. end
  151. it 'saves as copy' do
  152. within :draft_sidebar do
  153. expect { click '.js-create' }
  154. .to change(Ticket::SharedDraftStart, :count)
  155. .by 1
  156. end
  157. end
  158. end
  159. context 'delete' do
  160. it 'works as expected' do
  161. click :draft_sidebar_button
  162. within :draft_sidebar do
  163. click '.text-muted'
  164. end
  165. in_modal do
  166. click '.js-delete'
  167. end
  168. click_on 'Yes'
  169. expect(Ticket::SharedDraftStart).not_to exist(draft.id)
  170. within :draft_sidebar do
  171. expect(page).to have_no_text(draft.name)
  172. end
  173. end
  174. end
  175. context 'preview' do
  176. before do
  177. click :draft_sidebar_button
  178. within :draft_sidebar do
  179. click '.text-muted'
  180. end
  181. end
  182. it 'shows body' do
  183. in_modal do
  184. expect(page).to have_text(draft_body)
  185. end
  186. end
  187. it 'shows author' do
  188. in_modal do
  189. expect(page).to have_text(User.find(draft.created_by_id).fullname)
  190. end
  191. end
  192. end
  193. context 'apply' do
  194. before do
  195. create(:store, :image, o_id: draft.id, object: draft.class.name)
  196. click :draft_sidebar_button
  197. within :draft_sidebar do
  198. click '.text-muted'
  199. end
  200. in_modal do
  201. click '.js-submit'
  202. end
  203. end
  204. include_examples 'shared draft ID is present'
  205. it 'applies body' do
  206. within :active_content do
  207. expect(page).to have_text draft_body
  208. end
  209. end
  210. it 'applies meta' do
  211. within :active_content do
  212. expect(find('[name=priority_id]').value).to eq draft_options[:priority_id]
  213. end
  214. end
  215. it 'applies attachment' do
  216. within :active_content do
  217. expect(page).to have_text('1x1.png')
  218. end
  219. end
  220. context 'with a signature' do
  221. let(:signature_body) { 'Sample signature here' }
  222. let(:signature) { create(:signature, body: signature_body) }
  223. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  224. let(:draft_options) { { priority_id: '3', formSenderType: 'email-out' } }
  225. # https://github.com/zammad/zammad/issues/4042
  226. it 'applies with a signature' do
  227. within :active_content do
  228. expect(page).to have_text(signature_body).and(have_text(draft_body))
  229. end
  230. end
  231. end
  232. end
  233. end