object_create_update_with_ref_name_test.rb 5.4 KB

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