2018-12-18 20:19:20 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 09:22:22 -07:00
|
|
|
require 'rails_helper'
|
|
|
|
require 'pundit/rspec'
|
|
|
|
|
|
|
|
RSpec.describe StatusPolicy, type: :model do
|
|
|
|
subject { described_class }
|
|
|
|
|
2022-07-04 17:41:40 -07:00
|
|
|
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) }
|
2017-05-29 09:22:22 -07:00
|
|
|
let(:alice) { Fabricate(:account, username: 'alice') }
|
2017-05-30 13:56:31 -07:00
|
|
|
let(:bob) { Fabricate(:account, username: 'bob') }
|
2017-05-29 09:22:22 -07:00
|
|
|
let(:status) { Fabricate(:status, account: alice) }
|
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permissions of show? and reblog?' do
|
|
|
|
permissions :show?, :reblog? do
|
|
|
|
it 'grants access when no viewer' do
|
|
|
|
expect(subject).to permit(nil, status)
|
|
|
|
end
|
2017-05-30 06:16:14 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when viewer is blocked' do
|
|
|
|
block = Fabricate(:block)
|
|
|
|
status.visibility = :private
|
|
|
|
status.account = block.target_account
|
2017-05-30 06:16:14 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(block.account, status)
|
|
|
|
end
|
2017-05-30 06:16:14 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permission of show?' do
|
|
|
|
permissions :show? do
|
|
|
|
it 'grants access when direct and account is viewer' do
|
|
|
|
status.visibility = :direct
|
2017-05-30 13:14:32 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(status.account, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'grants access when direct and viewer is mentioned' do
|
|
|
|
status.visibility = :direct
|
|
|
|
status.mentions = [Fabricate(:mention, account: alice)]
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(alice, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'grants access when direct and non-owner viewer is mentioned and mentions are loaded' do
|
|
|
|
status.visibility = :direct
|
|
|
|
status.mentions = [Fabricate(:mention, account: bob)]
|
|
|
|
status.mentions.load
|
2023-03-04 07:57:22 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(bob, status)
|
|
|
|
end
|
2023-03-04 07:57:22 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when direct and viewer is not mentioned' do
|
|
|
|
viewer = Fabricate(:account)
|
|
|
|
status.visibility = :direct
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(viewer, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'grants access when private and account is viewer' do
|
|
|
|
status.visibility = :private
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(status.account, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'grants access when private and account is following viewer' do
|
|
|
|
follow = Fabricate(:follow)
|
|
|
|
status.visibility = :private
|
|
|
|
status.account = follow.target_account
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(follow.account, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'grants access when private and viewer is mentioned' do
|
|
|
|
status.visibility = :private
|
|
|
|
status.mentions = [Fabricate(:mention, account: alice)]
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(alice, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when private and viewer is not mentioned or followed' do
|
|
|
|
viewer = Fabricate(:account)
|
|
|
|
status.visibility = :private
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(viewer, status)
|
|
|
|
end
|
2017-11-17 07:07:21 -08:00
|
|
|
|
2023-05-25 15:10:57 -07:00
|
|
|
it 'denies access when local-only and the viewer is not logged in' do
|
|
|
|
allow(status).to receive(:local_only?).and_return(true)
|
2017-11-17 07:07:21 -08:00
|
|
|
|
2023-05-25 15:10:57 -07:00
|
|
|
expect(subject).to_not permit(nil, status)
|
|
|
|
end
|
2018-07-31 05:57:41 -07:00
|
|
|
|
2023-05-25 15:10:57 -07:00
|
|
|
it 'denies access when local-only and the viewer is from another domain' do
|
|
|
|
viewer = Fabricate(:account, domain: 'remote-domain')
|
|
|
|
allow(status).to receive(:local_only?).and_return(true)
|
|
|
|
expect(subject).to_not permit(viewer, status)
|
|
|
|
end
|
2018-07-31 05:57:41 -07:00
|
|
|
end
|
2017-05-30 06:16:14 -07:00
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permission of reblog?' do
|
|
|
|
permissions :reblog? do
|
|
|
|
it 'denies access when private' do
|
|
|
|
viewer = Fabricate(:account)
|
|
|
|
status.visibility = :private
|
2017-05-30 06:16:14 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(viewer, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when direct' do
|
|
|
|
viewer = Fabricate(:account)
|
|
|
|
status.visibility = :direct
|
2017-05-29 09:22:22 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(viewer, status)
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
end
|
|
|
|
end
|
2017-05-30 13:56:31 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permissions of destroy? and unreblog?' do
|
|
|
|
permissions :destroy?, :unreblog? do
|
|
|
|
it 'grants access when account is deleter' do
|
|
|
|
expect(subject).to permit(status.account, status)
|
|
|
|
end
|
2017-05-30 13:56:31 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when account is not deleter' do
|
|
|
|
expect(subject).to_not permit(bob, status)
|
|
|
|
end
|
2017-05-30 13:56:31 -07:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies access when no deleter' do
|
|
|
|
expect(subject).to_not permit(nil, status)
|
|
|
|
end
|
2017-05-30 13:56:31 -07:00
|
|
|
end
|
|
|
|
end
|
2018-12-18 20:19:20 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permission of favourite?' do
|
|
|
|
permissions :favourite? do
|
|
|
|
it 'grants access when viewer is not blocked' do
|
|
|
|
follow = Fabricate(:follow)
|
|
|
|
status.account = follow.target_account
|
2018-12-18 20:19:20 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to permit(follow.account, status)
|
|
|
|
end
|
2018-12-18 20:19:20 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
it 'denies when viewer is blocked' do
|
|
|
|
block = Fabricate(:block)
|
|
|
|
status.account = block.target_account
|
2018-12-18 20:19:20 -08:00
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
expect(subject).to_not permit(block.account, status)
|
|
|
|
end
|
2018-12-18 20:19:20 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-23 01:49:23 -07:00
|
|
|
context 'with the permission of update?' do
|
|
|
|
permissions :update? do
|
|
|
|
it 'grants access if owner' do
|
|
|
|
expect(subject).to permit(status.account, status)
|
|
|
|
end
|
2022-02-09 15:15:30 -08:00
|
|
|
end
|
|
|
|
end
|
2017-05-29 09:22:22 -07:00
|
|
|
end
|