karma_test.rb 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class KarmaTest < ActiveSupport::TestCase
  4. test 'basic' do
  5. groups = Group.all
  6. roles = Role.where(name: 'Agent')
  7. agent1 = User.create_or_update(
  8. login: 'karma-agent1@example.com',
  9. firstname: 'Karma',
  10. lastname: 'Agent1',
  11. email: 'karma-agent1@example.com',
  12. password: 'agentpw',
  13. active: true,
  14. roles: roles,
  15. groups: groups,
  16. updated_by_id: 1,
  17. created_by_id: 1,
  18. )
  19. agent2 = User.create_or_update(
  20. login: 'karma-agent2@example.com',
  21. firstname: 'Karma',
  22. lastname: 'Agent2',
  23. email: 'karma-agent2@example.com',
  24. password: 'agentpw',
  25. active: true,
  26. roles: roles,
  27. groups: groups,
  28. updated_by_id: 1,
  29. created_by_id: 1,
  30. )
  31. customer1 = User.create_or_update(
  32. login: 'karma-customer1@example.com',
  33. firstname: 'Karma',
  34. lastname: 'Customer1',
  35. email: 'karma-customer1@example.com',
  36. password: 'customerpw',
  37. active: true,
  38. roles: roles,
  39. groups: groups,
  40. updated_by_id: 1,
  41. created_by_id: 1,
  42. )
  43. ticket1 = Ticket.create(
  44. title: 'karma test 1',
  45. group: Group.lookup(name: 'Users'),
  46. customer: customer1,
  47. state: Ticket::State.lookup(name: 'new'),
  48. priority: Ticket::Priority.lookup(name: '2 normal'),
  49. updated_by_id: agent1.id,
  50. created_by_id: agent1.id,
  51. updated_at: Time.zone.now - 10.hours,
  52. created_at: Time.zone.now - 10.hours,
  53. )
  54. Ticket::Article.create(
  55. ticket_id: ticket1.id,
  56. from: 'some_sender@example.com',
  57. to: 'some_recipient@example.com',
  58. subject: 'some subject',
  59. message_id: 'some@id',
  60. body: 'agent phone 1 / init',
  61. internal: false,
  62. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  63. type: Ticket::Article::Type.lookup(name: 'phone'),
  64. updated_by_id: agent1.id,
  65. created_by_id: agent1.id,
  66. updated_at: Time.zone.now - 10.hours,
  67. created_at: Time.zone.now - 10.hours,
  68. )
  69. assert(ticket1)
  70. # execute object transaction
  71. Observer::Transaction.commit
  72. Scheduler.worker(true)
  73. assert_equal(10, Karma.score_by_user(agent1))
  74. assert_equal(0, Karma.score_by_user(agent2))
  75. assert_equal(0, Karma.score_by_user(customer1))
  76. karma_user = Karma::User.by_user(agent1)
  77. assert_equal('Beginner', karma_user.level)
  78. assert_equal(10, karma_user.score)
  79. karma_user = Karma::User.by_user(agent2)
  80. assert_equal('Beginner', karma_user.level)
  81. assert_equal(0, karma_user.score)
  82. ticket1.state = Ticket::State.lookup(name: 'pending reminder')
  83. ticket1.updated_by_id = agent1.id
  84. ticket1.updated_at = Time.zone.now - 9.hours
  85. ticket1.created_at = Time.zone.now - 9.hours
  86. ticket1.save
  87. # execute object transaction
  88. Observer::Transaction.commit
  89. Scheduler.worker(true)
  90. assert_equal(10 + 2, Karma.score_by_user(agent1))
  91. assert_equal(0, Karma.score_by_user(agent2))
  92. assert_equal(0, Karma.score_by_user(customer1))
  93. karma_user = Karma::User.by_user(agent1)
  94. assert_equal('Beginner', karma_user.level)
  95. assert_equal(12, karma_user.score)
  96. karma_user = Karma::User.by_user(agent2)
  97. assert_equal('Beginner', karma_user.level)
  98. assert_equal(0, karma_user.score)
  99. ticket1.state = Ticket::State.lookup(name: 'pending close')
  100. ticket1.updated_by_id = agent1.id
  101. ticket1.updated_at = Time.zone.now - 9.hours
  102. ticket1.created_at = Time.zone.now - 9.hours
  103. ticket1.save
  104. # execute object transaction
  105. Observer::Transaction.commit
  106. Scheduler.worker(true)
  107. assert_equal(10 + 2, Karma.score_by_user(agent1))
  108. assert_equal(0, Karma.score_by_user(agent2))
  109. assert_equal(0, Karma.score_by_user(customer1))
  110. ticket1.state = Ticket::State.lookup(name: 'closed')
  111. ticket1.updated_by_id = agent2.id
  112. ticket1.updated_at = Time.zone.now - 9.hours
  113. ticket1.created_at = Time.zone.now - 9.hours
  114. ticket1.save
  115. # execute object transaction
  116. Observer::Transaction.commit
  117. Scheduler.worker(true)
  118. assert_equal(10 + 2, Karma.score_by_user(agent1))
  119. assert_equal(0, Karma.score_by_user(agent2))
  120. assert_equal(0, Karma.score_by_user(customer1))
  121. ticket1.state = Ticket::State.lookup(name: 'open')
  122. ticket1.save
  123. Ticket::Article.create(
  124. ticket_id: ticket1.id,
  125. from: 'some_sender@example.com',
  126. to: 'some_recipient@example.com',
  127. subject: 'reply: some subject',
  128. message_id: 'some@id',
  129. body: 'agent phone 2',
  130. internal: false,
  131. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  132. type: Ticket::Article::Type.lookup(name: 'phone'),
  133. updated_by_id: agent2.id,
  134. created_by_id: agent2.id,
  135. updated_at: Time.zone.now - (9.hours + 15.minutes),
  136. created_at: Time.zone.now - (9.hours + 15.minutes),
  137. )
  138. # execute object transaction
  139. Observer::Transaction.commit
  140. Scheduler.worker(true)
  141. assert_equal(10 + 2, Karma.score_by_user(agent1))
  142. assert_equal(0, Karma.score_by_user(agent2))
  143. assert_equal(0, Karma.score_by_user(customer1))
  144. # execute object transaction
  145. Observer::Transaction.commit
  146. Scheduler.worker(true)
  147. ticket1.state = Ticket::State.lookup(name: 'closed')
  148. ticket1.save
  149. # execute object transaction
  150. Observer::Transaction.commit
  151. Scheduler.worker(true)
  152. assert_equal(10 + 2, Karma.score_by_user(agent1))
  153. assert_equal(5, Karma.score_by_user(agent2))
  154. assert_equal(0, Karma.score_by_user(customer1))
  155. Ticket::Article.create(
  156. ticket_id: ticket1.id,
  157. from: 'some_sender@example.com',
  158. to: 'some_recipient@example.com',
  159. subject: 'some subject',
  160. message_id: 'some@id',
  161. body: 'customer phone',
  162. internal: false,
  163. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  164. type: Ticket::Article::Type.lookup(name: 'phone'),
  165. updated_by_id: customer1.id,
  166. created_by_id: customer1.id,
  167. updated_at: Time.zone.now - 8.hours,
  168. created_at: Time.zone.now - 8.hours,
  169. )
  170. # execute object transaction
  171. Observer::Transaction.commit
  172. Scheduler.worker(true)
  173. assert_equal(10 + 2, Karma.score_by_user(agent1))
  174. assert_equal(5, Karma.score_by_user(agent2))
  175. assert_equal(0, Karma.score_by_user(customer1))
  176. Ticket::Article.create(
  177. ticket_id: ticket1.id,
  178. from: 'some_sender@example.com',
  179. to: 'some_recipient@example.com',
  180. subject: 'some subject',
  181. message_id: 'some@id',
  182. body: 'agent phone 3',
  183. internal: false,
  184. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  185. type: Ticket::Article::Type.lookup(name: 'phone'),
  186. updated_by_id: agent1.id,
  187. created_by_id: agent1.id,
  188. updated_at: Time.zone.now - (7.hours + 30.minutes),
  189. created_at: Time.zone.now - (7.hours + 30.minutes),
  190. )
  191. # execute object transaction
  192. Observer::Transaction.commit
  193. Scheduler.worker(true)
  194. assert_equal(10 + 2 + 25, Karma.score_by_user(agent1))
  195. assert_equal(5, Karma.score_by_user(agent2))
  196. assert_equal(0, Karma.score_by_user(customer1))
  197. Ticket::Article.create(
  198. ticket_id: ticket1.id,
  199. from: 'some_sender@example.com',
  200. to: 'some_recipient@example.com',
  201. subject: 'some subject',
  202. message_id: 'some@id',
  203. body: 'some message',
  204. internal: false,
  205. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  206. type: Ticket::Article::Type.lookup(name: 'phone'),
  207. updated_by_id: agent1.id,
  208. created_by_id: agent1.id,
  209. updated_at: Time.zone.now - (7.hours + 15.minutes),
  210. created_at: Time.zone.now - (7.hours + 15.minutes),
  211. )
  212. # execute object transaction
  213. Observer::Transaction.commit
  214. Scheduler.worker(true)
  215. assert_equal(10 + 2 + 25, Karma.score_by_user(agent1))
  216. assert_equal(5, Karma.score_by_user(agent2))
  217. assert_equal(0, Karma.score_by_user(customer1))
  218. Ticket::Article.create(
  219. ticket_id: ticket1.id,
  220. from: 'some_sender@example.com',
  221. to: 'some_recipient@example.com',
  222. subject: 'some subject',
  223. message_id: 'some@id',
  224. body: 'some message',
  225. internal: false,
  226. sender: Ticket::Article::Sender.lookup(name: 'Customer'),
  227. type: Ticket::Article::Type.lookup(name: 'phone'),
  228. updated_by_id: customer1.id,
  229. created_by_id: customer1.id,
  230. updated_at: Time.zone.now - 7.hours,
  231. created_at: Time.zone.now - 7.hours,
  232. )
  233. # execute object transaction
  234. Observer::Transaction.commit
  235. Scheduler.worker(true)
  236. assert_equal(10 + 2 + 25, Karma.score_by_user(agent1))
  237. assert_equal(5, Karma.score_by_user(agent2))
  238. assert_equal(0, Karma.score_by_user(customer1))
  239. Ticket::Article.create(
  240. ticket_id: ticket1.id,
  241. from: 'some_sender@example.com',
  242. to: 'some_recipient@example.com',
  243. subject: 'some subject',
  244. message_id: 'some@id',
  245. body: 'some message',
  246. internal: false,
  247. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  248. type: Ticket::Article::Type.lookup(name: 'phone'),
  249. updated_by_id: agent2.id,
  250. created_by_id: agent2.id,
  251. updated_at: Time.zone.now - (2.hours + 30.minutes),
  252. created_at: Time.zone.now - (2.hours + 30.minutes),
  253. )
  254. # execute object transaction
  255. Observer::Transaction.commit
  256. Scheduler.worker(true)
  257. assert_equal(10 + 2 + 25, Karma.score_by_user(agent1))
  258. assert_equal(5 + 10, Karma.score_by_user(agent2))
  259. assert_equal(0, Karma.score_by_user(customer1))
  260. Ticket::Article.create(
  261. ticket_id: ticket1.id,
  262. from: 'some_sender@example.com',
  263. to: 'some_recipient@example.com',
  264. subject: 'some subject',
  265. message_id: 'some@id',
  266. body: 'some message',
  267. internal: false,
  268. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  269. type: Ticket::Article::Type.lookup(name: 'phone'),
  270. updated_by_id: agent1.id,
  271. created_by_id: agent1.id,
  272. updated_at: Time.zone.now - (2.hours + 45.minutes),
  273. created_at: Time.zone.now - (2.hours + 45.minutes),
  274. )
  275. # execute object transaction
  276. Observer::Transaction.commit
  277. Scheduler.worker(true)
  278. assert_equal(10 + 2 + 25, Karma.score_by_user(agent1))
  279. assert_equal(5 + 10, Karma.score_by_user(agent2))
  280. assert_equal(0, Karma.score_by_user(customer1))
  281. Tag.tag_add(
  282. object: 'Ticket',
  283. o_id: ticket1.id,
  284. item: 'Tag1',
  285. created_by_id: agent1.id,
  286. )
  287. #travel 5.seconds
  288. Tag.tag_add(
  289. object: 'Ticket',
  290. o_id: ticket1.id,
  291. item: 'Tag2',
  292. created_by_id: agent1.id,
  293. )
  294. # execute object transaction
  295. Observer::Transaction.commit
  296. Scheduler.worker(true)
  297. assert_equal(10 + 2 + 25 + 4, Karma.score_by_user(agent1))
  298. assert_equal(5 + 10, Karma.score_by_user(agent2))
  299. assert_equal(0, Karma.score_by_user(customer1))
  300. Tag.tag_add(
  301. object: 'Ticket',
  302. o_id: ticket1.id,
  303. item: 'Tag3',
  304. created_by_id: agent1.id,
  305. )
  306. Tag.tag_add(
  307. object: 'Ticket',
  308. o_id: ticket1.id,
  309. item: 'Tag4',
  310. created_by_id: agent2.id,
  311. )
  312. # execute object transaction
  313. Observer::Transaction.commit
  314. Scheduler.worker(true)
  315. assert_equal(10 + 2 + 25 + 4, Karma.score_by_user(agent1))
  316. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  317. assert_equal(0, Karma.score_by_user(customer1))
  318. ticket2 = Ticket.create(
  319. title: 'karma test 1',
  320. group: Group.lookup(name: 'Users'),
  321. customer: customer1,
  322. state: Ticket::State.lookup(name: 'new'),
  323. owner_id: agent1.id,
  324. priority: Ticket::Priority.lookup(name: '2 normal'),
  325. updated_by_id: agent1.id,
  326. created_by_id: agent1.id,
  327. updated_at: Time.zone.now - 10.hours,
  328. created_at: Time.zone.now - 10.hours,
  329. )
  330. Ticket::Article.create(
  331. ticket_id: ticket2.id,
  332. from: 'some_sender@example.com',
  333. to: 'some_recipient@example.com',
  334. subject: 'some subject',
  335. message_id: 'some@id',
  336. body: 'some message',
  337. internal: false,
  338. sender: Ticket::Article::Sender.lookup(name: 'Agent'),
  339. type: Ticket::Article::Type.lookup(name: 'phone'),
  340. updated_by_id: agent1.id,
  341. created_by_id: agent1.id,
  342. updated_at: Time.zone.now - 2.hours,
  343. created_at: Time.zone.now - 2.hours,
  344. )
  345. assert(ticket2)
  346. # execute object transaction
  347. Observer::Transaction.commit
  348. Scheduler.worker(true)
  349. assert_equal(10 + 2 + 25 + 4 + 10, Karma.score_by_user(agent1))
  350. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  351. assert_equal(0, Karma.score_by_user(customer1))
  352. ticket2.state = Ticket::State.lookup(name: 'pending reminder')
  353. ticket2.pending_time = Time.zone.now - 1.day
  354. ticket2.save
  355. Ticket.process_pending
  356. assert_equal(10 + 2 + 25 + 4 + 10, Karma.score_by_user(agent1))
  357. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  358. assert_equal(0, Karma.score_by_user(customer1))
  359. ticket2.state = Ticket::State.lookup(name: 'pending reminder')
  360. ticket2.pending_time = Time.zone.now - 3.days
  361. ticket2.save
  362. Ticket.process_pending
  363. assert_equal(10 + 2 + 25 + 4 + 10 - 5, Karma.score_by_user(agent1))
  364. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  365. assert_equal(0, Karma.score_by_user(customer1))
  366. Ticket.process_pending
  367. assert_equal(10 + 2 + 25 + 4 + 10 - 5, Karma.score_by_user(agent1))
  368. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  369. assert_equal(0, Karma.score_by_user(customer1))
  370. calendar1 = Calendar.create_or_update(
  371. name: 'EU 1 - karma test',
  372. timezone: 'Europe/Berlin',
  373. business_hours: {
  374. mon: {
  375. active: true,
  376. timeframes: [ ['00:00', '23:59'] ]
  377. },
  378. tue: {
  379. active: true,
  380. timeframes: [ ['00:00', '23:59'] ]
  381. },
  382. wed: {
  383. active: true,
  384. timeframes: [ ['00:00', '23:59'] ]
  385. },
  386. thu: {
  387. active: true,
  388. timeframes: [ ['00:00', '23:59'] ]
  389. },
  390. fri: {
  391. active: true,
  392. timeframes: [ ['00:00', '23:59'] ]
  393. },
  394. sat: {
  395. active: true,
  396. timeframes: [ ['00:00', '23:59'] ]
  397. },
  398. sun: {
  399. active: true,
  400. timeframes: [ ['00:00', '23:59'] ]
  401. },
  402. },
  403. default: true,
  404. ical_url: nil,
  405. updated_by_id: 1,
  406. created_by_id: 1,
  407. )
  408. sla1 = Sla.create_or_update(
  409. name: 'test sla 1',
  410. condition: {},
  411. first_response_time: 20,
  412. update_time: 60,
  413. solution_time: 120,
  414. calendar_id: calendar1.id,
  415. updated_by_id: 1,
  416. created_by_id: 1,
  417. )
  418. ticket2.state = Ticket::State.lookup(name: 'open')
  419. ticket2.save
  420. Observer::Transaction.commit
  421. Scheduler.worker(true)
  422. #Scheduler.worker(true)
  423. #Ticket::Escalation.rebuild_all
  424. Ticket.process_escalation
  425. assert_equal(10 + 2 + 25 + 4 + 10 - 5 - 5, Karma.score_by_user(agent1))
  426. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  427. assert_equal(0, Karma.score_by_user(customer1))
  428. Ticket.process_escalation
  429. assert_equal(10 + 2 + 25 + 4 + 10 - 5 - 5, Karma.score_by_user(agent1))
  430. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  431. assert_equal(0, Karma.score_by_user(customer1))
  432. # check min score
  433. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  434. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  435. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  436. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  437. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  438. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  439. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  440. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  441. Karma::ActivityLog.add('ticket escalated', ticket2.owner, 'Ticket', ticket2.id, true)
  442. assert_equal(0, Karma.score_by_user(agent1), 'block - score, min is 0')
  443. assert_equal(5 + 10 + 4, Karma.score_by_user(agent2))
  444. assert_equal(0, Karma.score_by_user(customer1))
  445. # test score/level
  446. assert_equal('Beginner', Karma::User.level_by_score(0))
  447. assert_equal('Beginner', Karma::User.level_by_score(400))
  448. assert_equal('Beginner', Karma::User.level_by_score(499))
  449. assert_equal('Newbie', Karma::User.level_by_score(500))
  450. assert_equal('Newbie', Karma::User.level_by_score(1999))
  451. assert_equal('Intermediate', Karma::User.level_by_score(2000))
  452. assert_equal('Intermediate', Karma::User.level_by_score(4999))
  453. assert_equal('Professional', Karma::User.level_by_score(5000))
  454. assert_equal('Professional', Karma::User.level_by_score(6999))
  455. assert_equal('Expert', Karma::User.level_by_score(7000))
  456. assert_equal('Expert', Karma::User.level_by_score(8999))
  457. assert_equal('Master', Karma::User.level_by_score(9000))
  458. assert_equal('Master', Karma::User.level_by_score(18_999))
  459. assert_equal('Evangelist', Karma::User.level_by_score(19_000))
  460. assert_equal('Evangelist', Karma::User.level_by_score(45_999))
  461. assert_equal('Hero', Karma::User.level_by_score(50_000))
  462. # cleanup
  463. ticket1.destroy
  464. ticket2.destroy
  465. calendar1.destroy
  466. sla1.destroy
  467. end
  468. end