role_validate_agent_limit_test.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class RoleValidateAgentLimit < ActiveSupport::TestCase
  4. test 'role validate agent limit' do
  5. agent_max = User.with_permissions('ticket.agent').count
  6. UserInfo.current_user_id = 1
  7. Setting.set('system_agent_limit', agent_max + 2)
  8. permission_ticket_agent = Permission.where(name: 'ticket.agent')
  9. role_agent_limit_success = Role.create!(
  10. name: 'agent-limit-test-success',
  11. note: 'agent-limit-test-success Role.',
  12. permissions: [],
  13. active: true,
  14. )
  15. role_agent_limit_fail = Role.create!(
  16. name: 'agent-limit-test-fail',
  17. note: 'agent-limit-test-fail Role.',
  18. permissions: [],
  19. active: true,
  20. )
  21. user1 = User.create!(
  22. firstname: 'Firstname',
  23. lastname: 'Lastname',
  24. email: 'some-agentlimit-role@example.com',
  25. login: 'some-agentlimit-role@example.com',
  26. roles: [role_agent_limit_success],
  27. active: true,
  28. )
  29. user2 = User.create!(
  30. firstname: 'Firstname1',
  31. lastname: 'Lastname1',
  32. email: 'some-agentlimit-role-1@example.com',
  33. login: 'some-agentlimit-role-1@example.com',
  34. roles: [role_agent_limit_success],
  35. active: true,
  36. )
  37. user3 = User.create!(
  38. firstname: 'Firstname2',
  39. lastname: 'Lastname2',
  40. email: 'some-agentlimit-role-2@example.com',
  41. login: 'some-agentlimit-role-2@example.com',
  42. roles: [role_agent_limit_fail],
  43. active: true,
  44. )
  45. role_agent_limit_success.permissions = permission_ticket_agent
  46. assert_raises(Exceptions::UnprocessableEntity) do
  47. role_agent_limit_fail.permissions = permission_ticket_agent
  48. end
  49. role_agent_limit_fail.active = false
  50. role_agent_limit_fail.save!
  51. role_agent_limit_fail.permissions = permission_ticket_agent
  52. assert_raises(Exceptions::UnprocessableEntity) do
  53. role_agent_limit_fail.active = true
  54. role_agent_limit_fail.save!
  55. end
  56. user1.destroy!
  57. user2.destroy!
  58. user3.destroy!
  59. role_agent_limit_success.destroy!
  60. role_agent_limit_fail.destroy!
  61. Setting.set('system_agent_limit', nil)
  62. end
  63. test 'role validate agent limit as string' do
  64. agent_max = User.with_permissions('ticket.agent').count
  65. UserInfo.current_user_id = 1
  66. Setting.set('system_agent_limit', (agent_max + 2).to_s)
  67. permission_ticket_agent = Permission.where(name: 'ticket.agent')
  68. role_agent_limit_success = Role.create!(
  69. name: 'agent-limit-test-success',
  70. note: 'agent-limit-test-success Role.',
  71. permissions: [],
  72. active: true,
  73. )
  74. role_agent_limit_fail = Role.create!(
  75. name: 'agent-limit-test-fail',
  76. note: 'agent-limit-test-fail Role.',
  77. permissions: [],
  78. active: true,
  79. )
  80. user1 = User.create!(
  81. firstname: 'Firstname',
  82. lastname: 'Lastname',
  83. email: 'some-agentlimit-role@example.com',
  84. login: 'some-agentlimit-role@example.com',
  85. roles: [role_agent_limit_success],
  86. active: true,
  87. )
  88. user2 = User.create!(
  89. firstname: 'Firstname1',
  90. lastname: 'Lastname1',
  91. email: 'some-agentlimit-role-1@example.com',
  92. login: 'some-agentlimit-role-1@example.com',
  93. roles: [role_agent_limit_success],
  94. active: true,
  95. )
  96. user3 = User.create!(
  97. firstname: 'Firstname2',
  98. lastname: 'Lastname2',
  99. email: 'some-agentlimit-role-2@example.com',
  100. login: 'some-agentlimit-role-2@example.com',
  101. roles: [role_agent_limit_fail],
  102. active: true,
  103. )
  104. role_agent_limit_success.permissions = permission_ticket_agent
  105. assert_raises(Exceptions::UnprocessableEntity) do
  106. role_agent_limit_fail.permissions = permission_ticket_agent
  107. end
  108. role_agent_limit_fail.active = false
  109. role_agent_limit_fail.save!
  110. role_agent_limit_fail.permissions = permission_ticket_agent
  111. assert_raises(Exceptions::UnprocessableEntity) do
  112. role_agent_limit_fail.active = true
  113. role_agent_limit_fail.save!
  114. end
  115. user1.destroy!
  116. user2.destroy!
  117. user3.destroy!
  118. role_agent_limit_success.destroy!
  119. role_agent_limit_fail.destroy!
  120. Setting.set('system_agent_limit', nil)
  121. end
  122. test 'role validate agent limit - 1 user 2 ticket.agent roles' do
  123. current_agent_max = User.with_permissions('ticket.agent').count + 1
  124. UserInfo.current_user_id = 1
  125. Setting.set('system_agent_limit', current_agent_max)
  126. permission_ticket_agent = Permission.find_by(name: 'ticket.agent')
  127. role_agent_limit1 = Role.create!(
  128. name: 'agent-limit-test1',
  129. note: 'agent-limit-test1 Role.',
  130. permissions: [permission_ticket_agent],
  131. active: true,
  132. )
  133. role_agent_limit2 = Role.create!(
  134. name: 'agent-limit-test2',
  135. note: 'agent-limit-test2 Role.',
  136. permissions: [permission_ticket_agent],
  137. active: true,
  138. )
  139. user1 = User.create!(
  140. firstname: 'Firstname',
  141. lastname: 'Lastname',
  142. email: 'some-agentlimit-role@example.com',
  143. login: 'some-agentlimit-role@example.com',
  144. roles: [role_agent_limit1, role_agent_limit2],
  145. active: true,
  146. )
  147. user1.roles = Role.where(name: %w[Admin Agent])
  148. user1.role_ids = [Role.find_by(name: 'Agent').id]
  149. user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id]
  150. user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id, role_agent_limit2.id]
  151. assert_raises(Exceptions::UnprocessableEntity) do
  152. User.create!(
  153. firstname: 'Firstname2',
  154. lastname: 'Lastname2',
  155. email: 'some-agentlimit-role-2@example.com',
  156. login: 'some-agentlimit-role-2@example.com',
  157. roles: [role_agent_limit1],
  158. active: true,
  159. )
  160. end
  161. assert_equal(current_agent_max, User.with_permissions('ticket.agent').count)
  162. current_agent_max = User.with_permissions('ticket.agent').count + 1
  163. Setting.set('system_agent_limit', current_agent_max)
  164. User.create!(
  165. firstname: 'Firstname',
  166. lastname: 'Lastname',
  167. email: 'some-agentlimit-role-3@example.com',
  168. login: 'some-agentlimit-role-3@example.com',
  169. role_ids: [Role.find_by(name: 'Agent').id],
  170. active: true,
  171. )
  172. role_agent_limit1.destroy!
  173. role_agent_limit2.destroy!
  174. Setting.set('system_agent_limit', nil)
  175. end
  176. end