taskbar_spec.rb 27 KB

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