role_validate_agent_limit_test.rb 6.3 KB

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