status_policy_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require 'pundit/rspec'
  4. RSpec.describe StatusPolicy, type: :model do
  5. subject { described_class }
  6. let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
  7. let(:alice) { Fabricate(:account, username: 'alice') }
  8. let(:bob) { Fabricate(:account, username: 'bob') }
  9. let(:status) { Fabricate(:status, account: alice) }
  10. context 'with the permissions of show? and reblog?' do
  11. permissions :show?, :reblog? do
  12. it 'grants access when no viewer' do
  13. expect(subject).to permit(nil, status)
  14. end
  15. it 'denies access when viewer is blocked' do
  16. block = Fabricate(:block)
  17. status.visibility = :private
  18. status.account = block.target_account
  19. expect(subject).to_not permit(block.account, status)
  20. end
  21. end
  22. end
  23. context 'with the permission of show?' do
  24. permissions :show? do
  25. it 'grants access when direct and account is viewer' do
  26. status.visibility = :direct
  27. expect(subject).to permit(status.account, status)
  28. end
  29. it 'grants access when direct and viewer is mentioned' do
  30. status.visibility = :direct
  31. status.mentions = [Fabricate(:mention, account: alice)]
  32. expect(subject).to permit(alice, status)
  33. end
  34. it 'grants access when direct and non-owner viewer is mentioned and mentions are loaded' do
  35. status.visibility = :direct
  36. status.mentions = [Fabricate(:mention, account: bob)]
  37. status.mentions.load
  38. expect(subject).to permit(bob, status)
  39. end
  40. it 'denies access when direct and viewer is not mentioned' do
  41. viewer = Fabricate(:account)
  42. status.visibility = :direct
  43. expect(subject).to_not permit(viewer, status)
  44. end
  45. it 'grants access when private and account is viewer' do
  46. status.visibility = :private
  47. expect(subject).to permit(status.account, status)
  48. end
  49. it 'grants access when private and account is following viewer' do
  50. follow = Fabricate(:follow)
  51. status.visibility = :private
  52. status.account = follow.target_account
  53. expect(subject).to permit(follow.account, status)
  54. end
  55. it 'grants access when private and viewer is mentioned' do
  56. status.visibility = :private
  57. status.mentions = [Fabricate(:mention, account: alice)]
  58. expect(subject).to permit(alice, status)
  59. end
  60. it 'denies access when private and viewer is not mentioned or followed' do
  61. viewer = Fabricate(:account)
  62. status.visibility = :private
  63. expect(subject).to_not permit(viewer, status)
  64. end
  65. end
  66. end
  67. context 'with the permission of reblog?' do
  68. permissions :reblog? do
  69. it 'denies access when private' do
  70. viewer = Fabricate(:account)
  71. status.visibility = :private
  72. expect(subject).to_not permit(viewer, status)
  73. end
  74. it 'denies access when direct' do
  75. viewer = Fabricate(:account)
  76. status.visibility = :direct
  77. expect(subject).to_not permit(viewer, status)
  78. end
  79. end
  80. end
  81. context 'with the permissions of destroy? and unreblog?' do
  82. permissions :destroy?, :unreblog? do
  83. it 'grants access when account is deleter' do
  84. expect(subject).to permit(status.account, status)
  85. end
  86. it 'denies access when account is not deleter' do
  87. expect(subject).to_not permit(bob, status)
  88. end
  89. it 'denies access when no deleter' do
  90. expect(subject).to_not permit(nil, status)
  91. end
  92. end
  93. end
  94. context 'with the permission of favourite?' do
  95. permissions :favourite? do
  96. it 'grants access when viewer is not blocked' do
  97. follow = Fabricate(:follow)
  98. status.account = follow.target_account
  99. expect(subject).to permit(follow.account, status)
  100. end
  101. it 'denies when viewer is blocked' do
  102. block = Fabricate(:block)
  103. status.account = block.target_account
  104. expect(subject).to_not permit(block.account, status)
  105. end
  106. end
  107. end
  108. context 'with the permission of update?' do
  109. permissions :update? do
  110. it 'grants access if owner' do
  111. expect(subject).to permit(status.account, status)
  112. end
  113. end
  114. end
  115. end