object_cache_test.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. require 'test_helper'
  2. class ObjectCacheTest < ActiveSupport::TestCase
  3. test 'organization cache' do
  4. org = Organization.create_or_update(
  5. name: 'some org cache member',
  6. updated_by_id: 1,
  7. created_by_id: 1,
  8. )
  9. roles = Role.where( name: %w[Agent Admin] )
  10. groups = Group.all
  11. user1 = User.create_or_update(
  12. login: 'object_cache1@example.org',
  13. firstname: 'object_cache1',
  14. lastname: 'object_cache1',
  15. email: 'object_cache1@example.org',
  16. password: 'some_pass',
  17. active: true,
  18. updated_by_id: 1,
  19. created_by_id: 1,
  20. organization_id: org.id,
  21. roles: roles,
  22. groups: groups,
  23. )
  24. assets = org.assets({})
  25. assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
  26. user1.organization_id = nil
  27. user1.save
  28. org.reload
  29. assets = org.assets({})
  30. assert_equal(org.member_ids.sort, assets[:Organization][org.id]['member_ids'].sort)
  31. end
  32. test 'user cache' do
  33. roles = Role.where(name: %w[Agent Admin])
  34. groups = Group.all.order(:id)
  35. # be sure that minimum one admin is available
  36. User.create_or_update(
  37. login: 'last_admin_check@example.org',
  38. firstname: 'last_admin_check',
  39. lastname: 'last_admin_check',
  40. email: 'last_admin_check@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. user1 = User.create_or_update(
  49. login: 'object_cache1@example.org',
  50. firstname: 'object_cache1',
  51. lastname: 'object_cache1',
  52. email: 'object_cache1@example.org',
  53. password: 'some_pass',
  54. active: true,
  55. updated_by_id: 1,
  56. created_by_id: 1,
  57. roles: roles,
  58. groups: groups,
  59. )
  60. assets = user1.assets({})
  61. assert_equal(user1.group_ids_access_map.sort, assets[:User][user1.id]['group_ids'].sort)
  62. # update group
  63. group1 = groups.first
  64. group1.note = "some note #{rand(9_999_999_999)}"
  65. group1.save
  66. assets = user1.assets({})
  67. assert(assets[:Group][group1.id])
  68. assert_equal(group1.note, assets[:Group][group1.id]['note'])
  69. # update group
  70. assert_equal(user1.group_ids_access_map.sort, assets[:User][user1.id]['group_ids'].sort)
  71. user1.group_ids = []
  72. user1.save
  73. assets = user1.assets({})
  74. assert_equal(user1.group_ids_access_map.sort, assets[:User][user1.id]['group_ids'].sort)
  75. # update role
  76. assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
  77. user1.role_ids = []
  78. user1.save
  79. assets = user1.assets({})
  80. assert_equal(user1.role_ids.sort, assets[:User][user1.id]['role_ids'].sort)
  81. # update groups
  82. assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
  83. user1.organization_ids = [1]
  84. user1.save
  85. assets = user1.assets({})
  86. assert_equal(user1.organization_ids.sort, assets[:User][user1.id]['organization_ids'].sort)
  87. end
  88. end