ticket_selector_test.rb 38 KB

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