karma_test.rb 18 KB

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