checklist_spec.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Checklist, :aggregate_failures, current_user_id: 1, type: :model do
  4. let(:ticket) { create(:ticket) }
  5. let(:checklist) { create(:checklist, item_count: 0, ticket:) }
  6. describe 'validations' do
  7. context 'with valid attributes' do
  8. it 'succeeds creation' do
  9. expect(create(:checklist)).to be_persisted
  10. end
  11. end
  12. end
  13. describe '#complete' do
  14. it 'returns zero if list is empty' do
  15. expect(checklist.complete).to be_zero
  16. end
  17. it 'returns count of completed items' do
  18. 3.times { create(:checklist_item, checklist:, checked: true) }
  19. 2.times { create(:checklist_item, checklist:, checked: false) }
  20. expect(checklist.complete).to be 3
  21. end
  22. end
  23. describe '#completed?' do
  24. it 'returns true if list is empty' do
  25. expect(checklist).to be_completed
  26. end
  27. it 'returns true if all completed' do
  28. create(:checklist_item, checklist:, checked: true)
  29. create(:checklist_item, checklist:, checked: true)
  30. expect(checklist).to be_completed
  31. end
  32. it 'returns false if some incomplete' do
  33. create(:checklist_item, checklist:, checked: true)
  34. create(:checklist_item, checklist:, checked: false)
  35. expect(checklist).not_to be_completed
  36. end
  37. end
  38. describe '#incomplete' do
  39. it 'returns count of completed items' do
  40. 3.times { create(:checklist_item, checklist:, checked: true) }
  41. 2.times { create(:checklist_item, checklist:, checked: false) }
  42. expect(checklist.incomplete).to be 2
  43. end
  44. end
  45. describe '#total' do
  46. it 'returns count of completed items' do
  47. 3.times { create(:checklist_item, checklist:, checked: true) }
  48. 2.times { create(:checklist_item, checklist:, checked: false) }
  49. expect(checklist.total).to be 5
  50. end
  51. end
  52. describe '#update_ticket' do
  53. it 'touches ticket when updating the checklist' do
  54. checklist
  55. travel 10.minutes
  56. expect { checklist.update!(name: 'abc') }
  57. .to change { checklist.ticket.updated_at }
  58. end
  59. it 'touches ticket when destroying the checklist' do
  60. checklist
  61. travel 10.minutes
  62. expect { checklist.destroy! }
  63. .to change { checklist.ticket.updated_at }
  64. end
  65. it 'does not raise erorrs when ticket is destroyed' do
  66. checklist
  67. expect { checklist.ticket.destroy! }
  68. .not_to raise_error
  69. end
  70. end
  71. describe '.ticket_closed?' do
  72. it 'open ticket is not closed' do
  73. ticket = create(:ticket, state_name: 'open')
  74. expect(described_class).not_to be_ticket_closed(ticket)
  75. end
  76. it 'new ticket is not closed' do
  77. ticket = create(:ticket, state_name: 'new')
  78. expect(described_class).not_to be_ticket_closed(ticket)
  79. end
  80. it 'closed ticket is closed' do
  81. ticket = create(:ticket, state_name: 'closed')
  82. expect(described_class).to be_ticket_closed(ticket)
  83. end
  84. it 'merged ticket is closed' do
  85. ticket = create(:ticket, state_name: 'merged')
  86. expect(described_class).to be_ticket_closed(ticket)
  87. end
  88. end
  89. describe '.create_fresh!' do
  90. it 'creates a fresh checklist' do
  91. checklist = described_class.create_fresh!(ticket)
  92. expect(checklist.items).to contain_exactly(have_attributes(id: be_present, text: be_blank))
  93. end
  94. it 'does not create a checklist if the ticket already has one' do
  95. checklist
  96. expect { described_class.create_fresh!(ticket) }
  97. .to raise_error(
  98. ActiveRecord::RecordInvalid,
  99. 'Validation failed: This ticket already has a checklist.'
  100. )
  101. end
  102. end
  103. describe '.create_from_template!' do
  104. let(:template) { create(:checklist_template) }
  105. it 'creates a checklist' do
  106. checklist_from_template = described_class.create_from_template!(ticket, template)
  107. expect(checklist_from_template).to be_persisted
  108. end
  109. it 'copies entries in order' do
  110. checklist_from_template = described_class.create_from_template!(ticket, template)
  111. expect(checklist_from_template.sorted_items.map(&:text)).to eq(template.items.map(&:text))
  112. end
  113. it 'copies entries with initial_clone flag' do
  114. checklist_from_template = described_class.create_from_template!(ticket, template)
  115. expect(checklist_from_template.items).to all(have_attributes(initial_clone: true))
  116. end
  117. it 'raises an error if template is inactive' do
  118. template.update! active: false
  119. expect { described_class.create_from_template!(ticket, template) }
  120. .to raise_error(
  121. Exceptions::UnprocessableEntity,
  122. 'Checklist template must be active to use as a checklist starting point.'
  123. )
  124. end
  125. it 'does not create a checklist if the ticket already has one' do
  126. checklist
  127. expect { described_class.create_from_template!(ticket, template) }
  128. .to raise_error(
  129. ActiveRecord::RecordInvalid,
  130. 'Validation failed: This ticket already has a checklist.'
  131. )
  132. end
  133. end
  134. describe '.tickets_referencing' do
  135. let(:ticket) { create(:ticket, group:) }
  136. let(:other_ticket) { create(:ticket, group:) }
  137. let(:inaccessible_ticket) { create(:ticket) }
  138. let(:checklist) { create(:checklist) }
  139. let(:other_checklist) { create(:checklist) }
  140. let(:inaccessible_checklist) { create(:checklist, ticket: inaccessible_ticket) }
  141. let(:user) { create(:agent, groups: [group]) }
  142. let(:group) { create(:group) }
  143. before do
  144. other_checklist.items.create! ticket: other_ticket
  145. end
  146. it 'returns scope to work on' do
  147. expect(described_class.tickets_referencing(ticket)).to be_a(ActiveRecord::Relation)
  148. end
  149. it 'if user is given returns scope to work on' do
  150. expect(described_class.tickets_referencing(ticket, user)).to be_a(ActiveRecord::Relation)
  151. end
  152. context 'when ticket never referenced' do
  153. it 'returns 0 references' do
  154. expect(described_class.tickets_referencing(ticket)).to be_blank
  155. end
  156. end
  157. context 'when ticket is referenced' do
  158. before do
  159. checklist.items.create! ticket: ticket
  160. end
  161. it 'returns 1 reference' do
  162. expect(described_class.tickets_referencing(ticket))
  163. .to contain_exactly(checklist.ticket)
  164. end
  165. end
  166. context 'when ticket is referenced in multiple checklists' do
  167. before do
  168. checklist.items.create! ticket: ticket
  169. other_checklist.items.create! ticket: ticket
  170. end
  171. it 'returns 2 references' do
  172. expect(described_class.tickets_referencing(ticket))
  173. .to contain_exactly(checklist.ticket, other_checklist.ticket)
  174. end
  175. end
  176. context 'when ticket is referenced multiple times in the same checklist' do
  177. before do
  178. 3.times { checklist.items.create! ticket: ticket }
  179. end
  180. it 'returns 2 references' do
  181. expect(described_class.tickets_referencing(ticket))
  182. .to contain_exactly(checklist.ticket)
  183. end
  184. end
  185. context 'with inaccessible reference' do
  186. before do
  187. checklist.update! ticket: other_ticket
  188. checklist.items.create! ticket: ticket
  189. inaccessible_checklist.items.create! ticket: ticket
  190. end
  191. it 'returns tickets accessible to given user only' do
  192. expect(described_class.tickets_referencing(ticket, user))
  193. .to contain_exactly(checklist.ticket)
  194. end
  195. it 'returns all tickets accessible if no user given' do
  196. expect(described_class.tickets_referencing(ticket))
  197. .to contain_exactly(checklist.ticket, inaccessible_checklist.ticket)
  198. end
  199. end
  200. end
  201. describe '.search_index_attribute_lookup' do
  202. subject(:checklist) { create(:checklist, name: 'some name') }
  203. it 'verify name attribute' do
  204. expect(checklist.search_index_attribute_lookup['name']).to eq checklist.name
  205. end
  206. it 'verify items attribute' do
  207. expect(checklist.search_index_attribute_lookup['items']).not_to eq []
  208. end
  209. it 'verify items[0].count' do
  210. expect(checklist.search_index_attribute_lookup['items'].count).to eq checklist.items.count
  211. end
  212. it 'verify items[0].text attribute' do
  213. expect(checklist.search_index_attribute_lookup['items'][0]['text']).to eq checklist.items[0].text
  214. end
  215. end
  216. end