user_spec.rb 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/concerns/has_groups_examples'
  5. require 'models/concerns/has_history_examples'
  6. require 'models/concerns/has_roles_examples'
  7. require 'models/concerns/has_groups_permissions_examples'
  8. require 'models/concerns/has_xss_sanitized_note_examples'
  9. require 'models/concerns/can_be_imported_examples'
  10. require 'models/concerns/has_object_manager_attributes_examples'
  11. require 'models/user/can_lookup_search_index_attributes_examples'
  12. require 'models/user/performs_geo_lookup_examples'
  13. require 'models/concerns/has_taskbars_examples'
  14. RSpec.describe User, type: :model do
  15. subject(:user) { create(:user) }
  16. let(:customer) { create(:customer) }
  17. let(:agent) { create(:agent) }
  18. let(:admin) { create(:admin) }
  19. it_behaves_like 'ApplicationModel', can_assets: { associations: :organization }
  20. it_behaves_like 'HasGroups', group_access_factory: :agent
  21. it_behaves_like 'HasHistory'
  22. it_behaves_like 'HasRoles', group_access_factory: :agent
  23. it_behaves_like 'HasXssSanitizedNote', model_factory: :user
  24. it_behaves_like 'HasGroups and Permissions', group_access_no_permission_factory: :user
  25. it_behaves_like 'CanBeImported'
  26. it_behaves_like 'HasObjectManagerAttributes'
  27. it_behaves_like 'CanLookupSearchIndexAttributes'
  28. it_behaves_like 'HasTaskbars'
  29. it_behaves_like 'UserPerformsGeoLookup'
  30. describe 'Class methods:' do
  31. describe '.identify' do
  32. it 'returns users by given login' do
  33. expect(described_class.identify(user.login)).to eq(user)
  34. end
  35. it 'returns users by given email' do
  36. expect(described_class.identify(user.email)).to eq(user)
  37. end
  38. it 'returns nil for empty username' do
  39. expect(described_class.identify('')).to be_nil
  40. end
  41. end
  42. end
  43. describe 'Instance methods:' do
  44. describe '#out_of_office?' do
  45. context 'without any out_of_office_* attributes set' do
  46. it 'returns false' do
  47. expect(agent.out_of_office?).to be(false)
  48. end
  49. end
  50. context 'with valid #out_of_office_* attributes' do
  51. before do
  52. agent.update(
  53. out_of_office_start_at: Time.current.yesterday,
  54. out_of_office_end_at: Time.current.tomorrow,
  55. out_of_office_replacement_id: 1
  56. )
  57. end
  58. context 'but #out_of_office: false' do
  59. before { agent.update(out_of_office: false) }
  60. it 'returns false' do
  61. expect(agent.out_of_office?).to be(false)
  62. end
  63. end
  64. context 'and #out_of_office: true' do
  65. before { agent.update(out_of_office: true) }
  66. it 'returns true' do
  67. expect(agent.out_of_office?).to be(true)
  68. end
  69. context 'after the #out_of_office_end_at time has passed' do
  70. before { travel 2.days }
  71. it 'returns false (even though #out_of_office has not changed)' do
  72. expect(agent.out_of_office).to be(true)
  73. expect(agent.out_of_office?).to be(false)
  74. end
  75. end
  76. end
  77. end
  78. context 'date range is inclusive' do
  79. before do
  80. freeze_time
  81. agent.update(
  82. out_of_office: true,
  83. out_of_office_start_at: 1.day.from_now.to_date,
  84. out_of_office_end_at: 1.week.from_now.to_date,
  85. out_of_office_replacement_id: 1
  86. )
  87. end
  88. it 'today in office' do
  89. expect(agent).not_to be_out_of_office
  90. end
  91. it 'tomorrow not in office' do
  92. travel 1.day
  93. expect(agent).to be_out_of_office
  94. end
  95. it 'after 7 days not in office' do
  96. travel 7.days
  97. expect(agent).to be_out_of_office
  98. end
  99. it 'after 8 days in office' do
  100. travel 8.days
  101. expect(agent).not_to be_out_of_office
  102. end
  103. end
  104. # https://github.com/zammad/zammad/issues/3590
  105. context 'when setting the same date' do
  106. before do
  107. freeze_time
  108. target_date = 1.day.from_now.to_date
  109. agent.update(
  110. out_of_office: true,
  111. out_of_office_start_at: target_date,
  112. out_of_office_end_at: target_date,
  113. out_of_office_replacement_id: 1
  114. )
  115. end
  116. it 'agent is out of office tomorrow' do
  117. travel 1.day
  118. expect(agent).to be_out_of_office
  119. end
  120. it 'agent is not out of office the day after tomorrow' do
  121. travel 2.days
  122. expect(agent).not_to be_out_of_office
  123. end
  124. it 'agent is not out of office today' do
  125. expect(agent).not_to be_out_of_office
  126. end
  127. context 'given it respects system time zone' do
  128. before do
  129. travel_to Time.current.end_of_day
  130. end
  131. it 'agent is in office if in UTC' do
  132. expect(agent).not_to be_out_of_office
  133. end
  134. it 'agent is out of office if ahead of UTC' do
  135. travel_to Time.current.end_of_day
  136. Setting.set('timezone_default', 'Europe/Vilnius')
  137. expect(agent).to be_out_of_office
  138. end
  139. end
  140. end
  141. end
  142. describe '#out_of_office_agent' do
  143. it { is_expected.to respond_to(:out_of_office_agent) }
  144. context 'when user has no designated substitute' do
  145. it 'returns nil' do
  146. expect(user.out_of_office_agent).to be_nil
  147. end
  148. end
  149. context 'when user has designated substitute' do
  150. subject(:user) do
  151. create(:user,
  152. out_of_office: out_of_office,
  153. out_of_office_start_at: Time.zone.yesterday,
  154. out_of_office_end_at: Time.zone.tomorrow,
  155. out_of_office_replacement_id: substitute.id,)
  156. end
  157. let(:substitute) { create(:user) }
  158. context 'but is not out of office' do
  159. let(:out_of_office) { false }
  160. it 'returns nil' do
  161. expect(user.out_of_office_agent).to be_nil
  162. end
  163. end
  164. context 'and is out of office' do
  165. let(:out_of_office) { true }
  166. it 'returns the designated substitute' do
  167. expect(user.out_of_office_agent).to eq(substitute)
  168. end
  169. end
  170. context 'with recursive out of office structure' do
  171. let(:out_of_office) { true }
  172. let(:substitute) do
  173. create(:user,
  174. out_of_office: out_of_office,
  175. out_of_office_start_at: Time.zone.yesterday,
  176. out_of_office_end_at: Time.zone.tomorrow,
  177. out_of_office_replacement_id: user_active.id,)
  178. end
  179. let!(:user_active) { create(:user) }
  180. it 'returns the designated substitute recursive' do
  181. expect(user.out_of_office_agent).to eq(user_active)
  182. end
  183. end
  184. context 'with recursive out of office structure with a endless loop' do
  185. let(:out_of_office) { true }
  186. let(:substitute) do
  187. create(:user,
  188. out_of_office: out_of_office,
  189. out_of_office_start_at: Time.zone.yesterday,
  190. out_of_office_end_at: Time.zone.tomorrow,
  191. out_of_office_replacement_id: user_active.id,)
  192. end
  193. let!(:user_active) do
  194. create(:user,
  195. out_of_office: out_of_office,
  196. out_of_office_start_at: Time.zone.yesterday,
  197. out_of_office_end_at: Time.zone.tomorrow,
  198. out_of_office_replacement_id: agent.id,)
  199. end
  200. before do
  201. user_active.update(out_of_office_replacement_id: substitute.id)
  202. end
  203. it 'returns the designated substitute recursive with a endless loop' do
  204. expect(user.out_of_office_agent).to eq(substitute)
  205. end
  206. end
  207. context 'with stack depth exceeding limit' do
  208. let(:replacement_chain) do
  209. user = create(:agent)
  210. 14
  211. .times
  212. .each_with_object([user]) do |_, memo|
  213. memo << create(:agent, :ooo, ooo_agent: memo.last)
  214. end
  215. .reverse
  216. end
  217. let(:ids_executed) { [] }
  218. before do
  219. allow_any_instance_of(described_class).to receive(:out_of_office_agent).and_wrap_original do |method, *args| # rubocop:disable RSpec/AnyInstance
  220. ids_executed << method.receiver.id
  221. method.call(*args)
  222. end
  223. allow(Rails.logger).to receive(:warn)
  224. end
  225. it 'returns the last agent at the limit' do
  226. expect(replacement_chain.first.out_of_office_agent).to eq replacement_chain[10]
  227. end
  228. it 'does not evaluate element beyond the limit' do
  229. user_beyond_limit = replacement_chain[11]
  230. replacement_chain.first.out_of_office_agent
  231. expect(ids_executed).not_to include(user_beyond_limit.id)
  232. end
  233. it 'does evaluate element within the limit' do
  234. user_within_limit = replacement_chain[5]
  235. replacement_chain.first.out_of_office_agent
  236. expect(ids_executed).to include(user_within_limit.id)
  237. end
  238. it 'logs error below the limit' do
  239. replacement_chain.first.out_of_office_agent
  240. expect(Rails.logger).to have_received(:warn).with(%r{#{Regexp.escape('Found more than 10 replacement levels for agent')}})
  241. end
  242. it 'does not logs warn within the limit' do
  243. replacement_chain[10].out_of_office_agent
  244. expect(Rails.logger).not_to have_received(:warn)
  245. end
  246. end
  247. end
  248. end
  249. describe '#out_of_office_agent_of' do
  250. context 'when no other agents are out-of-office' do
  251. it 'returns an empty ActiveRecord::Relation' do
  252. expect(agent.out_of_office_agent_of)
  253. .to be_an(ActiveRecord::Relation)
  254. .and be_empty
  255. end
  256. end
  257. context 'when designated as the substitute' do
  258. let!(:agent_on_holiday) do
  259. create(
  260. :agent,
  261. out_of_office_start_at: Time.current.yesterday,
  262. out_of_office_end_at: Time.current.tomorrow,
  263. out_of_office_replacement_id: agent.id,
  264. out_of_office: out_of_office
  265. )
  266. end
  267. context 'of an in-office agent' do
  268. let(:out_of_office) { false }
  269. it 'returns an empty ActiveRecord::Relation' do
  270. expect(agent.out_of_office_agent_of)
  271. .to be_an(ActiveRecord::Relation)
  272. .and be_empty
  273. end
  274. end
  275. context 'of an out-of-office agent' do
  276. let(:out_of_office) { true }
  277. it 'returns an ActiveRecord::Relation including that agent' do
  278. expect(agent.out_of_office_agent_of)
  279. .to match_array([agent_on_holiday])
  280. end
  281. end
  282. context 'when inherited' do
  283. let(:out_of_office) { true }
  284. let!(:agent_on_holiday_sub) do
  285. create(
  286. :agent,
  287. out_of_office_start_at: Time.current.yesterday,
  288. out_of_office_end_at: Time.current.tomorrow,
  289. out_of_office_replacement_id: agent_on_holiday.id,
  290. out_of_office: out_of_office
  291. )
  292. end
  293. it 'returns an ActiveRecord::Relation including both agents' do
  294. expect(agent.out_of_office_agent_of)
  295. .to match_array([agent_on_holiday, agent_on_holiday_sub])
  296. end
  297. end
  298. context 'when inherited endless loop' do
  299. let(:out_of_office) { true }
  300. let!(:agent_on_holiday_sub) do
  301. create(
  302. :agent,
  303. out_of_office_start_at: Time.current.yesterday,
  304. out_of_office_end_at: Time.current.tomorrow,
  305. out_of_office_replacement_id: agent_on_holiday.id,
  306. out_of_office: out_of_office
  307. )
  308. end
  309. let!(:agent_on_holiday_sub2) do
  310. create(
  311. :agent,
  312. out_of_office_start_at: Time.current.yesterday,
  313. out_of_office_end_at: Time.current.tomorrow,
  314. out_of_office_replacement_id: agent_on_holiday_sub.id,
  315. out_of_office: out_of_office
  316. )
  317. end
  318. before do
  319. agent_on_holiday_sub.update(out_of_office_replacement_id: agent_on_holiday_sub2.id)
  320. end
  321. it 'returns an ActiveRecord::Relation including both agents referencing each other' do
  322. expect(agent_on_holiday_sub.out_of_office_agent_of)
  323. .to match_array([agent_on_holiday_sub, agent_on_holiday_sub2])
  324. end
  325. end
  326. end
  327. end
  328. describe '#by_reset_token' do
  329. subject(:user) { token.user }
  330. let(:token) { create(:token_password_reset) }
  331. context 'with a valid token' do
  332. it 'returns the matching user' do
  333. expect(described_class.by_reset_token(token.name)).to eq(user)
  334. end
  335. end
  336. context 'with an invalid token' do
  337. it 'returns nil' do
  338. expect(described_class.by_reset_token('not-existing')).to be_nil
  339. end
  340. end
  341. end
  342. describe '#password_reset_via_token' do
  343. subject(:user) { token.user }
  344. let!(:token) { create(:token_password_reset) }
  345. it 'changes the password of the token user and destroys the token' do
  346. expect { described_class.password_reset_via_token(token.name, Faker::Internet.password) }
  347. .to change { user.reload.password }
  348. .and change(Token, :count).by(-1)
  349. end
  350. end
  351. describe '#permissions' do
  352. let(:user) { create(:agent).tap { |u| u.roles = [u.roles.first] } }
  353. let(:role) { user.roles.first }
  354. let(:permissions) { role.permissions }
  355. it 'is a simple association getter' do
  356. expect(user.permissions).to match_array(permissions)
  357. end
  358. context 'for inactive permissions' do
  359. before { permissions.first.update(active: false) }
  360. it 'omits them from the returned hash' do
  361. expect(user.permissions).not_to include(permissions.first)
  362. end
  363. end
  364. context 'for permissions on inactive roles' do
  365. before { role.update(active: false) }
  366. it 'omits them from the returned hash' do
  367. expect(user.permissions).not_to include(*role.permissions)
  368. end
  369. end
  370. end
  371. describe '#permissions?' do
  372. subject(:user) { create(:user, roles: [role]) }
  373. let(:role) { create(:role, permissions: [permission]) }
  374. let(:permission) { create(:permission, name: permission_name) }
  375. context 'with privileges for a root permission (e.g., "foo", not "foo.bar")' do
  376. let(:permission_name) { 'foo' }
  377. context 'when given that exact permission' do
  378. it 'returns true' do
  379. expect(user.permissions?('foo')).to be(true)
  380. end
  381. end
  382. context 'when given an active sub-permission' do
  383. before { create(:permission, name: 'foo.bar') }
  384. it 'returns true' do
  385. expect(user.permissions?('foo.bar')).to be(true)
  386. end
  387. end
  388. describe 'chain-of-ancestry quirk' do
  389. context 'when given an inactive sub-permission' do
  390. before { create(:permission, name: 'foo.bar.baz', active: false) }
  391. it 'returns false, even with active ancestors' do
  392. expect(user.permissions?('foo.bar.baz')).to be(false)
  393. end
  394. end
  395. context 'when given a sub-permission that does not exist' do
  396. before { create(:permission, name: 'foo.bar', active: false) }
  397. it 'can return true, even with inactive ancestors' do
  398. expect(user.permissions?('foo.bar.baz')).to be(true)
  399. end
  400. end
  401. end
  402. context 'when given a glob' do
  403. context 'matching that permission' do
  404. it 'returns true' do
  405. expect(user.permissions?('foo.*')).to be(true)
  406. end
  407. end
  408. context 'NOT matching that permission' do
  409. it 'returns false' do
  410. expect(user.permissions?('bar.*')).to be(false)
  411. end
  412. end
  413. end
  414. end
  415. context 'with privileges for a sub-permission (e.g., "foo.bar", not "foo")' do
  416. let(:permission_name) { 'foo.bar' }
  417. context 'when given that exact sub-permission' do
  418. it 'returns true' do
  419. expect(user.permissions?('foo.bar')).to be(true)
  420. end
  421. context 'but the permission is inactive' do
  422. before { permission.update(active: false) }
  423. it 'returns false' do
  424. expect(user.permissions?('foo.bar')).to be(false)
  425. end
  426. end
  427. end
  428. context 'when given a sibling sub-permission' do
  429. let(:sibling_permission) { create(:permission, name: 'foo.baz') }
  430. context 'that exists' do
  431. before { sibling_permission }
  432. it 'returns false' do
  433. expect(user.permissions?('foo.baz')).to be(false)
  434. end
  435. end
  436. context 'that does not exist' do
  437. it 'returns false' do
  438. expect(user.permissions?('foo.baz')).to be(false)
  439. end
  440. end
  441. end
  442. context 'when given the parent permission' do
  443. it 'returns false' do
  444. expect(user.permissions?('foo')).to be(false)
  445. end
  446. end
  447. context 'when given a glob' do
  448. context 'matching that sub-permission' do
  449. it 'returns true' do
  450. expect(user.permissions?('foo.*')).to be(true)
  451. end
  452. context 'but the permission is inactive' do
  453. before { permission.update(active: false) }
  454. it 'returns false' do
  455. expect(user.permissions?('foo.*')).to be(false)
  456. end
  457. end
  458. end
  459. context 'NOT matching that sub-permission' do
  460. it 'returns false' do
  461. expect(user.permissions?('bar.*')).to be(false)
  462. end
  463. end
  464. end
  465. end
  466. end
  467. describe '#permissions_with_child_ids' do
  468. context 'with privileges for a root permission (e.g., "foo", not "foo.bar")' do
  469. subject(:user) { create(:user, roles: [role]) }
  470. let(:role) { create(:role, permissions: [permission]) }
  471. let!(:permission) { create(:permission, name: 'foo') }
  472. let!(:child_permission) { create(:permission, name: 'foo.bar') }
  473. let!(:inactive_child_permission) { create(:permission, name: 'foo.baz', active: false) }
  474. it 'includes the IDs of user’s explicit permissions' do
  475. expect(user.permissions_with_child_ids)
  476. .to include(permission.id)
  477. end
  478. it 'includes the IDs of user’s active sub-permissions' do
  479. expect(user.permissions_with_child_ids)
  480. .to include(child_permission.id)
  481. .and not_include(inactive_child_permission.id)
  482. end
  483. end
  484. end
  485. describe '#locale' do
  486. subject(:user) { create(:user, preferences: preferences) }
  487. context 'with no #preferences[:locale]' do
  488. let(:preferences) { {} }
  489. context 'with default locale' do
  490. before { Setting.set('locale_default', 'foo') }
  491. it 'returns the system-wide default locale' do
  492. expect(user.locale).to eq('foo')
  493. end
  494. end
  495. context 'without default locale' do
  496. before { Setting.set('locale_default', nil) }
  497. it 'returns en-us' do
  498. expect(user.locale).to eq('en-us')
  499. end
  500. end
  501. end
  502. context 'with a #preferences[:locale]' do
  503. let(:preferences) { { locale: 'bar' } }
  504. it 'returns the user’s configured locale' do
  505. expect(user.locale).to eq('bar')
  506. end
  507. end
  508. end
  509. describe '#check_login' do
  510. let(:agent) { create(:agent) }
  511. it 'does use the origin login' do
  512. new_agent = create(:agent)
  513. expect(new_agent.login).not_to end_with('1')
  514. end
  515. it 'does number up agent logins (1)' do
  516. new_agent = create(:agent, login: agent.login)
  517. expect(new_agent.login).to eq("#{agent.login}1")
  518. end
  519. it 'does number up agent logins (5)' do
  520. new_agent = create(:agent, login: agent.login)
  521. 4.times do
  522. new_agent = create(:agent, login: agent.login)
  523. end
  524. expect(new_agent.login).to eq("#{agent.login}5")
  525. end
  526. it 'does backup with uuid in cases of many duplicates' do
  527. new_agent = create(:agent, login: agent.login)
  528. 20.times do
  529. new_agent = create(:agent, login: agent.login)
  530. end
  531. expect(new_agent.login.sub!(agent.login, '')).to be_a_uuid
  532. end
  533. end
  534. describe '#check_name' do
  535. it 'guesses user first/last name with non-ASCII characters' do
  536. user = create(:user, firstname: 'perkūnas ąžuolas', lastname: '')
  537. expect(user).to have_attributes(firstname: 'Perkūnas', lastname: 'Ąžuolas')
  538. end
  539. end
  540. end
  541. describe 'Attributes:' do
  542. describe '#out_of_office' do
  543. context 'with #out_of_office_start_at: nil' do
  544. before { agent.update(out_of_office_start_at: nil, out_of_office_end_at: Time.current) }
  545. it 'cannot be set to true' do
  546. expect { agent.update(out_of_office: true) }
  547. .to raise_error(Exceptions::UnprocessableEntity)
  548. end
  549. end
  550. context 'with #out_of_office_end_at: nil' do
  551. before { agent.update(out_of_office_start_at: Time.current, out_of_office_end_at: nil) }
  552. it 'cannot be set to true' do
  553. expect { agent.update(out_of_office: true) }
  554. .to raise_error(Exceptions::UnprocessableEntity)
  555. end
  556. end
  557. context 'when #out_of_office_start_at is AFTER #out_of_office_end_at' do
  558. before { agent.update(out_of_office_start_at: Time.current.tomorrow, out_of_office_end_at: Time.current.next_month) }
  559. it 'cannot be set to true' do
  560. expect { agent.update(out_of_office: true) }
  561. .to raise_error(Exceptions::UnprocessableEntity)
  562. end
  563. end
  564. context 'when #out_of_office_start_at is AFTER Time.current' do
  565. before { agent.update(out_of_office_start_at: Time.current.tomorrow, out_of_office_end_at: Time.current.yesterday) }
  566. it 'cannot be set to true' do
  567. expect { agent.update(out_of_office: true) }
  568. .to raise_error(Exceptions::UnprocessableEntity)
  569. end
  570. end
  571. context 'when #out_of_office_end_at is BEFORE Time.current' do
  572. before { agent.update(out_of_office_start_at: Time.current.last_month, out_of_office_end_at: Time.current.yesterday) }
  573. it 'cannot be set to true' do
  574. expect { agent.update(out_of_office: true) }
  575. .to raise_error(Exceptions::UnprocessableEntity)
  576. end
  577. end
  578. end
  579. describe '#out_of_office_replacement_id' do
  580. it 'cannot be set to invalid user ID' do
  581. expect { agent.update(out_of_office_replacement_id: described_class.pluck(:id).max.next) }
  582. .to raise_error(ActiveRecord::InvalidForeignKey)
  583. end
  584. it 'can be set to a valid user ID' do
  585. expect { agent.update(out_of_office_replacement_id: 1) }
  586. .not_to raise_error
  587. end
  588. end
  589. describe '#login_failed' do
  590. before { user.update(login_failed: 1) }
  591. it 'is reset to 0 when password is updated' do
  592. expect { user.update(password: Faker::Internet.password) }
  593. .to change(user, :login_failed).to(0)
  594. end
  595. end
  596. describe '#password' do
  597. let(:password) { Faker::Internet.password }
  598. context 'when set to plaintext password' do
  599. it 'hashes password before saving to DB' do
  600. user.password = password
  601. expect { user.save }
  602. .to change { PasswordHash.crypted?(user.password) }
  603. end
  604. end
  605. context 'for existing user records' do
  606. context 'when changed to empty string' do
  607. before { user.update(password: password) }
  608. it 'keeps previous password' do
  609. expect { user.update!(password: '') }
  610. .not_to change(user, :password)
  611. end
  612. end
  613. context 'when changed to nil' do
  614. before { user.update(password: password) }
  615. it 'keeps previous password' do
  616. expect { user.update!(password: nil) }
  617. .not_to change(user, :password)
  618. end
  619. end
  620. end
  621. context 'for new user records' do
  622. context 'when passed as an empty string' do
  623. let(:another_user) { create(:user, password: '') }
  624. it 'sets password to nil' do
  625. expect(another_user.password).to be_nil
  626. end
  627. end
  628. context 'when passed as nil' do
  629. let(:another_user) { create(:user, password: nil) }
  630. it 'sets password to nil' do
  631. expect(another_user.password).to be_nil
  632. end
  633. end
  634. end
  635. context 'when set to SHA2 digest (to facilitate OTRS imports)' do
  636. it 'does not re-hash before saving' do
  637. user.password = "{sha2}#{Digest::SHA2.hexdigest(password)}"
  638. expect { user.save }.not_to change(user, :password)
  639. end
  640. end
  641. context 'when set to Argon2 digest' do
  642. it 'does not re-hash before saving' do
  643. user.password = PasswordHash.crypt(password)
  644. expect { user.save }.not_to change(user, :password)
  645. end
  646. end
  647. context 'when creating two users with the same password' do
  648. before { user.update(password: password) }
  649. let(:another_user) { create(:user, password: password) }
  650. it 'does not generate the same password hash' do
  651. expect(user.password).not_to eq(another_user.password)
  652. end
  653. end
  654. context 'when saving a very long password' do
  655. let(:long_string) { "asd1ASDasd!#{Faker::Lorem.characters(number: 1_000)}" }
  656. it 'marks object as invalid by adding error' do
  657. user.update(password: long_string)
  658. expect(user.errors.full_messages).to eq([['Invalid password, it must be shorter than %s characters!', 1000]])
  659. end
  660. end
  661. end
  662. describe '#phone' do
  663. subject(:user) { create(:user, phone: orig_number) }
  664. context 'when included on create' do
  665. let(:orig_number) { '1234567890' }
  666. it 'adds corresponding CallerId record' do
  667. expect { user }
  668. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(1)
  669. end
  670. end
  671. context 'when added on update' do
  672. let(:orig_number) { nil }
  673. let(:new_number) { '1234567890' }
  674. before { user } # create user
  675. it 'adds corresponding CallerId record' do
  676. expect { user.update(phone: new_number) }
  677. .to change { Cti::CallerId.where(caller_id: new_number).count }.by(1)
  678. end
  679. end
  680. context 'when falsely added on update (change: [nil, ""])' do
  681. let(:orig_number) { nil }
  682. let(:new_number) { '' }
  683. before { user } # create user
  684. it 'does not attempt to update CallerId record' do
  685. allow(Cti::CallerId).to receive(:build).with(any_args)
  686. expect(Cti::CallerId.where(object: 'User', o_id: user.id).count)
  687. .to eq(0)
  688. expect { user.update(phone: new_number) }
  689. .to change { Cti::CallerId.where(object: 'User', o_id: user.id).count }.by(0)
  690. expect(Cti::CallerId).not_to have_received(:build)
  691. end
  692. end
  693. context 'when removed on update' do
  694. let(:orig_number) { '1234567890' }
  695. let(:new_number) { nil }
  696. before { user } # create user
  697. it 'removes corresponding CallerId record' do
  698. expect { user.update(phone: nil) }
  699. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(-1)
  700. end
  701. end
  702. context 'when changed on update' do
  703. let(:orig_number) { '1234567890' }
  704. let(:new_number) { orig_number.next }
  705. before { user } # create user
  706. it 'replaces CallerId record' do
  707. expect { user.update(phone: new_number) }
  708. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(-1)
  709. .and change { Cti::CallerId.where(caller_id: new_number).count }.by(1)
  710. end
  711. end
  712. end
  713. describe '#preferences' do
  714. describe '"mail_delivery_failed{,_data}" keys' do
  715. before do
  716. user.update(
  717. preferences: {
  718. mail_delivery_failed: true,
  719. mail_delivery_failed_data: Time.current
  720. }
  721. )
  722. end
  723. it 'deletes "mail_delivery_failed"' do
  724. expect { user.update(email: Faker::Internet.email) }
  725. .to change { user.preferences.key?(:mail_delivery_failed) }.to(false)
  726. end
  727. it 'leaves "mail_delivery_failed_data" untouched' do
  728. expect { user.update(email: Faker::Internet.email) }
  729. .to not_change { user.preferences[:mail_delivery_failed_data] }
  730. end
  731. end
  732. end
  733. describe '#image' do
  734. describe 'when value is invalid' do
  735. let(:value) { 'Th1515n0t4v4l1dh45h' }
  736. it 'prevents create' do
  737. expect { create(:user, image: value) }.to raise_error(Exceptions::UnprocessableEntity, %r{#{value}})
  738. end
  739. it 'prevents update' do
  740. expect { create(:user).update!(image: value) }.to raise_error(Exceptions::UnprocessableEntity, %r{#{value}})
  741. end
  742. end
  743. end
  744. describe '#image_source' do
  745. describe 'when value is invalid' do
  746. let(:value) { 'Th1515n0t4v4l1dh45h' }
  747. let(:escaped) { Regexp.escape(value) }
  748. it 'valid create' do
  749. expect(create(:user, image_source: 'https://zammad.org/avatar.png').image_source).not_to be_nil
  750. end
  751. it 'removes invalid image source of create' do
  752. expect(create(:user, image_source: value).image_source).to be_nil
  753. end
  754. it 'removes invalid image source of update' do
  755. user = create(:user)
  756. user.update!(image_source: value)
  757. expect(user.image_source).to be_nil
  758. end
  759. end
  760. end
  761. end
  762. describe 'Associations:' do
  763. subject(:user) { create(:agent, groups: [group_subject]) }
  764. let!(:group_subject) { create(:group) }
  765. it 'does remove references before destroy' do
  766. refs_known = { 'Group' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  767. 'Token' => { 'user_id' => 1 },
  768. 'Ticket::Article' =>
  769. { 'created_by_id' => 1, 'updated_by_id' => 1, 'origin_by_id' => 1 },
  770. 'Ticket::StateType' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  771. 'Ticket::Article::Sender' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  772. 'Ticket::Article::Type' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  773. 'Ticket::Article::Flag' => { 'created_by_id' => 0 },
  774. 'Ticket::Priority' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  775. 'Ticket::SharedDraftStart' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  776. 'Ticket::SharedDraftZoom' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  777. 'Ticket::TimeAccounting' => { 'created_by_id' => 0 },
  778. 'Ticket::State' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  779. 'Ticket::Flag' => { 'created_by_id' => 0 },
  780. 'PostmasterFilter' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  781. 'OnlineNotification' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 },
  782. 'Ticket' =>
  783. { 'created_by_id' => 0, 'updated_by_id' => 0, 'owner_id' => 1, 'customer_id' => 3 },
  784. 'Template' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  785. 'Avatar' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  786. 'Scheduler' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  787. 'Chat' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  788. 'HttpLog' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  789. 'EmailAddress' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  790. 'Taskbar' => { 'user_id' => 1 },
  791. 'Sla' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  792. 'UserDevice' => { 'user_id' => 1 },
  793. 'Chat::Message' => { 'created_by_id' => 1 },
  794. 'Chat::Agent' => { 'created_by_id' => 1, 'updated_by_id' => 1 },
  795. 'Chat::Session' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 },
  796. 'Tag' => { 'created_by_id' => 0 },
  797. 'Karma::User' => { 'user_id' => 0 },
  798. 'Karma::ActivityLog' => { 'user_id' => 1 },
  799. 'RecentView' => { 'created_by_id' => 1 },
  800. 'KnowledgeBase::Answer::Translation' =>
  801. { 'created_by_id' => 0, 'updated_by_id' => 0 },
  802. 'KnowledgeBase::Answer' =>
  803. { 'archived_by_id' => 1, 'published_by_id' => 1, 'internal_by_id' => 1 },
  804. 'Report::Profile' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  805. 'Package' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  806. 'Job' => { 'created_by_id' => 0, 'updated_by_id' => 1 },
  807. 'Store' => { 'created_by_id' => 0 },
  808. 'Cti::CallerId' => { 'user_id' => 1 },
  809. 'DataPrivacyTask' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  810. 'Trigger' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  811. 'Translation' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  812. 'ObjectManager::Attribute' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  813. 'User' => { 'created_by_id' => 2, 'out_of_office_replacement_id' => 1, 'updated_by_id' => 2 },
  814. 'Organization' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  815. 'Macro' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  816. 'CoreWorkflow' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  817. 'Mention' => { 'created_by_id' => 1, 'updated_by_id' => 0, 'user_id' => 1 },
  818. 'Channel' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  819. 'Role' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  820. 'History' => { 'created_by_id' => 6 },
  821. 'Webhook' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  822. 'Overview' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  823. 'ActivityStream' => { 'created_by_id' => 0 },
  824. 'StatsStore' => { 'created_by_id' => 0 },
  825. 'TextModule' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  826. 'Calendar' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  827. 'UserGroup' => { 'user_id' => 1 },
  828. 'Signature' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  829. 'Authorization' => { 'user_id' => 1 } }
  830. # delete objects
  831. token = create(:token, user: user)
  832. online_notification = create(:online_notification, user: user)
  833. taskbar = create(:taskbar, user: user)
  834. user_device = create(:user_device, user: user)
  835. karma_activity_log = create(:karma_activity_log, user: user)
  836. cti_caller_id = create(:cti_caller_id, user: user)
  837. authorization = create(:twitter_authorization, user: user)
  838. recent_view = create(:recent_view, created_by: user)
  839. avatar = create(:avatar, o_id: user.id)
  840. overview = create(:overview, created_by_id: user.id, user_ids: [user.id])
  841. mention = create(:mention, mentionable: create(:ticket), user: user)
  842. mention_created_by = create(:mention, mentionable: create(:ticket), user: create(:agent), created_by: user)
  843. user_created_by = create(:customer, created_by_id: user.id, updated_by_id: user.id, out_of_office_replacement_id: user.id)
  844. chat_session = create(:'chat/session', user: user)
  845. chat_message = create(:'chat/message', chat_session: chat_session)
  846. chat_message2 = create(:'chat/message', chat_session: chat_session, created_by: user)
  847. draft_start = create(:ticket_shared_draft_start, created_by: user)
  848. draft_zoom = create(:ticket_shared_draft_zoom, created_by: user)
  849. expect(overview.reload.user_ids).to eq([user.id])
  850. # create a chat agent for admin user (id=1) before agent user
  851. # to be sure that the data gets removed and not mapped which
  852. # would result in a foreign key because of the unique key on the
  853. # created_by_id and updated_by_id.
  854. create(:'chat/agent')
  855. chat_agent_user = create(:'chat/agent', created_by_id: user.id, updated_by_id: user.id)
  856. # invalid user (by email) which has been updated by the user which
  857. # will get deleted (#3935)
  858. invalid_user = build(:user, email: 'abc', created_by_id: user.id, updated_by_id: user.id)
  859. invalid_user.save!(validate: false)
  860. # move ownership objects
  861. group = create(:group, created_by_id: user.id)
  862. job = create(:job, updated_by_id: user.id)
  863. ticket = create(:ticket, group: group_subject, owner: user)
  864. ticket_article = create(:ticket_article, ticket: ticket, created_by_id: user.id, updated_by_id: user.id, origin_by_id: user.id)
  865. customer_ticket1 = create(:ticket, group: group_subject, customer: user)
  866. customer_ticket2 = create(:ticket, group: group_subject, customer: user)
  867. customer_ticket3 = create(:ticket, group: group_subject, customer: user)
  868. knowledge_base_answer = create(:knowledge_base_answer, archived_by_id: user.id, published_by_id: user.id, internal_by_id: user.id)
  869. refs_user = Models.references('User', user.id, true)
  870. expect(refs_user).to eq(refs_known)
  871. user.destroy
  872. expect { token.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  873. expect { online_notification.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  874. expect { taskbar.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  875. expect { user_device.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  876. expect { karma_activity_log.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  877. expect { cti_caller_id.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  878. expect { authorization.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  879. expect { recent_view.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  880. expect { avatar.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  881. expect { customer_ticket1.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  882. expect { customer_ticket2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  883. expect { customer_ticket3.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  884. expect { chat_agent_user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  885. expect { mention.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  886. expect(mention_created_by.reload.created_by_id).not_to eq(user.id)
  887. expect(overview.reload.user_ids).to eq([])
  888. expect { chat_session.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  889. expect { chat_message.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  890. expect { chat_message2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  891. # move ownership objects
  892. expect { group.reload }.to change(group, :created_by_id).to(1)
  893. expect { job.reload }.to change(job, :updated_by_id).to(1)
  894. expect { ticket.reload }.to change(ticket, :owner_id).to(1)
  895. expect { ticket_article.reload }
  896. .to change(ticket_article, :origin_by_id).to(1)
  897. .and change(ticket_article, :updated_by_id).to(1)
  898. .and change(ticket_article, :created_by_id).to(1)
  899. expect { knowledge_base_answer.reload }
  900. .to change(knowledge_base_answer, :archived_by_id).to(1)
  901. .and change(knowledge_base_answer, :published_by_id).to(1)
  902. .and change(knowledge_base_answer, :internal_by_id).to(1)
  903. expect { user_created_by.reload }
  904. .to change(user_created_by, :created_by_id).to(1)
  905. .and change(user_created_by, :updated_by_id).to(1)
  906. .and change(user_created_by, :out_of_office_replacement_id).to(1)
  907. expect { draft_start.reload }.to change(draft_start, :created_by_id).to(1)
  908. expect { draft_zoom.reload }.to change(draft_zoom, :created_by_id).to(1)
  909. expect { invalid_user.reload }.to change(invalid_user, :created_by_id).to(1)
  910. end
  911. it 'does delete cache after user deletion' do
  912. online_notification = create(:online_notification, created_by_id: user.id)
  913. online_notification.attributes_with_association_ids
  914. user.destroy
  915. expect(online_notification.reload.attributes_with_association_ids['created_by_id']).to eq(1)
  916. end
  917. it 'does return an exception on blocking dependencies' do
  918. expect { user.send(:destroy_move_dependency_ownership) }.to raise_error(RuntimeError, 'Failed deleting references! Check logic for UserGroup->user_id.')
  919. end
  920. describe '#organization' do
  921. describe 'email domain-based assignment' do
  922. subject(:user) { build(:user) }
  923. context 'when not set on creation' do
  924. before { user.assign_attributes(organization: nil) }
  925. context 'and #email domain matches an existing Organization#domain' do
  926. before { user.assign_attributes(email: 'user@example.com') }
  927. let(:organization) { create(:organization, domain: 'example.com') }
  928. context 'and Organization#domain_assignment is false (default)' do
  929. before { organization.update(domain_assignment: false) }
  930. it 'remains nil' do
  931. expect { user.save }.not_to change(user, :organization)
  932. end
  933. end
  934. context 'and Organization#domain_assignment is true' do
  935. before { organization.update(domain_assignment: true) }
  936. it 'is automatically set to matching Organization' do
  937. expect { user.save }
  938. .to change(user, :organization).to(organization)
  939. end
  940. end
  941. end
  942. context 'and #email domain doesn’t match any Organization#domain' do
  943. before { user.assign_attributes(email: 'user@example.net') }
  944. let(:organization) { create(:organization, domain: 'example.com') }
  945. context 'and Organization#domain_assignment is true' do
  946. before { organization.update(domain_assignment: true) }
  947. it 'remains nil' do
  948. expect { user.save }.not_to change(user, :organization)
  949. end
  950. end
  951. end
  952. end
  953. context 'when set on creation' do
  954. before { user.assign_attributes(organization: specified_organization) }
  955. let(:specified_organization) { create(:organization, domain: 'example.net') }
  956. context 'and #email domain matches a DIFFERENT Organization#domain' do
  957. before { user.assign_attributes(email: 'user@example.com') }
  958. let!(:matching_organization) { create(:organization, domain: 'example.com') }
  959. context 'and Organization#domain_assignment is true' do
  960. before { matching_organization.update(domain_assignment: true) }
  961. it 'is NOT automatically set to matching Organization' do
  962. expect { user.save }
  963. .not_to change(user, :organization).from(specified_organization)
  964. end
  965. end
  966. end
  967. end
  968. end
  969. end
  970. end
  971. describe 'Callbacks, Observers, & Async Transactions -' do
  972. describe 'System-wide agent limit checks:' do
  973. let(:agent_role) { Role.lookup(name: 'Agent') }
  974. let(:admin_role) { Role.lookup(name: 'Admin') }
  975. let(:current_agents) { described_class.with_permissions('ticket.agent') }
  976. describe '#validate_agent_limit_by_role' do
  977. context 'for Integer value of system_agent_limit' do
  978. context 'before exceeding the agent limit' do
  979. before { Setting.set('system_agent_limit', current_agents.count + 1) }
  980. it 'grants agent creation' do
  981. expect { create(:agent) }
  982. .to change(current_agents, :count).by(1)
  983. end
  984. it 'grants role change' do
  985. future_agent = create(:customer)
  986. expect { future_agent.roles = [agent_role] }
  987. .to change(current_agents, :count).by(1)
  988. end
  989. describe 'role updates' do
  990. let(:agent) { create(:agent) }
  991. it 'grants update by instances' do
  992. expect { agent.roles = [admin_role, agent_role] }
  993. .not_to raise_error
  994. end
  995. it 'grants update by id (Integer)' do
  996. expect { agent.role_ids = [admin_role.id, agent_role.id] }
  997. .not_to raise_error
  998. end
  999. it 'grants update by id (String)' do
  1000. expect { agent.role_ids = [admin_role.id.to_s, agent_role.id.to_s] }
  1001. .not_to raise_error
  1002. end
  1003. end
  1004. end
  1005. context 'when exceeding the agent limit' do
  1006. it 'creation of new agents' do
  1007. Setting.set('system_agent_limit', current_agents.count + 2)
  1008. create_list(:agent, 2)
  1009. expect { create(:agent) }
  1010. .to raise_error(Exceptions::UnprocessableEntity)
  1011. .and change(current_agents, :count).by(0)
  1012. end
  1013. it 'prevents role change' do
  1014. Setting.set('system_agent_limit', current_agents.count)
  1015. future_agent = create(:customer)
  1016. expect { future_agent.roles = [agent_role] }
  1017. .to raise_error(Exceptions::UnprocessableEntity)
  1018. .and change(current_agents, :count).by(0)
  1019. end
  1020. end
  1021. end
  1022. context 'for String value of system_agent_limit' do
  1023. context 'before exceeding the agent limit' do
  1024. before { Setting.set('system_agent_limit', (current_agents.count + 1).to_s) }
  1025. it 'grants agent creation' do
  1026. expect { create(:agent) }
  1027. .to change(current_agents, :count).by(1)
  1028. end
  1029. it 'grants role change' do
  1030. future_agent = create(:customer)
  1031. expect { future_agent.roles = [agent_role] }
  1032. .to change(current_agents, :count).by(1)
  1033. end
  1034. describe 'role updates' do
  1035. let(:agent) { create(:agent) }
  1036. it 'grants update by instances' do
  1037. expect { agent.roles = [admin_role, agent_role] }
  1038. .not_to raise_error
  1039. end
  1040. it 'grants update by id (Integer)' do
  1041. expect { agent.role_ids = [admin_role.id, agent_role.id] }
  1042. .not_to raise_error
  1043. end
  1044. it 'grants update by id (String)' do
  1045. expect { agent.role_ids = [admin_role.id.to_s, agent_role.id.to_s] }
  1046. .not_to raise_error
  1047. end
  1048. end
  1049. end
  1050. context 'when exceeding the agent limit' do
  1051. it 'creation of new agents' do
  1052. Setting.set('system_agent_limit', (current_agents.count + 2).to_s)
  1053. create_list(:agent, 2)
  1054. expect { create(:agent) }
  1055. .to raise_error(Exceptions::UnprocessableEntity)
  1056. .and change(current_agents, :count).by(0)
  1057. end
  1058. it 'prevents role change' do
  1059. Setting.set('system_agent_limit', current_agents.count.to_s)
  1060. future_agent = create(:customer)
  1061. expect { future_agent.roles = [agent_role] }
  1062. .to raise_error(Exceptions::UnprocessableEntity)
  1063. .and change(current_agents, :count).by(0)
  1064. end
  1065. end
  1066. end
  1067. end
  1068. describe '#validate_agent_limit_by_attributes' do
  1069. context 'for Integer value of system_agent_limit' do
  1070. before { Setting.set('system_agent_limit', current_agents.count) }
  1071. context 'when exceeding the agent limit' do
  1072. it 'prevents re-activation of agents' do
  1073. inactive_agent = create(:agent, active: false)
  1074. expect { inactive_agent.update!(active: true) }
  1075. .to raise_error(Exceptions::UnprocessableEntity)
  1076. .and change(current_agents, :count).by(0)
  1077. end
  1078. end
  1079. end
  1080. context 'for String value of system_agent_limit' do
  1081. before { Setting.set('system_agent_limit', current_agents.count.to_s) }
  1082. context 'when exceeding the agent limit' do
  1083. it 'prevents re-activation of agents' do
  1084. inactive_agent = create(:agent, active: false)
  1085. expect { inactive_agent.update!(active: true) }
  1086. .to raise_error(Exceptions::UnprocessableEntity)
  1087. .and change(current_agents, :count).by(0)
  1088. end
  1089. end
  1090. end
  1091. end
  1092. end
  1093. describe 'Touching associations on update:' do
  1094. subject!(:user) { create(:customer) }
  1095. let!(:organization) { create(:organization) }
  1096. context 'when a customer gets a organization' do
  1097. it 'touches its organization' do
  1098. expect { user.update(organization: organization) }
  1099. .to change { organization.reload.updated_at }
  1100. end
  1101. end
  1102. end
  1103. describe 'Cti::CallerId syncing:' do
  1104. context 'with a #phone attribute' do
  1105. subject(:user) { build(:user, phone: '1234567890') }
  1106. it 'adds CallerId record on creation (via Cti::CallerId.build)' do
  1107. expect(Cti::CallerId).to receive(:build).with(user)
  1108. user.save
  1109. end
  1110. it 'does not update CallerId record on touch/update (via Cti::CallerId.build)' do
  1111. expect(Cti::CallerId).to receive(:build).with(user)
  1112. user.save
  1113. expect(Cti::CallerId).not_to receive(:build).with(user)
  1114. user.touch
  1115. end
  1116. it 'destroys CallerId record on deletion' do
  1117. user.save
  1118. expect { user.destroy }
  1119. .to change { Cti::CallerId.count }.by(-1)
  1120. end
  1121. end
  1122. end
  1123. describe 'Cti::Log syncing:' do
  1124. context 'with existing Log records' do
  1125. context 'for incoming calls from an unknown number' do
  1126. let!(:log) { create(:'cti/log', :with_preferences, from: '1234567890', direction: 'in') }
  1127. context 'when creating a new user with that number' do
  1128. subject(:user) { build(:user, phone: log.from) }
  1129. it 'populates #preferences[:from] hash in all associated Log records (in a bg job)' do
  1130. expect do
  1131. user.save
  1132. TransactionDispatcher.commit
  1133. Scheduler.worker(true)
  1134. end.to change { log.reload.preferences[:from]&.first }
  1135. .to(hash_including('caller_id' => user.phone))
  1136. end
  1137. end
  1138. context 'when updating a user with that number' do
  1139. subject(:user) { create(:user) }
  1140. it 'populates #preferences[:from] hash in all associated Log records (in a bg job)' do
  1141. expect do
  1142. user.update(phone: log.from)
  1143. TransactionDispatcher.commit
  1144. Scheduler.worker(true)
  1145. end.to change { log.reload.preferences[:from]&.first }
  1146. .to(hash_including('object' => 'User', 'o_id' => user.id))
  1147. end
  1148. end
  1149. context 'when creating a new user with an empty number' do
  1150. subject(:user) { build(:user, phone: '') }
  1151. it 'does not modify any Log records' do
  1152. expect do
  1153. user.save
  1154. TransactionDispatcher.commit
  1155. Scheduler.worker(true)
  1156. end.not_to change { log.reload.attributes }
  1157. end
  1158. end
  1159. context 'when creating a new user with no number' do
  1160. subject(:user) { build(:user, phone: nil) }
  1161. it 'does not modify any Log records' do
  1162. expect do
  1163. user.save
  1164. TransactionDispatcher.commit
  1165. Scheduler.worker(true)
  1166. end.not_to change { log.reload.attributes }
  1167. end
  1168. end
  1169. end
  1170. context 'for incoming calls from the given user' do
  1171. subject(:user) { create(:user, phone: '1234567890') }
  1172. let!(:logs) { create_list(:'cti/log', 5, :with_preferences, from: user.phone, direction: 'in') }
  1173. context 'when updating #phone attribute' do
  1174. context 'to another number' do
  1175. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1176. expect do
  1177. user.update(phone: '0123456789')
  1178. TransactionDispatcher.commit
  1179. Scheduler.worker(true)
  1180. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1181. .to(Array.new(5) { nil })
  1182. end
  1183. end
  1184. context 'to an empty string' do
  1185. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1186. expect do
  1187. user.update(phone: '')
  1188. TransactionDispatcher.commit
  1189. Scheduler.worker(true)
  1190. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1191. .to(Array.new(5) { nil })
  1192. end
  1193. end
  1194. context 'to nil' do
  1195. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1196. expect do
  1197. user.update(phone: nil)
  1198. TransactionDispatcher.commit
  1199. Scheduler.worker(true)
  1200. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1201. .to(Array.new(5) { nil })
  1202. end
  1203. end
  1204. end
  1205. context 'when updating attributes other than #phone' do
  1206. it 'does not modify any Log records' do
  1207. expect do
  1208. user.update(mobile: '2345678901')
  1209. TransactionDispatcher.commit
  1210. Scheduler.worker(true)
  1211. end.not_to change { logs.map(&:reload).map(&:attributes) }
  1212. end
  1213. end
  1214. end
  1215. end
  1216. end
  1217. end
  1218. end