shared_draft_zoom_spec.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket Shared Draft Zoom', 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(:ticket) { create(:ticket, group: group) }
  8. let(:ticket_with_draft) { create(:ticket, group: group) }
  9. let(:draft_body) { 'draft here' }
  10. let(:draft_type) { 'note' }
  11. let(:draft_internal) { true }
  12. let(:draft) do
  13. create(:ticket_shared_draft_zoom,
  14. ticket: ticket_with_draft,
  15. new_article: { body: draft_body, type: draft_type, internal: draft_internal },
  16. ticket_attributes: { priority_id: '3' })
  17. end
  18. let(:user) do
  19. user = create(:agent)
  20. user.user_groups.create! group: group, access: group_access
  21. user
  22. end
  23. def authenticate
  24. draft
  25. user
  26. end
  27. before do
  28. visit "ticket/zoom/#{ticket.id}"
  29. end
  30. shared_examples 'shared draft ID is present' do
  31. it 'sets shared draft ID' do
  32. within :active_content do
  33. elem = find('.article-add input[name=shared_draft_id]', visible: :all)
  34. expect(Ticket::SharedDraftZoom).to exist(elem.value)
  35. end
  36. end
  37. end
  38. context 'buttons' do
  39. context 'when drafts disabled for the group' do
  40. let(:group_shared_drafts) { false }
  41. it 'share button not visible' do
  42. expect(page).to have_no_selector :draft_share_button
  43. end
  44. it 'save button not visible' do
  45. click '.js-openDropdownMacro'
  46. expect(page).to have_no_selector :draft_save_button
  47. end
  48. end
  49. context 'when drafts enabled for the group' do
  50. it 'share button not visible initially' do
  51. expect(page).to have_no_selector :draft_share_button
  52. end
  53. it 'save button visible' do
  54. expect(page).to have_selector(:draft_save_button, visible: :all)
  55. end
  56. it 'share button visible when draft exists' do
  57. visit "ticket/zoom/#{ticket_with_draft.id}"
  58. within :active_content do
  59. expect(page).to have_selector :draft_share_button
  60. end
  61. end
  62. it 'share button appears when other user creates draft' do
  63. create(:ticket_shared_draft_zoom, ticket: ticket)
  64. expect(page).to have_selector :draft_share_button
  65. end
  66. end
  67. context 'when insufficient permissions' do
  68. let(:group_access) { :read }
  69. it 'share button not visible when draft exists' do
  70. visit "ticket/zoom/#{ticket_with_draft.id}"
  71. within :active_content do
  72. expect(page).to have_no_selector :draft_share_button
  73. end
  74. end
  75. it 'save button not visible' do
  76. expect(page).to have_no_selector :draft_save_button
  77. end
  78. end
  79. end
  80. context 'preview' do
  81. before do
  82. visit "ticket/zoom/#{ticket_with_draft.id}"
  83. within :active_content do
  84. click :draft_share_button
  85. end
  86. end
  87. it 'shows content' do
  88. in_modal do
  89. expect(page).to have_text draft_body
  90. end
  91. end
  92. it 'shows author' do
  93. in_modal do
  94. expect(page).to have_text(User.find(draft.created_by_id).fullname)
  95. end
  96. end
  97. end
  98. context 'delete' do
  99. it 'works as expected' do
  100. visit "ticket/zoom/#{ticket_with_draft.id}"
  101. within :active_content do
  102. click :draft_share_button
  103. end
  104. in_modal do
  105. click '.js-delete'
  106. end
  107. click_on 'Yes'
  108. within :active_content do
  109. expect(page).to have_no_selector :draft_share_button
  110. end
  111. end
  112. it 'hides button when another user deletes' do
  113. visit "ticket/zoom/#{ticket_with_draft.id}"
  114. draft.destroy
  115. within :active_content do
  116. expect(page).to have_no_selector :draft_share_button
  117. end
  118. end
  119. end
  120. context 'save' do
  121. it 'creates new draft' do
  122. find('.articleNewEdit-body').send_keys('Some reply')
  123. click '.js-openDropdownMacro'
  124. expect { click :draft_save_button }
  125. .to change { ticket.reload.shared_draft.present? }
  126. .to true
  127. end
  128. context 'with a signature' do
  129. let(:signature) { create(:signature) }
  130. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  131. # https://github.com/zammad/zammad/issues/4042
  132. it 'creates a draft without signature' do
  133. within :active_content do
  134. find('.articleNewEdit-body').send_keys(draft_body)
  135. click '.editControls-item.pop-select'
  136. click '.editControls-icon[data-value="email"]'
  137. click '.js-openDropdownMacro'
  138. click '.js-dropdownActionSaveDraft'
  139. end
  140. wait.until do
  141. draft = Ticket::SharedDraftZoom.last
  142. next false if draft.nil?
  143. expect(draft.new_article).to include(body: draft_body)
  144. end
  145. end
  146. end
  147. it 'shows overwrite warning when draft exists' do
  148. visit "ticket/zoom/#{ticket_with_draft.id}"
  149. within :active_content do
  150. find('.articleNewEdit-body').send_keys('another reply')
  151. click '.js-openDropdownMacro'
  152. click :draft_save_button
  153. end
  154. in_modal do
  155. click '.js-submit'
  156. end
  157. expect(draft.reload.new_article[:body]).to match %r{another reply}
  158. end
  159. context 'draft saved' do
  160. before do
  161. find('.articleNewEdit-body').send_keys('Some reply')
  162. click '.js-openDropdownMacro'
  163. click :draft_save_button
  164. end
  165. include_examples 'shared draft ID is present'
  166. end
  167. context 'draft loaded' do
  168. before do
  169. visit "ticket/zoom/#{ticket_with_draft.id}"
  170. click :draft_share_button
  171. in_modal do
  172. click '.js-submit'
  173. end
  174. end
  175. it 'updates existing draft' do
  176. click '.js-openDropdownMacro'
  177. click :draft_save_button
  178. expect(draft.reload.new_article[:body]).to match %r{draft here}
  179. end
  180. it 'shows overwrite warning when draft edited after loading' do
  181. find('.articleNewEdit-body').send_keys('another reply')
  182. click '.js-openDropdownMacro'
  183. click :draft_save_button
  184. in_modal do
  185. click '.js-submit'
  186. end
  187. expect(draft.reload.new_article[:body]).to match %r{another reply}
  188. end
  189. end
  190. end
  191. context 'apply' do
  192. before do
  193. create(:store, :image, o_id: draft.id, object: draft.class.name)
  194. visit "ticket/zoom/#{ticket_with_draft.id}"
  195. click :draft_share_button
  196. in_modal do
  197. click '.js-submit'
  198. end
  199. end
  200. include_examples 'shared draft ID is present'
  201. it 'applies new article body' do
  202. expect(page).to have_text draft_body
  203. end
  204. it 'applies sidebar changes' do
  205. expect(find('[name=priority_id]').value).to eq draft.ticket_attributes[:priority_id]
  206. end
  207. it 'applies attachment' do
  208. expect(page).to have_text('1x1.png')
  209. end
  210. context 'with a signature' do
  211. let(:signature_body) { 'Sample signature here' }
  212. let(:signature) { create(:signature, body: signature_body) }
  213. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  214. let(:draft_type) { 'email' }
  215. # https://github.com/zammad/zammad/issues/4042
  216. it 'applies with a signature' do
  217. within :active_content do
  218. expect(page).to have_text(signature_body).and(have_text(draft_body))
  219. end
  220. end
  221. end
  222. end
  223. context 'create ticket article' do
  224. before do
  225. visit "ticket/zoom/#{ticket_with_draft.id}"
  226. click :draft_share_button
  227. in_modal do
  228. click '.js-submit'
  229. end
  230. within :active_content do
  231. click '.js-submit'
  232. end
  233. end
  234. let(:draft_type) { 'phone' }
  235. it 'creates article with type' do
  236. wait.until do
  237. article = ticket_with_draft.articles.reload.first
  238. next false if !article
  239. expect(article).to have_attributes(
  240. type: Ticket::Article::Type.lookup(name: 'phone'),
  241. internal: true,
  242. body: article.body
  243. )
  244. end
  245. end
  246. context 'when draft is public' do
  247. let(:draft_internal) { false }
  248. it 'creates article with selected visibility' do
  249. wait.until do
  250. article = ticket_with_draft.articles.reload.first
  251. next false if !article
  252. expect(article).to have_attributes(
  253. type: Ticket::Article::Type.lookup(name: 'phone'),
  254. internal: false,
  255. body: article.body
  256. )
  257. end
  258. end
  259. end
  260. end
  261. end