taskbar_spec.rb 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 'multible 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. )
  104. UserInfo.current_user_id = 2
  105. taskbar2 = described_class.create(
  106. client_id: 123,
  107. key: 'Ticket-1234',
  108. callback: 'TicketZoom',
  109. params: {
  110. id: 1234,
  111. },
  112. state: {},
  113. prio: 2,
  114. notify: false,
  115. )
  116. taskbar1.reload
  117. expect(taskbar1.preferences[:tasks].count).to eq(2)
  118. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  119. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  120. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  121. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  122. taskbar2.reload
  123. expect(taskbar2.preferences[:tasks].count).to eq(2)
  124. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  125. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  126. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  127. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  128. taskbar3 = described_class.create(
  129. client_id: 123,
  130. key: 'Ticket-4444',
  131. callback: 'TicketZoom',
  132. params: {
  133. id: 4444,
  134. },
  135. state: {},
  136. prio: 2,
  137. notify: false,
  138. )
  139. taskbar1.reload
  140. expect(taskbar1.preferences[:tasks].count).to eq(2)
  141. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  142. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  143. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  144. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  145. taskbar2.reload
  146. expect(taskbar2.preferences[:tasks].count).to eq(2)
  147. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  148. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  149. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  150. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  151. taskbar3.reload
  152. expect(taskbar3.preferences[:tasks].count).to eq(1)
  153. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  154. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  155. agent_user_id = create(:agent_user).id
  156. UserInfo.current_user_id = agent_user_id
  157. taskbar4 = described_class.create(
  158. client_id: 123,
  159. key: 'Ticket-1234',
  160. callback: 'TicketZoom',
  161. params: {
  162. id: 1234,
  163. },
  164. state: {},
  165. prio: 4,
  166. notify: false,
  167. )
  168. taskbar1.reload
  169. expect(taskbar1.preferences[:tasks].count).to eq(3)
  170. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  171. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  172. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  173. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  174. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  175. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  176. taskbar2.reload
  177. expect(taskbar2.preferences[:tasks].count).to eq(3)
  178. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  179. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  180. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  181. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  182. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  183. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  184. taskbar3.reload
  185. expect(taskbar3.preferences[:tasks].count).to eq(1)
  186. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  187. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  188. taskbar4.reload
  189. expect(taskbar4.preferences[:tasks].count).to eq(3)
  190. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  191. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  192. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  193. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(false)
  194. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  195. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  196. UserInfo.current_user_id = 2
  197. taskbar2.state = { article: {}, ticket: {} }
  198. taskbar2.save!
  199. taskbar1.reload
  200. expect(taskbar1.preferences[:tasks].count).to eq(3)
  201. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  202. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  203. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  204. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(false)
  205. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  206. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  207. taskbar2.reload
  208. expect(taskbar2.preferences[:tasks].count).to eq(3)
  209. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  210. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  211. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  212. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(false)
  213. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  214. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  215. taskbar3.reload
  216. expect(taskbar3.preferences[:tasks].count).to eq(1)
  217. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  218. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  219. taskbar4.reload
  220. expect(taskbar4.preferences[:tasks].count).to eq(3)
  221. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  222. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  223. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  224. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(false)
  225. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  226. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  227. UserInfo.current_user_id = 2
  228. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  229. taskbar2.save!
  230. taskbar1.reload
  231. expect(taskbar1.preferences[:tasks].count).to eq(3)
  232. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  233. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(false)
  234. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  235. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  236. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  237. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  238. taskbar2.reload
  239. expect(taskbar2.preferences[:tasks].count).to eq(3)
  240. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  241. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(false)
  242. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  243. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  244. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  245. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  246. taskbar3.reload
  247. expect(taskbar3.preferences[:tasks].count).to eq(1)
  248. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  249. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  250. taskbar4.reload
  251. expect(taskbar4.preferences[:tasks].count).to eq(3)
  252. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  253. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(false)
  254. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  255. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  256. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  257. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  258. UserInfo.current_user_id = 1
  259. taskbar1.state = { article: { body: '' }, ticket: { state_id: 123 } }
  260. taskbar1.save!
  261. taskbar1.reload
  262. expect(taskbar1.preferences[:tasks].count).to eq(3)
  263. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  264. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  265. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  266. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  267. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  268. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  269. taskbar2.reload
  270. expect(taskbar2.preferences[:tasks].count).to eq(3)
  271. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  272. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  273. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  274. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  275. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  276. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  277. taskbar3.reload
  278. expect(taskbar3.preferences[:tasks].count).to eq(1)
  279. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  280. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  281. taskbar4.reload
  282. expect(taskbar4.preferences[:tasks].count).to eq(3)
  283. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  284. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  285. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  286. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  287. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  288. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  289. taskbar1_last_contact = taskbar1.last_contact.to_s
  290. taskbar2_last_contact = taskbar2.last_contact.to_s
  291. taskbar3_last_contact = taskbar3.last_contact.to_s
  292. taskbar4_last_contact = taskbar4.last_contact.to_s
  293. travel 2.minutes
  294. UserInfo.current_user_id = 2
  295. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  296. taskbar2.notify = true
  297. taskbar2.save!
  298. taskbar1.reload
  299. expect(taskbar1.preferences[:tasks].count).to eq(3)
  300. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  301. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  302. expect(taskbar1.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  303. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  304. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  305. expect(taskbar1.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  306. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  307. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  308. expect(taskbar1.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  309. taskbar2.reload
  310. expect(taskbar2.preferences[:tasks].count).to eq(3)
  311. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  312. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  313. expect(taskbar2.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  314. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  315. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  316. expect(taskbar2.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  317. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  318. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  319. expect(taskbar2.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  320. taskbar3.reload
  321. expect(taskbar3.preferences[:tasks].count).to eq(1)
  322. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  323. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  324. expect(taskbar3.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar3_last_contact)
  325. taskbar4.reload
  326. expect(taskbar4.preferences[:tasks].count).to eq(3)
  327. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  328. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  329. expect(taskbar4.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  330. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  331. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  332. expect(taskbar4.preferences[:tasks][1][:last_contact].to_s).to eq(taskbar2_last_contact)
  333. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  334. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  335. expect(taskbar4.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  336. UserInfo.current_user_id = 2
  337. taskbar2.state = { article: { body: 'some body 222' }, ticket: {} }
  338. taskbar2.notify = true
  339. taskbar2.save!
  340. taskbar1.reload
  341. expect(taskbar1.preferences[:tasks].count).to eq(3)
  342. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  343. expect(taskbar1.preferences[:tasks][0][:changed]).to eq(true)
  344. expect(taskbar1.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  345. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  346. expect(taskbar1.preferences[:tasks][1][:changed]).to eq(true)
  347. expect(taskbar1.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  348. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  349. expect(taskbar1.preferences[:tasks][2][:changed]).to eq(false)
  350. expect(taskbar1.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  351. taskbar2.reload
  352. expect(taskbar2.preferences[:tasks].count).to eq(3)
  353. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  354. expect(taskbar2.preferences[:tasks][0][:changed]).to eq(true)
  355. expect(taskbar2.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  356. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  357. expect(taskbar2.preferences[:tasks][1][:changed]).to eq(true)
  358. expect(taskbar2.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  359. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  360. expect(taskbar2.preferences[:tasks][2][:changed]).to eq(false)
  361. expect(taskbar2.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  362. taskbar3.reload
  363. expect(taskbar3.preferences[:tasks].count).to eq(1)
  364. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  365. expect(taskbar3.preferences[:tasks][0][:changed]).to eq(false)
  366. expect(taskbar3.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar3_last_contact)
  367. taskbar4.reload
  368. expect(taskbar4.preferences[:tasks].count).to eq(3)
  369. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  370. expect(taskbar4.preferences[:tasks][0][:changed]).to eq(true)
  371. expect(taskbar4.preferences[:tasks][0][:last_contact].to_s).to eq(taskbar1_last_contact)
  372. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  373. expect(taskbar4.preferences[:tasks][1][:changed]).to eq(true)
  374. expect(taskbar4.preferences[:tasks][1][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  375. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_user_id)
  376. expect(taskbar4.preferences[:tasks][2][:changed]).to eq(false)
  377. expect(taskbar4.preferences[:tasks][2][:last_contact].to_s).to eq(taskbar4_last_contact)
  378. travel_back
  379. UserInfo.current_user_id = nil
  380. end
  381. end
  382. end