taskbar_spec.rb 28 KB

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