account_policy_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'pundit/rspec'
  4. RSpec.describe AccountPolicy do
  5. subject { described_class }
  6. let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  7. let(:john) { Fabricate(:account) }
  8. let(:alice) { Fabricate(:account) }
  9. permissions :index? do
  10. context 'when staff' do
  11. it 'permits' do
  12. expect(subject).to permit(admin)
  13. end
  14. end
  15. context 'when not staff' do
  16. it 'denies' do
  17. expect(subject).to_not permit(john)
  18. end
  19. end
  20. end
  21. permissions :show?, :unsilence?, :unsensitive?, :remove_avatar?, :remove_header? do
  22. context 'when staff' do
  23. it 'permits' do
  24. expect(subject).to permit(admin, alice)
  25. end
  26. end
  27. context 'when not staff' do
  28. it 'denies' do
  29. expect(subject).to_not permit(john, alice)
  30. end
  31. end
  32. end
  33. permissions :unsuspend?, :unblock_email? do
  34. before do
  35. alice.suspend!
  36. end
  37. context 'when staff' do
  38. it 'permits' do
  39. expect(subject).to permit(admin, alice)
  40. end
  41. end
  42. context 'when not staff' do
  43. it 'denies' do
  44. expect(subject).to_not permit(john, alice)
  45. end
  46. end
  47. end
  48. permissions :redownload? do
  49. context 'when admin' do
  50. it 'permits' do
  51. expect(subject).to permit(admin)
  52. end
  53. end
  54. context 'when not admin' do
  55. it 'denies' do
  56. expect(subject).to_not permit(john)
  57. end
  58. end
  59. end
  60. permissions :suspend?, :silence? do
  61. let(:staff) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  62. context 'when staff' do
  63. context 'when record is staff' do
  64. it 'denies' do
  65. expect(subject).to_not permit(admin, staff)
  66. end
  67. end
  68. context 'when record is not staff' do
  69. it 'permits' do
  70. expect(subject).to permit(admin, john)
  71. end
  72. end
  73. end
  74. context 'when not staff' do
  75. it 'denies' do
  76. expect(subject).to_not permit(john, Account)
  77. end
  78. end
  79. end
  80. permissions :memorialize? do
  81. let(:other_admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
  82. context 'when admin' do
  83. context 'when record is admin' do
  84. it 'denies' do
  85. expect(subject).to_not permit(admin, other_admin)
  86. end
  87. end
  88. context 'when record is not admin' do
  89. it 'permits' do
  90. expect(subject).to permit(admin, john)
  91. end
  92. end
  93. end
  94. context 'when not admin' do
  95. it 'denies' do
  96. expect(subject).to_not permit(john, Account)
  97. end
  98. end
  99. end
  100. permissions :review? do
  101. context 'when admin' do
  102. it 'permits' do
  103. expect(subject).to permit(admin)
  104. end
  105. end
  106. context 'when not admin' do
  107. it 'denies' do
  108. expect(subject).to_not permit(john)
  109. end
  110. end
  111. end
  112. permissions :destroy? do
  113. context 'when admin' do
  114. context 'with a temporarily suspended account' do
  115. before { allow(alice).to receive(:suspended_temporarily?).and_return(true) }
  116. it 'permits' do
  117. expect(subject).to permit(admin, alice)
  118. end
  119. end
  120. context 'with a not temporarily suspended account' do
  121. before { allow(alice).to receive(:suspended_temporarily?).and_return(false) }
  122. it 'denies' do
  123. expect(subject).to_not permit(admin, alice)
  124. end
  125. end
  126. end
  127. context 'when not admin' do
  128. it 'denies' do
  129. expect(subject).to_not permit(john, alice)
  130. end
  131. end
  132. end
  133. end