role_spec.rb 8.7 KB

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