user_assets_test.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. require 'test_helper'
  2. class UserAssetsTest < ActiveSupport::TestCase
  3. test 'assets' do
  4. roles = Role.where(name: %w[Agent Admin])
  5. groups = Group.all
  6. org1 = Organization.create_or_update(
  7. name: 'some user org',
  8. updated_by_id: 1,
  9. created_by_id: 1,
  10. )
  11. user1 = User.create_or_update(
  12. login: 'assets1@example.org',
  13. firstname: 'assets1',
  14. lastname: 'assets1',
  15. email: 'assets1@example.org',
  16. password: 'some_pass',
  17. active: true,
  18. updated_by_id: 1,
  19. created_by_id: 1,
  20. organization_id: org1.id,
  21. roles: roles,
  22. groups: groups,
  23. )
  24. user2 = User.create_or_update(
  25. login: 'assets2@example.org',
  26. firstname: 'assets2',
  27. lastname: 'assets2',
  28. email: 'assets2@example.org',
  29. password: 'some_pass',
  30. active: true,
  31. updated_by_id: 1,
  32. created_by_id: 1,
  33. roles: roles,
  34. groups: groups,
  35. )
  36. user3 = User.create_or_update(
  37. login: 'assets3@example.org',
  38. firstname: 'assets3',
  39. lastname: 'assets3',
  40. email: 'assets3@example.org',
  41. password: 'some_pass',
  42. active: true,
  43. updated_by_id: user1.id,
  44. created_by_id: user2.id,
  45. roles: roles,
  46. groups: groups,
  47. )
  48. user3 = User.find(user3.id)
  49. assets = user3.assets({})
  50. org1 = Organization.find(org1.id)
  51. attributes = org1.attributes_with_association_ids
  52. attributes.delete('user_ids')
  53. assert(diff(attributes, assets[:Organization][org1.id]), 'check assets')
  54. user1 = User.find(user1.id)
  55. attributes = user1.attributes_with_association_ids
  56. attributes['accounts'] = {}
  57. attributes.delete('password')
  58. attributes.delete('token_ids')
  59. attributes.delete('authorization_ids')
  60. assert(diff(attributes, assets[:User][user1.id]), 'check assets')
  61. user2 = User.find(user2.id)
  62. attributes = user2.attributes_with_association_ids
  63. attributes['accounts'] = {}
  64. attributes.delete('password')
  65. attributes.delete('token_ids')
  66. attributes.delete('authorization_ids')
  67. assert(diff(attributes, assets[:User][user2.id]), 'check assets')
  68. user3 = User.find(user3.id)
  69. attributes = user3.attributes_with_association_ids
  70. attributes['accounts'] = {}
  71. attributes.delete('password')
  72. attributes.delete('token_ids')
  73. attributes.delete('authorization_ids')
  74. assert(diff(attributes, assets[:User][user3.id]), 'check assets')
  75. # touch org, check if user1 has changed
  76. travel 2.seconds
  77. org2 = Organization.find(org1.id)
  78. org2.note = "some note...#{rand(9_999_999_999_999)}"
  79. org2.save!
  80. attributes = org2.attributes_with_association_ids
  81. attributes.delete('user_ids')
  82. assert_not(diff(attributes, assets[:Organization][org2.id]), 'check assets')
  83. user1_new = User.find(user1.id)
  84. attributes = user1_new.attributes_with_association_ids
  85. attributes['accounts'] = {}
  86. attributes.delete('password')
  87. attributes.delete('token_ids')
  88. attributes.delete('authorization_ids')
  89. assert_not(diff(attributes, assets[:User][user1_new.id]), 'check assets')
  90. # check new assets lookup
  91. assets = user3.assets({})
  92. attributes = org2.attributes_with_association_ids
  93. attributes.delete('user_ids')
  94. assert(diff(attributes, assets[:Organization][org1.id]), 'check assets')
  95. user1 = User.find(user1.id)
  96. attributes = user1.attributes_with_association_ids
  97. attributes['accounts'] = {}
  98. attributes.delete('password')
  99. attributes.delete('token_ids')
  100. attributes.delete('authorization_ids')
  101. assert(diff(attributes, assets[:User][user1.id]), 'check assets')
  102. user2 = User.find(user2.id)
  103. attributes = user2.attributes_with_association_ids
  104. attributes['accounts'] = {}
  105. attributes.delete('password')
  106. attributes.delete('token_ids')
  107. attributes.delete('authorization_ids')
  108. assert(diff(attributes, assets[:User][user2.id]), 'check assets')
  109. user3 = User.find(user3.id)
  110. attributes = user3.attributes_with_association_ids
  111. attributes['accounts'] = {}
  112. attributes.delete('password')
  113. attributes.delete('token_ids')
  114. attributes.delete('authorization_ids')
  115. assert(diff(attributes, assets[:User][user3.id]), 'check assets')
  116. travel_back
  117. user3.destroy!
  118. user2.destroy!
  119. user1.destroy!
  120. org1.destroy!
  121. assert_not(Organization.find_by(id: org2.id))
  122. end
  123. def diff(object1, object2)
  124. return true if object1 == object2
  125. %w[updated_at created_at].each do |item|
  126. if object1[item]
  127. object1[item] = object1[item].to_s
  128. end
  129. if object2[item]
  130. object2[item] = object2[item].to_s
  131. end
  132. end
  133. return true if (object1.to_a - object2.to_a).blank?
  134. #puts "ERROR: difference \n1: #{object1.inspect}\n2: #{object2.inspect}\ndiff: #{(object1.to_a - object2.to_a).inspect}"
  135. false
  136. end
  137. end