user_assets_test.rb 3.6 KB

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