ticket_selector_test.rb 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class TicketSelectorTest < ActiveSupport::TestCase
  4. setup do
  5. @group = Group.create_or_update(
  6. name: 'SelectorTest',
  7. updated_at: '2015-02-05 16:37:00',
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. roles = Role.where(name: 'Agent')
  12. @agent1 = User.create_or_update(
  13. login: 'ticket-selector-agent1@example.com',
  14. firstname: 'Notification',
  15. lastname: 'Agent1',
  16. email: 'ticket-selector-agent1@example.com',
  17. password: 'agentpw',
  18. active: true,
  19. roles: roles,
  20. groups: [@group],
  21. updated_at: '2015-02-05 16:37:00',
  22. updated_by_id: 1,
  23. created_by_id: 1,
  24. )
  25. @agent2 = User.create_or_update(
  26. login: 'ticket-selector-agent2@example.com',
  27. firstname: 'Notification',
  28. lastname: 'Agent2',
  29. email: 'ticket-selector-agent2@example.com',
  30. password: 'agentpw',
  31. active: true,
  32. roles: roles,
  33. updated_at: '2015-02-05 16:38:00',
  34. updated_by_id: 1,
  35. created_by_id: 1,
  36. )
  37. roles = Role.where(name: 'Customer')
  38. @organization1 = Organization.create_if_not_exists(
  39. name: 'Selector Org',
  40. updated_at: '2015-02-05 16:37:00',
  41. updated_by_id: 1,
  42. created_by_id: 1,
  43. )
  44. @customer1 = User.create_or_update(
  45. login: 'ticket-selector-customer1@example.com',
  46. firstname: 'Notification',
  47. lastname: 'Customer1',
  48. email: 'ticket-selector-customer1@example.com',
  49. password: 'customerpw',
  50. active: true,
  51. organization_id: @organization1.id,
  52. roles: roles,
  53. updated_at: '2015-02-05 16:37:00',
  54. updated_by_id: 1,
  55. created_by_id: 1,
  56. )
  57. @customer2 = User.create_or_update(
  58. login: 'ticket-selector-customer2@example.com',
  59. firstname: 'Notification',
  60. lastname: 'Customer2',
  61. email: 'ticket-selector-customer2@example.com',
  62. password: 'customerpw',
  63. active: true,
  64. organization_id: nil,
  65. roles: roles,
  66. updated_at: '2015-02-05 16:37:00',
  67. updated_by_id: 1,
  68. created_by_id: 1,
  69. )
  70. Ticket.where(group_id: @group.id).destroy_all
  71. end
  72. test 'ticket create' do
  73. Ticket.destroy_all
  74. ticket1 = Ticket.create!(
  75. title: 'some title1',
  76. group: @group,
  77. customer_id: @customer1.id,
  78. owner_id: @agent1.id,
  79. state: Ticket::State.lookup(name: 'new'),
  80. priority: Ticket::Priority.lookup(name: '2 normal'),
  81. created_at: '2015-02-05 16:37:00',
  82. #updated_at: '2015-02-05 17:37:00',
  83. updated_by_id: 1,
  84. created_by_id: 1,
  85. )
  86. assert(ticket1, 'ticket created')
  87. assert_equal(ticket1.customer.id, @customer1.id)
  88. assert_equal(ticket1.organization.id, @organization1.id)
  89. travel 1.second
  90. ticket2 = Ticket.create!(
  91. title: 'some title2',
  92. group: @group,
  93. customer_id: @customer2.id,
  94. state: Ticket::State.lookup(name: 'new'),
  95. priority: Ticket::Priority.lookup(name: '2 normal'),
  96. created_at: '2015-02-05 16:37:00',
  97. #updated_at: '2015-02-05 17:37:00',
  98. updated_by_id: 1,
  99. created_by_id: 1,
  100. )
  101. assert(ticket2, 'ticket created')
  102. assert_equal(ticket2.customer.id, @customer2.id)
  103. assert_nil(ticket2.organization_id)
  104. travel 1.second
  105. ticket3 = Ticket.create!(
  106. title: 'some title3',
  107. group: @group,
  108. customer_id: @customer2.id,
  109. state: Ticket::State.lookup(name: 'open'),
  110. priority: Ticket::Priority.lookup(name: '2 normal'),
  111. created_at: '2015-02-05 16:37:00',
  112. #updated_at: '2015-02-05 17:37:00',
  113. updated_by_id: 1,
  114. created_by_id: 1,
  115. )
  116. ticket3.update_columns(escalation_at: '2015-02-06 10:00:00')
  117. assert(ticket3, 'ticket created')
  118. assert_equal(ticket3.customer.id, @customer2.id)
  119. assert_nil(ticket3.organization_id)
  120. travel 1.second
  121. # search not matching
  122. condition = {
  123. 'ticket.state_id' => {
  124. operator: 'is',
  125. value: [99],
  126. },
  127. }
  128. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  129. assert_equal(ticket_count, 0)
  130. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  131. assert_equal(ticket_count, 0)
  132. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  133. assert_equal(ticket_count, 0)
  134. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  135. assert_equal(ticket_count, 0)
  136. # search matching with empty value / missing key
  137. condition = {
  138. 'ticket.group_id' => {
  139. operator: 'is',
  140. value: @group.id,
  141. },
  142. 'ticket.state_id' => {
  143. operator: 'is',
  144. },
  145. }
  146. ticket_count, tickets = Ticket.selectors(condition, 10)
  147. assert_nil(ticket_count)
  148. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  149. assert_nil(ticket_count)
  150. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  151. assert_nil(ticket_count)
  152. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  153. assert_nil(ticket_count)
  154. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  155. assert_nil(ticket_count)
  156. # search matching with empty value []
  157. condition = {
  158. 'ticket.group_id' => {
  159. operator: 'is',
  160. value: @group.id,
  161. },
  162. 'ticket.state_id' => {
  163. operator: 'is',
  164. value: [],
  165. },
  166. }
  167. ticket_count, tickets = Ticket.selectors(condition, 10)
  168. assert_nil(ticket_count)
  169. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  170. assert_nil(ticket_count)
  171. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  172. assert_nil(ticket_count)
  173. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  174. assert_nil(ticket_count)
  175. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  176. assert_nil(ticket_count)
  177. # search matching with empty value ''
  178. condition = {
  179. 'ticket.group_id' => {
  180. operator: 'is',
  181. value: @group.id,
  182. },
  183. 'ticket.state_id' => {
  184. operator: 'is',
  185. value: '',
  186. },
  187. }
  188. ticket_count, tickets = Ticket.selectors(condition, 10)
  189. assert_nil(ticket_count)
  190. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  191. assert_nil(ticket_count)
  192. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  193. assert_nil(ticket_count)
  194. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  195. assert_nil(ticket_count)
  196. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  197. assert_nil(ticket_count)
  198. # search matching
  199. condition = {
  200. 'ticket.group_id' => {
  201. operator: 'is',
  202. value: @group.id,
  203. },
  204. 'ticket.state_id' => {
  205. operator: 'is',
  206. value: [Ticket::State.lookup(name: 'new').id],
  207. },
  208. }
  209. ticket_count, tickets = Ticket.selectors(condition, 10)
  210. assert_equal(ticket_count, 2)
  211. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  212. assert_equal(ticket_count, 2)
  213. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  214. assert_equal(ticket_count, 0)
  215. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  216. assert_equal(ticket_count, 1)
  217. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  218. assert_equal(ticket_count, 1)
  219. condition = {
  220. 'ticket.group_id' => {
  221. operator: 'is',
  222. value: @group.id,
  223. },
  224. 'ticket.state_id' => {
  225. operator: 'is not',
  226. value: [Ticket::State.lookup(name: 'open').id],
  227. },
  228. }
  229. ticket_count, tickets = Ticket.selectors(condition, 10)
  230. assert_equal(ticket_count, 2)
  231. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  232. assert_equal(ticket_count, 2)
  233. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  234. assert_equal(ticket_count, 0)
  235. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  236. assert_equal(ticket_count, 1)
  237. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  238. assert_equal(ticket_count, 1)
  239. condition = {
  240. 'ticket.escalation_at' => {
  241. operator: 'is not',
  242. value: nil,
  243. }
  244. }
  245. ticket_count, tickets = Ticket.selectors(condition, 10)
  246. assert_equal(ticket_count, 1)
  247. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  248. assert_equal(ticket_count, 1)
  249. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  250. assert_equal(ticket_count, 0)
  251. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  252. assert_equal(ticket_count, 0)
  253. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  254. assert_equal(ticket_count, 1)
  255. # search - created_at
  256. condition = {
  257. 'ticket.group_id' => {
  258. operator: 'is',
  259. value: @group.id,
  260. },
  261. 'ticket.created_at' => {
  262. operator: 'after (absolute)', # before (absolute)
  263. value: '2015-02-05T16:00:00.000Z',
  264. },
  265. }
  266. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  267. assert_equal(ticket_count, 3)
  268. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  269. assert_equal(ticket_count, 0)
  270. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  271. assert_equal(ticket_count, 1)
  272. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  273. assert_equal(ticket_count, 2)
  274. condition = {
  275. 'ticket.group_id' => {
  276. operator: 'is',
  277. value: @group.id,
  278. },
  279. 'ticket.created_at' => {
  280. operator: 'after (absolute)', # before (absolute)
  281. value: '2015-02-05T18:00:00.000Z',
  282. },
  283. }
  284. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  285. assert_equal(ticket_count, 0)
  286. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  287. assert_equal(ticket_count, 0)
  288. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  289. assert_equal(ticket_count, 0)
  290. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  291. assert_equal(ticket_count, 0)
  292. condition = {
  293. 'ticket.group_id' => {
  294. operator: 'is',
  295. value: @group.id,
  296. },
  297. 'ticket.created_at' => {
  298. operator: 'before (absolute)',
  299. value: '2015-02-05T18:00:00.000Z',
  300. },
  301. }
  302. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  303. assert_equal(ticket_count, 3)
  304. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  305. assert_equal(ticket_count, 0)
  306. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  307. assert_equal(ticket_count, 1)
  308. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  309. assert_equal(ticket_count, 2)
  310. condition = {
  311. 'ticket.group_id' => {
  312. operator: 'is',
  313. value: @group.id,
  314. },
  315. 'ticket.created_at' => {
  316. operator: 'before (absolute)',
  317. value: '2015-02-05T16:00:00.000Z',
  318. },
  319. }
  320. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  321. assert_equal(ticket_count, 0)
  322. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  323. assert_equal(ticket_count, 0)
  324. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  325. assert_equal(ticket_count, 0)
  326. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  327. assert_equal(ticket_count, 0)
  328. condition = {
  329. 'ticket.group_id' => {
  330. operator: 'is',
  331. value: @group.id,
  332. },
  333. 'ticket.created_at' => {
  334. operator: 'before (relative)',
  335. range: 'day', # minute|hour|day|month|
  336. value: '10',
  337. },
  338. }
  339. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  340. assert_equal(ticket_count, 3)
  341. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  342. assert_equal(ticket_count, 0)
  343. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  344. assert_equal(ticket_count, 1)
  345. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  346. assert_equal(ticket_count, 2)
  347. condition = {
  348. 'ticket.group_id' => {
  349. operator: 'is',
  350. value: @group.id,
  351. },
  352. 'ticket.created_at' => {
  353. operator: 'within next (relative)',
  354. range: 'year', # minute|hour|day|month|
  355. value: '10',
  356. },
  357. }
  358. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  359. assert_equal(ticket_count, 3)
  360. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  361. assert_equal(ticket_count, 0)
  362. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  363. assert_equal(ticket_count, 1)
  364. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  365. assert_equal(ticket_count, 2)
  366. condition = {
  367. 'ticket.group_id' => {
  368. operator: 'is',
  369. value: @group.id,
  370. },
  371. 'ticket.created_at' => {
  372. operator: 'within last (relative)',
  373. range: 'year', # minute|hour|day|month|
  374. value: '10',
  375. },
  376. }
  377. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  378. assert_equal(ticket_count, 3)
  379. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  380. assert_equal(ticket_count, 0)
  381. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  382. assert_equal(ticket_count, 1)
  383. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  384. assert_equal(ticket_count, 2)
  385. # search - updated_at
  386. condition = {
  387. 'ticket.group_id' => {
  388. operator: 'is',
  389. value: @group.id,
  390. },
  391. 'ticket.updated_at' => {
  392. operator: 'before (absolute)',
  393. value: (Time.zone.now + 1.day).iso8601,
  394. },
  395. }
  396. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  397. assert_equal(ticket_count, 3)
  398. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  399. assert_equal(ticket_count, 0)
  400. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  401. assert_equal(ticket_count, 1)
  402. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  403. assert_equal(ticket_count, 2)
  404. condition = {
  405. 'ticket.group_id' => {
  406. operator: 'is',
  407. value: @group.id,
  408. },
  409. 'ticket.updated_at' => {
  410. operator: 'before (absolute)',
  411. value: (Time.zone.now - 1.day).iso8601,
  412. },
  413. }
  414. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  415. assert_equal(ticket_count, 0)
  416. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  417. assert_equal(ticket_count, 0)
  418. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  419. assert_equal(ticket_count, 0)
  420. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  421. assert_equal(ticket_count, 0)
  422. condition = {
  423. 'ticket.group_id' => {
  424. operator: 'is',
  425. value: @group.id,
  426. },
  427. 'ticket.updated_at' => {
  428. operator: 'after (absolute)',
  429. value: (Time.zone.now + 1.day).iso8601,
  430. },
  431. }
  432. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  433. assert_equal(ticket_count, 0)
  434. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  435. assert_equal(ticket_count, 0)
  436. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  437. assert_equal(ticket_count, 0)
  438. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  439. assert_equal(ticket_count, 0)
  440. condition = {
  441. 'ticket.group_id' => {
  442. operator: 'is',
  443. value: @group.id,
  444. },
  445. 'ticket.updated_at' => {
  446. operator: 'after (absolute)',
  447. value: (Time.zone.now - 1.day).iso8601,
  448. },
  449. }
  450. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  451. assert_equal(ticket_count, 3)
  452. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  453. assert_equal(ticket_count, 0)
  454. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  455. assert_equal(ticket_count, 1)
  456. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  457. assert_equal(ticket_count, 2)
  458. condition = {
  459. 'ticket.group_id' => {
  460. operator: 'is',
  461. value: @group.id,
  462. },
  463. 'ticket.updated_at' => {
  464. operator: 'before (relative)',
  465. range: 'day', # minute|hour|day|month|
  466. value: '10',
  467. },
  468. }
  469. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  470. assert_equal(ticket_count, 0)
  471. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  472. assert_equal(ticket_count, 0)
  473. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  474. assert_equal(ticket_count, 0)
  475. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  476. assert_equal(ticket_count, 0)
  477. condition = {
  478. 'ticket.group_id' => {
  479. operator: 'is',
  480. value: @group.id,
  481. },
  482. 'ticket.updated_at' => {
  483. operator: 'within next (relative)',
  484. range: 'year', # minute|hour|day|month|
  485. value: '10',
  486. },
  487. }
  488. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  489. assert_equal(ticket_count, 3)
  490. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  491. assert_equal(ticket_count, 0)
  492. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  493. assert_equal(ticket_count, 1)
  494. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  495. assert_equal(ticket_count, 2)
  496. condition = {
  497. 'ticket.group_id' => {
  498. operator: 'is',
  499. value: @group.id,
  500. },
  501. 'ticket.updated_at' => {
  502. operator: 'within last (relative)',
  503. range: 'year', # minute|hour|day|month|
  504. value: '10',
  505. },
  506. }
  507. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  508. assert_equal(ticket_count, 3)
  509. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  510. assert_equal(ticket_count, 0)
  511. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  512. assert_equal(ticket_count, 1)
  513. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  514. assert_equal(ticket_count, 2)
  515. # invalid conditions
  516. assert_raise RuntimeError do
  517. ticket_count, tickets = Ticket.selectors(nil, 10)
  518. end
  519. # search with customers
  520. condition = {
  521. 'ticket.group_id' => {
  522. operator: 'is',
  523. value: @group.id,
  524. },
  525. 'customer.email' => {
  526. operator: 'contains',
  527. value: 'ticket-selector-customer1',
  528. },
  529. }
  530. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  531. assert_equal(ticket_count, 1)
  532. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  533. assert_equal(ticket_count, 0)
  534. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  535. assert_equal(ticket_count, 1)
  536. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  537. assert_equal(ticket_count, 0)
  538. condition = {
  539. 'ticket.group_id' => {
  540. operator: 'is',
  541. value: @group.id,
  542. },
  543. 'customer.email' => {
  544. operator: 'contains not',
  545. value: 'ticket-selector-customer1-not_existing',
  546. },
  547. }
  548. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  549. assert_equal(ticket_count, 3)
  550. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  551. assert_equal(ticket_count, 0)
  552. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  553. assert_equal(ticket_count, 1)
  554. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  555. assert_equal(ticket_count, 2)
  556. # search with organizations
  557. condition = {
  558. 'ticket.group_id' => {
  559. operator: 'is',
  560. value: @group.id,
  561. },
  562. 'organization.name' => {
  563. operator: 'contains',
  564. value: 'selector',
  565. },
  566. }
  567. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  568. assert_equal(ticket_count, 1)
  569. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  570. assert_equal(ticket_count, 0)
  571. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  572. assert_equal(ticket_count, 1)
  573. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  574. assert_equal(ticket_count, 0)
  575. # search with organizations
  576. condition = {
  577. 'ticket.group_id' => {
  578. operator: 'is',
  579. value: @group.id,
  580. },
  581. 'organization.name' => {
  582. operator: 'contains',
  583. value: 'selector',
  584. },
  585. 'customer.email' => {
  586. operator: 'contains',
  587. value: 'ticket-selector-customer1',
  588. },
  589. }
  590. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  591. assert_equal(ticket_count, 1)
  592. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  593. assert_equal(ticket_count, 0)
  594. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  595. assert_equal(ticket_count, 1)
  596. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  597. assert_equal(ticket_count, 0)
  598. condition = {
  599. 'ticket.group_id' => {
  600. operator: 'is',
  601. value: @group.id,
  602. },
  603. 'organization.name' => {
  604. operator: 'contains',
  605. value: 'selector',
  606. },
  607. 'customer.email' => {
  608. operator: 'contains not',
  609. value: 'ticket-selector-customer1',
  610. },
  611. }
  612. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  613. assert_equal(ticket_count, 0)
  614. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  615. assert_equal(ticket_count, 0)
  616. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  617. assert_equal(ticket_count, 0)
  618. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  619. assert_equal(ticket_count, 0)
  620. # with owner/customer/org
  621. condition = {
  622. 'ticket.group_id' => {
  623. operator: 'is',
  624. value: @group.id,
  625. },
  626. 'ticket.owner_id' => {
  627. operator: 'is',
  628. pre_condition: 'specific',
  629. value: @agent1.id,
  630. },
  631. }
  632. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  633. assert_equal(ticket_count, 1)
  634. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  635. assert_equal(ticket_count, 0)
  636. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  637. assert_equal(ticket_count, 1)
  638. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  639. assert_equal(ticket_count, 0)
  640. condition = {
  641. 'ticket.group_id' => {
  642. operator: 'is',
  643. value: @group.id,
  644. },
  645. 'ticket.owner_id' => {
  646. operator: 'is',
  647. pre_condition: 'specific',
  648. #value: @agent1.id, # value is not set, no result should be shown
  649. },
  650. }
  651. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  652. assert_nil(ticket_count)
  653. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  654. assert_nil(ticket_count)
  655. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  656. assert_nil(ticket_count)
  657. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  658. assert_nil(ticket_count)
  659. condition = {
  660. 'ticket.group_id' => {
  661. operator: 'is',
  662. value: @group.id,
  663. },
  664. 'ticket.owner_id' => {
  665. operator: 'is',
  666. pre_condition: 'not_set',
  667. },
  668. }
  669. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  670. assert_equal(ticket_count, 2)
  671. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  672. assert_equal(ticket_count, 0)
  673. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  674. assert_equal(ticket_count, 0)
  675. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  676. assert_equal(ticket_count, 2)
  677. condition = {
  678. 'ticket.group_id' => {
  679. operator: 'is',
  680. value: @group.id,
  681. },
  682. 'ticket.owner_id' => {
  683. operator: 'is not',
  684. pre_condition: 'not_set',
  685. },
  686. }
  687. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  688. assert_equal(ticket_count, 1)
  689. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  690. assert_equal(ticket_count, 0)
  691. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  692. assert_equal(ticket_count, 1)
  693. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  694. assert_equal(ticket_count, 0)
  695. UserInfo.current_user_id = @agent1.id
  696. condition = {
  697. 'ticket.group_id' => {
  698. operator: 'is',
  699. value: @group.id,
  700. },
  701. 'ticket.owner_id' => {
  702. operator: 'is',
  703. pre_condition: 'current_user.id',
  704. },
  705. }
  706. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  707. assert_equal(ticket_count, 1)
  708. ticket_count, tickets = Ticket.selectors(condition, 10)
  709. assert_equal(ticket_count, 1)
  710. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  711. assert_equal(ticket_count, 0)
  712. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  713. assert_equal(ticket_count, 0)
  714. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  715. assert_equal(ticket_count, 0)
  716. UserInfo.current_user_id = @agent2.id
  717. condition = {
  718. 'ticket.group_id' => {
  719. operator: 'is',
  720. value: @group.id,
  721. },
  722. 'ticket.owner_id' => {
  723. operator: 'is',
  724. pre_condition: 'current_user.id',
  725. },
  726. }
  727. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  728. assert_equal(ticket_count, 1)
  729. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  730. assert_equal(ticket_count, 0)
  731. ticket_count, tickets = Ticket.selectors(condition, 10)
  732. assert_equal(ticket_count, 0)
  733. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  734. assert_equal(ticket_count, 0)
  735. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  736. assert_equal(ticket_count, 0)
  737. UserInfo.current_user_id = @customer1.id
  738. condition = {
  739. 'ticket.group_id' => {
  740. operator: 'is',
  741. value: @group.id,
  742. },
  743. 'ticket.customer_id' => {
  744. operator: 'is',
  745. pre_condition: 'current_user.id',
  746. },
  747. }
  748. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  749. assert_equal(ticket_count, 0)
  750. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  751. assert_equal(ticket_count, 0)
  752. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  753. assert_equal(ticket_count, 1)
  754. ticket_count, tickets = Ticket.selectors(condition, 10)
  755. assert_equal(ticket_count, 1)
  756. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  757. assert_equal(ticket_count, 2)
  758. UserInfo.current_user_id = @customer2.id
  759. condition = {
  760. 'ticket.group_id' => {
  761. operator: 'is',
  762. value: @group.id,
  763. },
  764. 'ticket.customer_id' => {
  765. operator: 'is',
  766. pre_condition: 'current_user.id',
  767. },
  768. }
  769. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  770. assert_equal(ticket_count, 0)
  771. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  772. assert_equal(ticket_count, 0)
  773. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  774. assert_equal(ticket_count, 1)
  775. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  776. assert_equal(ticket_count, 2)
  777. ticket_count, tickets = Ticket.selectors(condition, 10)
  778. assert_equal(ticket_count, 2)
  779. UserInfo.current_user_id = @customer1.id
  780. condition = {
  781. 'ticket.group_id' => {
  782. operator: 'is',
  783. value: @group.id,
  784. },
  785. 'ticket.organization_id' => {
  786. operator: 'is',
  787. pre_condition: 'current_user.organization_id',
  788. },
  789. }
  790. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  791. assert_equal(ticket_count, 0)
  792. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  793. assert_equal(ticket_count, 0)
  794. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  795. assert_equal(ticket_count, 1)
  796. ticket_count, tickets = Ticket.selectors(condition, 10)
  797. assert_equal(ticket_count, 1)
  798. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  799. assert_equal(ticket_count, 0)
  800. UserInfo.current_user_id = @customer2.id
  801. condition = {
  802. 'ticket.group_id' => {
  803. operator: 'is',
  804. value: @group.id,
  805. },
  806. 'ticket.organization_id' => {
  807. operator: 'is',
  808. pre_condition: 'current_user.organization_id',
  809. },
  810. }
  811. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  812. assert_equal(ticket_count, 0)
  813. ticket_count, tickets = Ticket.selectors(condition, 10, @agent2)
  814. assert_equal(ticket_count, 0)
  815. ticket_count, tickets = Ticket.selectors(condition, 10, @customer1)
  816. assert_equal(ticket_count, 1)
  817. ticket_count, tickets = Ticket.selectors(condition, 10, @customer2)
  818. assert_equal(ticket_count, 0)
  819. ticket_count, tickets = Ticket.selectors(condition, 10)
  820. assert_equal(ticket_count, 0)
  821. travel_back
  822. end
  823. test 'ticket tags filter' do
  824. ticket_tags_1 = Ticket.create!(
  825. title: 'some title1',
  826. group: @group,
  827. customer_id: @customer1.id,
  828. owner_id: @agent1.id,
  829. state: Ticket::State.lookup(name: 'new'),
  830. priority: Ticket::Priority.lookup(name: '2 normal'),
  831. created_at: '2015-02-05 16:37:00',
  832. updated_by_id: 1,
  833. created_by_id: 1,
  834. )
  835. ticket_tags_2 = Ticket.create!(
  836. title: 'some title1',
  837. group: @group,
  838. customer_id: @customer1.id,
  839. owner_id: @agent1.id,
  840. state: Ticket::State.lookup(name: 'new'),
  841. priority: Ticket::Priority.lookup(name: '2 normal'),
  842. created_at: '2015-02-05 16:37:00',
  843. updated_by_id: 1,
  844. created_by_id: 1,
  845. )
  846. ticket_tags_3 = Ticket.create!(
  847. title: 'some title1',
  848. group: @group,
  849. customer_id: @customer1.id,
  850. owner_id: @agent1.id,
  851. state: Ticket::State.lookup(name: 'new'),
  852. priority: Ticket::Priority.lookup(name: '2 normal'),
  853. created_at: '2015-02-05 16:37:00',
  854. updated_by_id: 1,
  855. created_by_id: 1,
  856. )
  857. Tag.tag_add(
  858. object: 'Ticket',
  859. o_id: ticket_tags_1.id,
  860. item: 'contains_all_1',
  861. created_by_id: 1,
  862. )
  863. Tag.tag_add(
  864. object: 'Ticket',
  865. o_id: ticket_tags_1.id,
  866. item: 'contains_all_2',
  867. created_by_id: 1,
  868. )
  869. Tag.tag_add(
  870. object: 'Ticket',
  871. o_id: ticket_tags_1.id,
  872. item: 'contains_all_3',
  873. created_by_id: 1,
  874. )
  875. Tag.tag_add(
  876. object: 'Ticket',
  877. o_id: ticket_tags_2.id,
  878. item: 'contains_all_3',
  879. created_by_id: 1,
  880. )
  881. # search all with contains all
  882. condition = {
  883. 'ticket.tags' => {
  884. operator: 'contains all',
  885. value: 'contains_all_1, contains_all_2, contains_all_3',
  886. },
  887. }
  888. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  889. assert_equal(1, ticket_count)
  890. condition = {
  891. 'ticket.tags' => {
  892. operator: 'contains all',
  893. value: 'contains_all_1, contains_all_2, contains_all_3, xxx',
  894. },
  895. }
  896. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  897. assert_equal(0, ticket_count)
  898. # search all with contains one
  899. condition = {
  900. 'ticket.tags' => {
  901. operator: 'contains one',
  902. value: 'contains_all_1, contains_all_2, contains_all_3',
  903. },
  904. }
  905. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  906. assert_equal(2, ticket_count)
  907. condition = {
  908. 'ticket.tags' => {
  909. operator: 'contains one',
  910. value: 'contains_all_1, contains_all_2'
  911. },
  912. }
  913. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  914. assert_equal(1, ticket_count)
  915. # search all with contains one not
  916. condition = {
  917. 'ticket.tags' => {
  918. operator: 'contains one',
  919. value: 'contains_all_1, contains_all_3'
  920. },
  921. }
  922. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  923. assert_equal(2, ticket_count)
  924. condition = {
  925. 'ticket.tags' => {
  926. operator: 'contains one',
  927. value: 'contains_all_1, contains_all_2, contains_all_3'
  928. },
  929. }
  930. ticket_count, tickets = Ticket.selectors(condition, 10, @agent1)
  931. assert_equal(2, ticket_count)
  932. end
  933. end