organization_assets_test.rb 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. require 'test_helper'
  2. class OrganizationAssetsTest < ActiveSupport::TestCase
  3. test 'assets' do
  4. roles = Role.where( name: %w[Agent Admin] )
  5. admin1 = User.create_or_update(
  6. login: 'admin1@example.org',
  7. firstname: 'admin1',
  8. lastname: 'admin1',
  9. email: 'admin1@example.org',
  10. password: 'some_pass',
  11. active: true,
  12. updated_by_id: 1,
  13. created_by_id: 1,
  14. roles: roles,
  15. )
  16. roles = Role.where( name: %w[Customer] )
  17. org = Organization.create_or_update(
  18. name: 'some customer org',
  19. updated_by_id: admin1.id,
  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: org.id,
  32. roles: roles,
  33. )
  34. user2 = User.create_or_update(
  35. login: 'assets2@example.org',
  36. firstname: 'assets2',
  37. lastname: 'assets2',
  38. email: 'assets2@example.org',
  39. password: 'some_pass',
  40. active: true,
  41. updated_by_id: 1,
  42. created_by_id: 1,
  43. organization_id: org.id,
  44. roles: roles,
  45. )
  46. user3 = User.create_or_update(
  47. login: 'assets3@example.org',
  48. firstname: 'assets3',
  49. lastname: 'assets3',
  50. email: 'assets3@example.org',
  51. password: 'some_pass',
  52. active: true,
  53. updated_by_id: user1.id,
  54. created_by_id: user2.id,
  55. roles: roles,
  56. )
  57. org = Organization.find(org.id)
  58. assets = org.assets({})
  59. attributes = org.attributes_with_association_ids
  60. attributes.delete('user_ids')
  61. assert( diff(attributes, assets[:Organization][org.id]), 'check assets' )
  62. admin1 = User.find(admin1.id)
  63. attributes = admin1.attributes_with_association_ids
  64. attributes['accounts'] = {}
  65. attributes.delete('password')
  66. attributes.delete('token_ids')
  67. attributes.delete('authorization_ids')
  68. assert( diff(attributes, assets[:User][admin1.id]), 'check assets' )
  69. user1 = User.find(user1.id)
  70. attributes = user1.attributes_with_association_ids
  71. attributes['accounts'] = {}
  72. attributes.delete('password')
  73. attributes.delete('token_ids')
  74. attributes.delete('authorization_ids')
  75. assert( diff(attributes, assets[:User][user1.id]), 'check assets' )
  76. user2 = User.find(user2.id)
  77. attributes = user2.attributes_with_association_ids
  78. attributes['accounts'] = {}
  79. attributes.delete('password')
  80. attributes.delete('token_ids')
  81. attributes.delete('authorization_ids')
  82. assert( diff(attributes, assets[:User][user2.id]), 'check assets' )
  83. user3 = User.find(user3.id)
  84. attributes = user3.attributes_with_association_ids
  85. attributes['accounts'] = {}
  86. attributes.delete('password')
  87. attributes.delete('token_ids')
  88. attributes.delete('authorization_ids')
  89. assert_nil( assets[:User][user3.id], 'check assets' )
  90. # touch user 2, check if org has changed
  91. travel 2.seconds
  92. user_new_2 = User.find(user2.id)
  93. user_new_2.lastname = 'assets2'
  94. user_new_2.save!
  95. org_new = Organization.find(org.id)
  96. attributes = org_new.attributes_with_association_ids
  97. attributes.delete('user_ids')
  98. assert_not(diff(attributes, assets[:Organization][org_new.id]), 'check assets' )
  99. attributes = user_new_2.attributes_with_association_ids
  100. attributes['accounts'] = {}
  101. attributes.delete('password')
  102. attributes.delete('token_ids')
  103. attributes.delete('authorization_ids')
  104. assert( diff(attributes, assets[:User][user_new_2.id]), 'check assets' )
  105. # check new assets lookup
  106. assets = org_new.assets({})
  107. attributes = org_new.attributes_with_association_ids
  108. attributes.delete('user_ids')
  109. assert( diff(attributes, assets[:Organization][org_new.id]), 'check assets' )
  110. attributes = user_new_2.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][user_new_2.id]), 'check assets' )
  116. travel_back
  117. user3.destroy!
  118. user2.destroy!
  119. user1.destroy!
  120. org.destroy!
  121. assert_not(Organization.find_by(id: org_new.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