user_assets_test.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # NOTE: This test file is _almost_ fully migrated to RSpec, as of 4cc64d0ce.
  2. # It may be deleted once all missing spec coverage has been added.
  3. #
  4. # What's missing is coverage for
  5. # the non-standard implementation of #assets on the User class.
  6. # (It adds an { accounts: {} } key-value pair
  7. # to the resulting User attributes hash;
  8. # see lines 75:83:91:109:123:131:139 of this file).
  9. #
  10. # This omission is discussed in detail in
  11. # https://git.znuny.com/zammad/zammad/merge_requests/363
  12. require 'test_helper'
  13. class UserAssetsTest < ActiveSupport::TestCase
  14. test 'assets' do
  15. roles = Role.where(name: %w[Agent Admin])
  16. groups = Group.all
  17. org1 = Organization.create_or_update(
  18. name: 'some user org',
  19. updated_by_id: 1,
  20. created_by_id: 1,
  21. )
  22. user1 = User.create_or_update(
  23. login: 'assets1@example.org',
  24. firstname: 'assets1',
  25. lastname: 'assets1',
  26. email: 'assets1@example.org',
  27. password: 'some_pass',
  28. active: true,
  29. updated_by_id: 1,
  30. created_by_id: 1,
  31. organization_id: org1.id,
  32. roles: roles,
  33. groups: groups,
  34. )
  35. user2 = User.create_or_update(
  36. login: 'assets2@example.org',
  37. firstname: 'assets2',
  38. lastname: 'assets2',
  39. email: 'assets2@example.org',
  40. password: 'some_pass',
  41. active: true,
  42. updated_by_id: 1,
  43. created_by_id: 1,
  44. roles: roles,
  45. groups: groups,
  46. )
  47. user3 = User.create_or_update(
  48. login: 'assets3@example.org',
  49. firstname: 'assets3',
  50. lastname: 'assets3',
  51. email: 'assets3@example.org',
  52. password: 'some_pass',
  53. active: true,
  54. updated_by_id: user1.id,
  55. created_by_id: user2.id,
  56. roles: roles,
  57. groups: groups,
  58. )
  59. user3 = User.find(user3.id)
  60. assets = user3.assets({})
  61. org1 = Organization.find(org1.id)
  62. attributes = org1.attributes_with_association_ids
  63. attributes.delete('user_ids')
  64. assert(diff(attributes, assets[:Organization][org1.id]), 'check assets')
  65. user1 = User.find(user1.id)
  66. attributes = user1.attributes_with_association_ids
  67. attributes['accounts'] = {}
  68. attributes.delete('password')
  69. attributes.delete('token_ids')
  70. attributes.delete('authorization_ids')
  71. assert(diff(attributes, assets[:User][user1.id]), 'check assets')
  72. user2 = User.find(user2.id)
  73. attributes = user2.attributes_with_association_ids
  74. attributes['accounts'] = {}
  75. attributes.delete('password')
  76. attributes.delete('token_ids')
  77. attributes.delete('authorization_ids')
  78. assert(diff(attributes, assets[:User][user2.id]), 'check assets')
  79. user3 = User.find(user3.id)
  80. attributes = user3.attributes_with_association_ids
  81. attributes['accounts'] = {}
  82. attributes.delete('password')
  83. attributes.delete('token_ids')
  84. attributes.delete('authorization_ids')
  85. assert(diff(attributes, assets[:User][user3.id]), 'check assets')
  86. user3.destroy!
  87. user2.destroy!
  88. user1.destroy!
  89. org1.destroy!
  90. end
  91. def diff(object1, object2)
  92. return true if object1 == object2
  93. %w[updated_at created_at].each do |item|
  94. if object1[item]
  95. object1[item] = object1[item].to_s
  96. end
  97. if object2[item]
  98. object2[item] = object2[item].to_s
  99. end
  100. end
  101. return true if (object1.to_a - object2.to_a).blank?
  102. #puts "ERROR: difference \n1: #{object1.inspect}\n2: #{object2.inspect}\ndiff: #{(object1.to_a - object2.to_a).inspect}"
  103. false
  104. end
  105. end