taskbar_spec.rb 27 KB

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