You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.0 KiB

  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, admin: true) }
  7. let(:alice) { Fabricate(:account, username: 'alice') }
  8. let(:bob) { Fabricate(:account, username: 'bob') }
  9. let(:status) { Fabricate(:status, account: alice) }
  10. permissions :show?, :reblog? do
  11. it 'grants access when no viewer' do
  12. expect(subject).to permit(nil, status)
  13. end
  14. it 'denies access when viewer is blocked' do
  15. block = Fabricate(:block)
  16. status.visibility = :private
  17. status.account = block.target_account
  18. expect(subject).to_not permit(block.account, status)
  19. end
  20. end
  21. permissions :show? do
  22. it 'grants access when direct and account is viewer' do
  23. status.visibility = :direct
  24. expect(subject).to permit(status.account, status)
  25. end
  26. it 'grants access when direct and viewer is mentioned' do
  27. status.visibility = :direct
  28. status.mentions = [Fabricate(:mention, account: alice)]
  29. expect(subject).to permit(alice, status)
  30. end
  31. it 'denies access when direct and viewer is not mentioned' do
  32. viewer = Fabricate(:account)
  33. status.visibility = :direct
  34. expect(subject).to_not permit(viewer, status)
  35. end
  36. it 'grants access when private and account is viewer' do
  37. status.visibility = :private
  38. expect(subject).to permit(status.account, status)
  39. end
  40. it 'grants access when private and account is following viewer' do
  41. follow = Fabricate(:follow)
  42. status.visibility = :private
  43. status.account = follow.target_account
  44. expect(subject).to permit(follow.account, status)
  45. end
  46. it 'grants access when private and viewer is mentioned' do
  47. status.visibility = :private
  48. status.mentions = [Fabricate(:mention, account: alice)]
  49. expect(subject).to permit(alice, status)
  50. end
  51. it 'denies access when private and viewer is not mentioned or followed' do
  52. viewer = Fabricate(:account)
  53. status.visibility = :private
  54. expect(subject).to_not permit(viewer, status)
  55. end
  56. it 'denies access when local-only and the viewer is not logged in' do
  57. allow(status).to receive(:local_only?) { true }
  58. expect(subject).to_not permit(nil, status)
  59. end
  60. it 'denies access when local-only and the viewer is from another domain' do
  61. viewer = Fabricate(:account, domain: 'remote-domain')
  62. allow(status).to receive(:local_only?) { true }
  63. expect(subject).to_not permit(viewer, status)
  64. end
  65. end
  66. permissions :reblog? do
  67. it 'denies access when private' do
  68. viewer = Fabricate(:account)
  69. status.visibility = :private
  70. expect(subject).to_not permit(viewer, status)
  71. end
  72. it 'denies access when direct' do
  73. viewer = Fabricate(:account)
  74. status.visibility = :direct
  75. expect(subject).to_not permit(viewer, status)
  76. end
  77. end
  78. permissions :destroy?, :unreblog? do
  79. it 'grants access when account is deleter' do
  80. expect(subject).to permit(status.account, status)
  81. end
  82. it 'grants access when account is admin' do
  83. expect(subject).to permit(admin.account, status)
  84. end
  85. it 'denies access when account is not deleter' do
  86. expect(subject).to_not permit(bob, status)
  87. end
  88. it 'denies access when no deleter' do
  89. expect(subject).to_not permit(nil, status)
  90. end
  91. end
  92. permissions :favourite? do
  93. it 'grants access when viewer is not blocked' do
  94. follow = Fabricate(:follow)
  95. status.account = follow.target_account
  96. expect(subject).to permit(follow.account, status)
  97. end
  98. it 'denies when viewer is blocked' do
  99. block = Fabricate(:block)
  100. status.account = block.target_account
  101. expect(subject).to_not permit(block.account, status)
  102. end
  103. end
  104. permissions :index?, :update? do
  105. it 'grants access if staff' do
  106. expect(subject).to permit(admin.account)
  107. end
  108. it 'denies access unless staff' do
  109. expect(subject).to_not permit(alice)
  110. end
  111. end
  112. end