object_create_update_with_ref_name_test.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. require 'test_helper'
  2. class ObjectCreateUpdateWithRefNameTest < ActiveSupport::TestCase
  3. test 'organization' do
  4. roles = Role.where(name: %w[Agent Admin])
  5. groups = Group.all
  6. user1 = User.create_or_update(
  7. login: 'object_ref_name1@example.org',
  8. firstname: 'object_ref_name1',
  9. lastname: 'object_ref_name1',
  10. email: 'object_ref_name1@example.org',
  11. password: 'some_pass',
  12. active: true,
  13. updated_by_id: 1,
  14. created_by_id: 1,
  15. roles: roles,
  16. groups: groups,
  17. )
  18. user2 = User.create_or_update(
  19. login: 'object_ref_name2@example.org',
  20. firstname: 'object_ref_name2',
  21. lastname: 'object_ref_name2',
  22. email: 'object_ref_name2@example.org',
  23. password: 'some_pass',
  24. active: true,
  25. updated_by_id: 1,
  26. created_by_id: 1,
  27. roles: roles,
  28. groups: groups,
  29. )
  30. org1 = Organization.create_if_not_exists_with_ref(
  31. name: 'some org update_with_ref member',
  32. members: ['object_ref_name1@example.org'],
  33. updated_by_id: 1,
  34. created_by_id: 1,
  35. )
  36. assert(org1.member_ids.sort.include?(user1.id))
  37. assert_not(org1.member_ids.sort.include?(user2.id))
  38. org2 = Organization.create_or_update_with_ref(
  39. name: 'some org update_with_ref member',
  40. members: ['object_ref_name2@example.org'],
  41. updated_by_id: 1,
  42. created_by_id: 1,
  43. )
  44. assert_not(org2.member_ids.sort.include?(user1.id))
  45. assert(org2.member_ids.sort.include?(user2.id))
  46. assert_equal(org1.id, org2.id)
  47. org3 = Organization.create_or_update_with_ref(
  48. name: 'some org update_with_ref member2',
  49. members: ['object_ref_name2@example.org'],
  50. updated_by_id: 1,
  51. created_by_id: 1,
  52. )
  53. assert_not(org3.member_ids.sort.include?(user1.id))
  54. assert(org3.member_ids.sort.include?(user2.id))
  55. assert_not_equal(org2.id, org3.id)
  56. assert_raises( ActiveRecord::AssociationTypeMismatch ) do
  57. Organization.create_or_update_with_ref(
  58. name: 'some org update_with_ref member2',
  59. members: ['object_ref_name2@example.org'],
  60. member_ids: [2],
  61. updated_by_id: 1,
  62. created_by_id: 1,
  63. )
  64. end
  65. end
  66. test 'user' do
  67. Organization.create_if_not_exists_with_ref(
  68. name: 'some org update_with_ref user',
  69. updated_by_id: 1,
  70. created_by_id: 1,
  71. )
  72. user1 = User.create_or_update_with_ref(
  73. login: 'object_ref_name1@example.org',
  74. firstname: 'object_ref_name1',
  75. lastname: 'object_ref_name1',
  76. email: 'object_ref_name1@example.org',
  77. password: 'some_pass',
  78. active: true,
  79. organization: 'some org update_with_ref user',
  80. updated_by_id: 1,
  81. created_by_id: 1,
  82. roles: %w[Agent Admin],
  83. groups: ['Users'],
  84. )
  85. user2 = User.create_or_update_with_ref(
  86. login: 'object_ref_name2@example.org',
  87. firstname: 'object_ref_name2',
  88. lastname: 'object_ref_name2',
  89. email: 'object_ref_name2@example.org',
  90. password: 'some_pass',
  91. organization_id: nil,
  92. active: true,
  93. updated_by_id: 1,
  94. created_by_id: 1,
  95. roles: ['Customer'],
  96. groups: [],
  97. )
  98. admin_role = Role.lookup(name: 'Admin')
  99. agent_role = Role.lookup(name: 'Agent')
  100. customer_role = Role.lookup(name: 'Customer')
  101. users_group = Group.lookup(name: 'Users')
  102. assert(user1.organization.name, 'some org update_with_ref user')
  103. assert(user1.group_ids.include?(users_group.id))
  104. assert(user1.role_ids.include?(admin_role.id))
  105. assert(user1.role_ids.include?(agent_role.id))
  106. assert_not(user1.role_ids.include?(customer_role.id))
  107. assert_nil(user2.organization_id)
  108. assert_not(user2.group_ids.include?(users_group.id))
  109. assert_not(user2.role_ids.include?(admin_role.id))
  110. assert_not(user2.role_ids.include?(agent_role.id))
  111. assert(user2.role_ids.include?(customer_role.id))
  112. end
  113. test 'group' do
  114. user1 = User.create_or_update_with_ref(
  115. login: 'object_ref_name1@example.org',
  116. firstname: 'object_ref_name1',
  117. lastname: 'object_ref_name1',
  118. email: 'object_ref_name1@example.org',
  119. password: 'some_pass',
  120. active: true,
  121. organization_id: nil,
  122. updated_by_id: 1,
  123. created_by_id: 1,
  124. roles: %w[Agent Admin],
  125. groups: [],
  126. )
  127. user2 = User.create_or_update_with_ref(
  128. login: 'object_ref_name2@example.org',
  129. firstname: 'object_ref_name2',
  130. lastname: 'object_ref_name2',
  131. email: 'object_ref_name2@example.org',
  132. password: 'some_pass',
  133. organization_id: nil,
  134. active: true,
  135. updated_by_id: 1,
  136. created_by_id: 1,
  137. roles: ['Customer'],
  138. groups: [],
  139. )
  140. group1 = Group.create_if_not_exists_with_ref(
  141. name: 'some group update_with_ref',
  142. users: ['object_ref_name1@example.org'],
  143. updated_by_id: 1,
  144. created_by_id: 1,
  145. )
  146. assert(group1.name, 'some group update_with_ref')
  147. assert(group1.user_ids.include?(user1.id))
  148. assert_not(group1.user_ids.include?(user2.id))
  149. end
  150. end