role_spec.rb 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/application_model_examples'
  4. require 'models/concerns/can_be_imported_examples'
  5. require 'models/concerns/has_groups_examples'
  6. require 'models/concerns/has_collection_update_examples'
  7. require 'models/concerns/has_ticket_create_screen_impact_examples'
  8. require 'models/concerns/has_xss_sanitized_note_examples'
  9. RSpec.describe Role do
  10. subject(:role) { create(:role) }
  11. it_behaves_like 'ApplicationModel'
  12. it_behaves_like 'CanBeImported'
  13. it_behaves_like 'HasGroups', group_access_factory: :role
  14. it_behaves_like 'HasCollectionUpdate', collection_factory: :role
  15. it_behaves_like 'HasTicketCreateScreenImpact', create_screen_factory: :role
  16. it_behaves_like 'HasXssSanitizedNote', model_factory: :role
  17. describe 'Default state' do
  18. describe 'of whole table:' do
  19. it 'has three records ("Admin", "Agent", and "Customer")' do
  20. expect(described_class.pluck(:name)).to match_array(%w[Admin Agent Customer])
  21. end
  22. end
  23. describe 'of "Admin" role:' do
  24. it 'has default admin permissions' do
  25. expect(described_class.find_by(name: 'Admin').permissions.pluck(:name))
  26. .to match_array(%w[admin user_preferences report knowledge_base.editor])
  27. end
  28. end
  29. describe 'of "Agent" role:' do
  30. it 'has default agent permissions' do
  31. expect(described_class.find_by(name: 'Agent').permissions.pluck(:name))
  32. .to match_array(%w[ticket.agent chat.agent cti.agent user_preferences knowledge_base.reader])
  33. end
  34. end
  35. describe 'of "Customer" role:' do
  36. it 'has default customer permissions' do
  37. expect(described_class.find_by(name: 'Customer').permissions.pluck(:name))
  38. .to match_array(
  39. %w[
  40. user_preferences.password
  41. user_preferences.language
  42. user_preferences.linked_accounts
  43. user_preferences.avatar
  44. ticket.customer
  45. ]
  46. )
  47. end
  48. end
  49. end
  50. describe 'Callbacks -' do
  51. describe 'Permission validation:' do
  52. context 'with normal permission' do
  53. let(:permission) { create(:permission) }
  54. it 'can be created' do
  55. expect { create(:role, permissions: [permission]) }
  56. .to change(described_class, :count).by(1)
  57. end
  58. it 'can be added' do
  59. expect { role.permissions << permission }
  60. .to change { role.permissions.count }.by(1)
  61. end
  62. end
  63. context 'with disabled permission' do
  64. let(:permission) { create(:permission, preferences: { disabled: true }) }
  65. it 'cannot be created' do
  66. expect { create(:role, permissions: [permission]) }
  67. .to raise_error(%r{is disabled})
  68. .and change(described_class, :count).by(0)
  69. end
  70. it 'cannot be added' do
  71. expect { role.permissions << permission }
  72. .to raise_error(%r{is disabled})
  73. .and change { role.permissions.count }.by(0)
  74. end
  75. end
  76. context 'with multiple, explicitly incompatible permissions' do
  77. let(:permission) { create(:permission, preferences: { not: [Permission.first.name] }) }
  78. it 'cannot be created' do
  79. expect { create(:role, permissions: [Permission.first, permission]) }
  80. .to raise_error(%r{conflicts with})
  81. .and change(described_class, :count).by(0)
  82. end
  83. it 'cannot be added' do
  84. role.permissions << Permission.first
  85. expect { role.permissions << permission }
  86. .to raise_error(%r{conflicts with})
  87. .and change { role.permissions.count }.by(0)
  88. end
  89. end
  90. context 'with multiple, compatible permissions' do
  91. let(:permission) { create(:permission, preferences: { not: [Permission.pluck(:name).max.next] }) }
  92. it 'can be created' do
  93. expect { create(:role, permissions: [Permission.first, permission]) }
  94. .to change(described_class, :count).by(1)
  95. end
  96. it 'can be added' do
  97. role.permissions << Permission.first
  98. expect { role.permissions << permission }
  99. .to change { role.permissions.count }.by(1)
  100. end
  101. end
  102. end
  103. describe 'System-wide agent limit checks:' do
  104. let(:agents) { User.with_permissions('ticket.agent') }
  105. describe '#validate_agent_limit_by_attributes' do
  106. context 'when reactivating a role adds new agents' do
  107. subject(:role) { create(:agent_role, active: false) }
  108. before { create(:user, roles: [role]) }
  109. context 'exceeding the system limit' do
  110. before { Setting.set('system_agent_limit', agents.count) }
  111. it 'fails and raises an error' do
  112. expect { role.update!(active: true) }
  113. .to raise_error(Exceptions::UnprocessableEntity)
  114. .and change(agents, :count).by(0)
  115. end
  116. end
  117. end
  118. end
  119. end
  120. describe 'Restrictions on #default_at_signup:' do
  121. context 'for roles with "admin" permissions' do
  122. subject(:role) { build(:role, permissions: Permission.where(name: 'admin')) }
  123. it 'cannot be set to true on creation' do
  124. role.default_at_signup = true
  125. expect { role.save }
  126. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  127. end
  128. it 'cannot be changed to true' do
  129. role.save
  130. expect { role.update(default_at_signup: true) }
  131. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  132. end
  133. end
  134. context 'for roles with permissions that are children of "admin"' do
  135. subject(:role) { build(:role, permissions: [permission]) }
  136. let(:permission) { create(:permission, name: 'admin.foo') }
  137. it 'cannot be set to true on creation' do
  138. role.default_at_signup = true
  139. expect { role.save }
  140. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  141. end
  142. it 'cannot be changed to true' do
  143. role.save
  144. expect { role.update(default_at_signup: true) }
  145. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  146. end
  147. end
  148. context 'for roles with "ticket.agent" permissions' do
  149. subject(:role) { build(:role, permissions: Permission.where(name: 'ticket.agent')) }
  150. it 'cannot be set to true on creation' do
  151. role.default_at_signup = true
  152. expect { role.save }
  153. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  154. end
  155. it 'cannot be changed to true' do
  156. role.save
  157. expect { role.update(default_at_signup: true) }
  158. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  159. end
  160. end
  161. end
  162. end
  163. describe '.with_permissions' do
  164. context 'when given a name not matching any permissions' do
  165. let(:permission) { 'foo' }
  166. let(:result) { [] }
  167. it 'returns an empty array' do
  168. expect(described_class.with_permissions(permission)).to match_array(result)
  169. end
  170. end
  171. context 'when given the name of a top-level permission' do
  172. let(:permission) { 'user_preferences' }
  173. let(:result) { described_class.where(name: %w[Admin Agent]) }
  174. it 'returns an array of roles with that permission' do
  175. expect(described_class.with_permissions(permission)).to match_array(result)
  176. end
  177. end
  178. context 'when given the name of a child permission' do
  179. let(:permission) { 'user_preferences.language' }
  180. let(:result) { described_class.all }
  181. it 'returns an array of roles with either that permission or an ancestor' do
  182. expect(described_class.with_permissions(permission)).to match_array(result)
  183. end
  184. end
  185. context 'when given the names of multiple permissions' do
  186. let(:permissions) { %w[ticket.agent ticket.customer] }
  187. let(:result) { described_class.where(name: %w[Agent Customer]) }
  188. it 'returns an array of roles matching ANY given permission' do
  189. expect(described_class.with_permissions(permissions)).to match_array(result)
  190. end
  191. end
  192. end
  193. describe '#with_permission?' do
  194. subject(:role) { described_class.find_by(name: 'Admin') }
  195. context 'when given the name of a permission it has' do
  196. it 'returns true' do
  197. expect(role.with_permission?('admin')).to be(true)
  198. end
  199. end
  200. context 'when given the name of a permission it does NOT have' do
  201. it 'returns false' do
  202. expect(role.with_permission?('ticket.customer')).to be(false)
  203. end
  204. end
  205. context 'when given the name of multiple permissions' do
  206. it 'returns true as long as ANY match' do
  207. expect(role.with_permission?(['admin', 'ticket.customer'])).to be(true)
  208. end
  209. end
  210. end
  211. end