user_role_policy_spec.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UserRolePolicy do
  4. subject { described_class }
  5. let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  6. let(:account) { Fabricate(:account) }
  7. permissions :index?, :create? do
  8. context 'when admin' do
  9. it { is_expected.to permit(admin, UserRole.new) }
  10. end
  11. context 'when not admin' do
  12. it { is_expected.to_not permit(account, UserRole.new) }
  13. end
  14. end
  15. permissions :update? do
  16. context 'when admin' do
  17. context 'when role of admin overrides relevant role' do
  18. it { is_expected.to permit(admin, UserRole.new(position: admin.user.role.position - 10, id: 123)) }
  19. end
  20. context 'when role of admin does not override relevant role' do
  21. it { is_expected.to_not permit(admin, UserRole.new(position: admin.user.role.position + 10, id: 123)) }
  22. end
  23. end
  24. context 'when not admin' do
  25. it { is_expected.to_not permit(account, UserRole.new) }
  26. end
  27. end
  28. permissions :destroy? do
  29. context 'when admin' do
  30. context 'when role of admin overrides relevant role' do
  31. it { is_expected.to permit(admin, UserRole.new(position: admin.user.role.position - 10)) }
  32. end
  33. context 'when role of admin does not override relevant role' do
  34. it { is_expected.to_not permit(admin, UserRole.new(position: admin.user.role.position + 10)) }
  35. end
  36. context 'when everyone role' do
  37. it { is_expected.to_not permit(admin, UserRole.everyone) }
  38. end
  39. end
  40. context 'when not admin' do
  41. it { is_expected.to_not permit(account, UserRole.new) }
  42. end
  43. end
  44. end