role_spec.rb 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://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_xss_sanitized_note_examples'
  8. RSpec.describe Role do
  9. subject(:role) { create(:role) }
  10. it_behaves_like 'ApplicationModel'
  11. it_behaves_like 'CanBeImported'
  12. it_behaves_like 'HasGroups', group_access_factory: :role
  13. it_behaves_like 'HasCollectionUpdate', collection_factory: :role
  14. it_behaves_like 'HasXssSanitizedNote', model_factory: :role
  15. it_behaves_like 'Association clears cache', association: :permissions
  16. it_behaves_like 'Association clears cache', association: :users
  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.two_factor_authentication
  41. user_preferences.password
  42. user_preferences.language
  43. user_preferences.linked_accounts
  44. user_preferences.avatar
  45. user_preferences.appearance
  46. ticket.customer
  47. ]
  48. )
  49. end
  50. end
  51. end
  52. describe 'Callbacks -' do
  53. describe 'Permission validation:' do
  54. context 'with normal permission' do
  55. let(:permission) { create(:permission) }
  56. it 'can be created' do
  57. expect { create(:role, permissions: [permission]) }
  58. .to change(described_class, :count).by(1)
  59. end
  60. it 'can be added' do
  61. expect { role.permissions << permission }
  62. .to change { role.permissions.count }.by(1)
  63. end
  64. end
  65. context 'with disabled permission' do
  66. let(:permission) { create(:permission, preferences: { disabled: true }) }
  67. it 'cannot be created' do
  68. expect { create(:role, permissions: [permission]) }
  69. .to raise_error(%r{is disabled})
  70. .and not_change(described_class, :count)
  71. end
  72. it 'cannot be added' do
  73. expect { role.permissions << permission }
  74. .to raise_error(%r{is disabled})
  75. .and not_change { role.permissions.count }
  76. end
  77. end
  78. context 'with multiple, explicitly incompatible permissions' do
  79. let(:permission) { create(:permission, preferences: { not: [Permission.first.name] }) }
  80. it 'cannot be created' do
  81. expect { create(:role, permissions: [Permission.first, permission]) }
  82. .to raise_error(%r{conflicts with})
  83. .and not_change(described_class, :count)
  84. end
  85. it 'cannot be added' do
  86. role.permissions << Permission.first
  87. expect { role.permissions << permission }
  88. .to raise_error(%r{conflicts with})
  89. .and not_change { role.permissions.count }
  90. end
  91. end
  92. context 'with multiple, compatible permissions' do
  93. let(:permission) { create(:permission, preferences: { not: [Permission.pluck(:name).max.next] }) }
  94. it 'can be created' do
  95. expect { create(:role, permissions: [Permission.first, permission]) }
  96. .to change(described_class, :count).by(1)
  97. end
  98. it 'can be added' do
  99. role.permissions << Permission.first
  100. expect { role.permissions << permission }
  101. .to change { role.permissions.count }.by(1)
  102. end
  103. end
  104. end
  105. describe 'System-wide agent limit checks:' do
  106. let(:agents) { User.with_permissions('ticket.agent') }
  107. describe '#validate_agent_limit_by_attributes' do
  108. context 'when reactivating a role adds new agents' do
  109. subject(:role) { create(:role, :agent, active: false) }
  110. before { create(:user, roles: [role]) }
  111. context 'exceeding the system limit' do
  112. before { Setting.set('system_agent_limit', agents.count) }
  113. it 'fails and raises an error' do
  114. expect { role.update!(active: true) }
  115. .to raise_error(Exceptions::UnprocessableEntity)
  116. .and not_change(agents, :count)
  117. end
  118. end
  119. end
  120. end
  121. end
  122. describe 'Restrictions on #default_at_signup:' do
  123. context 'for roles with "admin" permissions' do
  124. subject(:role) { build(:role, permissions: Permission.where(name: 'admin')) }
  125. it 'cannot be set to true on creation' do
  126. role.default_at_signup = true
  127. expect { role.save }
  128. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  129. end
  130. it 'cannot be changed to true' do
  131. role.save
  132. expect { role.update(default_at_signup: true) }
  133. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  134. end
  135. end
  136. context 'for roles with permissions that are children of "admin"' do
  137. subject(:role) { build(:role, permissions: [permission]) }
  138. let(:permission) { create(:permission, name: 'admin.foo') }
  139. it 'cannot be set to true on creation' do
  140. role.default_at_signup = true
  141. expect { role.save }
  142. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  143. end
  144. it 'cannot be changed to true' do
  145. role.save
  146. expect { role.update(default_at_signup: true) }
  147. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  148. end
  149. end
  150. context 'for roles with "ticket.agent" permissions' do
  151. subject(:role) { build(:role, permissions: Permission.where(name: 'ticket.agent')) }
  152. it 'cannot be set to true on creation' do
  153. role.default_at_signup = true
  154. expect { role.save }
  155. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  156. end
  157. it 'cannot be changed to true' do
  158. role.save
  159. expect { role.update(default_at_signup: true) }
  160. .to raise_error(Exceptions::UnprocessableEntity, %r{Cannot set default at signup})
  161. end
  162. end
  163. end
  164. describe 'Cleaning up groups when ticket.agent permission is lost' do
  165. let(:group) { Group.first }
  166. it 'creates a ticket.agent role with groups' do
  167. role = build(:role, :agent)
  168. role.role_groups.build group: group, access: 'full'
  169. role.save!
  170. expect(role.groups).to contain_exactly(group)
  171. end
  172. it 'saves non-ticket.agent role without groups' do
  173. role = build(:role, :admin)
  174. role.role_groups.build group: group, access: 'full'
  175. role.save!
  176. expect(role.groups).to be_blank
  177. end
  178. end
  179. end
  180. describe '.with_permissions' do
  181. context 'when given a name not matching any permissions' do
  182. let(:permission) { 'foo' }
  183. let(:result) { [] }
  184. it 'returns an empty array' do
  185. expect(described_class.with_permissions(permission)).to match_array(result)
  186. end
  187. end
  188. context 'when given the name of a top-level permission' do
  189. let(:permission) { 'user_preferences' }
  190. let(:result) { described_class.where(name: %w[Admin Agent]) }
  191. it 'returns an array of roles with that permission' do
  192. expect(described_class.with_permissions(permission)).to match_array(result)
  193. end
  194. end
  195. context 'when given the name of a child permission' do
  196. let(:permission) { 'user_preferences.language' }
  197. let(:result) { described_class.all }
  198. it 'returns an array of roles with either that permission or an ancestor' do
  199. expect(described_class.with_permissions(permission)).to match_array(result)
  200. end
  201. end
  202. context 'when given the names of multiple permissions' do
  203. let(:permissions) { %w[ticket.agent ticket.customer] }
  204. let(:result) { described_class.where(name: %w[Agent Customer]) }
  205. it 'returns an array of roles matching ANY given permission' do
  206. expect(described_class.with_permissions(permissions)).to match_array(result)
  207. end
  208. end
  209. end
  210. describe '#with_permission?' do
  211. subject(:role) { described_class.find_by(name: 'Admin') }
  212. context 'when given the name of a permission it has' do
  213. it 'returns true' do
  214. expect(role.with_permission?('admin')).to be(true)
  215. end
  216. end
  217. context 'when given the name of a permission it does NOT have' do
  218. it 'returns false' do
  219. expect(role.with_permission?('ticket.customer')).to be(false)
  220. end
  221. end
  222. context 'when given the name of multiple permissions' do
  223. it 'returns true as long as ANY match' do
  224. expect(role.with_permission?(['admin', 'ticket.customer'])).to be(true)
  225. end
  226. end
  227. end
  228. # https://github.com/zammad/zammad/issues/5123
  229. describe 'Removing KB editor permission with existing KB' do
  230. it 'allows to remove editor but keep reader permission' do
  231. role = create(:role, permission_names: %w[knowledge_base.reader knowledge_base.editor])
  232. kb_permission = create(:knowledge_base_permission, role: role)
  233. role.permission_revoke('knowledge_base.editor')
  234. expect(kb_permission.reload).to have_attributes(access: 'reader')
  235. end
  236. end
  237. end