role_test.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. require 'test_helper'
  2. class RoleTest < ActiveSupport::TestCase
  3. test 'permission' do
  4. permission_test = Permission.create_or_update(
  5. name: 'test',
  6. note: 'parent test permission',
  7. preferences: {
  8. disabled: true
  9. },
  10. )
  11. permission_test_agent = Permission.create_or_update(
  12. name: 'test.agent',
  13. note: 'agent test permission',
  14. preferences: {
  15. not: ['test.customer'],
  16. },
  17. )
  18. permission_test_customer = Permission.create_or_update(
  19. name: 'test.customer',
  20. note: 'customer test permission',
  21. preferences: {
  22. not: ['test.agent'],
  23. },
  24. )
  25. permission_test_normal = Permission.create_or_update(
  26. name: 'test.normal',
  27. note: 'normal test permission',
  28. preferences: {},
  29. )
  30. assert_raises(RuntimeError) do
  31. Role.create(
  32. name: 'Test1',
  33. note: 'Test1 Role.',
  34. permissions: [permission_test],
  35. updated_by_id: 1,
  36. created_by_id: 1
  37. )
  38. end
  39. assert_raises(RuntimeError) do
  40. Role.create(
  41. name: 'Test1',
  42. note: 'Test1 Role.',
  43. permissions: [permission_test_agent, permission_test_customer],
  44. updated_by_id: 1,
  45. created_by_id: 1
  46. )
  47. end
  48. assert_raises(RuntimeError) do
  49. Role.create(
  50. name: 'Test1',
  51. note: 'Test1 Role.',
  52. permissions: [permission_test_normal, permission_test_agent, permission_test_customer],
  53. updated_by_id: 1,
  54. created_by_id: 1
  55. )
  56. end
  57. role11 = Role.create(
  58. name: 'Test1.1',
  59. note: 'Test1.1 Role.',
  60. permissions: [permission_test_agent],
  61. updated_by_id: 1,
  62. created_by_id: 1
  63. )
  64. role12 = Role.create(
  65. name: 'Test1.2',
  66. note: 'Test1.2 Role.',
  67. permissions: [permission_test_customer],
  68. updated_by_id: 1,
  69. created_by_id: 1
  70. )
  71. role13 = Role.create(
  72. name: 'Test1.3',
  73. note: 'Test1.3 Role.',
  74. permissions: [permission_test_normal],
  75. updated_by_id: 1,
  76. created_by_id: 1
  77. )
  78. role14 = Role.create(
  79. name: 'Test1.4',
  80. note: 'Test1.4 Role.',
  81. permissions: [permission_test_normal, permission_test_customer],
  82. updated_by_id: 1,
  83. created_by_id: 1
  84. )
  85. end
  86. test 'permission default' do
  87. roles = Role.with_permissions('not_existing')
  88. assert(roles.blank?)
  89. roles = Role.with_permissions('admin')
  90. assert_equal('Admin', roles.first.name)
  91. roles = Role.with_permissions('admin.session')
  92. assert_equal('Admin', roles.first.name)
  93. roles = Role.with_permissions(['admin.session', 'not_existing'])
  94. assert_equal('Admin', roles.first.name)
  95. roles = Role.with_permissions('ticket.agent')
  96. assert_equal('Agent', roles.first.name)
  97. roles = Role.with_permissions(['ticket.agent', 'not_existing'])
  98. assert_equal('Agent', roles.first.name)
  99. roles = Role.with_permissions('ticket.customer')
  100. assert_equal('Customer', roles.first.name)
  101. roles = Role.with_permissions(['ticket.customer', 'not_existing'])
  102. assert_equal('Customer', roles.first.name)
  103. end
  104. test 'with permission' do
  105. permission_test1 = Permission.create_or_update(
  106. name: 'test-with-permission1',
  107. note: 'parent test permission 1',
  108. )
  109. permission_test2 = Permission.create_or_update(
  110. name: 'test-with-permission2',
  111. note: 'parent test permission 2',
  112. )
  113. name = rand(999_999_999)
  114. role = Role.create(
  115. name: "Test with Permission? #{name}",
  116. note: "Test with Permission? #{name} Role.",
  117. permissions: [permission_test2],
  118. updated_by_id: 1,
  119. created_by_id: 1
  120. )
  121. assert_not(role.with_permission?('test-with-permission1'))
  122. assert(role.with_permission?('test-with-permission2'))
  123. assert(role.with_permission?(['test-with-permission2', 'some_other_permission']))
  124. end
  125. test 'default_at_signup' do
  126. agent_role = Role.find_by(name: 'Agent')
  127. assert_raises(Exceptions::UnprocessableEntity) do
  128. agent_role.default_at_signup = true
  129. agent_role.save!
  130. end
  131. admin_role = Role.find_by(name: 'Admin')
  132. assert_raises(Exceptions::UnprocessableEntity) do
  133. admin_role.default_at_signup = true
  134. admin_role.save!
  135. end
  136. assert_raises(Exceptions::UnprocessableEntity) do
  137. Role.create!(
  138. name: 'Test1',
  139. note: 'Test1 Role.',
  140. default_at_signup: true,
  141. permissions: [Permission.find_by(name: 'admin')],
  142. updated_by_id: 1,
  143. created_by_id: 1
  144. )
  145. end
  146. role = Role.create!(
  147. name: 'Test1',
  148. note: 'Test1 Role.',
  149. default_at_signup: false,
  150. permissions: [Permission.find_by(name: 'admin')],
  151. updated_by_id: 1,
  152. created_by_id: 1
  153. )
  154. assert(role)
  155. permissions = Permission.where('name LIKE ? OR name = ?', 'admin%', 'ticket.agent').pluck(:name) # get all administrative permissions
  156. permissions.each do |type|
  157. assert_raises(Exceptions::UnprocessableEntity) do
  158. Role.create!(
  159. name: "Test1_#{type}",
  160. note: 'Test1 Role.',
  161. default_at_signup: true,
  162. permissions: [Permission.find_by(name: type)],
  163. updated_by_id: 1,
  164. created_by_id: 1
  165. )
  166. end
  167. role = Role.create!(
  168. name: "Test1_#{type}",
  169. note: 'Test1 Role.',
  170. default_at_signup: false,
  171. permissions: [Permission.find_by(name: type)],
  172. updated_by_id: 1,
  173. created_by_id: 1
  174. )
  175. assert(role)
  176. end
  177. assert_raises(Exceptions::UnprocessableEntity) do
  178. Role.create!(
  179. name: 'Test2',
  180. note: 'Test2 Role.',
  181. default_at_signup: true,
  182. permissions: [Permission.find_by(name: 'ticket.agent')],
  183. updated_by_id: 1,
  184. created_by_id: 1
  185. )
  186. end
  187. role = Role.create!(
  188. name: 'Test2',
  189. note: 'Test2 Role.',
  190. default_at_signup: false,
  191. permissions: [Permission.find_by(name: 'ticket.agent')],
  192. updated_by_id: 1,
  193. created_by_id: 1
  194. )
  195. assert(role)
  196. end
  197. end