user_policy_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe UserPolicy do
  4. subject { described_class }
  5. let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  6. let(:john) { Fabricate(:account) }
  7. permissions :reset_password?, :change_email? do
  8. context 'when staff?' do
  9. context 'with !record.staff?' do
  10. it 'permits' do
  11. expect(subject).to permit(admin, john.user)
  12. end
  13. end
  14. context 'when record.staff?' do
  15. it 'denies' do
  16. expect(subject).to_not permit(admin, admin.user)
  17. end
  18. end
  19. end
  20. context 'with !staff?' do
  21. it 'denies' do
  22. expect(subject).to_not permit(john, User)
  23. end
  24. end
  25. end
  26. permissions :disable_2fa? do
  27. context 'when admin?' do
  28. context 'with !record.staff?' do
  29. it 'permits' do
  30. expect(subject).to permit(admin, john.user)
  31. end
  32. end
  33. context 'when record.staff?' do
  34. it 'denies' do
  35. expect(subject).to_not permit(admin, admin.user)
  36. end
  37. end
  38. end
  39. context 'with !admin?' do
  40. it 'denies' do
  41. expect(subject).to_not permit(john, User)
  42. end
  43. end
  44. end
  45. permissions :confirm? do
  46. context 'when staff?' do
  47. context 'with !record.confirmed?' do
  48. it 'permits' do
  49. john.user.update(confirmed_at: nil)
  50. expect(subject).to permit(admin, john.user)
  51. end
  52. end
  53. context 'when record.confirmed?' do
  54. it 'denies' do
  55. john.user.mark_email_as_confirmed!
  56. expect(subject).to_not permit(admin, john.user)
  57. end
  58. end
  59. end
  60. context 'with !staff?' do
  61. it 'denies' do
  62. expect(subject).to_not permit(john, User)
  63. end
  64. end
  65. end
  66. permissions :enable? do
  67. context 'when staff?' do
  68. it 'permits' do
  69. expect(subject).to permit(admin, User)
  70. end
  71. end
  72. context 'with !staff?' do
  73. it 'denies' do
  74. expect(subject).to_not permit(john, User)
  75. end
  76. end
  77. end
  78. permissions :disable? do
  79. context 'when staff?' do
  80. context 'with !record.admin?' do
  81. it 'permits' do
  82. expect(subject).to permit(admin, john.user)
  83. end
  84. end
  85. context 'when record.admin?' do
  86. it 'denies' do
  87. expect(subject).to_not permit(admin, admin.user)
  88. end
  89. end
  90. end
  91. context 'with !staff?' do
  92. it 'denies' do
  93. expect(subject).to_not permit(john, User)
  94. end
  95. end
  96. end
  97. permissions :approve?, :reject? do
  98. context 'when admin' do
  99. context 'when user is approved' do
  100. it { is_expected.to_not permit(admin, User.new(approved: true)) }
  101. end
  102. context 'when user is not approved' do
  103. it { is_expected.to permit(admin, User.new(approved: false)) }
  104. end
  105. end
  106. context 'when not admin' do
  107. it { is_expected.to_not permit(john, User.new) }
  108. end
  109. end
  110. permissions :change_role? do
  111. context 'when not admin' do
  112. it { is_expected.to_not permit(john, User.new) }
  113. end
  114. context 'when admin' do
  115. let(:user) { User.new(role: role) }
  116. context 'when role of admin overrides user role' do
  117. let(:role) { UserRole.new(position: admin.user.role.position - 10, id: 123) }
  118. it { is_expected.to permit(admin, user) }
  119. end
  120. context 'when role of admin does not override user role' do
  121. let(:role) { UserRole.new(position: admin.user.role.position + 10, id: 123) }
  122. it { is_expected.to_not permit(admin, user) }
  123. end
  124. end
  125. end
  126. end