mirror of https://github.com/mastodon/mastodon
Improve `app/policies` coverage (#32426)
parent
28131b4b00
commit
9e8f099d38
@ -0,0 +1,42 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AccountWarningPolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
permissions :show? do
|
||||
context 'with an admin' do
|
||||
it { is_expected.to permit(admin, AccountWarning.new) }
|
||||
end
|
||||
|
||||
context 'with a non-admin' do
|
||||
context 'when account is not target' do
|
||||
it { is_expected.to_not permit(account, AccountWarning.new) }
|
||||
end
|
||||
|
||||
context 'when account is target' do
|
||||
it { is_expected.to permit(account, AccountWarning.new(target_account_id: account.id)) }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
permissions :appeal? do
|
||||
context 'when account is not target' do
|
||||
it { is_expected.to_not permit(account, AccountWarning.new) }
|
||||
end
|
||||
|
||||
context 'when account is target' do
|
||||
context 'when record is appealable' do
|
||||
it { is_expected.to permit(account, AccountWarning.new(target_account_id: account.id, created_at: Appeal::MAX_STRIKE_AGE.ago + 1.hour)) }
|
||||
end
|
||||
|
||||
context 'when record is not appealable' do
|
||||
it { is_expected.to_not permit(account, AccountWarning.new(target_account_id: account.id, created_at: Appeal::MAX_STRIKE_AGE.ago - 1.hour)) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe AuditLogPolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
permissions :index? do
|
||||
context 'with an admin' do
|
||||
it { is_expected.to permit(admin, nil) }
|
||||
end
|
||||
|
||||
context 'with a non-admin' do
|
||||
it { is_expected.to_not permit(account, nil) }
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DashboardPolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
permissions :index? do
|
||||
context 'with an admin' do
|
||||
it { is_expected.to permit(admin, nil) }
|
||||
end
|
||||
|
||||
context 'with a non-admin' do
|
||||
it { is_expected.to_not permit(account, nil) }
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DomainAllowPolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
||||
let(:john) { Fabricate(:account) }
|
||||
|
||||
permissions :index?, :show?, :create?, :destroy? do
|
||||
context 'when admin' do
|
||||
it 'permits' do
|
||||
expect(subject).to permit(admin, DomainAllow)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when not admin' do
|
||||
it 'denies' do
|
||||
expect(subject).to_not permit(john, DomainAllow)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe PollPolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:account) { Fabricate(:account) }
|
||||
let(:poll) { Fabricate :poll }
|
||||
|
||||
permissions :vote? do
|
||||
context 'when account cannot view status' do
|
||||
before { poll.status.update(visibility: :private) }
|
||||
|
||||
it { is_expected.to_not permit(account, poll) }
|
||||
end
|
||||
|
||||
context 'when account can view status' do
|
||||
context 'when accounts do not block each other' do
|
||||
it { is_expected.to permit(account, poll) }
|
||||
end
|
||||
|
||||
context 'when view blocks poll creator' do
|
||||
before { Fabricate :block, account: account, target_account: poll.account }
|
||||
|
||||
it { is_expected.to_not permit(account, poll) }
|
||||
end
|
||||
|
||||
context 'when poll creator blocks viewer' do
|
||||
before { Fabricate :block, account: poll.account, target_account: account }
|
||||
|
||||
it { is_expected.to_not permit(account, poll) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,56 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe UserRolePolicy do
|
||||
subject { described_class }
|
||||
|
||||
let(:admin) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
permissions :index?, :create? do
|
||||
context 'when admin' do
|
||||
it { is_expected.to permit(admin, UserRole.new) }
|
||||
end
|
||||
|
||||
context 'when not admin' do
|
||||
it { is_expected.to_not permit(account, UserRole.new) }
|
||||
end
|
||||
end
|
||||
|
||||
permissions :update? do
|
||||
context 'when admin' do
|
||||
context 'when role of admin overrides relevant role' do
|
||||
it { is_expected.to permit(admin, UserRole.new(position: admin.user.role.position - 10, id: 123)) }
|
||||
end
|
||||
|
||||
context 'when role of admin does not override relevant role' do
|
||||
it { is_expected.to_not permit(admin, UserRole.new(position: admin.user.role.position + 10, id: 123)) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when not admin' do
|
||||
it { is_expected.to_not permit(account, UserRole.new) }
|
||||
end
|
||||
end
|
||||
|
||||
permissions :destroy? do
|
||||
context 'when admin' do
|
||||
context 'when role of admin overrides relevant role' do
|
||||
it { is_expected.to permit(admin, UserRole.new(position: admin.user.role.position - 10)) }
|
||||
end
|
||||
|
||||
context 'when role of admin does not override relevant role' do
|
||||
it { is_expected.to_not permit(admin, UserRole.new(position: admin.user.role.position + 10)) }
|
||||
end
|
||||
|
||||
context 'when everyone role' do
|
||||
it { is_expected.to_not permit(admin, UserRole.everyone) }
|
||||
end
|
||||
end
|
||||
|
||||
context 'when not admin' do
|
||||
it { is_expected.to_not permit(account, UserRole.new) }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue