Policies specs (#23924)

This commit is contained in:
Matt Jankowski 2023-03-04 10:57:22 -05:00 committed by GitHub
parent 6a57c42316
commit 00eb2269b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 393 additions and 3 deletions

View file

@ -116,4 +116,44 @@ RSpec.describe AccountPolicy do
end
end
end
permissions :review? do
context 'admin' do
it 'permits' do
expect(subject).to permit(admin)
end
end
context 'not admin' do
it 'denies' do
expect(subject).to_not permit(john)
end
end
end
permissions :destroy? do
context 'admin' do
context 'with a temporarily suspended account' do
before { allow(alice).to receive(:suspended_temporarily?).and_return(true) }
it 'permits' do
expect(subject).to permit(admin, alice)
end
end
context 'with a not temporarily suspended account' do
before { allow(alice).to receive(:suspended_temporarily?).and_return(false) }
it 'denies' do
expect(subject).to_not permit(admin, alice)
end
end
end
context 'not admin' do
it 'denies' do
expect(subject).to_not permit(john, alice)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe AccountWarningPresetPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :create?, :update?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe Admin::StatusPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
let(:status) { Fabricate(:status) }
permissions :index?, :update?, :review?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
permissions :show? do
context 'with an admin' do
context 'with a public visible status' do
before { allow(status).to receive(:public_visibility?).and_return(true) }
it 'permits' do
expect(policy).to permit(admin, status)
end
end
context 'with a not public visible status' do
before { allow(status).to receive(:public_visibility?).and_return(false) }
it 'denies' do
expect(policy).to_not permit(admin, status)
end
end
end
context 'with a non admin' do
it 'denies' do
expect(policy).to_not permit(john, status)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe AnnouncementPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :create?, :update?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,51 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe AppealPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
let(:appeal) { Fabricate(:appeal) }
permissions :index? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
permissions :reject? do
context 'with an admin' do
context 'with a pending appeal' do
before { allow(appeal).to receive(:pending?).and_return(true) }
it 'permits' do
expect(policy).to permit(admin, appeal)
end
end
context 'with a not pending appeal' do
before { allow(appeal).to receive(:pending?).and_return(false) }
it 'denies' do
expect(policy).to_not permit(admin, appeal)
end
end
end
context 'with a non admin' do
it 'denies' do
expect(policy).to_not permit(john, appeal)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe CanonicalEmailBlockPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :show?, :test?, :create?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe DeliveryPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :clear_delivery_errors?, :restart_delivery?, :stop_delivery? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -8,7 +8,7 @@ RSpec.describe EmailDomainBlockPolicy do
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :create?, :destroy? do
permissions :index?, :show?, :create?, :destroy? do
context 'admin' do
it 'permits' do
expect(subject).to permit(admin, EmailDomainBlock)

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe FollowRecommendationPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :show?, :suppress?, :unsuppress? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe IpBlockPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :show?, :create?, :update?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe PreviewCardPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :review? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe PreviewCardProviderPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :review? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe RulePolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :create?, :update?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end

View file

@ -8,7 +8,7 @@ RSpec.describe SettingsPolicy do
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :update?, :show? do
permissions :update?, :show?, :destroy? do
context 'admin?' do
it 'permits' do
expect(subject).to permit(admin, Settings)

View file

@ -39,6 +39,14 @@ RSpec.describe StatusPolicy, type: :model do
expect(subject).to permit(alice, status)
end
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
expect(subject).to permit(bob, status)
end
it 'denies access when direct and viewer is not mentioned' do
viewer = Fabricate(:account)
status.visibility = :direct

View file

@ -8,7 +8,7 @@ RSpec.describe TagPolicy do
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :show?, :update? do
permissions :index?, :show?, :update?, :review? do
context 'staff?' do
it 'permits' do
expect(subject).to permit(admin, Tag)

View file

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'rails_helper'
require 'pundit/rspec'
describe WebhookPolicy do
let(:policy) { described_class }
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
let(:john) { Fabricate(:account) }
permissions :index?, :create?, :show?, :update?, :enable?, :disable?, :rotate_secret?, :destroy? do
context 'with an admin' do
it 'permits' do
expect(policy).to permit(admin, Tag)
end
end
context 'with a non-admin' do
it 'denies' do
expect(policy).to_not permit(john, Tag)
end
end
end
end