ticket_selector_test.rb 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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_equal(ticket2.organization_id, nil)
  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_equal(ticket3.organization_id, nil)
  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_equal(ticket_count, nil)
  156. ticket_count, tickets = Ticket.selectors(condition, 10, agent1)
  157. assert_equal(ticket_count, nil)
  158. ticket_count, tickets = Ticket.selectors(condition, 10, agent2)
  159. assert_equal(ticket_count, nil)
  160. ticket_count, tickets = Ticket.selectors(condition, 10, customer1)
  161. assert_equal(ticket_count, nil)
  162. ticket_count, tickets = Ticket.selectors(condition, 10, customer2)
  163. assert_equal(ticket_count, nil)
  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_equal(ticket_count, nil)
  177. ticket_count, tickets = Ticket.selectors(condition, 10, agent1)
  178. assert_equal(ticket_count, nil)
  179. ticket_count, tickets = Ticket.selectors(condition, 10, agent2)
  180. assert_equal(ticket_count, nil)
  181. ticket_count, tickets = Ticket.selectors(condition, 10, customer1)
  182. assert_equal(ticket_count, nil)
  183. ticket_count, tickets = Ticket.selectors(condition, 10, customer2)
  184. assert_equal(ticket_count, nil)
  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_equal(ticket_count, nil)
  198. ticket_count, tickets = Ticket.selectors(condition, 10, agent1)
  199. assert_equal(ticket_count, nil)
  200. ticket_count, tickets = Ticket.selectors(condition, 10, agent2)
  201. assert_equal(ticket_count, nil)
  202. ticket_count, tickets = Ticket.selectors(condition, 10, customer1)
  203. assert_equal(ticket_count, nil)
  204. ticket_count, tickets = Ticket.selectors(condition, 10, customer2)
  205. assert_equal(ticket_count, nil)
  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_equal(ticket_count, nil)
  661. ticket_count, tickets = Ticket.selectors(condition, 10, agent2)
  662. assert_equal(ticket_count, nil)
  663. ticket_count, tickets = Ticket.selectors(condition, 10, customer1)
  664. assert_equal(ticket_count, nil)
  665. ticket_count, tickets = Ticket.selectors(condition, 10, customer2)
  666. assert_equal(ticket_count, nil)
  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. end