shared_draft_start_spec.rb 7.0 KB

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