user_spec.rb 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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 eq(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 eq(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 eq(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. end
  655. describe '#phone' do
  656. subject(:user) { create(:user, phone: orig_number) }
  657. context 'when included on create' do
  658. let(:orig_number) { '1234567890' }
  659. it 'adds corresponding CallerId record' do
  660. expect { user }
  661. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(1)
  662. end
  663. end
  664. context 'when added on update' do
  665. let(:orig_number) { nil }
  666. let(:new_number) { '1234567890' }
  667. before { user } # create user
  668. it 'adds corresponding CallerId record' do
  669. expect { user.update(phone: new_number) }
  670. .to change { Cti::CallerId.where(caller_id: new_number).count }.by(1)
  671. end
  672. end
  673. context 'when falsely added on update (change: [nil, ""])' do
  674. let(:orig_number) { nil }
  675. let(:new_number) { '' }
  676. before { user } # create user
  677. it 'does not attempt to update CallerId record' do
  678. allow(Cti::CallerId).to receive(:build).with(any_args)
  679. expect(Cti::CallerId.where(object: 'User', o_id: user.id).count)
  680. .to eq(0)
  681. expect { user.update(phone: new_number) }
  682. .to change { Cti::CallerId.where(object: 'User', o_id: user.id).count }.by(0)
  683. expect(Cti::CallerId).not_to have_received(:build)
  684. end
  685. end
  686. context 'when removed on update' do
  687. let(:orig_number) { '1234567890' }
  688. let(:new_number) { nil }
  689. before { user } # create user
  690. it 'removes corresponding CallerId record' do
  691. expect { user.update(phone: nil) }
  692. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(-1)
  693. end
  694. end
  695. context 'when changed on update' do
  696. let(:orig_number) { '1234567890' }
  697. let(:new_number) { orig_number.next }
  698. before { user } # create user
  699. it 'replaces CallerId record' do
  700. expect { user.update(phone: new_number) }
  701. .to change { Cti::CallerId.where(caller_id: orig_number).count }.by(-1)
  702. .and change { Cti::CallerId.where(caller_id: new_number).count }.by(1)
  703. end
  704. end
  705. end
  706. describe '#preferences' do
  707. describe '"mail_delivery_failed{,_data}" keys' do
  708. before do
  709. user.update(
  710. preferences: {
  711. mail_delivery_failed: true,
  712. mail_delivery_failed_data: Time.current
  713. }
  714. )
  715. end
  716. it 'deletes "mail_delivery_failed"' do
  717. expect { user.update(email: Faker::Internet.email) }
  718. .to change { user.preferences.key?(:mail_delivery_failed) }.to(false)
  719. end
  720. it 'leaves "mail_delivery_failed_data" untouched' do
  721. expect { user.update(email: Faker::Internet.email) }
  722. .to not_change { user.preferences[:mail_delivery_failed_data] }
  723. end
  724. end
  725. end
  726. describe '#image' do
  727. describe 'when value is invalid' do
  728. let(:value) { 'Th1515n0t4v4l1dh45h' }
  729. it 'prevents create' do
  730. expect { create(:user, image: value) }.to raise_error(Exceptions::UnprocessableEntity, %r{#{value}})
  731. end
  732. it 'prevents update' do
  733. expect { create(:user).update!(image: value) }.to raise_error(Exceptions::UnprocessableEntity, %r{#{value}})
  734. end
  735. end
  736. end
  737. describe '#image_source' do
  738. describe 'when value is invalid' do
  739. let(:value) { 'Th1515n0t4v4l1dh45h' }
  740. let(:escaped) { Regexp.escape(value) }
  741. it 'valid create' do
  742. expect(create(:user, image_source: 'https://zammad.org/avatar.png').image_source).not_to eq(nil)
  743. end
  744. it 'removes invalid image source of create' do
  745. expect(create(:user, image_source: value).image_source).to eq(nil)
  746. end
  747. it 'removes invalid image source of update' do
  748. user = create(:user)
  749. user.update!(image_source: value)
  750. expect(user.image_source).to eq(nil)
  751. end
  752. end
  753. end
  754. end
  755. describe 'Associations:' do
  756. subject(:user) { create(:agent, groups: [group_subject]) }
  757. let!(:group_subject) { create(:group) }
  758. it 'does remove references before destroy' do
  759. refs_known = { 'Group' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  760. 'Token' => { 'user_id' => 1 },
  761. 'Ticket::Article' =>
  762. { 'created_by_id' => 1, 'updated_by_id' => 1, 'origin_by_id' => 1 },
  763. 'Ticket::StateType' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  764. 'Ticket::Article::Sender' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  765. 'Ticket::Article::Type' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  766. 'Ticket::Article::Flag' => { 'created_by_id' => 0 },
  767. 'Ticket::Priority' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  768. 'Ticket::TimeAccounting' => { 'created_by_id' => 0 },
  769. 'Ticket::State' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  770. 'Ticket::Flag' => { 'created_by_id' => 0 },
  771. 'PostmasterFilter' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  772. 'OnlineNotification' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 },
  773. 'Ticket' =>
  774. { 'created_by_id' => 0, 'updated_by_id' => 0, 'owner_id' => 1, 'customer_id' => 3 },
  775. 'Template' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  776. 'Avatar' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  777. 'Scheduler' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  778. 'Chat' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  779. 'HttpLog' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  780. 'EmailAddress' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  781. 'Taskbar' => { 'user_id' => 1 },
  782. 'Sla' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  783. 'UserDevice' => { 'user_id' => 1 },
  784. 'Chat::Message' => { 'created_by_id' => 1 },
  785. 'Chat::Agent' => { 'created_by_id' => 1, 'updated_by_id' => 1 },
  786. 'Chat::Session' => { 'user_id' => 1, 'created_by_id' => 0, 'updated_by_id' => 0 },
  787. 'Tag' => { 'created_by_id' => 0 },
  788. 'Karma::User' => { 'user_id' => 0 },
  789. 'Karma::ActivityLog' => { 'user_id' => 1 },
  790. 'RecentView' => { 'created_by_id' => 1 },
  791. 'KnowledgeBase::Answer::Translation' =>
  792. { 'created_by_id' => 0, 'updated_by_id' => 0 },
  793. 'KnowledgeBase::Answer' =>
  794. { 'archived_by_id' => 1, 'published_by_id' => 1, 'internal_by_id' => 1 },
  795. 'Report::Profile' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  796. 'Package' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  797. 'Job' => { 'created_by_id' => 0, 'updated_by_id' => 1 },
  798. 'Store' => { 'created_by_id' => 0 },
  799. 'Cti::CallerId' => { 'user_id' => 1 },
  800. 'DataPrivacyTask' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  801. 'Trigger' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  802. 'Translation' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  803. 'ObjectManager::Attribute' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  804. 'User' => { 'created_by_id' => 1, 'out_of_office_replacement_id' => 1, 'updated_by_id' => 1 },
  805. 'Organization' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  806. 'Macro' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  807. 'CoreWorkflow' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  808. 'Mention' => { 'created_by_id' => 1, 'updated_by_id' => 0, 'user_id' => 1 },
  809. 'Channel' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  810. 'Role' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  811. 'History' => { 'created_by_id' => 4 },
  812. 'Webhook' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  813. 'Overview' => { 'created_by_id' => 1, 'updated_by_id' => 0 },
  814. 'ActivityStream' => { 'created_by_id' => 0 },
  815. 'StatsStore' => { 'created_by_id' => 0 },
  816. 'TextModule' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  817. 'Calendar' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  818. 'UserGroup' => { 'user_id' => 1 },
  819. 'Signature' => { 'created_by_id' => 0, 'updated_by_id' => 0 },
  820. 'Authorization' => { 'user_id' => 1 } }
  821. # delete objects
  822. token = create(:token, user: user)
  823. online_notification = create(:online_notification, user: user)
  824. taskbar = create(:taskbar, user: user)
  825. user_device = create(:user_device, user: user)
  826. karma_activity_log = create(:karma_activity_log, user: user)
  827. cti_caller_id = create(:cti_caller_id, user: user)
  828. authorization = create(:twitter_authorization, user: user)
  829. recent_view = create(:recent_view, created_by: user)
  830. avatar = create(:avatar, o_id: user.id)
  831. overview = create(:overview, created_by_id: user.id, user_ids: [user.id])
  832. mention = create(:mention, mentionable: create(:ticket), user: user)
  833. mention_created_by = create(:mention, mentionable: create(:ticket), user: create(:agent), created_by: user)
  834. user_created_by = create(:customer, created_by_id: user.id, updated_by_id: user.id, out_of_office_replacement_id: user.id)
  835. chat_session = create(:'chat/session', user: user)
  836. chat_message = create(:'chat/message', chat_session: chat_session)
  837. chat_message2 = create(:'chat/message', chat_session: chat_session, created_by: user)
  838. expect(overview.reload.user_ids).to eq([user.id])
  839. # create a chat agent for admin user (id=1) before agent user
  840. # to be sure that the data gets removed and not mapped which
  841. # would result in a foreign key because of the unique key on the
  842. # created_by_id and updated_by_id.
  843. create(:'chat/agent')
  844. chat_agent_user = create(:'chat/agent', created_by_id: user.id, updated_by_id: user.id)
  845. # move ownership objects
  846. group = create(:group, created_by_id: user.id)
  847. job = create(:job, updated_by_id: user.id)
  848. ticket = create(:ticket, group: group_subject, owner: user)
  849. ticket_article = create(:ticket_article, ticket: ticket, created_by_id: user.id, updated_by_id: user.id, origin_by_id: user.id)
  850. customer_ticket1 = create(:ticket, group: group_subject, customer: user)
  851. customer_ticket2 = create(:ticket, group: group_subject, customer: user)
  852. customer_ticket3 = create(:ticket, group: group_subject, customer: user)
  853. knowledge_base_answer = create(:knowledge_base_answer, archived_by_id: user.id, published_by_id: user.id, internal_by_id: user.id)
  854. refs_user = Models.references('User', user.id, true)
  855. expect(refs_user).to eq(refs_known)
  856. user.destroy
  857. expect { token.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  858. expect { online_notification.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  859. expect { taskbar.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  860. expect { user_device.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  861. expect { karma_activity_log.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  862. expect { cti_caller_id.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  863. expect { authorization.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  864. expect { recent_view.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  865. expect { avatar.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  866. expect { customer_ticket1.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  867. expect { customer_ticket2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  868. expect { customer_ticket3.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  869. expect { chat_agent_user.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  870. expect { mention.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  871. expect(mention_created_by.reload.created_by_id).not_to eq(user.id)
  872. expect(overview.reload.user_ids).to eq([])
  873. expect { chat_session.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  874. expect { chat_message.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  875. expect { chat_message2.reload }.to raise_exception(ActiveRecord::RecordNotFound)
  876. # move ownership objects
  877. expect { group.reload }.to change(group, :created_by_id).to(1)
  878. expect { job.reload }.to change(job, :updated_by_id).to(1)
  879. expect { ticket.reload }.to change(ticket, :owner_id).to(1)
  880. expect { ticket_article.reload }
  881. .to change(ticket_article, :origin_by_id).to(1)
  882. .and change(ticket_article, :updated_by_id).to(1)
  883. .and change(ticket_article, :created_by_id).to(1)
  884. expect { knowledge_base_answer.reload }
  885. .to change(knowledge_base_answer, :archived_by_id).to(1)
  886. .and change(knowledge_base_answer, :published_by_id).to(1)
  887. .and change(knowledge_base_answer, :internal_by_id).to(1)
  888. expect { user_created_by.reload }
  889. .to change(user_created_by, :created_by_id).to(1)
  890. .and change(user_created_by, :updated_by_id).to(1)
  891. .and change(user_created_by, :out_of_office_replacement_id).to(1)
  892. end
  893. it 'does delete cache after user deletion' do
  894. online_notification = create(:online_notification, created_by_id: user.id)
  895. online_notification.attributes_with_association_ids
  896. user.destroy
  897. expect(online_notification.reload.attributes_with_association_ids['created_by_id']).to eq(1)
  898. end
  899. it 'does return an exception on blocking dependencies' do
  900. expect { user.send(:destroy_move_dependency_ownership) }.to raise_error(RuntimeError, 'Failed deleting references! Check logic for UserGroup->user_id.')
  901. end
  902. describe '#organization' do
  903. describe 'email domain-based assignment' do
  904. subject(:user) { build(:user) }
  905. context 'when not set on creation' do
  906. before { user.assign_attributes(organization: nil) }
  907. context 'and #email domain matches an existing Organization#domain' do
  908. before { user.assign_attributes(email: 'user@example.com') }
  909. let(:organization) { create(:organization, domain: 'example.com') }
  910. context 'and Organization#domain_assignment is false (default)' do
  911. before { organization.update(domain_assignment: false) }
  912. it 'remains nil' do
  913. expect { user.save }.not_to change(user, :organization)
  914. end
  915. end
  916. context 'and Organization#domain_assignment is true' do
  917. before { organization.update(domain_assignment: true) }
  918. it 'is automatically set to matching Organization' do
  919. expect { user.save }
  920. .to change(user, :organization).to(organization)
  921. end
  922. end
  923. end
  924. context 'and #email domain doesn’t match any Organization#domain' do
  925. before { user.assign_attributes(email: 'user@example.net') }
  926. let(:organization) { create(:organization, domain: 'example.com') }
  927. context 'and Organization#domain_assignment is true' do
  928. before { organization.update(domain_assignment: true) }
  929. it 'remains nil' do
  930. expect { user.save }.not_to change(user, :organization)
  931. end
  932. end
  933. end
  934. end
  935. context 'when set on creation' do
  936. before { user.assign_attributes(organization: specified_organization) }
  937. let(:specified_organization) { create(:organization, domain: 'example.net') }
  938. context 'and #email domain matches a DIFFERENT Organization#domain' do
  939. before { user.assign_attributes(email: 'user@example.com') }
  940. let!(:matching_organization) { create(:organization, domain: 'example.com') }
  941. context 'and Organization#domain_assignment is true' do
  942. before { matching_organization.update(domain_assignment: true) }
  943. it 'is NOT automatically set to matching Organization' do
  944. expect { user.save }
  945. .not_to change(user, :organization).from(specified_organization)
  946. end
  947. end
  948. end
  949. end
  950. end
  951. end
  952. end
  953. describe 'Callbacks, Observers, & Async Transactions -' do
  954. describe 'System-wide agent limit checks:' do
  955. let(:agent_role) { Role.lookup(name: 'Agent') }
  956. let(:admin_role) { Role.lookup(name: 'Admin') }
  957. let(:current_agents) { described_class.with_permissions('ticket.agent') }
  958. describe '#validate_agent_limit_by_role' do
  959. context 'for Integer value of system_agent_limit' do
  960. context 'before exceeding the agent limit' do
  961. before { Setting.set('system_agent_limit', current_agents.count + 1) }
  962. it 'grants agent creation' do
  963. expect { create(:agent) }
  964. .to change(current_agents, :count).by(1)
  965. end
  966. it 'grants role change' do
  967. future_agent = create(:customer)
  968. expect { future_agent.roles = [agent_role] }
  969. .to change(current_agents, :count).by(1)
  970. end
  971. describe 'role updates' do
  972. let(:agent) { create(:agent) }
  973. it 'grants update by instances' do
  974. expect { agent.roles = [admin_role, agent_role] }
  975. .not_to raise_error
  976. end
  977. it 'grants update by id (Integer)' do
  978. expect { agent.role_ids = [admin_role.id, agent_role.id] }
  979. .not_to raise_error
  980. end
  981. it 'grants update by id (String)' do
  982. expect { agent.role_ids = [admin_role.id.to_s, agent_role.id.to_s] }
  983. .not_to raise_error
  984. end
  985. end
  986. end
  987. context 'when exceeding the agent limit' do
  988. it 'creation of new agents' do
  989. Setting.set('system_agent_limit', current_agents.count + 2)
  990. create_list(:agent, 2)
  991. expect { create(:agent) }
  992. .to raise_error(Exceptions::UnprocessableEntity)
  993. .and change(current_agents, :count).by(0)
  994. end
  995. it 'prevents role change' do
  996. Setting.set('system_agent_limit', current_agents.count)
  997. future_agent = create(:customer)
  998. expect { future_agent.roles = [agent_role] }
  999. .to raise_error(Exceptions::UnprocessableEntity)
  1000. .and change(current_agents, :count).by(0)
  1001. end
  1002. end
  1003. end
  1004. context 'for String value of system_agent_limit' do
  1005. context 'before exceeding the agent limit' do
  1006. before { Setting.set('system_agent_limit', (current_agents.count + 1).to_s) }
  1007. it 'grants agent creation' do
  1008. expect { create(:agent) }
  1009. .to change(current_agents, :count).by(1)
  1010. end
  1011. it 'grants role change' do
  1012. future_agent = create(:customer)
  1013. expect { future_agent.roles = [agent_role] }
  1014. .to change(current_agents, :count).by(1)
  1015. end
  1016. describe 'role updates' do
  1017. let(:agent) { create(:agent) }
  1018. it 'grants update by instances' do
  1019. expect { agent.roles = [admin_role, agent_role] }
  1020. .not_to raise_error
  1021. end
  1022. it 'grants update by id (Integer)' do
  1023. expect { agent.role_ids = [admin_role.id, agent_role.id] }
  1024. .not_to raise_error
  1025. end
  1026. it 'grants update by id (String)' do
  1027. expect { agent.role_ids = [admin_role.id.to_s, agent_role.id.to_s] }
  1028. .not_to raise_error
  1029. end
  1030. end
  1031. end
  1032. context 'when exceeding the agent limit' do
  1033. it 'creation of new agents' do
  1034. Setting.set('system_agent_limit', (current_agents.count + 2).to_s)
  1035. create_list(:agent, 2)
  1036. expect { create(:agent) }
  1037. .to raise_error(Exceptions::UnprocessableEntity)
  1038. .and change(current_agents, :count).by(0)
  1039. end
  1040. it 'prevents role change' do
  1041. Setting.set('system_agent_limit', current_agents.count.to_s)
  1042. future_agent = create(:customer)
  1043. expect { future_agent.roles = [agent_role] }
  1044. .to raise_error(Exceptions::UnprocessableEntity)
  1045. .and change(current_agents, :count).by(0)
  1046. end
  1047. end
  1048. end
  1049. end
  1050. describe '#validate_agent_limit_by_attributes' do
  1051. context 'for Integer value of system_agent_limit' do
  1052. before { Setting.set('system_agent_limit', current_agents.count) }
  1053. context 'when exceeding the agent limit' do
  1054. it 'prevents re-activation of agents' do
  1055. inactive_agent = create(:agent, active: false)
  1056. expect { inactive_agent.update!(active: true) }
  1057. .to raise_error(Exceptions::UnprocessableEntity)
  1058. .and change(current_agents, :count).by(0)
  1059. end
  1060. end
  1061. end
  1062. context 'for String value of system_agent_limit' do
  1063. before { Setting.set('system_agent_limit', current_agents.count.to_s) }
  1064. context 'when exceeding the agent limit' do
  1065. it 'prevents re-activation of agents' do
  1066. inactive_agent = create(:agent, active: false)
  1067. expect { inactive_agent.update!(active: true) }
  1068. .to raise_error(Exceptions::UnprocessableEntity)
  1069. .and change(current_agents, :count).by(0)
  1070. end
  1071. end
  1072. end
  1073. end
  1074. end
  1075. describe 'Touching associations on update:' do
  1076. subject!(:user) { create(:customer) }
  1077. let!(:organization) { create(:organization) }
  1078. context 'when a customer gets a organization' do
  1079. it 'touches its organization' do
  1080. expect { user.update(organization: organization) }
  1081. .to change { organization.reload.updated_at }
  1082. end
  1083. end
  1084. end
  1085. describe 'Cti::CallerId syncing:' do
  1086. context 'with a #phone attribute' do
  1087. subject(:user) { build(:user, phone: '1234567890') }
  1088. it 'adds CallerId record on creation (via Cti::CallerId.build)' do
  1089. expect(Cti::CallerId).to receive(:build).with(user)
  1090. user.save
  1091. end
  1092. it 'does not update CallerId record on touch/update (via Cti::CallerId.build)' do
  1093. expect(Cti::CallerId).to receive(:build).with(user)
  1094. user.save
  1095. expect(Cti::CallerId).not_to receive(:build).with(user)
  1096. user.touch
  1097. end
  1098. it 'destroys CallerId record on deletion' do
  1099. user.save
  1100. expect { user.destroy }
  1101. .to change { Cti::CallerId.count }.by(-1)
  1102. end
  1103. end
  1104. end
  1105. describe 'Cti::Log syncing:' do
  1106. context 'with existing Log records' do
  1107. context 'for incoming calls from an unknown number' do
  1108. let!(:log) { create(:'cti/log', :with_preferences, from: '1234567890', direction: 'in') }
  1109. context 'when creating a new user with that number' do
  1110. subject(:user) { build(:user, phone: log.from) }
  1111. it 'populates #preferences[:from] hash in all associated Log records (in a bg job)' do
  1112. expect do
  1113. user.save
  1114. TransactionDispatcher.commit
  1115. Scheduler.worker(true)
  1116. end.to change { log.reload.preferences[:from]&.first }
  1117. .to(hash_including('caller_id' => user.phone))
  1118. end
  1119. end
  1120. context 'when updating a user with that number' do
  1121. subject(:user) { create(:user) }
  1122. it 'populates #preferences[:from] hash in all associated Log records (in a bg job)' do
  1123. expect do
  1124. user.update(phone: log.from)
  1125. TransactionDispatcher.commit
  1126. Scheduler.worker(true)
  1127. end.to change { log.reload.preferences[:from]&.first }
  1128. .to(hash_including('object' => 'User', 'o_id' => user.id))
  1129. end
  1130. end
  1131. context 'when creating a new user with an empty number' do
  1132. subject(:user) { build(:user, phone: '') }
  1133. it 'does not modify any Log records' do
  1134. expect do
  1135. user.save
  1136. TransactionDispatcher.commit
  1137. Scheduler.worker(true)
  1138. end.not_to change { log.reload.attributes }
  1139. end
  1140. end
  1141. context 'when creating a new user with no number' do
  1142. subject(:user) { build(:user, phone: nil) }
  1143. it 'does not modify any Log records' do
  1144. expect do
  1145. user.save
  1146. TransactionDispatcher.commit
  1147. Scheduler.worker(true)
  1148. end.not_to change { log.reload.attributes }
  1149. end
  1150. end
  1151. end
  1152. context 'for incoming calls from the given user' do
  1153. subject(:user) { create(:user, phone: '1234567890') }
  1154. let!(:logs) { create_list(:'cti/log', 5, :with_preferences, from: user.phone, direction: 'in') }
  1155. context 'when updating #phone attribute' do
  1156. context 'to another number' do
  1157. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1158. expect do
  1159. user.update(phone: '0123456789')
  1160. TransactionDispatcher.commit
  1161. Scheduler.worker(true)
  1162. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1163. .to(Array.new(5) { nil })
  1164. end
  1165. end
  1166. context 'to an empty string' do
  1167. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1168. expect do
  1169. user.update(phone: '')
  1170. TransactionDispatcher.commit
  1171. Scheduler.worker(true)
  1172. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1173. .to(Array.new(5) { nil })
  1174. end
  1175. end
  1176. context 'to nil' do
  1177. it 'empties #preferences[:from] hash in all associated Log records (in a bg job)' do
  1178. expect do
  1179. user.update(phone: nil)
  1180. TransactionDispatcher.commit
  1181. Scheduler.worker(true)
  1182. end.to change { logs.map(&:reload).map { |log| log.preferences[:from] } }
  1183. .to(Array.new(5) { nil })
  1184. end
  1185. end
  1186. end
  1187. context 'when updating attributes other than #phone' do
  1188. it 'does not modify any Log records' do
  1189. expect do
  1190. user.update(mobile: '2345678901')
  1191. TransactionDispatcher.commit
  1192. Scheduler.worker(true)
  1193. end.not_to change { logs.map(&:reload).map(&:attributes) }
  1194. end
  1195. end
  1196. end
  1197. end
  1198. end
  1199. end
  1200. end