role_test.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class RoleTest < ActiveSupport::TestCase
  4. test 'permission' do
  5. permission_test = Permission.create_or_update(
  6. name: 'test',
  7. note: 'parent test permission',
  8. preferences: {
  9. disabled: true
  10. },
  11. )
  12. permission_test_agent = Permission.create_or_update(
  13. name: 'test.agent',
  14. note: 'agent test permission',
  15. preferences: {
  16. not: ['test.customer'],
  17. },
  18. )
  19. permission_test_customer = Permission.create_or_update(
  20. name: 'test.customer',
  21. note: 'customer test permission',
  22. preferences: {
  23. not: ['test.agent'],
  24. },
  25. )
  26. permission_test_normal = Permission.create_or_update(
  27. name: 'test.normal',
  28. note: 'normal test permission',
  29. preferences: {},
  30. )
  31. assert_raises(RuntimeError) {
  32. Role.create(
  33. name: 'Test1',
  34. note: 'Test1 Role.',
  35. permissions: [permission_test],
  36. updated_by_id: 1,
  37. created_by_id: 1
  38. )
  39. }
  40. assert_raises(RuntimeError) {
  41. Role.create(
  42. name: 'Test1',
  43. note: 'Test1 Role.',
  44. permissions: [permission_test_agent, permission_test_customer],
  45. updated_by_id: 1,
  46. created_by_id: 1
  47. )
  48. }
  49. assert_raises(RuntimeError) {
  50. Role.create(
  51. name: 'Test1',
  52. note: 'Test1 Role.',
  53. permissions: [permission_test_normal, permission_test_agent, permission_test_customer],
  54. updated_by_id: 1,
  55. created_by_id: 1
  56. )
  57. }
  58. role11 = Role.create(
  59. name: 'Test1.1',
  60. note: 'Test1.1 Role.',
  61. permissions: [permission_test_agent],
  62. updated_by_id: 1,
  63. created_by_id: 1
  64. )
  65. role12 = Role.create(
  66. name: 'Test1.2',
  67. note: 'Test1.2 Role.',
  68. permissions: [permission_test_customer],
  69. updated_by_id: 1,
  70. created_by_id: 1
  71. )
  72. role13 = Role.create(
  73. name: 'Test1.3',
  74. note: 'Test1.3 Role.',
  75. permissions: [permission_test_normal],
  76. updated_by_id: 1,
  77. created_by_id: 1
  78. )
  79. role14 = Role.create(
  80. name: 'Test1.4',
  81. note: 'Test1.4 Role.',
  82. permissions: [permission_test_normal, permission_test_customer],
  83. updated_by_id: 1,
  84. created_by_id: 1
  85. )
  86. end
  87. test 'permission default' do
  88. roles = Role.with_permissions('not_existing')
  89. assert(roles.empty?)
  90. roles = Role.with_permissions('admin')
  91. assert_equal('Admin', roles.first.name)
  92. roles = Role.with_permissions('admin.session')
  93. assert_equal('Admin', roles.first.name)
  94. roles = Role.with_permissions(['admin.session', 'not_existing'])
  95. assert_equal('Admin', roles.first.name)
  96. roles = Role.with_permissions('ticket.agent')
  97. assert_equal('Agent', roles.first.name)
  98. roles = Role.with_permissions(['ticket.agent', 'not_existing'])
  99. assert_equal('Agent', roles.first.name)
  100. roles = Role.with_permissions('ticket.customer')
  101. assert_equal('Customer', roles.first.name)
  102. roles = Role.with_permissions(['ticket.customer', 'not_existing'])
  103. assert_equal('Customer', roles.first.name)
  104. end
  105. end