user_test.rb 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. require 'test_helper'
  2. class UserTest < ActiveSupport::TestCase
  3. test 'user' do
  4. tests = [
  5. {
  6. name: '#1 - simple create',
  7. create: {
  8. firstname: 'Firstname',
  9. lastname: 'Lastname',
  10. email: 'some@example.com',
  11. login: 'some@example.com',
  12. updated_by_id: 1,
  13. created_by_id: 1,
  14. },
  15. create_verify: {
  16. firstname: 'Firstname',
  17. lastname: 'Lastname',
  18. image: nil,
  19. fullname: 'Firstname Lastname',
  20. email: 'some@example.com',
  21. login: 'some@example.com',
  22. },
  23. },
  24. {
  25. name: '#2 - simple create - no lastname',
  26. create: {
  27. firstname: 'Firstname Lastname',
  28. lastname: '',
  29. email: 'some@example.com',
  30. login: 'some@example.com',
  31. updated_by_id: 1,
  32. created_by_id: 1,
  33. },
  34. create_verify: {
  35. firstname: 'Firstname',
  36. lastname: 'Lastname',
  37. image: nil,
  38. email: 'some@example.com',
  39. login: 'some@example.com',
  40. },
  41. },
  42. {
  43. name: '#3 - simple create - no firstname',
  44. create: {
  45. firstname: '',
  46. lastname: 'Firstname Lastname',
  47. email: 'some@example.com',
  48. login: 'some@example.com',
  49. updated_by_id: 1,
  50. created_by_id: 1,
  51. },
  52. create_verify: {
  53. firstname: 'Firstname',
  54. lastname: 'Lastname',
  55. image: nil,
  56. email: 'some@example.com',
  57. login: 'some@example.com',
  58. },
  59. },
  60. {
  61. name: '#4 - simple create - nil as lastname',
  62. create: {
  63. firstname: 'Firstname Lastname',
  64. lastname: '',
  65. email: 'some@example.com',
  66. login: 'some@example.com',
  67. updated_by_id: 1,
  68. created_by_id: 1,
  69. },
  70. create_verify: {
  71. firstname: 'Firstname',
  72. lastname: 'Lastname',
  73. image: nil,
  74. email: 'some@example.com',
  75. login: 'some@example.com',
  76. },
  77. },
  78. {
  79. name: '#5 - simple create - no lastname, firstname with ","',
  80. create: {
  81. firstname: 'Lastname, Firstname',
  82. lastname: '',
  83. email: 'some@example.com',
  84. login: 'some@example.com',
  85. updated_by_id: 1,
  86. created_by_id: 1,
  87. },
  88. create_verify: {
  89. firstname: 'Firstname',
  90. lastname: 'Lastname',
  91. email: 'some@example.com',
  92. login: 'some@example.com',
  93. },
  94. },
  95. {
  96. name: '#6 - simple create - no lastname/firstname',
  97. create: {
  98. firstname: '',
  99. lastname: '',
  100. email: 'firstname.lastname@example.com',
  101. login: 'login-1',
  102. updated_by_id: 1,
  103. created_by_id: 1,
  104. },
  105. create_verify: {
  106. firstname: 'Firstname',
  107. lastname: 'Lastname',
  108. fullname: 'Firstname Lastname',
  109. email: 'firstname.lastname@example.com',
  110. login: 'login-1',
  111. },
  112. },
  113. {
  114. name: '#7 - simple create - no lastname/firstnam',
  115. create: {
  116. firstname: '',
  117. lastname: '',
  118. email: 'FIRSTNAME.lastname@example.com',
  119. login: 'login-2',
  120. updated_by_id: 1,
  121. created_by_id: 1,
  122. },
  123. create_verify: {
  124. firstname: 'Firstname',
  125. lastname: 'Lastname',
  126. email: 'firstname.lastname@example.com',
  127. login: 'login-2',
  128. },
  129. },
  130. {
  131. name: '#8 - simple create - nill as fristname and lastname',
  132. create: {
  133. firstname: '',
  134. lastname: '',
  135. email: 'FIRSTNAME.lastname@example.com',
  136. login: 'login-3',
  137. updated_by_id: 1,
  138. created_by_id: 1,
  139. },
  140. create_verify: {
  141. firstname: 'Firstname',
  142. lastname: 'Lastname',
  143. email: 'firstname.lastname@example.com',
  144. login: 'login-3',
  145. },
  146. },
  147. {
  148. name: '#9 - update with avatar check',
  149. create: {
  150. firstname: 'Bob',
  151. lastname: 'Smith',
  152. email: 'bob.smith@example.com',
  153. login: 'login-4',
  154. updated_by_id: 1,
  155. created_by_id: 1,
  156. },
  157. create_verify: {
  158. firstname: 'Bob',
  159. lastname: 'Smith',
  160. image: nil,
  161. email: 'bob.smith@example.com',
  162. login: 'login-4',
  163. },
  164. update: {
  165. email: 'unit-test1@znuny.com',
  166. },
  167. update_verify: {
  168. firstname: 'Bob',
  169. lastname: 'Smith',
  170. image: 'a11ed3970e6d3a680527d6f3f075ff89',
  171. image_md5: 'a11ed3970e6d3a680527d6f3f075ff89',
  172. email: 'unit-test1@znuny.com',
  173. login: 'login-4',
  174. }
  175. },
  176. {
  177. name: '#10 - update create with avatar check',
  178. create: {
  179. firstname: 'Bob',
  180. lastname: 'Smith',
  181. email: 'unit-test2@znuny.com',
  182. login: 'login-5',
  183. updated_by_id: 1,
  184. created_by_id: 1,
  185. },
  186. create_verify: {
  187. firstname: 'Bob',
  188. lastname: 'Smith',
  189. image: 'd76099edb79f39624b35187873184e3c',
  190. image_md5: 'd76099edb79f39624b35187873184e3c',
  191. email: 'unit-test2@znuny.com',
  192. login: 'login-5',
  193. },
  194. update: {
  195. email: 'unit-test1@znuny.com',
  196. },
  197. update_verify: {
  198. firstname: 'Bob',
  199. lastname: 'Smith',
  200. image: 'a11ed3970e6d3a680527d6f3f075ff89',
  201. image_md5: 'a11ed3970e6d3a680527d6f3f075ff89',
  202. email: 'unit-test1@znuny.com',
  203. login: 'login-5',
  204. }
  205. },
  206. {
  207. name: '#11 - update create with login/email check',
  208. create: {
  209. firstname: '',
  210. lastname: '',
  211. email: 'caoyaoewfzfw@21222cn.com',
  212. updated_by_id: 1,
  213. created_by_id: 1,
  214. },
  215. create_verify: {
  216. firstname: '',
  217. lastname: '',
  218. fullname: 'caoyaoewfzfw@21222cn.com',
  219. email: 'caoyaoewfzfw@21222cn.com',
  220. login: 'caoyaoewfzfw@21222cn.com',
  221. },
  222. update: {
  223. email: 'caoyaoewfzfw@212224cn.com',
  224. },
  225. update_verify: {
  226. firstname: '',
  227. lastname: '',
  228. email: 'caoyaoewfzfw@212224cn.com',
  229. fullname: 'caoyaoewfzfw@212224cn.com',
  230. login: 'caoyaoewfzfw@212224cn.com',
  231. }
  232. },
  233. {
  234. name: '#12 - update create with login/email check',
  235. create: {
  236. firstname: 'Firstname',
  237. lastname: 'Lastname',
  238. email: 'some_tEst11@example.com',
  239. updated_by_id: 1,
  240. created_by_id: 1,
  241. },
  242. create_verify: {
  243. firstname: 'Firstname',
  244. lastname: 'Lastname',
  245. fullname: 'Firstname Lastname',
  246. email: 'some_test11@example.com',
  247. },
  248. update: {
  249. email: 'some_Test11-1@example.com',
  250. },
  251. update_verify: {
  252. firstname: 'Firstname',
  253. lastname: 'Lastname',
  254. email: 'some_test11-1@example.com',
  255. fullname: 'Firstname Lastname',
  256. login: 'some_test11-1@example.com',
  257. }
  258. },
  259. ]
  260. tests.each do |test|
  261. # check if user exists
  262. user = User.find_by(login: test[:create][:login])
  263. user&.destroy!
  264. user = User.create!(test[:create])
  265. test[:create_verify].each do |key, value|
  266. next if key == :image_md5
  267. if user.respond_to?(key)
  268. result = user.send(key)
  269. if value.nil?
  270. assert_nil(result, "create check #{key} in (#{test[:name]})")
  271. else
  272. assert_equal(result, value, "create check #{key} in (#{test[:name]})")
  273. end
  274. else
  275. assert_equal(user[key], value, "create check #{key} in (#{test[:name]})")
  276. end
  277. end
  278. if test[:create_verify][:image_md5]
  279. file = Avatar.get_by_hash(user.image)
  280. file_md5 = Digest::MD5.hexdigest(file.content)
  281. assert_equal(file_md5, test[:create_verify][:image_md5], "create avatar md5 check in (#{test[:name]})")
  282. end
  283. if test[:update]
  284. user.update!(test[:update])
  285. test[:update_verify].each do |key, value|
  286. next if key == :image_md5
  287. if user.respond_to?(key)
  288. assert_equal(user.send(key), value, "update check #{key} in (#{test[:name]})")
  289. else
  290. assert_equal(user[key], value, "update check #{key} in (#{test[:name]})")
  291. end
  292. end
  293. if test[:update_verify][:image_md5]
  294. file = Avatar.get_by_hash(user.image)
  295. file_md5 = Digest::MD5.hexdigest(file.content)
  296. assert_equal(file_md5, test[:update_verify][:image_md5], "update avatar md5 check in (#{test[:name]})")
  297. end
  298. end
  299. user.destroy!
  300. end
  301. end
  302. test 'strange spaces' do
  303. name = "#{Time.zone.now.to_i}-#{rand(999_999_999_999)}"
  304. email = "customer_email#{name}@example.com"
  305. customer = User.create!(
  306. firstname: 'Role',
  307. lastname: "Customer#{name}",
  308. email: " #{email} ",
  309. password: 'customerpw',
  310. active: true,
  311. roles: Role.where(name: %w[Customer]),
  312. updated_by_id: 1,
  313. created_by_id: 1,
  314. )
  315. assert(customer)
  316. assert_equal(email, customer.email)
  317. customer.destroy!
  318. name = "#{Time.zone.now.to_i}-#{rand(999_999_999_999)}"
  319. email = "customer_email#{name}@example.com"
  320. customer = User.create!(
  321. firstname: "\u{00a0}\u{00a0}Role",
  322. lastname: "Customer#{name} \u{00a0}",
  323. email: "\u{00a0}#{email}\u{00a0}",
  324. password: 'customerpw',
  325. active: true,
  326. roles: Role.where(name: %w[Customer]),
  327. updated_by_id: 1,
  328. created_by_id: 1,
  329. )
  330. assert(customer)
  331. assert_equal('Role', customer.firstname)
  332. assert_equal("Customer#{name}", customer.lastname)
  333. assert_equal(email, customer.email)
  334. customer.destroy!
  335. name = "#{Time.zone.now.to_i}-#{rand(999_999_999_999)}"
  336. email = "customer_email#{name}@example.com"
  337. customer = User.create!(
  338. firstname: "\u{200B}\u{200B}Role",
  339. lastname: "Customer#{name} \u{200B}",
  340. email: "\u{200B}#{email}\u{200B}",
  341. password: 'customerpw',
  342. active: true,
  343. roles: Role.where(name: %w[Customer]),
  344. updated_by_id: 1,
  345. created_by_id: 1,
  346. )
  347. assert(customer)
  348. assert_equal('Role', customer.firstname)
  349. assert_equal("Customer#{name}", customer.lastname)
  350. assert_equal(email, customer.email)
  351. customer.destroy!
  352. name = "#{Time.zone.now.to_i}-#{rand(999_999_999_999)}"
  353. email = "customer_email#{name}@example.com"
  354. customer = User.create!(
  355. firstname: "\u{200B}\u{200B}Role\u{00a0}",
  356. lastname: "\u{00a0}\u{00a0}Customer#{name} \u{200B}",
  357. email: "\u{200B}#{email}\u{200B}",
  358. password: 'customerpw',
  359. active: true,
  360. roles: Role.where(name: %w[Customer]),
  361. updated_by_id: 1,
  362. created_by_id: 1,
  363. )
  364. assert(customer)
  365. assert_equal('Role', customer.firstname)
  366. assert_equal("Customer#{name}", customer.lastname)
  367. assert_equal(email, customer.email)
  368. customer.destroy!
  369. name = "#{Time.zone.now.to_i}-#{rand(999_999_999_999)}"
  370. email = "customer_email#{name}@example.com"
  371. customer = User.create!(
  372. firstname: "\u{200a}\u{200b}\u{202F}\u{205F}Role\u{2007}\u{2008}",
  373. lastname: "\u{00a0}\u{00a0}Customer#{name}\u{3000}\u{FEFF}\u{2000}",
  374. email: "\u{200B}#{email}\u{200B}\u{2007}\u{2008}",
  375. password: 'customerpw',
  376. active: true,
  377. roles: Role.where(name: %w[Customer]),
  378. updated_by_id: 1,
  379. created_by_id: 1,
  380. )
  381. assert(customer)
  382. assert_equal('Role', customer.firstname)
  383. assert_equal("Customer#{name}", customer.lastname)
  384. assert_equal(email, customer.email)
  385. customer.destroy!
  386. end
  387. test 'without email - but login eq email' do
  388. name = rand(999_999_999)
  389. login = "admin-role_without_email#{name}@example.com"
  390. email = "admin-role_without_email#{name}@example.com"
  391. admin = User.create_or_update(
  392. login: login,
  393. firstname: 'Role',
  394. lastname: "Admin#{name}",
  395. #email: "",
  396. password: 'adminpw',
  397. active: true,
  398. roles: Role.where(name: %w[Admin Agent]),
  399. updated_by_id: 1,
  400. created_by_id: 1,
  401. )
  402. assert(admin.id)
  403. assert_equal(admin.login, login)
  404. assert_equal(admin.email, '')
  405. admin.email = email
  406. admin.save!
  407. assert_equal(admin.login, login)
  408. assert_equal(admin.email, email)
  409. admin.email = ''
  410. admin.save!
  411. assert(admin.id)
  412. assert(admin.login)
  413. assert_not_equal(admin.login, login)
  414. assert_equal(admin.email, '')
  415. admin.destroy!
  416. end
  417. test 'without email - but login ne email' do
  418. name = rand(999_999_999)
  419. login = "admin-role_without_email#{name}"
  420. email = "admin-role_without_email#{name}@example.com"
  421. admin = User.create_or_update(
  422. login: login,
  423. firstname: 'Role',
  424. lastname: "Admin#{name}",
  425. #email: "",
  426. password: 'adminpw',
  427. active: true,
  428. roles: Role.where(name: %w[Admin Agent]),
  429. updated_by_id: 1,
  430. created_by_id: 1,
  431. )
  432. assert(admin.id)
  433. assert_equal(admin.login, login)
  434. assert_equal(admin.email, '')
  435. admin.email = email
  436. admin.save!
  437. assert_equal(admin.login, login)
  438. assert_equal(admin.email, email)
  439. admin.email = ''
  440. admin.save!
  441. assert(admin.id)
  442. assert_equal(admin.login, login)
  443. assert_equal(admin.email, '')
  444. admin.destroy!
  445. end
  446. test 'uniq email' do
  447. name = rand(999_999_999)
  448. email1 = "admin1-role_without_email#{name}@example.com"
  449. admin1 = User.create!(
  450. login: email1,
  451. firstname: 'Role',
  452. lastname: "Admin1#{name}",
  453. email: email1,
  454. password: 'adminpw',
  455. active: true,
  456. roles: Role.where(name: %w[Admin Agent]),
  457. updated_by_id: 1,
  458. created_by_id: 1,
  459. )
  460. assert(admin1.id)
  461. assert_equal(admin1.email, email1)
  462. assert_raises(Exceptions::UnprocessableEntity) do
  463. User.create!(
  464. login: "#{email1}-1",
  465. firstname: 'Role',
  466. lastname: "Admin1#{name}",
  467. email: email1,
  468. password: 'adminpw',
  469. active: true,
  470. roles: Role.where(name: %w[Admin Agent]),
  471. updated_by_id: 1,
  472. created_by_id: 1,
  473. )
  474. end
  475. email2 = "admin2-role_without_email#{name}@example.com"
  476. admin2 = User.create!(
  477. firstname: 'Role',
  478. lastname: "Admin2#{name}",
  479. email: email2,
  480. password: 'adminpw',
  481. active: true,
  482. roles: Role.where(name: %w[Admin Agent]),
  483. updated_by_id: 1,
  484. created_by_id: 1,
  485. )
  486. assert_raises(Exceptions::UnprocessableEntity) do
  487. admin2.email = email1
  488. admin2.save!
  489. end
  490. admin1.email = admin1.email
  491. admin1.save!
  492. admin2.destroy!
  493. admin1.destroy!
  494. end
  495. test 'uniq email - multiple use' do
  496. Setting.set('user_email_multiple_use', true)
  497. name = rand(999_999_999)
  498. email1 = "admin1-role_without_email#{name}@example.com"
  499. admin1 = User.create!(
  500. login: email1,
  501. firstname: 'Role',
  502. lastname: "Admin1#{name}",
  503. email: email1,
  504. password: 'adminpw',
  505. active: true,
  506. roles: Role.where(name: %w[Admin Agent]),
  507. updated_by_id: 1,
  508. created_by_id: 1,
  509. )
  510. assert(admin1.id)
  511. assert_equal(admin1.email, email1)
  512. admin2 = User.create!(
  513. login: "#{email1}-1",
  514. firstname: 'Role',
  515. lastname: "Admin1#{name}",
  516. email: email1,
  517. password: 'adminpw',
  518. active: true,
  519. roles: Role.where(name: %w[Admin Agent]),
  520. updated_by_id: 1,
  521. created_by_id: 1,
  522. )
  523. assert_equal(admin2.email, email1)
  524. admin2.destroy!
  525. admin1.destroy!
  526. Setting.set('user_email_multiple_use', false)
  527. end
  528. test 'ensure roles' do
  529. name = rand(999_999_999)
  530. admin = User.create_or_update(
  531. login: "admin-role#{name}@example.com",
  532. firstname: 'Role',
  533. lastname: "Admin#{name}",
  534. email: "admin-role#{name}@example.com",
  535. password: 'adminpw',
  536. active: true,
  537. roles: Role.where(name: %w[Admin Agent]),
  538. updated_by_id: 1,
  539. created_by_id: 1,
  540. )
  541. customer1 = User.create_or_update(
  542. login: "user-ensure-role1-#{name}@example.com",
  543. firstname: 'Role',
  544. lastname: "Customer#{name}",
  545. email: "user-ensure-role1-#{name}@example.com",
  546. password: 'customerpw',
  547. active: true,
  548. updated_by_id: 1,
  549. created_by_id: 1,
  550. )
  551. assert_equal(customer1.role_ids.sort, Role.signup_role_ids)
  552. roles = Role.where(name: 'Agent')
  553. customer1.roles = roles
  554. customer1.save!
  555. assert_equal(customer1.role_ids.count, 1)
  556. assert_equal(customer1.role_ids.first, roles.first.id)
  557. assert_equal(customer1.roles.first.id, roles.first.id)
  558. customer1.roles = []
  559. customer1.save!
  560. assert_equal(customer1.role_ids.sort, Role.signup_role_ids)
  561. customer1.destroy!
  562. customer2 = User.create_or_update(
  563. login: "user-ensure-role2-#{name}@example.com",
  564. firstname: 'Role',
  565. lastname: "Customer#{name}",
  566. email: "user-ensure-role2-#{name}@example.com",
  567. password: 'customerpw',
  568. roles: roles,
  569. active: true,
  570. updated_by_id: 1,
  571. created_by_id: 1,
  572. )
  573. assert_equal(customer2.role_ids.count, 1)
  574. assert_equal(customer2.role_ids.first, roles.first.id)
  575. assert_equal(customer2.roles.first.id, roles.first.id)
  576. roles = Role.where(name: 'Admin')
  577. customer2.role_ids = [roles.first.id]
  578. customer2.save!
  579. assert_equal(customer2.role_ids.count, 1)
  580. assert_equal(customer2.role_ids.first, roles.first.id)
  581. assert_equal(customer2.roles.first.id, roles.first.id)
  582. customer2.roles = []
  583. customer2.save!
  584. assert_equal(customer2.role_ids.sort, Role.signup_role_ids)
  585. customer2.destroy!
  586. customer3 = User.create_or_update(
  587. login: "user-ensure-role2-#{name}@example.com",
  588. firstname: 'Role',
  589. lastname: "Customer#{name}",
  590. email: "user-ensure-role2-#{name}@example.com",
  591. password: 'customerpw',
  592. roles: roles,
  593. active: true,
  594. updated_by_id: 1,
  595. created_by_id: 1,
  596. )
  597. customer3.roles = Role.where(name: %w[Admin Agent])
  598. customer3.roles.each do |role|
  599. assert_not_equal(role.name, 'Customer')
  600. end
  601. customer3.roles = Role.where(name: 'Admin')
  602. customer3.roles.each do |role|
  603. assert_not_equal(role.name, 'Customer')
  604. end
  605. customer3.roles = Role.where(name: 'Agent')
  606. customer3.roles.each do |role|
  607. assert_not_equal(role.name, 'Customer')
  608. end
  609. customer3.destroy!
  610. admin.destroy!
  611. end
  612. test 'user default preferences' do
  613. name = rand(999_999_999)
  614. groups = Group.where(name: 'Users')
  615. roles = Role.where(name: 'Agent')
  616. agent1 = User.create_or_update(
  617. login: "agent-default-preferences#{name}@example.com",
  618. firstname: 'Preferences',
  619. lastname: "Agent#{name}",
  620. email: "agent-default-preferences#{name}@example.com",
  621. password: 'agentpw',
  622. active: true,
  623. roles: roles,
  624. groups: groups,
  625. preferences: {
  626. locale: 'de-de',
  627. },
  628. updated_by_id: 1,
  629. created_by_id: 1,
  630. )
  631. agent1 = User.find(agent1.id)
  632. assert(agent1.preferences)
  633. assert(agent1.preferences['locale'])
  634. assert_equal(agent1.preferences['locale'], 'de-de')
  635. assert(agent1.preferences['notification_config'])
  636. assert(agent1.preferences['notification_config']['matrix'])
  637. assert(agent1.preferences['notification_config']['matrix']['create'])
  638. assert(agent1.preferences['notification_config']['matrix']['update'])
  639. roles = Role.where(name: 'Customer')
  640. customer1 = User.create_or_update(
  641. login: "customer-default-preferences#{name}@example.com",
  642. firstname: 'Preferences',
  643. lastname: "Customer#{name}",
  644. email: "customer-default-preferences#{name}@example.com",
  645. password: 'customerpw',
  646. active: true,
  647. roles: roles,
  648. preferences: {
  649. locale: 'de-de',
  650. },
  651. updated_by_id: 1,
  652. created_by_id: 1,
  653. )
  654. customer1 = User.find(customer1.id)
  655. assert(customer1.preferences)
  656. assert(customer1.preferences['locale'])
  657. assert_equal(customer1.preferences['locale'], 'de-de')
  658. assert_not(customer1.preferences['notification_config'])
  659. customer1 = User.find(customer1.id)
  660. customer1.roles = Role.where(name: 'Agent')
  661. customer1 = User.find(customer1.id)
  662. assert(customer1.preferences)
  663. assert(customer1.preferences['locale'])
  664. assert_equal(customer1.preferences['locale'], 'de-de')
  665. assert(customer1.preferences['notification_config'])
  666. assert(customer1.preferences['notification_config']['matrix']['create'])
  667. assert(customer1.preferences['notification_config']['matrix']['update'])
  668. end
  669. test 'permission' do
  670. test_role_1 = Role.create_or_update(
  671. name: 'Test1',
  672. note: 'To configure your system.',
  673. preferences: {
  674. not: ['Test3'],
  675. },
  676. updated_by_id: 1,
  677. created_by_id: 1
  678. )
  679. test_role_2 = Role.create_or_update(
  680. name: 'Test2',
  681. note: 'To work on Tickets.',
  682. preferences: {
  683. not: ['Test3'],
  684. },
  685. updated_by_id: 1,
  686. created_by_id: 1
  687. )
  688. test_role_3 = Role.create_or_update(
  689. name: 'Test3',
  690. note: 'People who create Tickets ask for help.',
  691. preferences: {
  692. not: %w[Test1 Test2],
  693. },
  694. updated_by_id: 1,
  695. created_by_id: 1
  696. )
  697. test_role_4 = Role.create_or_update(
  698. name: 'Test4',
  699. note: 'Access the report area.',
  700. preferences: {},
  701. created_by_id: 1,
  702. updated_by_id: 1,
  703. )
  704. name = rand(999_999_999)
  705. assert_raises(RuntimeError) do
  706. User.create_or_update(
  707. login: "customer-role#{name}@example.com",
  708. firstname: 'Role',
  709. lastname: "Customer#{name}",
  710. email: "customer-role#{name}@example.com",
  711. password: 'customerpw',
  712. active: true,
  713. roles: [test_role_1, test_role_3],
  714. updated_by_id: 1,
  715. created_by_id: 1,
  716. )
  717. end
  718. assert_raises(RuntimeError) do
  719. User.create_or_update(
  720. login: "customer-role#{name}@example.com",
  721. firstname: 'Role',
  722. lastname: "Customer#{name}",
  723. email: "customer-role#{name}@example.com",
  724. password: 'customerpw',
  725. active: true,
  726. roles: [test_role_2, test_role_3],
  727. updated_by_id: 1,
  728. created_by_id: 1,
  729. )
  730. end
  731. user1 = User.create_or_update(
  732. login: "customer-role#{name}@example.com",
  733. firstname: 'Role',
  734. lastname: "Customer#{name}",
  735. email: "customer-role#{name}@example.com",
  736. password: 'customerpw',
  737. active: true,
  738. roles: [test_role_1, test_role_2],
  739. updated_by_id: 1,
  740. created_by_id: 1,
  741. )
  742. assert(user1.role_ids.include?(test_role_1.id))
  743. assert(user1.role_ids.include?(test_role_2.id))
  744. assert_not(user1.role_ids.include?(test_role_3.id))
  745. assert_not(user1.role_ids.include?(test_role_4.id))
  746. user1 = User.create_or_update(
  747. login: "customer-role#{name}@example.com",
  748. firstname: 'Role',
  749. lastname: "Customer#{name}",
  750. email: "customer-role#{name}@example.com",
  751. password: 'customerpw',
  752. active: true,
  753. roles: [test_role_1, test_role_4],
  754. updated_by_id: 1,
  755. created_by_id: 1,
  756. )
  757. assert(user1.role_ids.include?(test_role_1.id))
  758. assert_not(user1.role_ids.include?(test_role_2.id))
  759. assert_not(user1.role_ids.include?(test_role_3.id))
  760. assert(user1.role_ids.include?(test_role_4.id))
  761. assert_raises(RuntimeError) do
  762. User.create_or_update(
  763. login: "customer-role#{name}@example.com",
  764. firstname: 'Role',
  765. lastname: "Customer#{name}",
  766. email: "customer-role#{name}@example.com",
  767. password: 'customerpw',
  768. active: true,
  769. roles: [test_role_1, test_role_3],
  770. updated_by_id: 1,
  771. created_by_id: 1,
  772. )
  773. end
  774. assert_raises(RuntimeError) do
  775. User.create_or_update(
  776. login: "customer-role#{name}@example.com",
  777. firstname: 'Role',
  778. lastname: "Customer#{name}",
  779. email: "customer-role#{name}@example.com",
  780. password: 'customerpw',
  781. active: true,
  782. roles: [test_role_2, test_role_3],
  783. updated_by_id: 1,
  784. created_by_id: 1,
  785. )
  786. end
  787. assert(user1.role_ids.include?(test_role_1.id))
  788. assert_not(user1.role_ids.include?(test_role_2.id))
  789. assert_not(user1.role_ids.include?(test_role_3.id))
  790. assert(user1.role_ids.include?(test_role_4.id))
  791. end
  792. test 'permission default' do
  793. name = rand(999_999_999)
  794. admin_count = User.with_permissions('admin').count
  795. admin = User.create_or_update(
  796. login: "admin-role#{name}@example.com",
  797. firstname: 'Role',
  798. lastname: "Admin#{name}",
  799. email: "admin-role#{name}@example.com",
  800. password: 'adminpw',
  801. active: true,
  802. roles: Role.where(name: %w[Admin Agent]),
  803. updated_by_id: 1,
  804. created_by_id: 1,
  805. )
  806. agent_count = User.with_permissions('ticket.agent').count
  807. agent = User.create_or_update(
  808. login: "agent-role#{name}@example.com",
  809. firstname: 'Role',
  810. lastname: "Agent#{name}",
  811. email: "agent-role#{name}@example.com",
  812. password: 'agentpw',
  813. active: true,
  814. roles: Role.where(name: 'Agent'),
  815. updated_by_id: 1,
  816. created_by_id: 1,
  817. )
  818. customer_count = User.with_permissions('ticket.customer').count
  819. customer = User.create_or_update(
  820. login: "customer-role#{name}@example.com",
  821. firstname: 'Role',
  822. lastname: "Customer#{name}",
  823. email: "customer-role#{name}@example.com",
  824. password: 'customerpw',
  825. active: true,
  826. roles: Role.where(name: 'Customer'),
  827. updated_by_id: 1,
  828. created_by_id: 1,
  829. )
  830. users = User.with_permissions('not_existing')
  831. assert(users.blank?)
  832. users = User.with_permissions('admin')
  833. assert_equal(admin_count + 1, users.count)
  834. assert_equal(admin.login, users.last.login)
  835. users = User.with_permissions('admin.session')
  836. assert_equal(admin_count + 1, users.count)
  837. assert_equal(admin.login, users.last.login)
  838. users = User.with_permissions(['admin.session', 'not_existing'])
  839. assert_equal(admin_count + 1, users.count)
  840. assert_equal(admin.login, users.last.login)
  841. users = User.with_permissions('ticket.agent')
  842. assert_equal(agent_count + 1, users.count)
  843. assert_equal(agent.login, users.last.login)
  844. users = User.with_permissions(['ticket.agent', 'not_existing'])
  845. assert_equal(agent_count + 1, users.count)
  846. assert_equal(agent.login, users.last.login)
  847. users = User.with_permissions('ticket.customer')
  848. assert_equal(customer_count + 1, users.count)
  849. assert_equal(customer.login, users.last.login)
  850. users = User.with_permissions(['ticket.customer', 'not_existing'])
  851. assert_equal(customer_count + 1, users.count)
  852. assert_equal(customer.login, users.last.login)
  853. end
  854. test 'min admin permission check' do
  855. # delete inital admin
  856. User.find_by(login: 'admin@example.com').destroy
  857. # store current admin count
  858. admin_count_inital = User.with_permissions('admin').count
  859. assert_equal(0, admin_count_inital)
  860. # create two admin users
  861. random = rand(999_999_999)
  862. admin1 = User.create_or_update(
  863. login: "1admin-role#{random}@example.com",
  864. firstname: 'Role',
  865. lastname: "Admin#{random}",
  866. email: "admin-role#{random}@example.com",
  867. password: 'adminpw',
  868. active: true,
  869. roles: Role.where(name: %w[Admin Agent]),
  870. updated_by_id: 1,
  871. created_by_id: 1,
  872. )
  873. random = rand(999_999_999)
  874. admin2 = User.create_or_update(
  875. login: "2admin-role#{random}@example.com",
  876. firstname: 'Role',
  877. lastname: "Admin#{random}",
  878. email: "admin-role#{random}@example.com",
  879. password: 'adminpw',
  880. active: true,
  881. roles: Role.where(name: %w[Admin Agent]),
  882. updated_by_id: 1,
  883. created_by_id: 1,
  884. )
  885. random = rand(999_999_999)
  886. admin3 = User.create_or_update(
  887. login: "2admin-role#{random}@example.com",
  888. firstname: 'Role',
  889. lastname: "Admin#{random}",
  890. email: "admin-role#{random}@example.com",
  891. password: 'adminpw',
  892. active: true,
  893. roles: Role.where(name: %w[Admin Agent]),
  894. updated_by_id: 1,
  895. created_by_id: 1,
  896. )
  897. admin_count_inital = User.with_permissions('admin').count
  898. assert_equal(3, admin_count_inital)
  899. admin1.update!(roles: Role.where(name: %w[Agent]))
  900. admin_count_inital = User.with_permissions('admin').count
  901. assert_equal(2, admin_count_inital)
  902. admin2.update!(roles: Role.where(name: %w[Agent]))
  903. admin_count_inital = User.with_permissions('admin').count
  904. assert_equal(1, admin_count_inital)
  905. assert_raises(Exceptions::UnprocessableEntity) do
  906. admin3.update!(roles: Role.where(name: %w[Agent]))
  907. end
  908. admin_count_inital = User.with_permissions('admin').count
  909. assert_equal(1, admin_count_inital)
  910. assert_raises(Exceptions::UnprocessableEntity) do
  911. admin3.active = false
  912. admin3.save!
  913. end
  914. assert_equal(1, User.with_permissions('admin').count)
  915. admin_role = Role.find_by(name: 'Admin')
  916. assert_raises(Exceptions::UnprocessableEntity) do
  917. admin_role.active = false
  918. admin_role.save!
  919. end
  920. assert_raises(Exceptions::UnprocessableEntity) do
  921. admin_role.permission_revoke('admin')
  922. end
  923. assert_equal(1, User.with_permissions('admin').count)
  924. end
  925. test 'only valid agent in group permission check' do
  926. name = rand(999_999_999)
  927. group = Group.create!(
  928. name: "ValidAgentGroupPermission-#{name}",
  929. active: true,
  930. updated_by_id: 1,
  931. created_by_id: 1,
  932. )
  933. roles = Role.where(name: 'Agent')
  934. User.create_or_update(
  935. login: "agent-default-valid_agent_group_permission-1#{name}@example.com",
  936. firstname: 'valid_agent_group_permission-1',
  937. lastname: "Agent#{name}",
  938. email: "agent-default-valid_agent_group_permission-1#{name}@example.com",
  939. password: 'agentpw',
  940. active: true,
  941. roles: roles,
  942. groups: [group],
  943. updated_by_id: 1,
  944. created_by_id: 1,
  945. )
  946. agent2 = User.create_or_update(
  947. login: "agent-default-valid_agent_group_permission-2#{name}@example.com",
  948. firstname: 'valid_agent_group_permission-2',
  949. lastname: "Agent#{name}",
  950. email: "agent-default-valid_agent_group_permission-2#{name}@example.com",
  951. password: 'agentpw',
  952. active: true,
  953. roles: roles,
  954. groups: [group],
  955. updated_by_id: 1,
  956. created_by_id: 1,
  957. )
  958. assert_equal(2, User.group_access(group.id, 'full').count)
  959. agent2.active = false
  960. agent2.save!
  961. assert_equal(1, User.group_access(group.id, 'full').count)
  962. agent2.active = true
  963. agent2.save!
  964. assert_equal(2, User.group_access(group.id, 'full').count)
  965. roles = Role.where(name: 'Customer')
  966. agent2.roles = roles
  967. agent2.save!
  968. assert_equal(1, User.group_access(group.id, 'full').count)
  969. end
  970. test 'preferences[:notification_sound][:enabled] value check' do
  971. name = rand(999_999_999)
  972. roles = Role.where(name: 'Agent')
  973. agent1 = User.create!(
  974. login: "agent-default-preferences-1#{name}@example.com",
  975. firstname: 'valid_agent_group_permission-1',
  976. lastname: "Agent#{name}",
  977. email: "agent-default-preferences-1#{name}@example.com",
  978. password: 'agentpw',
  979. active: true,
  980. roles: roles,
  981. preferences: {
  982. notification_sound: {
  983. enabled: true,
  984. }
  985. },
  986. updated_by_id: 1,
  987. created_by_id: 1,
  988. )
  989. assert_equal(true, agent1.preferences[:notification_sound][:enabled])
  990. agent2 = User.create!(
  991. login: "agent-default-preferences-2#{name}@example.com",
  992. firstname: 'valid_agent_group_permission-2',
  993. lastname: "Agent#{name}",
  994. email: "agent-default-preferences-2#{name}@example.com",
  995. password: 'agentpw',
  996. active: true,
  997. roles: roles,
  998. preferences: {
  999. notification_sound: {
  1000. enabled: false,
  1001. }
  1002. },
  1003. updated_by_id: 1,
  1004. created_by_id: 1,
  1005. )
  1006. assert_equal(false, agent2.preferences[:notification_sound][:enabled])
  1007. agent3 = User.create!(
  1008. login: "agent-default-preferences-3#{name}@example.com",
  1009. firstname: 'valid_agent_group_permission-3',
  1010. lastname: "Agent#{name}",
  1011. email: "agent-default-preferences-3#{name}@example.com",
  1012. password: 'agentpw',
  1013. active: true,
  1014. roles: roles,
  1015. preferences: {
  1016. notification_sound: {
  1017. enabled: true,
  1018. }
  1019. },
  1020. updated_by_id: 1,
  1021. created_by_id: 1,
  1022. )
  1023. assert_equal(true, agent3.preferences[:notification_sound][:enabled])
  1024. agent3.preferences[:notification_sound][:enabled] = 'false'
  1025. agent3.save!
  1026. agent3.reload
  1027. assert_equal(false, agent3.preferences[:notification_sound][:enabled])
  1028. agent4 = User.create!(
  1029. login: "agent-default-preferences-4#{name}@example.com",
  1030. firstname: 'valid_agent_group_permission-4',
  1031. lastname: "Agent#{name}",
  1032. email: "agent-default-preferences-4#{name}@example.com",
  1033. password: 'agentpw',
  1034. active: true,
  1035. roles: roles,
  1036. preferences: {
  1037. notification_sound: {
  1038. enabled: false,
  1039. }
  1040. },
  1041. updated_by_id: 1,
  1042. created_by_id: 1,
  1043. )
  1044. assert_equal(false, agent4.preferences[:notification_sound][:enabled])
  1045. agent4.preferences[:notification_sound][:enabled] = 'true'
  1046. agent4.save!
  1047. agent4.reload
  1048. assert_equal(true, agent4.preferences[:notification_sound][:enabled])
  1049. agent4.preferences[:notification_sound][:enabled] = 'invalid'
  1050. assert_raises(Exceptions::UnprocessableEntity) do
  1051. agent4.save!
  1052. end
  1053. assert_raises(Exceptions::UnprocessableEntity) do
  1054. User.create!(
  1055. login: "agent-default-preferences-5#{name}@example.com",
  1056. firstname: 'valid_agent_group_permission-5',
  1057. lastname: "Agent#{name}",
  1058. email: "agent-default-preferences-5#{name}@example.com",
  1059. password: 'agentpw',
  1060. active: true,
  1061. roles: roles,
  1062. preferences: {
  1063. notification_sound: {
  1064. enabled: 'invalid string',
  1065. }
  1066. },
  1067. updated_by_id: 1,
  1068. created_by_id: 1,
  1069. )
  1070. end
  1071. end
  1072. test 'cleanup references on destroy' do
  1073. agent1 = User.create!(
  1074. login: "agent-cleanup_check-1#{name}@example.com",
  1075. firstname: 'valid_agent_group_permission-1',
  1076. lastname: "Agent#{name}",
  1077. email: "agent-cleanup_check-1#{name}@example.com",
  1078. password: 'agentpw',
  1079. active: true,
  1080. roles: Role.where(name: 'Agent'),
  1081. groups: Group.all,
  1082. updated_by_id: 1,
  1083. created_by_id: 1,
  1084. )
  1085. agent1_id = agent1.id
  1086. assert_equal(1, Avatar.list('User', agent1_id).count)
  1087. UserDevice.add(
  1088. 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36',
  1089. '91.115.248.231',
  1090. agent1_id,
  1091. 'fingerprint1234',
  1092. 'session',
  1093. )
  1094. assert_equal(1, UserDevice.where(user_id: agent1_id).count)
  1095. Karma::User.sync(agent1)
  1096. assert_equal(1, Karma::User.where(user_id: agent1_id).count)
  1097. OnlineNotification.add(
  1098. type: 'Assigned to you',
  1099. object: 'Ticket',
  1100. o_id: 1,
  1101. seen: false,
  1102. user_id: agent1_id,
  1103. created_by_id: 1,
  1104. updated_by_id: 1,
  1105. created_at: Time.zone.now,
  1106. updated_at: Time.zone.now,
  1107. )
  1108. assert_equal(1, OnlineNotification.where(user_id: agent1_id).count)
  1109. Authorization.create!(
  1110. user: agent1,
  1111. uid: '123',
  1112. username: '123',
  1113. provider: 'some',
  1114. token: 'token',
  1115. secret: 'secret',
  1116. )
  1117. assert_equal(1, Authorization.where(user_id: agent1_id).count)
  1118. Cti::CallerId.maybe_add(
  1119. caller_id: '49123456789',
  1120. comment: 'Hairdresser Bob Smith, San Francisco', #optional
  1121. level: 'maybe', # known|maybe
  1122. user_id: agent1_id, # optional
  1123. object: 'Ticket',
  1124. o_id: 1,
  1125. )
  1126. assert_equal(1, Cti::CallerId.where(user_id: agent1_id).count)
  1127. Taskbar.create!(
  1128. client_id: 123,
  1129. key: 'Ticket-1',
  1130. callback: 'TicketZoom',
  1131. params: {
  1132. id: 1,
  1133. },
  1134. state: {},
  1135. user_id: agent1_id,
  1136. prio: 1,
  1137. notify: false,
  1138. )
  1139. assert_equal(1, Taskbar.where(user_id: agent1_id).count)
  1140. ticket1 = Ticket.create!(
  1141. title: 'test 1234-1',
  1142. group: Group.lookup(name: 'Users'),
  1143. customer_id: 2,
  1144. owner_id: 2,
  1145. updated_by_id: 1,
  1146. created_by_id: 1,
  1147. )
  1148. RecentView.log(ticket1.class.to_s, ticket1.id, agent1)
  1149. assert_equal(1, RecentView.where(created_by_id: agent1_id).count)
  1150. Token.create!(action: 'api', user_id: agent1_id)
  1151. StatsStore.create(
  1152. stats_storable: agent1,
  1153. key: 'some_key',
  1154. data: { A: 1, B: 2 },
  1155. created_at: Time.zone.now,
  1156. created_by_id: 1,
  1157. )
  1158. item = StatsStore.find_by(
  1159. stats_storable: agent1,
  1160. key: 'some_key',
  1161. )
  1162. assert(item)
  1163. agent1.destroy!
  1164. assert_equal(0, UserDevice.where(user_id: agent1_id).count)
  1165. assert_equal(0, Avatar.list('User', agent1_id, false).count)
  1166. assert_equal(0, Karma::User.where(user_id: agent1_id).count)
  1167. assert_equal(0, OnlineNotification.where(user_id: agent1_id).count)
  1168. assert_equal(0, Authorization.where(user_id: agent1_id).count)
  1169. assert_equal(0, Cti::CallerId.where(user_id: agent1_id).count)
  1170. assert_equal(0, Taskbar.where(user_id: agent1_id).count)
  1171. assert_equal(0, RecentView.where(created_by_id: agent1_id).count)
  1172. assert_equal(0, Token.where(user_id: agent1_id).count)
  1173. assert_equal(0, Token.where(user_id: agent1_id).count)
  1174. item = StatsStore.find_by(
  1175. stats_storable: agent1,
  1176. key: 'some_key',
  1177. )
  1178. assert_nil(item)
  1179. end
  1180. test 'adding group drops cache' do
  1181. agent1 = User.create!(
  1182. login: "agent-cleanup_check-1#{name}@example.com",
  1183. firstname: 'valid_agent_group_permission-1',
  1184. lastname: "Agent#{name}",
  1185. email: "agent-cleanup_check-1#{name}@example.com",
  1186. password: 'agentpw',
  1187. active: true,
  1188. roles: Role.where(name: 'Agent'),
  1189. groups: Group.all,
  1190. updated_by_id: 1,
  1191. created_by_id: 1,
  1192. )
  1193. group1 = Group.create_or_update(
  1194. name: "GroupWithoutPermission-#{rand(9_999_999_999)}",
  1195. active: true,
  1196. updated_by_id: 1,
  1197. created_by_id: 1,
  1198. )
  1199. differences = %w[
  1200. group1.attributes_with_association_ids['user_ids'].count
  1201. agent1.attributes_with_association_ids['group_ids'].keys.count
  1202. ]
  1203. assert_difference differences, 1 do
  1204. agent1.groups << group1
  1205. end
  1206. end
  1207. end