ticket_spec.rb 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/concerns/can_be_imported_examples'
  5. require 'models/concerns/can_csv_import_examples'
  6. require 'models/concerns/checks_core_workflow_examples'
  7. require 'models/concerns/has_history_examples'
  8. require 'models/concerns/has_tags_examples'
  9. require 'models/concerns/has_taskbars_examples'
  10. require 'models/concerns/has_xss_sanitized_note_examples'
  11. require 'models/concerns/has_object_manager_attributes_examples'
  12. require 'models/tag/writes_to_ticket_history_examples'
  13. require 'models/ticket/calls_stats_ticket_reopen_log_examples'
  14. require 'models/ticket/enqueues_user_ticket_counter_job_examples'
  15. require 'models/ticket/escalation_examples'
  16. require 'models/ticket/resets_pending_time_seconds_examples'
  17. require 'models/ticket/sets_close_time_examples'
  18. require 'models/ticket/sets_last_owner_update_time_examples'
  19. require 'models/ticket/selector_2_sql_examples'
  20. RSpec.describe Ticket, type: :model do
  21. subject(:ticket) { create(:ticket) }
  22. it_behaves_like 'ApplicationModel'
  23. it_behaves_like 'CanBeImported'
  24. it_behaves_like 'CanCsvImport'
  25. it_behaves_like 'ChecksCoreWorkflow'
  26. it_behaves_like 'HasHistory', history_relation_object: ['Ticket::Article', 'Mention', 'Ticket::SharedDraftZoom']
  27. it_behaves_like 'HasTags'
  28. it_behaves_like 'TagWritesToTicketHistory'
  29. it_behaves_like 'HasTaskbars'
  30. it_behaves_like 'HasXssSanitizedNote', model_factory: :ticket
  31. it_behaves_like 'HasObjectManagerAttributes'
  32. it_behaves_like 'Ticket::Escalation'
  33. it_behaves_like 'TicketCallsStatsTicketReopenLog'
  34. it_behaves_like 'TicketEnqueuesTicketUserTicketCounterJob'
  35. it_behaves_like 'TicketResetsPendingTimeSeconds'
  36. it_behaves_like 'TicketSetsCloseTime'
  37. it_behaves_like 'TicketSetsLastOwnerUpdateTime'
  38. it_behaves_like 'TicketSelector2Sql'
  39. describe 'Class methods:' do
  40. describe '.selectors' do
  41. # https://github.com/zammad/zammad/issues/1769
  42. context 'when matching multiple tickets, each with multiple articles' do
  43. let(:tickets) { create_list(:ticket, 2) }
  44. before do
  45. create(:ticket_article, ticket: tickets.first, from: 'asdf1@blubselector.de')
  46. create(:ticket_article, ticket: tickets.first, from: 'asdf2@blubselector.de')
  47. create(:ticket_article, ticket: tickets.first, from: 'asdf3@blubselector.de')
  48. create(:ticket_article, ticket: tickets.last, from: 'asdf4@blubselector.de')
  49. create(:ticket_article, ticket: tickets.last, from: 'asdf5@blubselector.de')
  50. create(:ticket_article, ticket: tickets.last, from: 'asdf6@blubselector.de')
  51. end
  52. let(:condition) do
  53. {
  54. 'article.from' => {
  55. operator: 'contains',
  56. value: 'blubselector.de',
  57. },
  58. }
  59. end
  60. it 'returns a list of unique tickets (i.e., no duplicates)' do
  61. expect(described_class.selectors(condition, limit: 100, access: 'full'))
  62. .to match_array([2, tickets.to_a])
  63. end
  64. end
  65. context 'when customer has multiple organizations' do
  66. let(:organization1) { create(:organization) }
  67. let(:organization2) { create(:organization) }
  68. let(:organization3) { create(:organization) }
  69. let(:customer) { create(:customer, organization: organization1, organizations: [organization2, organization3]) }
  70. let(:ticket1) { create(:ticket, customer: customer, organization: organization1) }
  71. let(:ticket2) { create(:ticket, customer: customer, organization: organization2) }
  72. let(:ticket3) { create(:ticket, customer: customer, organization: organization3) }
  73. before do
  74. ticket1 && ticket2 && ticket3
  75. end
  76. context 'when current user organization is used' do
  77. let(:condition) do
  78. {
  79. 'ticket.organization_id' => {
  80. operator: 'is', # is not
  81. pre_condition: 'current_user.organization_id',
  82. },
  83. }
  84. end
  85. it 'returns the customer tickets' do
  86. expect(described_class.selectors(condition, limit: 100, access: 'full', current_user: customer))
  87. .to match_array([3, include(ticket1, ticket2, ticket3)])
  88. end
  89. end
  90. end
  91. end
  92. end
  93. describe 'Instance methods:' do
  94. describe '#merge_to' do
  95. let(:target_ticket) { create(:ticket) }
  96. context 'when source ticket has Links' do
  97. let(:linked_tickets) { create_list(:ticket, 3) }
  98. let(:links) { linked_tickets.map { |l| create(:link, from: ticket, to: l) } }
  99. it 'reassigns all links to the target ticket after merge' do
  100. expect { ticket.merge_to(ticket_id: target_ticket.id, user_id: 1) }
  101. .to change { links.each(&:reload).map(&:link_object_source_value) }
  102. .to(Array.new(3) { target_ticket.id })
  103. end
  104. end
  105. context 'when attempting to cross-merge (i.e., to merge B → A after merging A → B)' do
  106. before { target_ticket.merge_to(ticket_id: ticket.id, user_id: 1) }
  107. it 'raises an error' do
  108. expect { ticket.merge_to(ticket_id: target_ticket.id, user_id: 1) }
  109. .to raise_error('ticket already merged, no merge into merged ticket possible')
  110. end
  111. end
  112. context 'when attempting to self-merge (i.e., to merge A → A)' do
  113. it 'raises an error' do
  114. expect { ticket.merge_to(ticket_id: ticket.id, user_id: 1) }
  115. .to raise_error("Can't merge ticket with itself!")
  116. end
  117. end
  118. context 'when both tickets are linked with the same parent (parent->child)' do
  119. let(:parent) { create(:ticket) }
  120. before do
  121. create(:link,
  122. link_type: 'child',
  123. link_object_source_value: ticket.id,
  124. link_object_target_value: parent.id)
  125. create(:link,
  126. link_type: 'child',
  127. link_object_source_value: target_ticket.id,
  128. link_object_target_value: parent.id)
  129. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  130. end
  131. it 'does remove the link from the merged ticket' do
  132. links = Link.list(
  133. link_object: 'Ticket',
  134. link_object_value: ticket.id
  135. )
  136. expect(links.count).to eq(1) # one link to the source ticket (no parent link)
  137. end
  138. it 'does not remove the link from the target ticket' do
  139. links = Link.list(
  140. link_object: 'Ticket',
  141. link_object_value: target_ticket.id
  142. )
  143. expect(links.count).to eq(2) # one link to the merged ticket + parent link
  144. end
  145. end
  146. context 'when both tickets are linked with the same parent (child->parent)' do
  147. let(:parent) { create(:ticket) }
  148. before do
  149. create(:link,
  150. link_type: 'child',
  151. link_object_source_value: parent.id,
  152. link_object_target_value: ticket.id)
  153. create(:link,
  154. link_type: 'child',
  155. link_object_source_value: parent.id,
  156. link_object_target_value: target_ticket.id)
  157. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  158. end
  159. it 'does remove the link from the merged ticket' do
  160. links = Link.list(
  161. link_object: 'Ticket',
  162. link_object_value: ticket.id
  163. )
  164. expect(links.count).to eq(1) # one link to the source ticket (no parent link)
  165. end
  166. it 'does not remove the link from the target ticket' do
  167. links = Link.list(
  168. link_object: 'Ticket',
  169. link_object_value: target_ticket.id
  170. )
  171. expect(links.count).to eq(2) # one link to the merged ticket + parent link
  172. end
  173. end
  174. context 'when both tickets are linked with the same parent (different link types)' do
  175. let(:parent) { create(:ticket) }
  176. before do
  177. create(:link,
  178. link_type: 'normal',
  179. link_object_source_value: parent.id,
  180. link_object_target_value: ticket.id)
  181. create(:link,
  182. link_type: 'child',
  183. link_object_source_value: parent.id,
  184. link_object_target_value: target_ticket.id)
  185. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  186. end
  187. it 'does remove the link from the merged ticket' do
  188. links = Link.list(
  189. link_object: 'Ticket',
  190. link_object_value: ticket.id
  191. )
  192. expect(links.count).to eq(1) # one link to the source ticket (no normal link)
  193. end
  194. it 'does not remove the link from the target ticket' do
  195. links = Link.list(
  196. link_object: 'Ticket',
  197. link_object_value: target_ticket.id
  198. )
  199. expect(links.count).to eq(3) # one lin to the merged ticket + parent link + normal link
  200. end
  201. end
  202. context 'when both tickets having mentions to the same user' do
  203. let(:watcher) { create(:agent) }
  204. before do
  205. create(:mention, mentionable: ticket, user: watcher)
  206. create(:mention, mentionable: target_ticket, user: watcher)
  207. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  208. end
  209. it 'does remove the link from the merged ticket' do
  210. expect(target_ticket.mentions.count).to eq(1) # one mention to watcher user
  211. end
  212. end
  213. context 'when merging' do
  214. let(:merge_user) { create(:user) }
  215. before do
  216. # create target ticket early
  217. # to avoid a race condition
  218. # when creating the history entries
  219. target_ticket
  220. travel 5.minutes
  221. ticket.merge_to(ticket_id: target_ticket.id, user_id: merge_user.id)
  222. end
  223. # Issue #2469 - Add information "Ticket merged" to History
  224. it 'creates history entries in both the origin ticket and the target ticket' do
  225. expect(target_ticket.history_get.size).to eq 2
  226. target_history = target_ticket.history_get.last
  227. expect(target_history['object']).to eq 'Ticket'
  228. expect(target_history['type']).to eq 'received_merge'
  229. expect(target_history['created_by_id']).to eq merge_user.id
  230. expect(target_history['o_id']).to eq target_ticket.id
  231. expect(target_history['id_to']).to eq target_ticket.id
  232. expect(target_history['id_from']).to eq ticket.id
  233. expect(ticket.history_get.size).to eq 4
  234. origin_history = ticket.reload.history_get[1]
  235. expect(origin_history['object']).to eq 'Ticket'
  236. expect(origin_history['type']).to eq 'merged_into'
  237. expect(origin_history['created_by_id']).to eq merge_user.id
  238. expect(origin_history['o_id']).to eq ticket.id
  239. expect(origin_history['id_to']).to eq target_ticket.id
  240. expect(origin_history['id_from']).to eq ticket.id
  241. end
  242. it 'sends ExternalSync.migrate' do
  243. allow(ExternalSync).to receive(:migrate)
  244. ticket.merge_to(ticket_id: target_ticket.id, user_id: merge_user.id)
  245. expect(ExternalSync).to have_received(:migrate).with('Ticket', ticket.id, target_ticket.id)
  246. end
  247. # Issue #2960 - Ticket removal of merged / linked tickets doesn't remove references
  248. context 'and deleting the origin ticket' do
  249. it 'adds reference number and title to the target ticket' do
  250. expect { ticket.destroy }
  251. .to change { target_ticket.history_get.find { |elem| elem.fetch('type') == 'received_merge' }['value_from'] }
  252. .to("##{ticket.number} #{ticket.title}")
  253. end
  254. end
  255. # Issue #2960 - Ticket removal of merged / linked tickets doesn't remove references
  256. context 'and deleting the target ticket' do
  257. it 'adds reference number and title to the origin ticket' do
  258. expect { target_ticket.destroy }
  259. .to change { ticket.history_get.find { |elem| elem.fetch('type') == 'merged_into' }['value_to'] }
  260. .to("##{target_ticket.number} #{target_ticket.title}")
  261. end
  262. end
  263. end
  264. # https://github.com/zammad/zammad/issues/3105
  265. context 'when merge actions triggers exist', :performs_jobs do
  266. before do
  267. ticket && target_ticket
  268. merged_into_trigger && received_merge_trigger && update_trigger
  269. allow_any_instance_of(described_class).to receive(:perform_changes) do |ticket, trigger|
  270. log << { ticket: ticket.id, trigger: trigger.id }
  271. end
  272. perform_enqueued_jobs do
  273. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  274. end
  275. end
  276. let(:merged_into_trigger) { create(:trigger, :conditionable, condition_ticket_action: 'update.merged_into') }
  277. let(:received_merge_trigger) { create(:trigger, :conditionable, condition_ticket_action: 'update.received_merge') }
  278. let(:update_trigger) { create(:trigger, :conditionable, condition_ticket_action: 'update') }
  279. let(:log) { [] }
  280. it 'merge_into triggered with source ticket' do
  281. expect(log).to include({ ticket: ticket.id, trigger: merged_into_trigger.id })
  282. end
  283. it 'received_merge not triggered with source ticket' do
  284. expect(log).not_to include({ ticket: ticket.id, trigger: received_merge_trigger.id })
  285. end
  286. it 'update not triggered with source ticket' do
  287. expect(log).not_to include({ ticket: ticket.id, trigger: update_trigger.id })
  288. end
  289. it 'merge_into not triggered with target ticket' do
  290. expect(log).not_to include({ ticket: target_ticket.id, trigger: merged_into_trigger.id })
  291. end
  292. it 'received_merge triggered with target ticket' do
  293. expect(log).to include({ ticket: target_ticket.id, trigger: received_merge_trigger.id })
  294. end
  295. it 'update not triggered with target ticket' do
  296. expect(log).not_to include({ ticket: target_ticket.id, trigger: update_trigger.id })
  297. end
  298. end
  299. # https://github.com/zammad/zammad/issues/3105
  300. context 'when user has notifications enabled', :performs_jobs do
  301. before do
  302. user
  303. allow(OnlineNotification).to receive(:add) do |**args|
  304. next if args[:object] != 'Ticket'
  305. log << { type: :online, event: args[:type], ticket_id: args[:o_id], user_id: args[:user_id] }
  306. end
  307. allow(NotificationFactory::Mailer).to receive(:notification) do |**args|
  308. log << { type: :email, event: args[:template], ticket_id: args[:objects][:ticket].id, user_id: args[:user].id }
  309. end
  310. perform_enqueued_jobs do
  311. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  312. end
  313. end
  314. let(:user) { create(:agent, :preferencable, notification_group_ids: [ticket, target_ticket].map(&:group_id), groups: [ticket, target_ticket].map(&:group)) }
  315. let(:log) { [] }
  316. it 'merge_into notification sent with source ticket' do
  317. expect(log).to include({ type: :online, event: 'update.merged_into', ticket_id: ticket.id, user_id: user.id })
  318. end
  319. it 'received_merge notification not sent with source ticket' do
  320. expect(log).not_to include({ type: :online, event: 'update.received_merge', ticket_id: ticket.id, user_id: user.id })
  321. end
  322. it 'update notification not sent with source ticket' do
  323. expect(log).not_to include({ type: :online, event: 'update', ticket_id: ticket.id, user_id: user.id })
  324. end
  325. it 'merge_into notification not sent with target ticket' do
  326. expect(log).not_to include({ type: :online, event: 'update.merged_into', ticket_id: target_ticket.id, user_id: user.id })
  327. end
  328. it 'received_merge notification sent with target ticket' do
  329. expect(log).to include({ type: :online, event: 'update.received_merge', ticket_id: target_ticket.id, user_id: user.id })
  330. end
  331. it 'update notification not sent with target ticket' do
  332. expect(log).not_to include({ type: :online, event: 'update', ticket_id: target_ticket.id, user_id: user.id })
  333. end
  334. it 'merge_into email sent with source ticket' do
  335. expect(log).to include({ type: :email, event: 'ticket_update_merged_into', ticket_id: ticket.id, user_id: user.id })
  336. end
  337. it 'received_merge email not sent with source ticket' do
  338. expect(log).not_to include({ type: :email, event: 'ticket_update_received_merge', ticket_id: ticket.id, user_id: user.id })
  339. end
  340. it 'update email not sent with source ticket' do
  341. expect(log).not_to include({ type: :email, event: 'ticket_update', ticket_id: ticket.id, user_id: user.id })
  342. end
  343. it 'merge_into email not sent with target ticket' do
  344. expect(log).not_to include({ type: :email, event: 'ticket_update_merged_into', ticket_id: target_ticket.id, user_id: user.id })
  345. end
  346. it 'received_merge email sent with target ticket' do
  347. expect(log).to include({ type: :email, event: 'ticket_update_received_merge', ticket_id: target_ticket.id, user_id: user.id })
  348. end
  349. it 'update email not sent with target ticket' do
  350. expect(log).not_to include({ type: :email, event: 'ticket_update', ticket_id: target_ticket.id, user_id: user.id })
  351. end
  352. end
  353. # https://github.com/zammad/zammad/issues/3105
  354. context 'when sending notification email correct template', :performs_jobs do
  355. before do
  356. user
  357. allow(NotificationFactory::Mailer).to receive(:send) do |**args|
  358. log << args[:subject]
  359. end
  360. perform_enqueued_jobs do
  361. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  362. end
  363. end
  364. let(:user) { create(:agent, :preferencable, notification_group_ids: [ticket, target_ticket].map(&:group_id), groups: [ticket, target_ticket].map(&:group)) }
  365. let(:log) { [] }
  366. it 'is used for merged_into' do
  367. expect(log).to include(start_with("Ticket (#{ticket.title}) was merged into another ticket"))
  368. end
  369. it 'is used for received_merge' do
  370. expect(log).to include(start_with("Another ticket was merged into ticket (#{target_ticket.title})"))
  371. end
  372. end
  373. context 'ApplicationHandleInfo context' do
  374. it 'gets switched to "merge"' do
  375. allow(ApplicationHandleInfo).to receive('context=')
  376. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  377. expect(ApplicationHandleInfo).to have_received('context=').with('merge').at_least(1)
  378. end
  379. it 'reverts back to default' do
  380. allow(ApplicationHandleInfo).to receive('context=')
  381. ticket.merge_to(ticket_id: target_ticket.id, user_id: 1)
  382. expect(ApplicationHandleInfo.context).not_to eq 'merge'
  383. end
  384. end
  385. end
  386. describe '#perform_changes' do
  387. before do
  388. stub_const('PERFORMABLE_STRUCT', Struct.new(:id, :perform, keyword_init: true))
  389. end
  390. # a `performable` can be a Trigger or a Job
  391. # we use DuckTyping and expect that a performable
  392. # implements the following interface
  393. let(:performable) do
  394. PERFORMABLE_STRUCT.new(id: 1, perform: perform)
  395. end
  396. # Regression test for https://github.com/zammad/zammad/issues/2001
  397. describe 'argument handling' do
  398. let(:perform) do
  399. {
  400. 'notification.email' => {
  401. body: "Hello \#{ticket.customer.firstname} \#{ticket.customer.lastname},",
  402. recipient: %w[article_last_sender ticket_owner ticket_customer ticket_agents],
  403. subject: "Autoclose (\#{ticket.title})"
  404. }
  405. }
  406. end
  407. it 'does not mutate contents of "perform" hash' do
  408. expect { ticket.perform_changes(performable, 'trigger', {}, 1) }
  409. .not_to change { perform }
  410. end
  411. end
  412. context 'with "ticket.state_id" key in "perform" hash' do
  413. let(:perform) do
  414. {
  415. 'ticket.state_id' => {
  416. 'value' => Ticket::State.lookup(name: 'closed').id
  417. }
  418. }
  419. end
  420. it 'changes #state to specified value' do
  421. expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }
  422. .to change { ticket.reload.state.name }.to('closed')
  423. end
  424. end
  425. # Test for backwards compatibility after PR https://github.com/zammad/zammad/pull/2862
  426. context 'with "pending_time" => { "value": DATE } in "perform" hash' do
  427. let(:perform) do
  428. {
  429. 'ticket.state_id' => {
  430. 'value' => Ticket::State.lookup(name: 'pending reminder').id.to_s
  431. },
  432. 'ticket.pending_time' => {
  433. 'value' => timestamp,
  434. },
  435. }
  436. end
  437. let(:timestamp) { Time.zone.now }
  438. it 'changes pending date to given date' do
  439. freeze_time do
  440. expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }
  441. .to change(ticket, :pending_time).to(be_within(1.minute).of(timestamp))
  442. end
  443. end
  444. end
  445. # Test for PR https://github.com/zammad/zammad/pull/2862
  446. context 'with "pending_time" => { "operator": "relative" } in "perform" hash' do
  447. shared_examples 'verify' do
  448. it 'verify relative pending time rule' do
  449. freeze_time do
  450. interval = relative_value.send(relative_range).from_now
  451. expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }
  452. .to change(ticket, :pending_time).to(be_within(1.minute).of(interval))
  453. end
  454. end
  455. end
  456. let(:perform) do
  457. {
  458. 'ticket.state_id' => {
  459. 'value' => Ticket::State.lookup(name: 'pending reminder').id.to_s
  460. },
  461. 'ticket.pending_time' => {
  462. 'operator' => 'relative',
  463. 'value' => relative_value,
  464. 'range' => relative_range_config
  465. },
  466. }
  467. end
  468. let(:relative_range_config) { relative_range.to_s.singularize }
  469. context 'and value in days' do
  470. let(:relative_value) { 2 }
  471. let(:relative_range) { :days }
  472. include_examples 'verify'
  473. end
  474. context 'and value in minutes' do
  475. let(:relative_value) { 60 }
  476. let(:relative_range) { :minutes }
  477. include_examples 'verify'
  478. end
  479. end
  480. context 'with "ticket.action" => { "value" => "delete" } in "perform" hash' do
  481. let(:perform) do
  482. {
  483. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s },
  484. 'ticket.action' => { 'value' => 'delete' },
  485. }
  486. end
  487. it 'performs a ticket deletion on a ticket' do
  488. expect { ticket.perform_changes(performable, 'trigger', ticket, User.first) }
  489. .to change(ticket, :destroyed?).to(true)
  490. end
  491. end
  492. context 'with a "notification.email" trigger' do
  493. # Regression test for https://github.com/zammad/zammad/issues/1543
  494. #
  495. # If a new article fires an email notification trigger,
  496. # and then another article is added to the same ticket
  497. # before that trigger is performed,
  498. # the email template's 'article' var should refer to the originating article,
  499. # not the newest one.
  500. #
  501. # (This occurs whenever one action fires multiple email notification triggers.)
  502. context 'when two articles are created before the trigger fires once (race condition)' do
  503. let!(:article) { create(:ticket_article, ticket: ticket) }
  504. let!(:new_article) { create(:ticket_article, ticket: ticket) }
  505. let(:trigger) do
  506. build(:trigger,
  507. perform: {
  508. 'notification.email' => {
  509. body: '',
  510. recipient: 'ticket_customer',
  511. subject: ''
  512. }
  513. })
  514. end
  515. # required by Ticket#perform_changes for email notifications
  516. before { article.ticket.group.update(email_address: create(:email_address)) }
  517. it 'passes the first article to NotificationFactory::Mailer' do
  518. expect(NotificationFactory::Mailer)
  519. .to receive(:template)
  520. .with(hash_including(objects: { ticket: ticket, article: article }))
  521. .at_least(:once)
  522. .and_call_original
  523. expect(NotificationFactory::Mailer)
  524. .not_to receive(:template)
  525. .with(hash_including(objects: { ticket: ticket, article: new_article }))
  526. ticket.perform_changes(trigger, 'trigger', { article_id: article.id }, 1)
  527. end
  528. end
  529. end
  530. context 'with a notification trigger' do
  531. # https://github.com/zammad/zammad/issues/2782
  532. #
  533. # Notification triggers should log notification as private or public
  534. # according to given configuration
  535. let(:user) { create(:admin, mobile: '+37061010000') }
  536. before { ticket.group.users << user }
  537. let(:perform) do
  538. {
  539. notification_key => {
  540. body: 'Old programmers never die. They just branch to a new address.',
  541. recipient: 'ticket_agents',
  542. subject: 'Old programmers never die. They just branch to a new address.'
  543. }
  544. }.deep_merge(additional_options).deep_stringify_keys
  545. end
  546. let(:notification_key) { "notification.#{notification_type}" }
  547. shared_examples 'verify log visibility status' do
  548. shared_examples 'notification trigger' do
  549. it 'adds Ticket::Article' do
  550. expect { ticket.perform_changes(performable, 'trigger', ticket, user) }
  551. .to change { ticket.articles.count }.by(1)
  552. end
  553. it 'new Ticket::Article visibility reflects setting' do
  554. ticket.perform_changes(performable, 'trigger', ticket, User.first)
  555. new_article = ticket.articles.reload.last
  556. expect(new_article.internal).to be target_internal_value
  557. end
  558. end
  559. context 'when set to private' do
  560. let(:additional_options) do
  561. {
  562. notification_key => {
  563. internal: true
  564. }
  565. }
  566. end
  567. let(:target_internal_value) { true }
  568. it_behaves_like 'notification trigger'
  569. end
  570. context 'when set to internal' do
  571. let(:additional_options) do
  572. {
  573. notification_key => {
  574. internal: false
  575. }
  576. }
  577. end
  578. let(:target_internal_value) { false }
  579. it_behaves_like 'notification trigger'
  580. end
  581. context 'when no selection was made' do # ensure previously created triggers default to public
  582. let(:additional_options) do
  583. {}
  584. end
  585. let(:target_internal_value) { false }
  586. it_behaves_like 'notification trigger'
  587. end
  588. end
  589. context 'dispatching email' do
  590. let(:notification_type) { :email }
  591. include_examples 'verify log visibility status'
  592. end
  593. shared_examples 'add a new article' do
  594. it 'adds a new article' do
  595. expect { ticket.perform_changes(performable, 'trigger', ticket, user) }
  596. .to change { ticket.articles.count }.by(1)
  597. end
  598. end
  599. shared_examples 'add attachment to new article' do
  600. include_examples 'add a new article'
  601. it 'adds attachment to the new article' do
  602. ticket.perform_changes(performable, 'trigger', ticket, user)
  603. article = ticket.articles.last
  604. expect(article.type.name).to eq('email')
  605. expect(article.sender.name).to eq('System')
  606. expect(article.attachments.count).to eq(1)
  607. expect(article.attachments[0].filename).to eq('some_file.pdf')
  608. expect(article.attachments[0].preferences['Content-ID']).to eq('image/pdf@01CAB192.K8H512Y9')
  609. end
  610. end
  611. shared_examples 'does not add attachment to new article' do
  612. include_examples 'add a new article'
  613. it 'does not add attachment to the new article' do
  614. ticket.perform_changes(performable, 'trigger', ticket, user)
  615. article = ticket.articles.last
  616. expect(article.type.name).to eq('email')
  617. expect(article.sender.name).to eq('System')
  618. expect(article.attachments.count).to eq(0)
  619. end
  620. end
  621. context 'dispatching email with include attachment present' do
  622. let(:notification_type) { :email }
  623. let(:additional_options) do
  624. {
  625. notification_key => {
  626. include_attachments: 'true'
  627. }
  628. }
  629. end
  630. context 'when ticket has an attachment' do
  631. before do
  632. UserInfo.current_user_id = 1
  633. ticket_article = create(:ticket_article, ticket: ticket)
  634. create(:store,
  635. object: 'Ticket::Article',
  636. o_id: ticket_article.id,
  637. data: 'dGVzdCAxMjM=',
  638. filename: 'some_file.pdf',
  639. preferences: {
  640. 'Content-Type': 'image/pdf',
  641. 'Content-ID': 'image/pdf@01CAB192.K8H512Y9',
  642. })
  643. end
  644. include_examples 'add attachment to new article'
  645. end
  646. context 'when ticket does not have an attachment' do
  647. include_examples 'does not add attachment to new article'
  648. end
  649. end
  650. context 'dispatching email with include attachment not present' do
  651. let(:notification_type) { :email }
  652. let(:additional_options) do
  653. {
  654. notification_key => {
  655. include_attachments: 'false'
  656. }
  657. }
  658. end
  659. context 'when ticket has an attachment' do
  660. before do
  661. UserInfo.current_user_id = 1
  662. ticket_article = create(:ticket_article, ticket: ticket)
  663. create(:store,
  664. object: 'Ticket::Article',
  665. o_id: ticket_article.id,
  666. data: 'dGVzdCAxMjM=',
  667. filename: 'some_file.pdf',
  668. preferences: {
  669. 'Content-Type': 'image/pdf',
  670. 'Content-ID': 'image/pdf@01CAB192.K8H512Y9',
  671. })
  672. end
  673. include_examples 'does not add attachment to new article'
  674. end
  675. context 'when ticket does not have an attachment' do
  676. include_examples 'does not add attachment to new article'
  677. end
  678. end
  679. context 'dispatching SMS' do
  680. let(:notification_type) { :sms }
  681. before { create(:channel, area: 'Sms::Notification') }
  682. include_examples 'verify log visibility status'
  683. end
  684. end
  685. context 'with a "notification.webhook" trigger', performs_jobs: true do
  686. let(:webhook) { create(:webhook, endpoint: 'http://api.example.com/webhook', signature_token: '53CR3t') }
  687. let(:trigger) do
  688. create(:trigger,
  689. perform: {
  690. 'notification.webhook' => { 'webhook_id' => webhook.id }
  691. })
  692. end
  693. it 'schedules the webhooks notification job' do
  694. expect { ticket.perform_changes(trigger, 'trigger', {}, 1) }.to have_enqueued_job(TriggerWebhookJob).with(trigger, ticket, nil)
  695. end
  696. end
  697. end
  698. describe '#trigger_based_notification?' do
  699. let(:ticket) { create(:ticket) }
  700. context 'with a normal user' do
  701. let(:customer) { create(:customer) }
  702. it 'send trigger base notification' do
  703. expect(ticket.send(:trigger_based_notification?, customer)).to be(true)
  704. end
  705. end
  706. context 'with a permanent failed user' do
  707. let(:failed_date) { 1.second.ago }
  708. let(:customer) do
  709. user = create(:customer)
  710. user.preferences.merge!(mail_delivery_failed: true, mail_delivery_failed_data: failed_date)
  711. user.save!
  712. user
  713. end
  714. it 'send no trigger base notification' do
  715. expect(ticket.send(:trigger_based_notification?, customer)).to be(false)
  716. end
  717. context 'with failed date 61 days ago' do
  718. let(:failed_date) { 61.days.ago }
  719. it 'send trigger base notification' do
  720. expect(ticket.send(:trigger_based_notification?, customer)).to be(true)
  721. end
  722. end
  723. end
  724. end
  725. describe '#subject_build' do
  726. context 'with default "ticket_hook_position" setting ("right")' do
  727. it 'returns the given string followed by a ticket reference (of the form "[Ticket#123]")' do
  728. expect(ticket.subject_build('foo'))
  729. .to eq("foo [Ticket##{ticket.number}]")
  730. end
  731. context 'and a non-default value for the "ticket_hook" setting' do
  732. before { Setting.set('ticket_hook', 'bar baz') }
  733. it 'replaces "Ticket#" with the new ticket hook' do
  734. expect(ticket.subject_build('foo'))
  735. .to eq("foo [bar baz#{ticket.number}]")
  736. end
  737. end
  738. context 'and a non-default value for the "ticket_hook_divider" setting' do
  739. before { Setting.set('ticket_hook_divider', ': ') }
  740. it 'inserts the new ticket hook divider between "Ticket#" and the ticket number' do
  741. expect(ticket.subject_build('foo'))
  742. .to eq("foo [Ticket#: #{ticket.number}]")
  743. end
  744. end
  745. context 'when the given string already contains a ticket reference, but in the wrong place' do
  746. it 'moves the ticket reference to the end' do
  747. expect(ticket.subject_build("[Ticket##{ticket.number}] foo"))
  748. .to eq("foo [Ticket##{ticket.number}]")
  749. end
  750. end
  751. context 'when the given string already contains an alternately formatted ticket reference' do
  752. it 'reformats the ticket reference' do
  753. expect(ticket.subject_build("foo [Ticket#: #{ticket.number}]"))
  754. .to eq("foo [Ticket##{ticket.number}]")
  755. end
  756. end
  757. end
  758. context 'with alternate "ticket_hook_position" setting ("left")' do
  759. before { Setting.set('ticket_hook_position', 'left') }
  760. it 'returns a ticket reference (of the form "[Ticket#123]") followed by the given string' do
  761. expect(ticket.subject_build('foo'))
  762. .to eq("[Ticket##{ticket.number}] foo")
  763. end
  764. context 'and a non-default value for the "ticket_hook" setting' do
  765. before { Setting.set('ticket_hook', 'bar baz') }
  766. it 'replaces "Ticket#" with the new ticket hook' do
  767. expect(ticket.subject_build('foo'))
  768. .to eq("[bar baz#{ticket.number}] foo")
  769. end
  770. end
  771. context 'and a non-default value for the "ticket_hook_divider" setting' do
  772. before { Setting.set('ticket_hook_divider', ': ') }
  773. it 'inserts the new ticket hook divider between "Ticket#" and the ticket number' do
  774. expect(ticket.subject_build('foo'))
  775. .to eq("[Ticket#: #{ticket.number}] foo")
  776. end
  777. end
  778. context 'when the given string already contains a ticket reference, but in the wrong place' do
  779. it 'moves the ticket reference to the start' do
  780. expect(ticket.subject_build("foo [Ticket##{ticket.number}]"))
  781. .to eq("[Ticket##{ticket.number}] foo")
  782. end
  783. end
  784. context 'when the given string already contains an alternately formatted ticket reference' do
  785. it 'reformats the ticket reference' do
  786. expect(ticket.subject_build("[Ticket#: #{ticket.number}] foo"))
  787. .to eq("[Ticket##{ticket.number}] foo")
  788. end
  789. end
  790. end
  791. end
  792. describe '#last_original_update_at' do
  793. let(:result) { ticket.last_original_update_at }
  794. it 'returns initial customer enquiry time when customer contacted repeatedly' do
  795. ticket
  796. target = create(:ticket_article, :inbound_email, ticket: ticket)
  797. travel 10.minutes
  798. create(:ticket_article, :inbound_email, ticket: ticket)
  799. expect(result).to eq target.created_at
  800. end
  801. it 'returns agent contact time when customer did not respond to agent reach out' do
  802. ticket
  803. create(:ticket_article, :outbound_email, ticket: ticket)
  804. expect(result).to eq ticket.last_contact_agent_at
  805. end
  806. it 'returns nil if no customer response' do
  807. ticket
  808. expect(result).to be_nil
  809. end
  810. context 'with customer enquiry and agent response' do
  811. before do
  812. ticket
  813. create(:ticket_article, :inbound_email, ticket: ticket)
  814. travel 10.minutes
  815. create(:ticket_article, :outbound_email, ticket: ticket)
  816. travel 10.minutes
  817. end
  818. it 'returns last customer enquiry time when agent did not respond yet' do
  819. target = create(:ticket_article, :inbound_email, ticket: ticket)
  820. expect(result).to eq target.created_at
  821. end
  822. it 'returns agent response time when agent responded to customer enquiry' do
  823. expect(result).to eq ticket.last_contact_agent_at
  824. end
  825. end
  826. end
  827. describe '#param_cleanup' do
  828. it 'does only remove parameters which are invalid and not the complete params hash if one element is invalid (#3743)' do
  829. expect(described_class.param_cleanup({ state_id: 3, customer_id: 'guess:1234' }, true, false, false)).to eq({ 'state_id' => 3 })
  830. end
  831. end
  832. end
  833. describe 'Attributes:' do
  834. describe '#owner' do
  835. let(:original_owner) { create(:agent, groups: [ticket.group]) }
  836. before { ticket.update(owner: original_owner) }
  837. context 'when assigned directly' do
  838. context 'to an active agent belonging to ticket.group' do
  839. let(:agent) { create(:agent, groups: [ticket.group]) }
  840. it 'can be set' do
  841. expect { ticket.update(owner: agent) }
  842. .to change { ticket.reload.owner }.to(agent)
  843. end
  844. end
  845. context 'to an agent not belonging to ticket.group' do
  846. let(:agent) { create(:agent, groups: [other_group]) }
  847. let(:other_group) { create(:group) }
  848. it 'resets to default user (id: 1) instead' do
  849. expect { ticket.update(owner: agent) }
  850. .to change { ticket.reload.owner }.to(User.first)
  851. end
  852. end
  853. context 'to an inactive agent' do
  854. let(:agent) { create(:agent, groups: [ticket.group], active: false) }
  855. it 'resets to default user (id: 1) instead' do
  856. expect { ticket.update(owner: agent) }
  857. .to change { ticket.reload.owner }.to(User.first)
  858. end
  859. end
  860. context 'to a non-agent' do
  861. let(:agent) { create(:customer, groups: [ticket.group]) }
  862. it 'resets to default user (id: 1) instead' do
  863. expect { ticket.update(owner: agent) }
  864. .to change { ticket.reload.owner }.to(User.first)
  865. end
  866. end
  867. end
  868. context 'when the ticket is updated for any other reason' do
  869. context 'if original owner is still an active agent belonging to ticket.group' do
  870. it 'does not change' do
  871. expect { create(:ticket_article, ticket: ticket) }
  872. .not_to change { ticket.reload.owner }
  873. end
  874. end
  875. context 'if original owner has left ticket.group' do
  876. before { original_owner.groups = [] }
  877. it 'resets to default user (id: 1)' do
  878. expect { create(:ticket_article, ticket: ticket) }
  879. .to change { ticket.reload.owner }.to(User.first)
  880. end
  881. end
  882. context 'if original owner has become inactive' do
  883. before { original_owner.update(active: false) }
  884. it 'resets to default user (id: 1)' do
  885. expect { create(:ticket_article, ticket: ticket) }
  886. .to change { ticket.reload.owner }.to(User.first)
  887. end
  888. end
  889. context 'if original owner has lost agent status' do
  890. before { original_owner.roles = [create(:role)] }
  891. it 'resets to default user (id: 1)' do
  892. Rails.cache.clear
  893. expect { create(:ticket_article, ticket: ticket) }
  894. .to change { ticket.reload.owner }.to(User.first)
  895. end
  896. end
  897. context 'when the Ticket is closed' do
  898. before do
  899. ticket.update!(state: Ticket::State.lookup(name: 'closed'))
  900. end
  901. context 'if original owner is still an active agent belonging to ticket.group' do
  902. it 'does not change' do
  903. expect { create(:ticket_article, ticket: ticket) }
  904. .not_to change { ticket.reload.owner }
  905. end
  906. end
  907. context 'if original owner has left ticket.group' do
  908. before { original_owner.groups = [] }
  909. it 'does not change' do
  910. expect { create(:ticket_article, ticket: ticket) }
  911. .not_to change { ticket.reload.owner }
  912. end
  913. end
  914. context 'if original owner has become inactive' do
  915. before { original_owner.update(active: false) }
  916. it 'does not change' do
  917. expect { create(:ticket_article, ticket: ticket) }
  918. .not_to change { ticket.reload.owner }
  919. end
  920. end
  921. context 'if original owner has lost agent status' do
  922. before { original_owner.roles = [create(:role)] }
  923. it 'does not change' do
  924. expect { create(:ticket_article, ticket: ticket) }
  925. .not_to change { ticket.reload.owner }
  926. end
  927. end
  928. end
  929. end
  930. end
  931. describe '#state' do
  932. context 'when originally "new" (default)' do
  933. context 'and a customer article is added' do
  934. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Customer') }
  935. it 'stays "new"' do
  936. expect { article }
  937. .not_to change { ticket.state.name }.from('new')
  938. end
  939. end
  940. context 'and a non-customer article is added' do
  941. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  942. it 'switches to "open"' do
  943. expect { article }
  944. .to change { ticket.reload.state.name }.from('new').to('open')
  945. end
  946. end
  947. end
  948. context 'when originally "closed"' do
  949. before { ticket.update(state: Ticket::State.find_by(name: 'closed')) }
  950. context 'when a non-customer article is added' do
  951. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  952. it 'stays "closed"' do
  953. expect { article }.not_to change { ticket.reload.state.name }
  954. end
  955. end
  956. end
  957. end
  958. describe '#pending_time' do
  959. subject(:ticket) { create(:ticket, pending_time: 2.days.from_now) }
  960. context 'when #state is updated to any non-"pending" value' do
  961. it 'is reset to nil' do
  962. expect { ticket.update!(state: Ticket::State.lookup(name: 'open')) }
  963. .to change(ticket, :pending_time).to(nil)
  964. end
  965. end
  966. # Regression test for commit 92f227786f298bad1ccaf92d4478a7062ea6a49f
  967. context 'when #state is updated to nil (violating DB NOT NULL constraint)' do
  968. it 'does not prematurely raise within the callback (#reset_pending_time)' do
  969. expect { ticket.update!(state: nil) }
  970. .to raise_error(ActiveRecord::StatementInvalid)
  971. end
  972. end
  973. end
  974. describe '#escalation_at' do
  975. before { travel_to(Time.current) } # freeze time
  976. let(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 180, solution_time: 240) }
  977. let(:calendar) { create(:calendar, :'24/7') }
  978. context 'with no SLAs in the system' do
  979. it 'defaults to nil' do
  980. expect(ticket.escalation_at).to be_nil
  981. end
  982. end
  983. context 'with an SLA in the system' do
  984. before { sla } # create sla
  985. it 'is set based on SLA’s #first_response_time' do
  986. expect(ticket.reload.escalation_at.to_i)
  987. .to eq(1.hour.from_now.to_i)
  988. end
  989. context 'after first agent’s response' do
  990. before { ticket } # create ticket
  991. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  992. it 'is updated based on the SLA’s #close_escalation_at' do
  993. travel(1.minute) # time is frozen: if we don't travel forward, pre- and post-update values will be the same
  994. expect { article }
  995. .to change { ticket.reload.escalation_at }
  996. .to(ticket.reload.close_escalation_at)
  997. end
  998. context 'when new #update_time is later than original #solution_time' do
  999. it 'is updated based on the original #solution_time' do
  1000. travel(2.hours) # time is frozen: if we don't travel forward, pre- and post-update values will be the same
  1001. expect { article }
  1002. .to change { ticket.reload.escalation_at }
  1003. .to(4.hours.after(ticket.created_at))
  1004. end
  1005. end
  1006. end
  1007. end
  1008. context 'when updated after an SLA has been added to the system' do
  1009. before do
  1010. ticket # create ticket
  1011. sla # create sla
  1012. end
  1013. it 'is updated based on the new SLA’s #first_response_time' do
  1014. expect { ticket.save! }
  1015. .to change { ticket.reload.escalation_at.to_i }.from(0).to(1.hour.from_now.to_i)
  1016. end
  1017. end
  1018. context 'when updated after all SLAs have been removed from the system' do
  1019. before do
  1020. sla # create sla
  1021. ticket # create ticket
  1022. sla.destroy
  1023. end
  1024. it 'is set to nil' do
  1025. expect { ticket.save! }
  1026. .to change { ticket.reload.escalation_at }.to(nil)
  1027. end
  1028. end
  1029. context 'when within last (relative)' do
  1030. let(:first_response_time) { 5 }
  1031. let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) }
  1032. let(:within_condition) do
  1033. { 'ticket.escalation_at'=>{ 'operator' => 'within last (relative)', 'value' => '30', 'range' => 'minute' } }
  1034. end
  1035. before do
  1036. sla
  1037. travel_to '2020-11-05 11:37:00'
  1038. ticket = create(:ticket)
  1039. create(:ticket_article, :inbound_email, ticket: ticket)
  1040. travel_to '2020-11-05 11:50:00'
  1041. end
  1042. context 'when in range' do
  1043. it 'does find the ticket' do
  1044. count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true)
  1045. expect(count).to eq(1)
  1046. end
  1047. end
  1048. context 'when out of range' do
  1049. let(:first_response_time) { 500 }
  1050. it 'does not find the ticket' do
  1051. count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true)
  1052. expect(count).to eq(0)
  1053. end
  1054. end
  1055. end
  1056. context 'when till (relative)' do
  1057. let(:first_response_time) { 5 }
  1058. let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) }
  1059. let(:condition) do
  1060. { 'ticket.escalation_at'=>{ 'operator' => 'till (relative)', 'value' => '30', 'range' => 'minute' } }
  1061. end
  1062. before do
  1063. sla
  1064. travel_to '2020-11-05 11:37:00'
  1065. ticket = create(:ticket)
  1066. create(:ticket_article, :inbound_email, ticket: ticket)
  1067. travel_to '2020-11-05 11:50:00'
  1068. end
  1069. context 'when in range' do
  1070. it 'does find the ticket' do
  1071. count, _tickets = described_class.selectors(condition, limit: 2_000, execution_time: true)
  1072. expect(count).to eq(1)
  1073. end
  1074. end
  1075. context 'when out of range' do
  1076. let(:first_response_time) { 500 }
  1077. it 'does not find the ticket' do
  1078. count, _tickets = described_class.selectors(condition, limit: 2_000, execution_time: true)
  1079. expect(count).to eq(0)
  1080. end
  1081. end
  1082. end
  1083. context 'when from (relative)' do
  1084. let(:first_response_time) { 5 }
  1085. let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) }
  1086. let(:condition) do
  1087. { 'ticket.escalation_at'=>{ 'operator' => 'from (relative)', 'value' => '30', 'range' => 'minute' } }
  1088. end
  1089. before do
  1090. sla
  1091. travel_to '2020-11-05 11:37:00'
  1092. ticket = create(:ticket)
  1093. create(:ticket_article, :inbound_email, ticket: ticket)
  1094. end
  1095. context 'when in range' do
  1096. it 'does find the ticket' do
  1097. travel_to '2020-11-05 11:50:00'
  1098. count, _tickets = described_class.selectors(condition, limit: 2_000, execution_time: true)
  1099. expect(count).to eq(1)
  1100. end
  1101. end
  1102. context 'when out of range' do
  1103. let(:first_response_time) { 5 }
  1104. it 'does not find the ticket' do
  1105. travel_to '2020-11-05 13:50:00'
  1106. count, _tickets = described_class.selectors(condition, limit: 2_000, execution_time: true)
  1107. expect(count).to eq(0)
  1108. end
  1109. end
  1110. end
  1111. context 'when within next (relative)' do
  1112. let(:first_response_time) { 5 }
  1113. let(:sla) { create(:sla, calendar: calendar, first_response_time: first_response_time) }
  1114. let(:within_condition) do
  1115. { 'ticket.escalation_at'=>{ 'operator' => 'within next (relative)', 'value' => '30', 'range' => 'minute' } }
  1116. end
  1117. before do
  1118. sla
  1119. travel_to '2020-11-05 11:50:00'
  1120. ticket = create(:ticket)
  1121. create(:ticket_article, :inbound_email, ticket: ticket)
  1122. travel_to '2020-11-05 11:37:00'
  1123. end
  1124. context 'when in range' do
  1125. it 'does find the ticket' do
  1126. count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true)
  1127. expect(count).to eq(1)
  1128. end
  1129. end
  1130. context 'when out of range' do
  1131. let(:first_response_time) { 500 }
  1132. it 'does not find the ticket' do
  1133. count, _tickets = described_class.selectors(within_condition, limit: 2_000, execution_time: true)
  1134. expect(count).to eq(0)
  1135. end
  1136. end
  1137. end
  1138. end
  1139. describe '#first_response_escalation_at' do
  1140. before { travel_to(Time.current) } # freeze time
  1141. let(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 180, solution_time: 240) }
  1142. let(:calendar) { create(:calendar, :'24/7') }
  1143. context 'with no SLAs in the system' do
  1144. it 'defaults to nil' do
  1145. expect(ticket.first_response_escalation_at).to be_nil
  1146. end
  1147. end
  1148. context 'with an SLA in the system' do
  1149. before { sla } # create sla
  1150. it 'is set based on SLA’s #first_response_time' do
  1151. expect(ticket.reload.first_response_escalation_at.to_i)
  1152. .to eq(1.hour.from_now.to_i)
  1153. end
  1154. context 'after first agent’s response' do
  1155. before { ticket } # create ticket
  1156. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  1157. it 'is cleared' do
  1158. expect { article }.to change { ticket.reload.first_response_escalation_at }.to(nil)
  1159. end
  1160. end
  1161. end
  1162. end
  1163. describe '#update_escalation_at' do
  1164. before { travel_to(Time.current) } # freeze time
  1165. let(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 180, solution_time: 240) }
  1166. let(:calendar) { create(:calendar, :'24/7') }
  1167. context 'with no SLAs in the system' do
  1168. it 'defaults to nil' do
  1169. expect(ticket.update_escalation_at).to be_nil
  1170. end
  1171. end
  1172. context 'with an SLA in the system' do
  1173. before { sla } # create sla
  1174. it 'is set based on SLA’s #update_time' do
  1175. travel 1.minute
  1176. create(:ticket_article, ticket: ticket, sender_name: 'Customer')
  1177. expect(ticket.reload.update_escalation_at.to_i)
  1178. .to eq(3.hours.from_now.to_i)
  1179. end
  1180. context 'after first agent’s response' do
  1181. before { ticket } # create ticket
  1182. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  1183. it 'is updated based on the SLA’s #update_time' do
  1184. create(:ticket_article, ticket: ticket, sender_name: 'Customer')
  1185. travel(1.minute)
  1186. expect { article }
  1187. .to change { ticket.reload.update_escalation_at }
  1188. .to(nil)
  1189. end
  1190. end
  1191. end
  1192. end
  1193. describe '#close_escalation_at' do
  1194. before { travel_to(Time.current) } # freeze time
  1195. let(:sla) { create(:sla, calendar: calendar, first_response_time: 60, response_time: 180, solution_time: 240) }
  1196. let(:calendar) { create(:calendar, :'24/7') }
  1197. context 'with no SLAs in the system' do
  1198. it 'defaults to nil' do
  1199. expect(ticket.close_escalation_at).to be_nil
  1200. end
  1201. end
  1202. context 'with an SLA in the system' do
  1203. before { sla } # create sla
  1204. it 'is set based on SLA’s #solution_time' do
  1205. expect(ticket.reload.close_escalation_at.to_i)
  1206. .to eq(4.hours.from_now.to_i)
  1207. end
  1208. context 'after first agent’s response' do
  1209. before { ticket } # create ticket
  1210. let(:article) { create(:ticket_article, ticket: ticket, sender_name: 'Agent') }
  1211. it 'does not change' do
  1212. expect { article }.not_to change(ticket, :close_escalation_at)
  1213. end
  1214. end
  1215. end
  1216. end
  1217. end
  1218. describe '.search' do
  1219. shared_examples 'search permissions' do
  1220. let(:group) { create(:group) }
  1221. before do
  1222. ticket
  1223. end
  1224. shared_examples 'permitted' do
  1225. it 'finds Ticket' do
  1226. expect(described_class.search(query: ticket.number, current_user: current_user).count).to eq(1)
  1227. end
  1228. end
  1229. shared_examples 'no permission' do
  1230. it "doesn't find Ticket" do
  1231. expect(described_class.search(query: ticket.number, current_user: current_user)).to be_blank
  1232. end
  1233. end
  1234. context 'Agent with Group access' do
  1235. let(:ticket) do
  1236. ticket = create(:ticket, group: group)
  1237. create(:ticket_article, ticket: ticket)
  1238. ticket
  1239. end
  1240. let(:current_user) { create(:agent, groups: [group]) }
  1241. it_behaves_like 'permitted'
  1242. end
  1243. context 'when Agent is Customer of Ticket' do
  1244. let(:ticket) do
  1245. ticket = create(:ticket, customer: current_user)
  1246. create(:ticket_article, ticket: ticket)
  1247. ticket
  1248. end
  1249. let(:current_user) { create(:agent_and_customer) }
  1250. it_behaves_like 'permitted'
  1251. end
  1252. context 'for Organization access' do
  1253. let(:ticket) do
  1254. ticket = create(:ticket, customer: customer)
  1255. create(:ticket_article, ticket: ticket)
  1256. ticket
  1257. end
  1258. let(:customer) { create(:customer, organization: organization) }
  1259. context 'when Organization is shared' do
  1260. let(:organization) { create(:organization, shared: true) }
  1261. context 'for unrelated Agent' do
  1262. let(:current_user) { create(:agent) }
  1263. it_behaves_like 'no permission'
  1264. end
  1265. context 'for Agent in same Organization' do
  1266. let(:current_user) { create(:agent_and_customer, organization: organization) }
  1267. it_behaves_like 'permitted'
  1268. end
  1269. context 'for Customer of Ticket' do
  1270. let(:current_user) { customer }
  1271. it_behaves_like 'permitted'
  1272. end
  1273. end
  1274. context 'when Organization is not shared' do
  1275. let(:organization) { create(:organization, shared: false) }
  1276. context 'for unrelated Agent' do
  1277. let(:current_user) { create(:agent) }
  1278. it_behaves_like 'no permission'
  1279. end
  1280. context 'for Agent in same Organization' do
  1281. let(:current_user) { create(:agent_and_customer, organization: organization) }
  1282. it_behaves_like 'no permission'
  1283. end
  1284. context 'for Customer of Ticket' do
  1285. let(:current_user) { customer }
  1286. it_behaves_like 'permitted'
  1287. end
  1288. end
  1289. end
  1290. end
  1291. context 'with searchindex', searchindex: true do
  1292. include_examples 'search permissions' do
  1293. before do
  1294. configure_elasticsearch(required: true, rebuild: true)
  1295. end
  1296. end
  1297. end
  1298. context 'without searchindex' do
  1299. include_examples 'search permissions'
  1300. end
  1301. end
  1302. describe 'Callbacks & Observers -' do
  1303. describe 'NULL byte handling (via ChecksAttributeValuesAndLength concern):' do
  1304. it 'removes them from title on creation, if necessary (postgres doesn’t like them)' do
  1305. expect { create(:ticket, title: "some title \u0000 123") }
  1306. .not_to raise_error
  1307. end
  1308. end
  1309. describe 'XSS protection:' do
  1310. subject(:ticket) { create(:ticket, title: title) }
  1311. let(:title) { 'test 123 <script type="text/javascript">alert("XSS!");</script>' }
  1312. it 'does not sanitize title' do
  1313. expect(ticket.title).to eq(title)
  1314. end
  1315. end
  1316. describe 'Cti::CallerId syncing:', performs_jobs: true do
  1317. subject(:ticket) { build(:ticket) }
  1318. before { allow(Cti::CallerId).to receive(:build) }
  1319. it 'adds numbers in article bodies (via Cti::CallerId.build)' do
  1320. expect(Cti::CallerId).to receive(:build).with(ticket)
  1321. ticket.save
  1322. perform_enqueued_jobs commit_transaction: true
  1323. end
  1324. end
  1325. describe 'Touching associations on update:' do
  1326. subject(:ticket) { create(:ticket, customer: customer) }
  1327. let(:customer) { create(:customer, organization: organization) }
  1328. let(:organization) { create(:organization) }
  1329. let(:other_customer) { create(:customer, organization: other_organization) }
  1330. let(:other_organization) { create(:organization) }
  1331. context 'on creation' do
  1332. it 'touches its customer and his organization' do
  1333. expect { ticket }
  1334. .to change { customer.reload.updated_at }
  1335. .and change { organization.reload.updated_at }
  1336. end
  1337. end
  1338. context 'on destruction' do
  1339. before { ticket }
  1340. it 'touches its customer and his organization' do
  1341. expect { ticket.destroy }
  1342. .to change { customer.reload.updated_at }
  1343. .and change { organization.reload.updated_at }
  1344. end
  1345. end
  1346. context 'when customer association is changed' do
  1347. it 'touches both old and new customer, and their organizations' do
  1348. expect { ticket.update(customer: other_customer) }
  1349. .to change { customer.reload.updated_at }
  1350. .and change { organization.reload.updated_at }
  1351. .and change { other_customer.reload.updated_at }
  1352. .and change { other_organization.reload.updated_at }
  1353. end
  1354. end
  1355. end
  1356. describe 'Association & attachment management:' do
  1357. it 'deletes all related ActivityStreams on destroy' do
  1358. create_list(:activity_stream, 3, o: ticket)
  1359. expect { ticket.destroy }
  1360. .to change { ActivityStream.exists?(activity_stream_object_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id) }
  1361. .to(false)
  1362. end
  1363. it 'deletes all related Links on destroy' do
  1364. create(:link, from: ticket, to: create(:ticket))
  1365. create(:link, from: create(:ticket), to: ticket)
  1366. create(:link, from: ticket, to: create(:ticket))
  1367. expect { ticket.destroy }
  1368. .to change { Link.where('link_object_source_value = :id OR link_object_target_value = :id', id: ticket.id).any? }
  1369. .to(false)
  1370. end
  1371. it 'deletes all related Articles on destroy' do
  1372. create_list(:ticket_article, 3, ticket: ticket)
  1373. expect { ticket.destroy }
  1374. .to change { Ticket::Article.exists?(ticket: ticket) }
  1375. .to(false)
  1376. end
  1377. it 'deletes all related OnlineNotifications on destroy' do
  1378. create_list(:online_notification, 3, o: ticket)
  1379. expect { ticket.destroy }
  1380. .to change { OnlineNotification.where(object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id).any? }
  1381. .to(false)
  1382. end
  1383. it 'deletes all related Tags on destroy' do
  1384. create_list(:tag, 3, o: ticket)
  1385. expect { ticket.destroy }
  1386. .to change { Tag.exists?(tag_object_id: Tag::Object.lookup(name: 'Ticket').id, o_id: ticket.id) }
  1387. .to(false)
  1388. end
  1389. it 'deletes all related Histories on destroy' do
  1390. create_list(:history, 3, o: ticket)
  1391. expect { ticket.destroy }
  1392. .to change { History.exists?(history_object_id: History::Object.lookup(name: 'Ticket').id, o_id: ticket.id) }
  1393. .to(false)
  1394. end
  1395. it 'deletes all related Karma::ActivityLogs on destroy' do
  1396. create_list(:'karma/activity_log', 3, o: ticket)
  1397. expect { ticket.destroy }
  1398. .to change { Karma::ActivityLog.exists?(object_lookup_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id) }
  1399. .to(false)
  1400. end
  1401. it 'deletes all related RecentViews on destroy' do
  1402. create_list(:recent_view, 3, o: ticket)
  1403. expect { ticket.destroy }
  1404. .to change { RecentView.exists?(recent_view_object_id: ObjectLookup.by_name('Ticket'), o_id: ticket.id) }
  1405. .to(false)
  1406. end
  1407. it 'destroys all related dependencies' do
  1408. refs_known = { 'Ticket::Article' => { 'ticket_id'=>1 },
  1409. 'Ticket::TimeAccounting' => { 'ticket_id'=>1 },
  1410. 'Ticket::SharedDraftZoom' => { 'ticket_id'=>0 },
  1411. 'Ticket::Flag' => { 'ticket_id'=>1 } }
  1412. ticket = create(:ticket)
  1413. article = create(:ticket_article, ticket: ticket)
  1414. accounting = create(:ticket_time_accounting, ticket: ticket)
  1415. flag = create(:ticket_flag, ticket: ticket)
  1416. refs_ticket = Models.references('Ticket', ticket.id, true)
  1417. expect(refs_ticket).to eq(refs_known)
  1418. ticket.destroy
  1419. expect { ticket.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  1420. expect { article.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  1421. expect { accounting.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  1422. expect { flag.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  1423. end
  1424. context 'when ticket is generated from email (with attachments)' do
  1425. subject(:ticket) { Channel::EmailParser.new.process({}, raw_email).first }
  1426. let(:raw_email) { File.read(Rails.root.join('test/data/mail/mail001.box')) }
  1427. it 'adds attachments to the Store{::File,::Provider::DB} tables' do
  1428. expect { ticket }
  1429. .to change(Store, :count).by(2)
  1430. .and change(Store::File, :count).by(2)
  1431. .and change(Store::Provider::DB, :count).by(2)
  1432. end
  1433. context 'and subsequently destroyed' do
  1434. it 'deletes all related attachments' do
  1435. ticket # create ticket
  1436. expect { ticket.destroy }
  1437. .to change(Store, :count).by(-2)
  1438. .and change(Store::File, :count).by(-2)
  1439. .and change(Store::Provider::DB, :count).by(-2)
  1440. end
  1441. end
  1442. context 'and a duplicate ticket is generated from the same email' do
  1443. before { ticket } # create ticket
  1444. let(:duplicate) { Channel::EmailParser.new.process({}, raw_email).first }
  1445. it 'adds duplicate attachments to the Store table only' do
  1446. expect { duplicate }
  1447. .to change(Store, :count).by(2)
  1448. .and not_change(Store::File, :count)
  1449. .and not_change(Store::Provider::DB, :count)
  1450. end
  1451. context 'when only the duplicate ticket is destroyed' do
  1452. it 'deletes only the duplicate attachments' do
  1453. duplicate # create ticket
  1454. expect { duplicate.destroy }
  1455. .to change(Store, :count).by(-2)
  1456. .and not_change(Store::File, :count)
  1457. .and not_change(Store::Provider::DB, :count)
  1458. end
  1459. it 'deletes all related attachments' do
  1460. duplicate.destroy
  1461. expect { ticket.destroy }
  1462. .to change(Store, :count).by(-2)
  1463. .and change(Store::File, :count).by(-2)
  1464. .and change(Store::Provider::DB, :count).by(-2)
  1465. end
  1466. end
  1467. end
  1468. end
  1469. end
  1470. describe 'Ticket lifecycle order-of-operations:', performs_jobs: true do
  1471. subject!(:ticket) { create(:ticket) }
  1472. let!(:agent) { create(:agent, groups: [group]) }
  1473. let(:group) { create(:group) }
  1474. before do
  1475. create(
  1476. :trigger,
  1477. condition: { 'ticket.action' => { 'operator' => 'is', 'value' => 'create' } },
  1478. perform: { 'ticket.group_id' => { 'value' => group.id } }
  1479. )
  1480. end
  1481. it 'fires triggers before new ticket notifications are sent' do
  1482. expect { TransactionDispatcher.commit }
  1483. .to change { ticket.reload.group }.to(group)
  1484. expect { perform_enqueued_jobs }
  1485. .to change { NotificationFactory::Mailer.already_sent?(ticket, agent, 'email') }.to(1)
  1486. end
  1487. end
  1488. describe 'Ticket has changed attributes:' do
  1489. subject!(:ticket) { create(:ticket) }
  1490. let(:group) { create(:group) }
  1491. let(:condition_field) { nil }
  1492. shared_examples 'updated ticket group with trigger condition' do
  1493. it 'updated ticket group with has changed trigger condition' do
  1494. expect { TransactionDispatcher.commit }.to change { ticket.reload.group }.to(group)
  1495. end
  1496. end
  1497. before do
  1498. create(
  1499. :trigger,
  1500. condition: { "ticket.#{condition_field}" => { 'operator' => 'has changed', 'value' => 'create' } },
  1501. perform: { 'ticket.group_id' => { 'value' => group.id } }
  1502. )
  1503. ticket.update!(condition_field => Time.zone.now)
  1504. end
  1505. context "when changing 'first_response_at' attribute" do
  1506. let(:condition_field) { 'first_response_at' }
  1507. include_examples 'updated ticket group with trigger condition'
  1508. end
  1509. context "when changing 'close_at' attribute" do
  1510. let(:condition_field) { 'close_at' }
  1511. include_examples 'updated ticket group with trigger condition'
  1512. end
  1513. context "when changing 'last_contact_agent_at' attribute" do
  1514. let(:condition_field) { 'last_contact_agent_at' }
  1515. include_examples 'updated ticket group with trigger condition'
  1516. end
  1517. context "when changing 'last_contact_customer_at' attribute" do
  1518. let(:condition_field) { 'last_contact_customer_at' }
  1519. include_examples 'updated ticket group with trigger condition'
  1520. end
  1521. context "when changing 'last_contact_at' attribute" do
  1522. let(:condition_field) { 'last_contact_at' }
  1523. include_examples 'updated ticket group with trigger condition'
  1524. end
  1525. end
  1526. end
  1527. describe 'Mentions:', sends_notification_emails: true do
  1528. context 'when notifications', performs_jobs: true do
  1529. let(:prefs_matrix_no_mentions) do
  1530. { 'notification_config' =>
  1531. { 'matrix' =>
  1532. { 'create' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true, 'subscribed' => false, 'no' => true }, 'channel' => { 'email' => true, 'online' => true } },
  1533. 'update' => { 'criteria' => { 'owned_by_me' => true, 'owned_by_nobody' => true, 'subscribed' => false, 'no' => true }, 'channel' => { 'email' => true, 'online' => true } },
  1534. 'reminder_reached' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => false, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } },
  1535. 'escalation' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => false, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } } } } }
  1536. end
  1537. let(:prefs_matrix_only_mentions) do
  1538. { 'notification_config' =>
  1539. { 'matrix' =>
  1540. { 'create' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => true, 'online' => true } },
  1541. 'update' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => true, 'online' => true } },
  1542. 'reminder_reached' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } },
  1543. 'escalation' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } } } } }
  1544. end
  1545. let(:prefs_matrix_only_mentions_groups) do
  1546. { 'notification_config' =>
  1547. { 'matrix' =>
  1548. { 'create' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => true, 'online' => true } },
  1549. 'update' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => true, 'online' => true } },
  1550. 'reminder_reached' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } },
  1551. 'escalation' => { 'criteria' => { 'owned_by_me' => false, 'owned_by_nobody' => false, 'subscribed' => true, 'no' => false }, 'channel' => { 'email' => false, 'online' => false } } },
  1552. 'group_ids' => [create(:group).id, create(:group).id, create(:group).id] } }
  1553. end
  1554. let(:mention_group) { create(:group) }
  1555. let(:no_access_group) { create(:group) }
  1556. let(:user_only_mentions) { create(:agent, groups: [mention_group], preferences: prefs_matrix_only_mentions) }
  1557. let(:user_read_mentions) { create(:agent, groups: [mention_group], preferences: prefs_matrix_only_mentions_groups) }
  1558. let(:user_no_mentions) { create(:agent, groups: [mention_group], preferences: prefs_matrix_no_mentions) }
  1559. let(:ticket) { create(:ticket, group: mention_group, owner: user_no_mentions) }
  1560. it 'does inform mention user about the ticket update' do
  1561. create(:mention, mentionable: ticket, user: user_only_mentions)
  1562. create(:mention, mentionable: ticket, user: user_read_mentions)
  1563. create(:mention, mentionable: ticket, user: user_no_mentions)
  1564. perform_enqueued_jobs commit_transaction: true
  1565. check_notification do
  1566. ticket.update(priority: Ticket::Priority.find_by(name: '3 high'))
  1567. perform_enqueued_jobs commit_transaction: true
  1568. sent(
  1569. template: 'ticket_update',
  1570. user: user_no_mentions,
  1571. )
  1572. sent(
  1573. template: 'ticket_update',
  1574. user: user_read_mentions,
  1575. )
  1576. sent(
  1577. template: 'ticket_update',
  1578. user: user_only_mentions,
  1579. )
  1580. end
  1581. end
  1582. it 'does not inform mention user about the ticket update' do
  1583. ticket
  1584. perform_enqueued_jobs commit_transaction: true
  1585. check_notification do
  1586. ticket.update(priority: Ticket::Priority.find_by(name: '3 high'))
  1587. perform_enqueued_jobs commit_transaction: true
  1588. sent(
  1589. template: 'ticket_update',
  1590. user: user_no_mentions,
  1591. )
  1592. not_sent(
  1593. template: 'ticket_update',
  1594. user: user_read_mentions,
  1595. )
  1596. not_sent(
  1597. template: 'ticket_update',
  1598. user: user_only_mentions,
  1599. )
  1600. end
  1601. end
  1602. it 'does inform mention user about ticket creation' do
  1603. check_notification do
  1604. ticket = create(:ticket, owner: user_no_mentions, group: mention_group)
  1605. create(:mention, mentionable: ticket, user: user_read_mentions)
  1606. create(:mention, mentionable: ticket, user: user_only_mentions)
  1607. perform_enqueued_jobs commit_transaction: true
  1608. sent(
  1609. template: 'ticket_create',
  1610. user: user_no_mentions,
  1611. )
  1612. sent(
  1613. template: 'ticket_create',
  1614. user: user_read_mentions,
  1615. )
  1616. sent(
  1617. template: 'ticket_create',
  1618. user: user_only_mentions,
  1619. )
  1620. end
  1621. end
  1622. it 'does not inform mention user about ticket creation' do
  1623. check_notification do
  1624. create(:ticket, owner: user_no_mentions, group: mention_group)
  1625. perform_enqueued_jobs commit_transaction: true
  1626. sent(
  1627. template: 'ticket_create',
  1628. user: user_no_mentions,
  1629. )
  1630. not_sent(
  1631. template: 'ticket_create',
  1632. user: user_read_mentions,
  1633. )
  1634. not_sent(
  1635. template: 'ticket_create',
  1636. user: user_only_mentions,
  1637. )
  1638. end
  1639. end
  1640. it 'does not inform mention user about ticket creation because of no permissions' do
  1641. check_notification do
  1642. ticket = create(:ticket, group: no_access_group)
  1643. create(:mention, mentionable: ticket, user: user_read_mentions)
  1644. create(:mention, mentionable: ticket, user: user_only_mentions)
  1645. perform_enqueued_jobs commit_transaction: true
  1646. not_sent(
  1647. template: 'ticket_create',
  1648. user: user_read_mentions,
  1649. )
  1650. not_sent(
  1651. template: 'ticket_create',
  1652. user: user_only_mentions,
  1653. )
  1654. end
  1655. end
  1656. end
  1657. context 'selectors' do
  1658. let(:mention_group) { create(:group) }
  1659. let(:ticket_mentions) { create(:ticket, group: mention_group) }
  1660. let(:ticket_normal) { create(:ticket, group: mention_group) }
  1661. let(:user_mentions) { create(:agent, groups: [mention_group]) }
  1662. let(:user_no_mentions) { create(:agent, groups: [mention_group]) }
  1663. before do
  1664. described_class.destroy_all
  1665. ticket_normal
  1666. user_no_mentions
  1667. create(:mention, mentionable: ticket_mentions, user: user_mentions)
  1668. end
  1669. it 'pre condition is not_set' do
  1670. condition = {
  1671. 'ticket.mention_user_ids' => {
  1672. pre_condition: 'not_set',
  1673. operator: 'is',
  1674. },
  1675. }
  1676. expect(described_class.selectors(condition, limit: 100, access: 'full'))
  1677. .to match_array([1, [ticket_normal].to_a])
  1678. end
  1679. it 'pre condition is not not_set' do
  1680. condition = {
  1681. 'ticket.mention_user_ids' => {
  1682. pre_condition: 'not_set',
  1683. operator: 'is not',
  1684. },
  1685. }
  1686. expect(described_class.selectors(condition, limit: 100, access: 'full'))
  1687. .to match_array([1, [ticket_mentions].to_a])
  1688. end
  1689. it 'pre condition is current_user.id' do
  1690. condition = {
  1691. 'ticket.mention_user_ids' => {
  1692. pre_condition: 'current_user.id',
  1693. operator: 'is',
  1694. },
  1695. }
  1696. expect(described_class.selectors(condition, limit: 100, access: 'full', current_user: user_mentions))
  1697. .to match_array([1, [ticket_mentions].to_a])
  1698. end
  1699. it 'pre condition is not current_user.id' do
  1700. condition = {
  1701. 'ticket.mention_user_ids' => {
  1702. pre_condition: 'current_user.id',
  1703. operator: 'is not',
  1704. },
  1705. }
  1706. expect(described_class.selectors(condition, limit: 100, access: 'full', current_user: user_mentions))
  1707. .to match_array([0, []])
  1708. end
  1709. it 'pre condition is specific' do
  1710. condition = {
  1711. 'ticket.mention_user_ids' => {
  1712. pre_condition: 'specific',
  1713. operator: 'is',
  1714. value: user_mentions.id
  1715. },
  1716. }
  1717. expect(described_class.selectors(condition, limit: 100, access: 'full'))
  1718. .to match_array([1, [ticket_mentions].to_a])
  1719. end
  1720. it 'pre condition is not specific' do
  1721. condition = {
  1722. 'ticket.mention_user_ids' => {
  1723. pre_condition: 'specific',
  1724. operator: 'is not',
  1725. value: user_mentions.id
  1726. },
  1727. }
  1728. expect(described_class.selectors(condition, limit: 100, access: 'full'))
  1729. .to match_array([0, []])
  1730. end
  1731. end
  1732. end
  1733. describe '.search_index_attribute_lookup_oversized?' do
  1734. subject!(:ticket) { create(:ticket) }
  1735. context 'when payload is ok' do
  1736. let(:current_payload_size) { 3.megabyte }
  1737. it 'return false' do
  1738. expect(ticket.send(:search_index_attribute_lookup_oversized?, current_payload_size)).to be false
  1739. end
  1740. end
  1741. context 'when payload is bigger' do
  1742. let(:current_payload_size) { 350.megabyte }
  1743. it 'return true' do
  1744. expect(ticket.send(:search_index_attribute_lookup_oversized?, current_payload_size)).to be true
  1745. end
  1746. end
  1747. end
  1748. describe '.search_index_attribute_lookup_file_oversized?' do
  1749. subject!(:store) do
  1750. create(:store,
  1751. object: 'SomeObject',
  1752. o_id: 1,
  1753. data: 'a' * ((1024**2) * 2.4), # with 2.4 mb
  1754. filename: 'test.TXT')
  1755. end
  1756. context 'when total payload is ok' do
  1757. let(:current_payload_size) { 200.megabyte }
  1758. it 'return false' do
  1759. expect(ticket.send(:search_index_attribute_lookup_file_oversized?, store, current_payload_size)).to be false
  1760. end
  1761. end
  1762. context 'when total payload is oversized' do
  1763. let(:current_payload_size) { 299.megabyte }
  1764. it 'return true' do
  1765. expect(ticket.send(:search_index_attribute_lookup_file_oversized?, store, current_payload_size)).to be true
  1766. end
  1767. end
  1768. end
  1769. describe '.search_index_attribute_lookup_file_ignored?' do
  1770. context 'when attachment is indexable' do
  1771. subject!(:store_with_indexable_extention) do
  1772. create(:store,
  1773. object: 'SomeObject',
  1774. o_id: 1,
  1775. data: 'some content',
  1776. filename: 'test.TXT')
  1777. end
  1778. it 'return false' do
  1779. expect(ticket.send(:search_index_attribute_lookup_file_ignored?, store_with_indexable_extention)).to be false
  1780. end
  1781. end
  1782. context 'when attachment is no indexable' do
  1783. subject!(:store_without_indexable_extention) do
  1784. create(:store,
  1785. object: 'SomeObject',
  1786. o_id: 1,
  1787. data: 'some content',
  1788. filename: 'test.BIN')
  1789. end
  1790. it 'return true' do
  1791. expect(ticket.send(:search_index_attribute_lookup_file_ignored?, store_without_indexable_extention)).to be true
  1792. end
  1793. end
  1794. end
  1795. describe '.search_index_attribute_lookup' do
  1796. subject!(:ticket) { create(:ticket) }
  1797. let(:search_index_attribute_lookup) do
  1798. article1 = create(:ticket_article, ticket: ticket)
  1799. create(:store,
  1800. object: 'Ticket::Article',
  1801. o_id: article1.id,
  1802. data: 'some content',
  1803. filename: 'some_file.bin',
  1804. preferences: {
  1805. 'Content-Type' => 'text/plain',
  1806. })
  1807. create(:store,
  1808. object: 'Ticket::Article',
  1809. o_id: article1.id,
  1810. data: 'a' * ((1024**2) * 2.4), # with 2.4 mb
  1811. filename: 'some_file.pdf',
  1812. preferences: {
  1813. 'Content-Type' => 'image/pdf',
  1814. })
  1815. create(:store,
  1816. object: 'Ticket::Article',
  1817. o_id: article1.id,
  1818. data: 'a' * ((1024**2) * 5.8), # with 5,8 mb
  1819. filename: 'some_file.txt',
  1820. preferences: {
  1821. 'Content-Type' => 'text/plain',
  1822. })
  1823. create(:ticket_article, ticket: ticket, body: 'a' * ((1024**2) * 1.2)) # body with 1,2 mb
  1824. create(:ticket_article, ticket: ticket)
  1825. ticket.search_index_attribute_lookup
  1826. end
  1827. context 'when es_attachment_max_size_in_mb takes all attachments' do
  1828. before { Setting.set('es_attachment_max_size_in_mb', 15) }
  1829. it 'verify count of articles' do
  1830. expect(search_index_attribute_lookup['article'].count).to eq 3
  1831. end
  1832. it 'verify count of attachments' do
  1833. expect(search_index_attribute_lookup['article'][0]['attachment'].count).to eq 2
  1834. end
  1835. it 'verify if pdf exists' do
  1836. expect(search_index_attribute_lookup['article'][0]['attachment'][0]['_name']).to eq 'some_file.pdf'
  1837. end
  1838. it 'verify if txt exists' do
  1839. expect(search_index_attribute_lookup['article'][0]['attachment'][1]['_name']).to eq 'some_file.txt'
  1840. end
  1841. end
  1842. context 'when es_attachment_max_size_in_mb takes only one attachment' do
  1843. before { Setting.set('es_attachment_max_size_in_mb', 4) }
  1844. it 'verify count of articles' do
  1845. expect(search_index_attribute_lookup['article'].count).to eq 3
  1846. end
  1847. it 'verify count of attachments' do
  1848. expect(search_index_attribute_lookup['article'][0]['attachment'].count).to eq 1
  1849. end
  1850. it 'verify if pdf exists' do
  1851. expect(search_index_attribute_lookup['article'][0]['attachment'][0]['_name']).to eq 'some_file.pdf'
  1852. end
  1853. end
  1854. context 'when es_attachment_max_size_in_mb takes no attachment' do
  1855. before { Setting.set('es_attachment_max_size_in_mb', 2) }
  1856. it 'verify count of articles' do
  1857. expect(search_index_attribute_lookup['article'].count).to eq 3
  1858. end
  1859. it 'verify count of attachments' do
  1860. expect(search_index_attribute_lookup['article'][0]['attachment'].count).to eq 0
  1861. end
  1862. end
  1863. context 'when es_total_max_size_in_mb takes no attachment and no oversized article' do
  1864. before { Setting.set('es_total_max_size_in_mb', 1) }
  1865. it 'verify count of articles' do
  1866. expect(search_index_attribute_lookup['article'].count).to eq 2
  1867. end
  1868. it 'verify count of attachments' do
  1869. expect(search_index_attribute_lookup['article'][0]['attachment'].count).to eq 0
  1870. end
  1871. end
  1872. end
  1873. describe 'Automatic assignment assigns tickets in each group, not just the marked ones #4308' do
  1874. let(:ticket) { create(:ticket, group: Group.first, state: Ticket::State.find_by(name: 'closed')) }
  1875. let(:agent) { create(:agent, groups: [Group.first]) }
  1876. context 'when the condition does match' do
  1877. before do
  1878. Setting.set('ticket_auto_assignment', true)
  1879. Setting.set('ticket_auto_assignment_selector', { condition: { 'ticket.state_id' => { operator: 'is', value: Ticket::State.all.pluck(:id) } } })
  1880. end
  1881. it 'does auto assign' do
  1882. ticket.auto_assign(agent)
  1883. expect(ticket.reload.owner_id).to eq(agent.id)
  1884. end
  1885. end
  1886. context 'when the condition does not match' do
  1887. before do
  1888. Setting.set('ticket_auto_assignment', true)
  1889. Setting.set('ticket_auto_assignment_selector', { condition: { 'ticket.state_id' => { operator: 'is', value: Ticket::State.by_category(:work_on).pluck(:id) } } })
  1890. end
  1891. it 'does not auto assign' do
  1892. ticket.auto_assign(agent)
  1893. expect(ticket.reload.owner_id).to eq(1)
  1894. end
  1895. end
  1896. end
  1897. end