shared_draft_start_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 'draft saved' do
  88. before do
  89. within :draft_sidebar do
  90. find('.js-name').fill_in with: 'Draft Name'
  91. click '.js-create'
  92. end
  93. end
  94. include_examples 'shared draft ID is present'
  95. end
  96. end
  97. context 'update' do
  98. before do
  99. create(:store_image, o_id: draft.id, object: draft.class.name)
  100. click :draft_sidebar_button
  101. within :draft_sidebar do
  102. click '.label-subtle'
  103. end
  104. in_modal do
  105. click '.js-submit'
  106. end
  107. end
  108. it 'changes content' do
  109. within :active_content do
  110. find(:richtext).send_keys('add update')
  111. click '.js-update'
  112. end
  113. expect(draft.reload.content['body']).to match %r{add update}
  114. end
  115. it 'changes name' do
  116. within :active_content do
  117. find('.js-name').fill_in with: 'new name'
  118. click '.js-update'
  119. end
  120. expect(draft.reload.name).to eq 'new name'
  121. end
  122. it 'requires name' do
  123. within :draft_sidebar do
  124. find('.js-name').fill_in with: ''
  125. expect { click '.js-update' }
  126. .to change { has_css? '.has-error', wait: false }
  127. .to true
  128. end
  129. end
  130. it 'saves as copy' do
  131. within :draft_sidebar do
  132. expect { click '.js-create' }
  133. .to change { Ticket::SharedDraftStart.count }
  134. .by 1
  135. end
  136. end
  137. end
  138. context 'delete' do
  139. it 'works' do
  140. click :draft_sidebar_button
  141. within :draft_sidebar do
  142. click '.label-subtle'
  143. end
  144. in_modal do
  145. click '.js-delete'
  146. end
  147. click_on 'Yes'
  148. expect(Ticket::SharedDraftStart).not_to be_exist(draft.id)
  149. within :draft_sidebar do
  150. expect(page).to have_no_text(draft.name)
  151. end
  152. end
  153. end
  154. context 'preview' do
  155. before do
  156. click :draft_sidebar_button
  157. within :draft_sidebar do
  158. click '.label-subtle'
  159. end
  160. end
  161. it 'shows body' do
  162. in_modal do
  163. expect(page).to have_text(draft_body)
  164. end
  165. end
  166. it 'shows author' do
  167. in_modal do
  168. expect(page).to have_text(User.find(draft.created_by_id).fullname)
  169. end
  170. end
  171. end
  172. context 'apply' do
  173. before do
  174. create(:store_image, o_id: draft.id, object: draft.class.name)
  175. click :draft_sidebar_button
  176. within :draft_sidebar do
  177. click '.label-subtle'
  178. end
  179. in_modal do
  180. click '.js-submit'
  181. end
  182. end
  183. include_examples 'shared draft ID is present'
  184. it 'applies body' do
  185. within :active_content do
  186. expect(page).to have_text draft_body
  187. end
  188. end
  189. it 'applies meta' do
  190. within :active_content do
  191. expect(find('[name=priority_id]').value).to eq draft_options[:priority_id]
  192. end
  193. end
  194. it 'applies attachment' do
  195. within :active_content do
  196. expect(page).to have_text('1x1.png')
  197. end
  198. end
  199. end
  200. end