taskbar_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. require 'rails_helper'
  2. RSpec.describe Taskbar do
  3. context 'key = Search' do
  4. context 'multiple taskbars', current_user_id: 1 do
  5. let(:key) { 'Search' }
  6. let(:other_taskbar) { create(:taskbar, key: key) }
  7. describe '#create' do
  8. it "doesn't update other taskbar" do
  9. expect do
  10. create(:taskbar, key: key)
  11. end.not_to change { other_taskbar.reload.updated_at }
  12. end
  13. end
  14. context 'existing taskbar' do
  15. subject(:taskbar) { create(:taskbar, key: key) }
  16. describe '#update' do
  17. it "doesn't update other taskbar" do
  18. expect do
  19. taskbar.update!(state: { foo: :bar })
  20. end.not_to change { other_taskbar.reload.updated_at }
  21. end
  22. end
  23. describe '#destroy' do
  24. it "doesn't update other taskbar" do
  25. expect do
  26. taskbar.destroy!
  27. end.not_to change { other_taskbar.reload.updated_at }
  28. end
  29. end
  30. end
  31. end
  32. end
  33. context 'single creation' do
  34. let(:taskbar) do
  35. described_class.destroy_all
  36. UserInfo.current_user_id = 1
  37. create(:taskbar, params: {
  38. id: 1234,
  39. })
  40. end
  41. it 'existing key' do
  42. expect(taskbar.key).to eq('Ticket-1234')
  43. end
  44. it 'params' do
  45. expect(taskbar.params[:id]).to eq(1234)
  46. end
  47. it 'state' do
  48. expect(taskbar.state.blank?).to eq(true)
  49. end
  50. it 'check last_contact' do
  51. UserInfo.current_user_id = 1
  52. last_contact1 = taskbar.last_contact
  53. travel 2.minutes
  54. taskbar.notify = false
  55. taskbar.state = { a: 1 }
  56. taskbar.save!
  57. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  58. last_contact2 = taskbar.last_contact
  59. travel 2.minutes
  60. taskbar.notify = true
  61. taskbar.save!
  62. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  63. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  64. travel 2.minutes
  65. taskbar.notify = true
  66. taskbar.save!
  67. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  68. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  69. travel 2.minutes
  70. taskbar.notify = false
  71. taskbar.state = { a: 1 }
  72. taskbar.save!
  73. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  74. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  75. travel 2.minutes
  76. taskbar.notify = true
  77. taskbar.state = { a: 1 }
  78. taskbar.save!
  79. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  80. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  81. travel 2.minutes
  82. taskbar.notify = true
  83. taskbar.state = { a: 2 }
  84. taskbar.save!
  85. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  86. expect(taskbar.last_contact.to_s).not_to eq(last_contact2.to_s)
  87. end
  88. end
  89. context 'multiple creation' do
  90. it 'create tasks' do
  91. described_class.destroy_all
  92. UserInfo.current_user_id = 1
  93. taskbar1 = described_class.create(
  94. client_id: 123,
  95. key: 'Ticket-1234',
  96. callback: 'TicketZoom',
  97. params: {
  98. id: 1234,
  99. },
  100. state: {},
  101. prio: 1,
  102. notify: false,
  103. user_id: 1,
  104. )
  105. UserInfo.current_user_id = 2
  106. taskbar2 = described_class.create(
  107. client_id: 123,
  108. key: 'Ticket-1234',
  109. callback: 'TicketZoom',
  110. params: {
  111. id: 1234,
  112. },
  113. state: {},
  114. prio: 2,
  115. notify: false,
  116. user_id: 1,
  117. )
  118. taskbar1.reload
  119. expect(taskbar1.preferences[:tasks].count).to eq(2)
  120. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  121. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  122. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  123. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  124. taskbar2.reload
  125. expect(taskbar2.preferences[:tasks].count).to eq(2)
  126. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  127. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  128. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  129. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  130. taskbar3 = described_class.create(
  131. client_id: 123,
  132. key: 'Ticket-4444',
  133. callback: 'TicketZoom',
  134. params: {
  135. id: 4444,
  136. },
  137. state: {},
  138. prio: 2,
  139. notify: false,
  140. user_id: 1,
  141. )
  142. taskbar1.reload
  143. expect(taskbar1.preferences[:tasks].count).to eq(2)
  144. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  145. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  146. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  147. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  148. taskbar2.reload
  149. expect(taskbar2.preferences[:tasks].count).to eq(2)
  150. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  151. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  152. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  153. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  154. taskbar3.reload
  155. expect(taskbar3.preferences[:tasks].count).to eq(1)
  156. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  157. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  158. agent_id = create(:agent).id
  159. UserInfo.current_user_id = agent_id
  160. taskbar4 = described_class.create(
  161. client_id: 123,
  162. key: 'Ticket-1234',
  163. callback: 'TicketZoom',
  164. params: {
  165. id: 1234,
  166. },
  167. state: {},
  168. prio: 4,
  169. notify: false,
  170. user_id: 1,
  171. )
  172. taskbar1.reload
  173. expect(taskbar1.preferences[:tasks].count).to eq(3)
  174. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  175. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  176. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  177. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  178. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  179. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  180. taskbar2.reload
  181. expect(taskbar2.preferences[:tasks].count).to eq(3)
  182. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  183. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  184. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  185. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  186. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  187. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  188. taskbar3.reload
  189. expect(taskbar3.preferences[:tasks].count).to eq(1)
  190. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  191. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  192. taskbar4.reload
  193. expect(taskbar4.preferences[:tasks].count).to eq(3)
  194. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  195. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  196. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  197. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(false)
  198. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  199. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  200. UserInfo.current_user_id = 2
  201. taskbar2.state = { article: {}, ticket: {} }
  202. taskbar2.save!
  203. taskbar1.reload
  204. expect(taskbar1.preferences[:tasks].count).to eq(3)
  205. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  206. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  207. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  208. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  209. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  210. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  211. taskbar2.reload
  212. expect(taskbar2.preferences[:tasks].count).to eq(3)
  213. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  214. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  215. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  216. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  217. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  218. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  219. taskbar3.reload
  220. expect(taskbar3.preferences[:tasks].count).to eq(1)
  221. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  222. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  223. taskbar4.reload
  224. expect(taskbar4.preferences[:tasks].count).to eq(3)
  225. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  226. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  227. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  228. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(false)
  229. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  230. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  231. UserInfo.current_user_id = 2
  232. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  233. taskbar2.save!
  234. taskbar1.reload
  235. expect(taskbar1.preferences[:tasks].count).to eq(3)
  236. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  237. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  238. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  239. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  240. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  241. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  242. taskbar2.reload
  243. expect(taskbar2.preferences[:tasks].count).to eq(3)
  244. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  245. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  246. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  247. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  248. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  249. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  250. taskbar3.reload
  251. expect(taskbar3.preferences[:tasks].count).to eq(1)
  252. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  253. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  254. taskbar4.reload
  255. expect(taskbar4.preferences[:tasks].count).to eq(3)
  256. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  257. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  258. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  259. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  260. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  261. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  262. UserInfo.current_user_id = 1
  263. taskbar1.state = { article: { body: '' }, ticket: { state_id: 123 } }
  264. taskbar1.save!
  265. taskbar1.reload
  266. expect(taskbar1.preferences[:tasks].count).to eq(3)
  267. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  268. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  269. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  270. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  271. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  272. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  273. taskbar2.reload
  274. expect(taskbar2.preferences[:tasks].count).to eq(3)
  275. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  276. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  277. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  278. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  279. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  280. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  281. taskbar3.reload
  282. expect(taskbar3.preferences[:tasks].count).to eq(1)
  283. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  284. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  285. taskbar4.reload
  286. expect(taskbar4.preferences[:tasks].count).to eq(3)
  287. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  288. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  289. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  290. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  291. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  292. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  293. taskbar1_last_contact = taskbar1.last_contact.to_s
  294. taskbar2_last_contact = taskbar2.last_contact.to_s
  295. taskbar3_last_contact = taskbar3.last_contact.to_s
  296. taskbar4_last_contact = taskbar4.last_contact.to_s
  297. travel 2.minutes
  298. UserInfo.current_user_id = 2
  299. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  300. taskbar2.notify = true
  301. taskbar2.save!
  302. taskbar1.reload
  303. expect(taskbar1.preferences[:tasks].count).to eq(3)
  304. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  305. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  306. expect(taskbar1.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  307. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  308. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  309. expect(taskbar1.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  310. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  311. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  312. expect(taskbar1.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  313. taskbar2.reload
  314. expect(taskbar2.preferences[:tasks].count).to eq(3)
  315. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  316. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  317. expect(taskbar2.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  318. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  319. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  320. expect(taskbar2.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  321. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  322. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  323. expect(taskbar2.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  324. taskbar3.reload
  325. expect(taskbar3.preferences[:tasks].count).to eq(1)
  326. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  327. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  328. expect(taskbar3.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar3_last_contact)
  329. taskbar4.reload
  330. expect(taskbar4.preferences[:tasks].count).to eq(3)
  331. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  332. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  333. expect(taskbar4.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  334. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  335. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  336. expect(taskbar4.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  337. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  338. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  339. expect(taskbar4.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  340. UserInfo.current_user_id = 2
  341. taskbar2.state = { article: { body: 'some body 222' }, ticket: {} }
  342. taskbar2.notify = true
  343. taskbar2.save!
  344. taskbar1.reload
  345. expect(taskbar1.preferences[:tasks].count).to eq(3)
  346. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  347. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  348. expect(taskbar1.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  349. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  350. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  351. expect(taskbar1.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  352. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  353. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  354. expect(taskbar1.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  355. taskbar2.reload
  356. expect(taskbar2.preferences[:tasks].count).to eq(3)
  357. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  358. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  359. expect(taskbar2.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  360. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  361. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  362. expect(taskbar2.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  363. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  364. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  365. expect(taskbar2.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  366. taskbar3.reload
  367. expect(taskbar3.preferences[:tasks].count).to eq(1)
  368. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  369. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  370. expect(taskbar3.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar3_last_contact)
  371. taskbar4.reload
  372. expect(taskbar4.preferences[:tasks].count).to eq(3)
  373. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  374. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  375. expect(taskbar4.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  376. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  377. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  378. expect(taskbar4.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  379. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  380. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  381. expect(taskbar4.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  382. travel_back
  383. UserInfo.current_user_id = nil
  384. end
  385. end
  386. end