taskbar_spec.rb 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/taskbar/has_attachments_examples'
  4. RSpec.describe Taskbar, type: :model do
  5. it_behaves_like 'Taskbar::HasAttachments'
  6. context 'item' do
  7. subject(:taskbar) { create(:taskbar) }
  8. it { is_expected.to validate_inclusion_of(:app).in_array(%w[desktop mobile]) }
  9. end
  10. context 'key = Search' do
  11. context 'multiple taskbars', current_user_id: 1 do
  12. let(:key) { 'Search' }
  13. let(:other_taskbar) { create(:taskbar, key: key) }
  14. describe '#create' do
  15. it "doesn't update other taskbar" do
  16. expect do
  17. create(:taskbar, key: key)
  18. end.not_to change { other_taskbar.reload.updated_at }
  19. end
  20. end
  21. context 'existing taskbar' do
  22. subject(:taskbar) { create(:taskbar, key: key) }
  23. describe '#update' do
  24. it "doesn't update other taskbar" do
  25. expect do
  26. taskbar.update!(state: { foo: :bar })
  27. end.not_to change { other_taskbar.reload.updated_at }
  28. end
  29. end
  30. describe '#destroy' do
  31. it "doesn't update other taskbar" do
  32. expect do
  33. taskbar.destroy!
  34. end.not_to change { other_taskbar.reload.updated_at }
  35. end
  36. end
  37. end
  38. end
  39. end
  40. context 'single creation' do
  41. let(:taskbar) do
  42. described_class.destroy_all
  43. UserInfo.current_user_id = 1
  44. create(:taskbar, params: {
  45. id: 1234,
  46. })
  47. end
  48. it 'existing key' do
  49. expect(taskbar.key).to eq('Ticket-1234')
  50. end
  51. it 'params' do
  52. expect(taskbar.params[:id]).to eq(1234)
  53. end
  54. it 'state' do
  55. expect(taskbar.state.blank?).to be(true)
  56. end
  57. it 'check last_contact' do
  58. UserInfo.current_user_id = 1
  59. last_contact1 = taskbar.last_contact
  60. travel 2.minutes
  61. taskbar.notify = false
  62. taskbar.state = { a: 1 }
  63. taskbar.save!
  64. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  65. last_contact2 = taskbar.last_contact
  66. travel 2.minutes
  67. taskbar.notify = true
  68. taskbar.save!
  69. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  70. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  71. travel 2.minutes
  72. taskbar.notify = true
  73. taskbar.save!
  74. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  75. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  76. travel 2.minutes
  77. taskbar.notify = false
  78. taskbar.state = { a: 1 }
  79. taskbar.save!
  80. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  81. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  82. travel 2.minutes
  83. taskbar.notify = true
  84. taskbar.state = { a: 1 }
  85. taskbar.save!
  86. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  87. expect(taskbar.last_contact.to_s).to eq(last_contact2.to_s)
  88. travel 2.minutes
  89. taskbar.notify = true
  90. taskbar.state = { a: 2 }
  91. taskbar.save!
  92. expect(taskbar.last_contact.to_s).not_to eq(last_contact1.to_s)
  93. expect(taskbar.last_contact.to_s).not_to eq(last_contact2.to_s)
  94. end
  95. end
  96. context 'multiple creation' do
  97. it 'create tasks' do
  98. described_class.destroy_all
  99. UserInfo.current_user_id = 1
  100. taskbar1 = described_class.create(
  101. key: 'Ticket-1234',
  102. callback: 'TicketZoom',
  103. params: {
  104. id: 1234,
  105. },
  106. state: {},
  107. prio: 1,
  108. notify: false,
  109. user_id: 1,
  110. )
  111. UserInfo.current_user_id = 2
  112. taskbar2 = described_class.create(
  113. key: 'Ticket-1234',
  114. callback: 'TicketZoom',
  115. params: {
  116. id: 1234,
  117. },
  118. state: {},
  119. prio: 2,
  120. notify: false,
  121. user_id: 1,
  122. )
  123. taskbar1.reload
  124. expect(taskbar1.preferences[:tasks].count).to eq(2)
  125. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  126. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  127. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  128. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  129. taskbar2.reload
  130. expect(taskbar2.preferences[:tasks].count).to eq(2)
  131. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  132. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  133. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  134. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  135. taskbar3 = described_class.create(
  136. key: 'Ticket-4444',
  137. callback: 'TicketZoom',
  138. params: {
  139. id: 4444,
  140. },
  141. state: {},
  142. prio: 2,
  143. notify: false,
  144. user_id: 1,
  145. )
  146. taskbar1.reload
  147. expect(taskbar1.preferences[:tasks].count).to eq(2)
  148. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  149. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  150. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  151. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  152. taskbar2.reload
  153. expect(taskbar2.preferences[:tasks].count).to eq(2)
  154. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  155. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  156. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  157. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  158. taskbar3.reload
  159. expect(taskbar3.preferences[:tasks].count).to eq(1)
  160. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  161. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  162. agent_id = create(:agent).id
  163. UserInfo.current_user_id = agent_id
  164. taskbar4 = described_class.create(
  165. key: 'Ticket-1234',
  166. callback: 'TicketZoom',
  167. params: {
  168. id: 1234,
  169. },
  170. state: {},
  171. prio: 4,
  172. notify: false,
  173. user_id: 1,
  174. )
  175. taskbar1.reload
  176. expect(taskbar1.preferences[:tasks].count).to eq(3)
  177. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  178. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  179. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  180. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  181. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  182. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  183. taskbar2.reload
  184. expect(taskbar2.preferences[:tasks].count).to eq(3)
  185. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  186. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  187. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  188. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  189. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  190. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  191. taskbar3.reload
  192. expect(taskbar3.preferences[:tasks].count).to eq(1)
  193. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  194. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  195. taskbar4.reload
  196. expect(taskbar4.preferences[:tasks].count).to eq(3)
  197. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  198. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  199. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  200. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  201. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  202. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  203. UserInfo.current_user_id = 2
  204. taskbar2.state = { article: {}, ticket: {} }
  205. taskbar2.save!
  206. taskbar1.reload
  207. expect(taskbar1.preferences[:tasks].count).to eq(3)
  208. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  209. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  210. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  211. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  212. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  213. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  214. taskbar2.reload
  215. expect(taskbar2.preferences[:tasks].count).to eq(3)
  216. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  217. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  218. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  219. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  220. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  221. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  222. taskbar3.reload
  223. expect(taskbar3.preferences[:tasks].count).to eq(1)
  224. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  225. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  226. taskbar4.reload
  227. expect(taskbar4.preferences[:tasks].count).to eq(3)
  228. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  229. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  230. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  231. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(false)
  232. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  233. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  234. UserInfo.current_user_id = 2
  235. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  236. taskbar2.save!
  237. taskbar1.reload
  238. expect(taskbar1.preferences[:tasks].count).to eq(3)
  239. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  240. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  241. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  242. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  243. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  244. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  245. taskbar2.reload
  246. expect(taskbar2.preferences[:tasks].count).to eq(3)
  247. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  248. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  249. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  250. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  251. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  252. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  253. taskbar3.reload
  254. expect(taskbar3.preferences[:tasks].count).to eq(1)
  255. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  256. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  257. taskbar4.reload
  258. expect(taskbar4.preferences[:tasks].count).to eq(3)
  259. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  260. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  261. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  262. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  263. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  264. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  265. UserInfo.current_user_id = 1
  266. taskbar1.state = { article: { body: '' }, ticket: { state_id: 123 } }
  267. taskbar1.save!
  268. taskbar1.reload
  269. expect(taskbar1.preferences[:tasks].count).to eq(3)
  270. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  271. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  272. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  273. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  274. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  275. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  276. taskbar2.reload
  277. expect(taskbar2.preferences[:tasks].count).to eq(3)
  278. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  279. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  280. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  281. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  282. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  283. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  284. taskbar3.reload
  285. expect(taskbar3.preferences[:tasks].count).to eq(1)
  286. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  287. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  288. taskbar4.reload
  289. expect(taskbar4.preferences[:tasks].count).to eq(3)
  290. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  291. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  292. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  293. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  294. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  295. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  296. taskbar1_last_contact = taskbar1.last_contact.to_s
  297. taskbar2_last_contact = taskbar2.last_contact.to_s
  298. taskbar3_last_contact = taskbar3.last_contact.to_s
  299. taskbar4_last_contact = taskbar4.last_contact.to_s
  300. travel 2.minutes
  301. UserInfo.current_user_id = 2
  302. taskbar2.state = { article: { body: 'some body' }, ticket: {} }
  303. taskbar2.notify = true
  304. taskbar2.save!
  305. taskbar1.reload
  306. expect(taskbar1.preferences[:tasks].count).to eq(3)
  307. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  308. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  309. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  310. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  311. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  312. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).to eq(taskbar2_last_contact)
  313. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  314. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  315. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  316. taskbar2.reload
  317. expect(taskbar2.preferences[:tasks].count).to eq(3)
  318. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  319. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  320. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  321. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  322. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  323. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).to eq(taskbar2_last_contact)
  324. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  325. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  326. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  327. taskbar3.reload
  328. expect(taskbar3.preferences[:tasks].count).to eq(1)
  329. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  330. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  331. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar3_last_contact)
  332. taskbar4.reload
  333. expect(taskbar4.preferences[:tasks].count).to eq(3)
  334. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  335. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  336. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  337. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  338. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  339. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).to eq(taskbar2_last_contact)
  340. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  341. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  342. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  343. UserInfo.current_user_id = 2
  344. taskbar2.state = { article: { body: 'some body 222' }, ticket: {} }
  345. taskbar2.notify = true
  346. taskbar2.save!
  347. taskbar1.reload
  348. expect(taskbar1.preferences[:tasks].count).to eq(3)
  349. expect(taskbar1.preferences[:tasks][0][:user_id]).to eq(1)
  350. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  351. expect(taskbar1.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  352. expect(taskbar1.preferences[:tasks][1][:user_id]).to eq(2)
  353. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  354. expect(taskbar1.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  355. expect(taskbar1.preferences[:tasks][2][:user_id]).to eq(agent_id)
  356. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  357. expect(taskbar1.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  358. taskbar2.reload
  359. expect(taskbar2.preferences[:tasks].count).to eq(3)
  360. expect(taskbar2.preferences[:tasks][0][:user_id]).to eq(1)
  361. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  362. expect(taskbar2.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  363. expect(taskbar2.preferences[:tasks][1][:user_id]).to eq(2)
  364. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  365. expect(taskbar2.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  366. expect(taskbar2.preferences[:tasks][2][:user_id]).to eq(agent_id)
  367. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  368. expect(taskbar2.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  369. taskbar3.reload
  370. expect(taskbar3.preferences[:tasks].count).to eq(1)
  371. expect(taskbar3.preferences[:tasks][0][:user_id]).to eq(2)
  372. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:changed]).to be(false)
  373. expect(taskbar3.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar3_last_contact)
  374. taskbar4.reload
  375. expect(taskbar4.preferences[:tasks].count).to eq(3)
  376. expect(taskbar4.preferences[:tasks][0][:user_id]).to eq(1)
  377. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:changed]).to be(true)
  378. expect(taskbar4.preferences[:tasks][0][:apps][:desktop][:last_contact].to_s).to eq(taskbar1_last_contact)
  379. expect(taskbar4.preferences[:tasks][1][:user_id]).to eq(2)
  380. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:changed]).to be(true)
  381. expect(taskbar4.preferences[:tasks][1][:apps][:desktop][:last_contact].to_s).not_to eq(taskbar2_last_contact)
  382. expect(taskbar4.preferences[:tasks][2][:user_id]).to eq(agent_id)
  383. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:changed]).to be(false)
  384. expect(taskbar4.preferences[:tasks][2][:apps][:desktop][:last_contact].to_s).to eq(taskbar4_last_contact)
  385. travel_back
  386. UserInfo.current_user_id = nil
  387. end
  388. end
  389. describe '#preferences_task_info' do
  390. it 'returns task info for an existing taskbar without changes' do
  391. taskbar = create(:taskbar)
  392. expect(taskbar.preferences_task_info)
  393. .to eq({
  394. id: taskbar.id, user_id: 1, apps: { desktop: { last_contact: taskbar.last_contact, changed: false } }
  395. })
  396. end
  397. it 'returns task info for an existing taskbar with changes' do
  398. taskbar = create(:taskbar, state: { a: 123 })
  399. expect(taskbar.preferences_task_info)
  400. .to eq({
  401. id: taskbar.id, user_id: 1, apps: { desktop: { last_contact: taskbar.last_contact, changed: true } }
  402. })
  403. end
  404. it 'returns task info for a new taskbar' do
  405. taskbar = build(:taskbar)
  406. expect(taskbar.preferences_task_info)
  407. .to eq({
  408. user_id: 1, apps: { desktop: { last_contact: taskbar.last_contact, changed: false } }
  409. })
  410. end
  411. end
  412. describe '#update_preferences_infos' do
  413. it 'do not process search taskbars' do
  414. taskbar = build(:taskbar, key: 'Search')
  415. allow(taskbar).to receive(:collect_related_tasks)
  416. taskbar.save
  417. expect(taskbar).not_to have_received(:collect_related_tasks)
  418. end
  419. it 'do not process items with local_update flag' do
  420. taskbar = create(:taskbar)
  421. allow(taskbar).to receive(:collect_related_tasks)
  422. taskbar.state = { a: 'b' }
  423. taskbar.local_update = true
  424. taskbar.save
  425. expect(taskbar).not_to have_received(:collect_related_tasks)
  426. end
  427. context 'with other taskbars' do
  428. let(:key) { Random.hex }
  429. let(:other_user) { create(:user) }
  430. let(:other_taskbar) { create(:taskbar, key: key, user: other_user) }
  431. before { other_taskbar }
  432. it 'sets tasks when creating a taskbar' do
  433. taskbar = create(:taskbar, key: key)
  434. expect(taskbar.preferences[:tasks]).to include(include(user_id: other_user.id), include(user_id: 1))
  435. end
  436. it 'updates related items when creating a taskbar' do
  437. create(:taskbar, key: key)
  438. expect(other_taskbar.reload.preferences[:tasks]).to include(include(user_id: other_user.id), include(user_id: 1))
  439. end
  440. it 'sets tasks when updating a taskbar' do
  441. taskbar = create(:taskbar, key: key)
  442. taskbar.update_columns preferences: {}
  443. taskbar.update! state: { a: :b }
  444. expect(taskbar.preferences[:tasks]).to include(include(user_id: other_user.id), include(user_id: 1))
  445. end
  446. it 'sets tasks when updating a taskbar with same user but different app' do
  447. taskbar = create(:taskbar, key: key, user: other_user, app: 'mobile')
  448. taskbar.update_columns preferences: {}
  449. taskbar.update! state: { a: :b }
  450. expect(taskbar.preferences[:tasks])
  451. .to include(include(user_id: other_user.id, apps: have_key(:desktop).and(have_key(:mobile))))
  452. end
  453. it 'updates related items when updating a taskbar' do
  454. taskbar = create(:taskbar, key: key)
  455. other_taskbar.update_columns preferences: {}
  456. taskbar.update! state: { a: :b }
  457. expect(other_taskbar.reload.preferences[:tasks]).to include(include(user_id: other_user.id), include(user_id: 1))
  458. end
  459. it 'updates related items when destroying a taskbar' do
  460. taskbar = create(:taskbar, key: key)
  461. taskbar.destroy!
  462. expect(other_taskbar.reload.preferences[:tasks]).to include(include(user_id: other_user.id))
  463. end
  464. end
  465. end
  466. describe '#collect_related_tasks' do
  467. let(:key) { Random.hex }
  468. let(:taskbar_1) { create(:taskbar, key: key, user: create(:user)) }
  469. let(:taskbar_2) { create(:taskbar, key: key, user: create(:user)) }
  470. before { taskbar_2 }
  471. it 'returns tasks for self and related items' do
  472. expect(taskbar_1.send(:collect_related_tasks))
  473. .to eq([taskbar_2.preferences_task_info, taskbar_1.preferences_task_info])
  474. end
  475. it 'returns tasks for a new taskbar' do
  476. new_taskbar = build(:taskbar, key: key)
  477. expect(new_taskbar.send(:collect_related_tasks))
  478. .to eq([taskbar_2.preferences_task_info, new_taskbar.preferences_task_info])
  479. end
  480. it 'do not include task of the destroyed taskbar' do
  481. taskbar_1
  482. taskbar_2.destroy!
  483. expect(taskbar_2.send(:collect_related_tasks))
  484. .to eq([taskbar_1.preferences_task_info])
  485. end
  486. end
  487. describe '#reduce_related_tasks' do
  488. let(:elem) { { user_id: 123, changed: { desktop: false } } }
  489. let(:memo) { {} }
  490. it 'adds new task details' do
  491. taskbar = create(:taskbar)
  492. taskbar.send(:reduce_related_tasks, elem, memo)
  493. expect(memo).to include(elem[:user_id] => include(changed: include(desktop: false)))
  494. end
  495. it 'extends existing task details with additional apps' do
  496. taskbar = create(:taskbar)
  497. another_elem = { user_id: 123, changed: { mobile: true } }
  498. taskbar.send(:reduce_related_tasks, elem, memo)
  499. taskbar.send(:reduce_related_tasks, another_elem, memo)
  500. expect(memo).to include(elem[:user_id] => include(changed: include(desktop: false, mobile: true)))
  501. end
  502. end
  503. describe '#update_related_taskbars' do
  504. let(:key) { Random.hex }
  505. let(:taskbar_1) { create(:taskbar, key: key, user: create(:user)) }
  506. let(:taskbar_2) { create(:taskbar, key: key, user: create(:user)) }
  507. let(:taskbar_3) { create(:taskbar, user: taskbar_1.user) }
  508. before { taskbar_1 && taskbar_2 && taskbar_3 }
  509. it 'updates related taskbars' do
  510. taskbar_1.send(:update_related_taskbars, { a: 1 })
  511. expect(taskbar_2.reload).to have_attributes(preferences: { a: 1 })
  512. expect(taskbar_3.reload).not_to have_attributes(preferences: { a: 1 })
  513. end
  514. end
  515. describe '#related_taskbars' do
  516. let(:key) { Random.hex }
  517. let(:taskbar_1) { create(:taskbar, key: key, user: create(:user)) }
  518. let(:taskbar_2) { create(:taskbar, key: key, user: taskbar_1.user, app: 'mobile') }
  519. let(:taskbar_3) { create(:taskbar, key: key, user: create(:user)) }
  520. let(:taskbar_4) { create(:taskbar, user: create(:user)) }
  521. it 'calls related_taskbars scope' do
  522. taskbar = create(:taskbar)
  523. allow(described_class).to receive(:related_taskbars)
  524. taskbar.related_taskbars
  525. expect(described_class).to have_received(:related_taskbars).with(taskbar)
  526. end
  527. end
  528. describe '.related_taskbars' do
  529. let(:key) { Random.hex }
  530. let(:taskbar_1) { create(:taskbar, key: key, user: create(:user)) }
  531. let(:taskbar_2) { create(:taskbar, key: key, user: taskbar_1.user, app: 'mobile') }
  532. let(:taskbar_3) { create(:taskbar, key: key, user: create(:user)) }
  533. let(:taskbar_4) { create(:taskbar, user: create(:user)) }
  534. before { taskbar_1 && taskbar_2 && taskbar_3 && taskbar_4 }
  535. it 'returns all taskbars with the same key except given taskbars' do
  536. expect(described_class.related_taskbars(taskbar_1)).to contain_exactly(taskbar_2, taskbar_3)
  537. end
  538. end
  539. end