chat_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. require 'test_helper'
  2. class ChatTest < ActiveSupport::TestCase
  3. setup do
  4. groups = Group.all
  5. roles = Role.where(name: %w[Agent])
  6. @agent1 = User.create_or_update(
  7. login: 'ticket-chat-agent1@example.com',
  8. firstname: 'Notification',
  9. lastname: 'Agent1',
  10. email: 'ticket-chat-agent1@example.com',
  11. password: 'agentpw',
  12. active: true,
  13. roles: roles,
  14. groups: groups,
  15. updated_at: '2015-02-05 16:37:00',
  16. updated_by_id: 1,
  17. created_by_id: 1,
  18. )
  19. @agent2 = User.create_or_update(
  20. login: 'ticket-chat-agent2@example.com',
  21. firstname: 'Notification',
  22. lastname: 'Agent2',
  23. email: 'ticket-chat-agent2@example.com',
  24. password: 'agentpw',
  25. active: true,
  26. roles: roles,
  27. groups: groups,
  28. updated_at: '2015-02-05 16:38:00',
  29. updated_by_id: 1,
  30. created_by_id: 1,
  31. )
  32. Chat.delete_all
  33. Chat::Session.delete_all
  34. Chat::Message.delete_all
  35. Chat::Agent.delete_all
  36. Setting.set('chat', false)
  37. end
  38. test 'default test' do
  39. chat = Chat.create_or_update(
  40. name: 'default',
  41. max_queue: 5,
  42. note: '',
  43. active: true,
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. # check if feature is disabled
  48. assert_equal('chat_disabled', chat.customer_state[:state])
  49. assert_equal('chat_disabled', Chat.agent_state(@agent1.id)[:state])
  50. Setting.set('chat', true)
  51. # check customer state
  52. assert_equal('offline', chat.customer_state[:state])
  53. # check agent state
  54. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  55. assert_equal(0, agent_state[:waiting_chat_count])
  56. assert_equal(0, agent_state[:running_chat_count])
  57. assert_equal([], agent_state[:active_sessions])
  58. assert_equal(0, agent_state[:seads_available])
  59. assert_equal(0, agent_state[:seads_total])
  60. assert_equal(false, agent_state[:active])
  61. # set agent 1 to active
  62. chat_agent1 = Chat::Agent.create_or_update(
  63. active: true,
  64. concurrent: 4,
  65. updated_by_id: @agent1.id,
  66. created_by_id: @agent1.id,
  67. )
  68. # check customer state
  69. assert_equal('online', chat.customer_state[:state])
  70. # check agent state
  71. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  72. assert_equal(0, agent_state[:waiting_chat_count])
  73. assert_equal(0, agent_state[:running_chat_count])
  74. assert_equal([], agent_state[:active_sessions])
  75. assert_equal(4, agent_state[:seads_available])
  76. assert_equal(4, agent_state[:seads_total])
  77. assert_equal(true, agent_state[:active])
  78. # start session
  79. chat_session1 = Chat::Session.create(
  80. chat_id: chat.id,
  81. user_id: @agent1.id,
  82. )
  83. assert(chat_session1.session_id)
  84. # check customer state
  85. assert_equal('online', chat.customer_state[:state])
  86. # check agent state
  87. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  88. assert_equal(1, agent_state[:waiting_chat_count])
  89. assert_equal(0, agent_state[:running_chat_count])
  90. assert_equal([], agent_state[:active_sessions])
  91. assert_equal(3, agent_state[:seads_available])
  92. assert_equal(4, agent_state[:seads_total])
  93. assert_equal(true, agent_state[:active])
  94. # activate second agent
  95. chat_agent2 = Chat::Agent.create_or_update(
  96. active: true,
  97. concurrent: 2,
  98. updated_by_id: @agent2.id,
  99. created_by_id: @agent2.id,
  100. )
  101. # check customer state
  102. assert_equal('online', chat.customer_state[:state])
  103. # check agent1 state
  104. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  105. assert_equal(1, agent_state[:waiting_chat_count])
  106. assert_equal(0, agent_state[:running_chat_count])
  107. assert_equal([], agent_state[:active_sessions])
  108. assert_equal(5, agent_state[:seads_available])
  109. assert_equal(6, agent_state[:seads_total])
  110. assert_equal(true, agent_state[:active])
  111. # check agent2 state
  112. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  113. assert_equal(1, agent_state[:waiting_chat_count])
  114. assert_equal(0, agent_state[:running_chat_count])
  115. assert_equal([], agent_state[:active_sessions])
  116. assert_equal(5, agent_state[:seads_available])
  117. assert_equal(6, agent_state[:seads_total])
  118. assert_equal(true, agent_state[:active])
  119. # start next chat
  120. chat_session2 = Chat::Session.create(
  121. chat_id: chat.id,
  122. )
  123. # check customer state
  124. assert_equal('online', chat.customer_state[:state])
  125. # check agent1 state
  126. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  127. assert_equal(2, agent_state[:waiting_chat_count])
  128. assert_equal(0, agent_state[:running_chat_count])
  129. assert_equal([], agent_state[:active_sessions])
  130. assert_equal(4, agent_state[:seads_available])
  131. assert_equal(6, agent_state[:seads_total])
  132. assert_equal(true, agent_state[:active])
  133. # check agent2 state
  134. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  135. assert_equal(2, agent_state[:waiting_chat_count])
  136. assert_equal(0, agent_state[:running_chat_count])
  137. assert_equal([], agent_state[:active_sessions])
  138. assert_equal(4, agent_state[:seads_available])
  139. assert_equal(6, agent_state[:seads_total])
  140. assert_equal(true, agent_state[:active])
  141. # start new chats
  142. chat_session3 = Chat::Session.create(
  143. chat_id: chat.id,
  144. )
  145. chat_session4 = Chat::Session.create(
  146. chat_id: chat.id,
  147. )
  148. chat_session5 = Chat::Session.create(
  149. chat_id: chat.id,
  150. )
  151. chat_session6 = Chat::Session.create(
  152. chat_id: chat.id,
  153. )
  154. # check customer state
  155. assert_equal('no_seats_available', chat.customer_state[:state])
  156. # check agent1 state
  157. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  158. assert_equal(6, agent_state[:waiting_chat_count])
  159. assert_equal(0, agent_state[:running_chat_count])
  160. assert_equal([], agent_state[:active_sessions])
  161. assert_equal(0, agent_state[:seads_available])
  162. assert_equal(6, agent_state[:seads_total])
  163. assert_equal(true, agent_state[:active])
  164. # check agent2 state
  165. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  166. assert_equal(6, agent_state[:waiting_chat_count])
  167. assert_equal(0, agent_state[:running_chat_count])
  168. assert_equal([], agent_state[:active_sessions])
  169. assert_equal(0, agent_state[:seads_available])
  170. assert_equal(6, agent_state[:seads_total])
  171. assert_equal(true, agent_state[:active])
  172. chat_session6.user_id = @agent1.id
  173. chat_session6.state = 'running'
  174. chat_session6.save
  175. Chat::Message.create(
  176. chat_session_id: chat_session6.id,
  177. content: 'message 1',
  178. created_by_id: @agent1.id,
  179. )
  180. travel 1.second
  181. Chat::Message.create(
  182. chat_session_id: chat_session6.id,
  183. content: 'message 2',
  184. created_by_id: @agent1.id,
  185. )
  186. travel 1.second
  187. Chat::Message.create(
  188. chat_session_id: chat_session6.id,
  189. content: 'message 3',
  190. created_by_id: @agent1.id,
  191. )
  192. travel 1.second
  193. Chat::Message.create(
  194. chat_session_id: chat_session6.id,
  195. content: 'message 4',
  196. created_by_id: nil,
  197. )
  198. # check customer state
  199. customer_state = chat.customer_state
  200. assert_equal('no_seats_available', customer_state[:state])
  201. assert_equal(5, customer_state[:queue])
  202. # customer chat state
  203. customer_state = chat.customer_state(chat_session6.session_id)
  204. assert_equal('reconnect', customer_state[:state])
  205. assert(customer_state[:session])
  206. assert_equal(Array, customer_state[:session].class)
  207. assert_equal('message 1', customer_state[:session][0]['content'])
  208. assert_equal('message 2', customer_state[:session][1]['content'])
  209. assert_equal('message 3', customer_state[:session][2]['content'])
  210. assert_equal('message 4', customer_state[:session][3]['content'])
  211. assert_equal('Notification Agent1', customer_state[:agent][:name])
  212. assert_nil(customer_state[:agent][:avatar])
  213. # check agent1 state
  214. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  215. assert_equal(5, agent_state[:waiting_chat_count])
  216. assert_equal(1, agent_state[:running_chat_count])
  217. assert_equal(Array, agent_state[:active_sessions].class)
  218. assert_equal(chat.id, agent_state[:active_sessions][0]['chat_id'])
  219. assert_equal(@agent1.id, agent_state[:active_sessions][0]['user_id'])
  220. assert(agent_state[:active_sessions][0]['messages'])
  221. assert_equal(Array, agent_state[:active_sessions][0]['messages'].class)
  222. assert_equal('message 1', agent_state[:active_sessions][0]['messages'][0]['content'])
  223. assert_equal('message 2', agent_state[:active_sessions][0]['messages'][1]['content'])
  224. assert_equal('message 3', agent_state[:active_sessions][0]['messages'][2]['content'])
  225. assert_equal('message 4', agent_state[:active_sessions][0]['messages'][3]['content'])
  226. assert_equal(0, agent_state[:seads_available])
  227. assert_equal(6, agent_state[:seads_total])
  228. assert_equal(true, agent_state[:active])
  229. # check agent2 state
  230. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  231. assert_equal(5, agent_state[:waiting_chat_count])
  232. assert_equal(1, agent_state[:running_chat_count])
  233. assert_equal([], agent_state[:active_sessions])
  234. assert_equal(0, agent_state[:seads_available])
  235. assert_equal(6, agent_state[:seads_total])
  236. assert_equal(true, agent_state[:active])
  237. chat_agent2.active = false
  238. chat_agent2.save
  239. # check customer state
  240. assert_equal('no_seats_available', chat.customer_state[:state])
  241. assert_equal(5, chat.customer_state[:queue])
  242. # check agent1 state
  243. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  244. assert_equal(5, agent_state[:waiting_chat_count])
  245. assert_equal(1, agent_state[:running_chat_count])
  246. assert_equal(Array, agent_state[:active_sessions].class)
  247. assert_equal(chat.id, agent_state[:active_sessions][0]['chat_id'])
  248. assert_equal(@agent1.id, agent_state[:active_sessions][0]['user_id'])
  249. assert(agent_state[:active_sessions][0]['messages'])
  250. assert_equal(Array, agent_state[:active_sessions][0]['messages'].class)
  251. assert_equal('message 1', agent_state[:active_sessions][0]['messages'][0]['content'])
  252. assert_equal('message 2', agent_state[:active_sessions][0]['messages'][1]['content'])
  253. assert_equal('message 3', agent_state[:active_sessions][0]['messages'][2]['content'])
  254. assert_equal('message 4', agent_state[:active_sessions][0]['messages'][3]['content'])
  255. assert_equal(-2, agent_state[:seads_available])
  256. assert_equal(4, agent_state[:seads_total])
  257. assert_equal(true, agent_state[:active])
  258. # check agent2 state
  259. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  260. assert_equal(5, agent_state[:waiting_chat_count])
  261. assert_equal(1, agent_state[:running_chat_count])
  262. assert_equal([], agent_state[:active_sessions])
  263. assert_equal(-2, agent_state[:seads_available])
  264. assert_equal(4, agent_state[:seads_total])
  265. assert_equal(false, agent_state[:active])
  266. chat_session6.state = 'closed'
  267. chat_session6.save
  268. # check customer state
  269. assert_equal('no_seats_available', chat.customer_state[:state])
  270. assert_equal(5, chat.customer_state[:queue])
  271. # check agent1 state
  272. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  273. assert_equal(5, agent_state[:waiting_chat_count])
  274. assert_equal(0, agent_state[:running_chat_count])
  275. assert_equal([], agent_state[:active_sessions])
  276. assert_equal(-1, agent_state[:seads_available])
  277. assert_equal(4, agent_state[:seads_total])
  278. assert_equal(true, agent_state[:active])
  279. # check agent2 state
  280. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  281. assert_equal(5, agent_state[:waiting_chat_count])
  282. assert_equal(0, agent_state[:running_chat_count])
  283. assert_equal([], agent_state[:active_sessions])
  284. assert_equal(-1, agent_state[:seads_available])
  285. assert_equal(4, agent_state[:seads_total])
  286. assert_equal(false, agent_state[:active])
  287. chat_session5.destroy
  288. chat_session4.destroy
  289. # check customer state
  290. assert_equal('online', chat.customer_state[:state])
  291. # check agent1 state
  292. agent_state = Chat.agent_state_with_sessions(@agent1.id)
  293. assert_equal(3, agent_state[:waiting_chat_count])
  294. assert_equal(0, agent_state[:running_chat_count])
  295. assert_equal([], agent_state[:active_sessions])
  296. assert_equal(1, agent_state[:seads_available])
  297. assert_equal(4, agent_state[:seads_total])
  298. assert_equal(true, agent_state[:active])
  299. # check agent2 state
  300. agent_state = Chat.agent_state_with_sessions(@agent2.id)
  301. assert_equal(3, agent_state[:waiting_chat_count])
  302. assert_equal(0, agent_state[:running_chat_count])
  303. assert_equal([], agent_state[:active_sessions])
  304. assert_equal(1, agent_state[:seads_available])
  305. assert_equal(4, agent_state[:seads_total])
  306. assert_equal(false, agent_state[:active])
  307. travel_back
  308. end
  309. test 'blocked ip test' do
  310. chat = Chat.create!(
  311. name: 'ip test',
  312. max_queue: 5,
  313. note: '',
  314. block_ip: '127.0.0.1;127.0.0.2;127.1.0.*',
  315. active: true,
  316. updated_by_id: 1,
  317. created_by_id: 1,
  318. )
  319. assert_not(chat.blocked_ip?('128.0.0.1'))
  320. assert_not(chat.blocked_ip?('127.0.0.30'))
  321. assert(chat.blocked_ip?('127.0.0.1'))
  322. assert(chat.blocked_ip?('127.0.0.2'))
  323. assert(chat.blocked_ip?('127.1.0.1'))
  324. assert(chat.blocked_ip?('127.1.0.100'))
  325. end
  326. test 'blocked country test' do
  327. chat = Chat.create!(
  328. name: 'country test',
  329. max_queue: 5,
  330. note: '',
  331. block_country: 'AU;CH',
  332. active: true,
  333. updated_by_id: 1,
  334. created_by_id: 1,
  335. )
  336. assert_not(chat.blocked_country?('127.0.0.1'))
  337. assert(chat.blocked_country?('1.1.1.8'))
  338. end
  339. end