shared_draft_zoom_spec.rb 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket Shared Draft Zoom', type: :system, authenticated_as: :authenticate 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 be_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. click '.js-openDropdownMacro'
  77. expect(page).to have_no_selector :draft_save_button
  78. end
  79. end
  80. end
  81. context 'preview' do
  82. before do
  83. visit "ticket/zoom/#{ticket_with_draft.id}"
  84. within :active_content do
  85. click :draft_share_button
  86. end
  87. end
  88. it 'shows content' do
  89. in_modal do
  90. expect(page).to have_text draft_body
  91. end
  92. end
  93. it 'shows author' do
  94. in_modal do
  95. expect(page).to have_text(User.find(draft.created_by_id).fullname)
  96. end
  97. end
  98. end
  99. context 'delete' do
  100. it 'works' do
  101. visit "ticket/zoom/#{ticket_with_draft.id}"
  102. within :active_content do
  103. click :draft_share_button
  104. end
  105. in_modal do
  106. click '.js-delete'
  107. end
  108. click_on 'Yes'
  109. within :active_content do
  110. expect(page).to have_no_selector :draft_share_button
  111. end
  112. end
  113. it 'hides button when another user deletes' do
  114. visit "ticket/zoom/#{ticket_with_draft.id}"
  115. draft.destroy
  116. within :active_content do
  117. expect(page).to have_no_selector :draft_share_button
  118. end
  119. end
  120. end
  121. context 'save' do
  122. it 'creates new draft' do
  123. find('.articleNewEdit-body').send_keys('Some reply')
  124. click '.js-openDropdownMacro'
  125. expect { click :draft_save_button }
  126. .to change { ticket.reload.shared_draft.present? }
  127. .to true
  128. end
  129. context 'with a signature' do
  130. let(:signature) { create(:signature) }
  131. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  132. # https://github.com/zammad/zammad/issues/4042
  133. it 'creates a draft without signature' do
  134. within :active_content do
  135. find('.articleNewEdit-body').send_keys(draft_body)
  136. click '.editControls-item.pop-select'
  137. click '.editControls-icon[data-value="email"]'
  138. click '.js-openDropdownMacro'
  139. click '.js-dropdownActionSaveDraft'
  140. end
  141. wait.until do
  142. draft = Ticket::SharedDraftZoom.last
  143. next false if draft.nil?
  144. expect(draft.new_article).to include(body: draft_body)
  145. end
  146. end
  147. end
  148. it 'shows overwrite warning when draft exists' do
  149. visit "ticket/zoom/#{ticket_with_draft.id}"
  150. within :active_content do
  151. find('.articleNewEdit-body').send_keys('another reply')
  152. click '.js-openDropdownMacro'
  153. click :draft_save_button
  154. end
  155. in_modal do
  156. click '.js-submit'
  157. end
  158. expect(draft.reload.new_article[:body]).to match %r{another reply}
  159. end
  160. context 'draft saved' do
  161. before do
  162. find('.articleNewEdit-body').send_keys('Some reply')
  163. click '.js-openDropdownMacro'
  164. click :draft_save_button
  165. end
  166. include_examples 'shared draft ID is present'
  167. end
  168. context 'draft loaded' do
  169. before do
  170. visit "ticket/zoom/#{ticket_with_draft.id}"
  171. click :draft_share_button
  172. in_modal do
  173. click '.js-submit'
  174. end
  175. end
  176. it 'updates existing draft' do
  177. click '.js-openDropdownMacro'
  178. click :draft_save_button
  179. expect(draft.reload.new_article[:body]).to match %r{draft here}
  180. end
  181. it 'shows overwrite warning when draft edited after loading' do
  182. find('.articleNewEdit-body').send_keys('another reply')
  183. click '.js-openDropdownMacro'
  184. click :draft_save_button
  185. in_modal do
  186. click '.js-submit'
  187. end
  188. expect(draft.reload.new_article[:body]).to match %r{another reply}
  189. end
  190. end
  191. end
  192. context 'apply' do
  193. before do
  194. create(:store_image, o_id: draft.id, object: draft.class.name)
  195. visit "ticket/zoom/#{ticket_with_draft.id}"
  196. click :draft_share_button
  197. in_modal do
  198. click '.js-submit'
  199. end
  200. end
  201. include_examples 'shared draft ID is present'
  202. it 'applies new article body' do
  203. expect(page).to have_text draft_body
  204. end
  205. it 'applies sidebar changes' do
  206. expect(find('[name=priority_id]').value).to eq draft.ticket_attributes[:priority_id]
  207. end
  208. it 'applies attachment' do
  209. expect(page).to have_text('1x1.png')
  210. end
  211. context 'with a signature' do
  212. let(:signature_body) { 'Sample signature here' }
  213. let(:signature) { create(:signature, body: signature_body) }
  214. let(:group) { create(:group, shared_drafts: group_shared_drafts, signature: signature) }
  215. let(:draft_type) { 'email' }
  216. # https://github.com/zammad/zammad/issues/4042
  217. it 'applies with a signature' do
  218. within :active_content do
  219. expect(page).to have_text(signature_body).and(have_text(draft_body))
  220. end
  221. end
  222. end
  223. end
  224. context 'create ticket article' do
  225. before do
  226. visit "ticket/zoom/#{ticket_with_draft.id}"
  227. click :draft_share_button
  228. in_modal do
  229. click '.js-submit'
  230. end
  231. within :active_content do
  232. click '.js-submit'
  233. end
  234. end
  235. let(:draft_type) { 'phone' }
  236. it 'creates article with type' do
  237. wait.until do
  238. article = ticket_with_draft.articles.reload.first
  239. next false if !article
  240. expect(article).to have_attributes(
  241. type: Ticket::Article::Type.lookup(name: 'phone'),
  242. internal: true,
  243. body: article.body
  244. )
  245. end
  246. end
  247. context 'when draft is public' do
  248. let(:draft_internal) { false }
  249. it 'creates article with selected visibility' do
  250. wait.until do
  251. article = ticket_with_draft.articles.reload.first
  252. next false if !article
  253. expect(article).to have_attributes(
  254. type: Ticket::Article::Type.lookup(name: 'phone'),
  255. internal: false,
  256. body: article.body
  257. )
  258. end
  259. end
  260. end
  261. end
  262. end