role_validate_agent_limit_test.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 - 1 user 2 ticket.agent roles' do
  63. current_agent_max = User.with_permissions('ticket.agent').count + 1
  64. UserInfo.current_user_id = 1
  65. Setting.set('system_agent_limit', current_agent_max)
  66. permission_ticket_agent = Permission.find_by(name: 'ticket.agent')
  67. role_agent_limit1 = Role.create!(
  68. name: 'agent-limit-test1',
  69. note: 'agent-limit-test1 Role.',
  70. permissions: [permission_ticket_agent],
  71. active: true,
  72. )
  73. role_agent_limit2 = Role.create!(
  74. name: 'agent-limit-test2',
  75. note: 'agent-limit-test2 Role.',
  76. permissions: [permission_ticket_agent],
  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_limit1, role_agent_limit2],
  85. active: true,
  86. )
  87. user1.roles = Role.where(name: %w[Admin Agent])
  88. user1.role_ids = [Role.find_by(name: 'Agent').id]
  89. user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id]
  90. user1.role_ids = [Role.find_by(name: 'Agent').id, role_agent_limit1.id, role_agent_limit2.id]
  91. assert_raises(Exceptions::UnprocessableEntity) do
  92. user2 = User.create!(
  93. firstname: 'Firstname2',
  94. lastname: 'Lastname2',
  95. email: 'some-agentlimit-role-2@example.com',
  96. login: 'some-agentlimit-role-2@example.com',
  97. roles: [role_agent_limit1],
  98. active: true,
  99. )
  100. end
  101. assert_equal(current_agent_max, User.with_permissions('ticket.agent').count)
  102. current_agent_max = User.with_permissions('ticket.agent').count + 1
  103. Setting.set('system_agent_limit', current_agent_max)
  104. user3 = User.create!(
  105. firstname: 'Firstname',
  106. lastname: 'Lastname',
  107. email: 'some-agentlimit-role-3@example.com',
  108. login: 'some-agentlimit-role-3@example.com',
  109. role_ids: [Role.find_by(name: 'Agent').id],
  110. active: true,
  111. )
  112. role_agent_limit1.destroy!
  113. role_agent_limit2.destroy!
  114. Setting.set('system_agent_limit', nil)
  115. end
  116. end