taskbar_spec.rb 27 KB

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