ticket_selector_test.rb 32 KB

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