user_assets_test.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. # touch org, check if user1 has changed
  87. travel 2.seconds
  88. org2 = Organization.find(org1.id)
  89. org2.note = "some note...#{rand(9_999_999_999_999)}"
  90. org2.save!
  91. attributes = org2.attributes_with_association_ids
  92. attributes.delete('user_ids')
  93. assert_not(diff(attributes, assets[:Organization][org2.id]), 'check assets')
  94. user1_new = User.find(user1.id)
  95. attributes = user1_new.attributes_with_association_ids
  96. attributes['accounts'] = {}
  97. attributes.delete('password')
  98. attributes.delete('token_ids')
  99. attributes.delete('authorization_ids')
  100. assert_not(diff(attributes, assets[:User][user1_new.id]), 'check assets')
  101. # check new assets lookup
  102. assets = user3.assets({})
  103. attributes = org2.attributes_with_association_ids
  104. attributes.delete('user_ids')
  105. assert(diff(attributes, assets[:Organization][org1.id]), 'check assets')
  106. user1 = User.find(user1.id)
  107. attributes = user1.attributes_with_association_ids
  108. attributes['accounts'] = {}
  109. attributes.delete('password')
  110. attributes.delete('token_ids')
  111. attributes.delete('authorization_ids')
  112. assert(diff(attributes, assets[:User][user1.id]), 'check assets')
  113. user2 = User.find(user2.id)
  114. attributes = user2.attributes_with_association_ids
  115. attributes['accounts'] = {}
  116. attributes.delete('password')
  117. attributes.delete('token_ids')
  118. attributes.delete('authorization_ids')
  119. assert(diff(attributes, assets[:User][user2.id]), 'check assets')
  120. user3 = User.find(user3.id)
  121. attributes = user3.attributes_with_association_ids
  122. attributes['accounts'] = {}
  123. attributes.delete('password')
  124. attributes.delete('token_ids')
  125. attributes.delete('authorization_ids')
  126. assert(diff(attributes, assets[:User][user3.id]), 'check assets')
  127. travel_back
  128. user3.destroy!
  129. user2.destroy!
  130. user1.destroy!
  131. org1.destroy!
  132. assert_not(Organization.find_by(id: org2.id))
  133. end
  134. def diff(object1, object2)
  135. return true if object1 == object2
  136. %w[updated_at created_at].each do |item|
  137. if object1[item]
  138. object1[item] = object1[item].to_s
  139. end
  140. if object2[item]
  141. object2[item] = object2[item].to_s
  142. end
  143. end
  144. return true if (object1.to_a - object2.to_a).blank?
  145. #puts "ERROR: difference \n1: #{object1.inspect}\n2: #{object2.inspect}\ndiff: #{(object1.to_a - object2.to_a).inspect}"
  146. false
  147. end
  148. end