diff options
author | Matt Jankowski <matt@jankowski.online> | 2024-10-15 10:24:12 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-15 14:24:12 +0000 |
commit | 9e8f099d38029b74d46daa7edbadeaf7057aebfc (patch) | |
tree | 7e7495ec645da34edd01bfab4cd1f51fa612f54e | |
parent | 28131b4b00e65826ac58a034d99b13123461940d (diff) |
Improve `app/policies` coverage (#32426)
35 files changed, 264 insertions, 55 deletions
diff --git a/spec/policies/account_moderation_note_policy_spec.rb b/spec/policies/account_moderation_note_policy_spec.rb index 8c37acc39fe..8b33a710127 100644 --- a/spec/policies/account_moderation_note_policy_spec.rb +++ b/spec/policies/account_moderation_note_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe AccountModerationNotePolicy do subject { described_class } @@ -12,13 +11,13 @@ RSpec.describe AccountModerationNotePolicy do permissions :create? do context 'when staff' do it 'grants to create' do - expect(subject).to permit(admin, described_class) + expect(subject).to permit(admin, AccountModerationNote) end end context 'when not staff' do it 'denies to create' do - expect(subject).to_not permit(john, described_class) + expect(subject).to_not permit(john, AccountModerationNote) end end end diff --git a/spec/policies/account_policy_spec.rb b/spec/policies/account_policy_spec.rb index d7a21d8e39a..75724e831be 100644 --- a/spec/policies/account_policy_spec.rb +++ b/spec/policies/account_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe AccountPolicy do subject { described_class } @@ -24,7 +23,7 @@ RSpec.describe AccountPolicy do end end - permissions :show?, :unsilence?, :unsensitive?, :remove_avatar?, :remove_header? do + permissions :show?, :unsilence?, :unsensitive?, :remove_avatar?, :remove_header?, :sensitive?, :warn? do context 'when staff' do it 'permits' do expect(subject).to permit(admin, alice) diff --git a/spec/policies/account_warning_policy_spec.rb b/spec/policies/account_warning_policy_spec.rb new file mode 100644 index 00000000000..9abc9d35d63 --- /dev/null +++ b/spec/policies/account_warning_policy_spec.rb @@ -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 diff --git a/spec/policies/account_warning_preset_policy_spec.rb b/spec/policies/account_warning_preset_policy_spec.rb index 53e224f19f7..33f2fb11870 100644 --- a/spec/policies/account_warning_preset_policy_spec.rb +++ b/spec/policies/account_warning_preset_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe AccountWarningPresetPolicy do let(:policy) { described_class } @@ -11,13 +10,13 @@ RSpec.describe AccountWarningPresetPolicy do permissions :index?, :create?, :update?, :destroy? do context 'with an admin' do it 'permits' do - expect(policy).to permit(admin, Tag) + expect(policy).to permit(admin, AccountWarningPreset) end end context 'with a non-admin' do it 'denies' do - expect(policy).to_not permit(john, Tag) + expect(policy).to_not permit(john, AccountWarningPreset) end end end diff --git a/spec/policies/admin/status_policy_spec.rb b/spec/policies/admin/status_policy_spec.rb index 07af425516d..4df29393e3a 100644 --- a/spec/policies/admin/status_policy_spec.rb +++ b/spec/policies/admin/status_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe Admin::StatusPolicy do let(:policy) { described_class } @@ -13,13 +12,13 @@ RSpec.describe Admin::StatusPolicy do permissions :index?, :update?, :review?, :destroy? do context 'with an admin' do it 'permits' do - expect(policy).to permit(admin, Tag) + expect(policy).to permit(admin, Status) end end context 'with a non-admin' do it 'denies' do - expect(policy).to_not permit(john, Tag) + expect(policy).to_not permit(john, Status) end end end diff --git a/spec/policies/announcement_policy_spec.rb b/spec/policies/announcement_policy_spec.rb index 503ffca6dca..ab0c1dbaf5a 100644 --- a/spec/policies/announcement_policy_spec.rb +++ b/spec/policies/announcement_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe AnnouncementPolicy do let(:policy) { described_class } @@ -11,13 +10,13 @@ RSpec.describe AnnouncementPolicy do permissions :index?, :create?, :update?, :destroy? do context 'with an admin' do it 'permits' do - expect(policy).to permit(admin, Tag) + expect(policy).to permit(admin, Announcement) end end context 'with a non-admin' do it 'denies' do - expect(policy).to_not permit(john, Tag) + expect(policy).to_not permit(john, Announcement) end end end diff --git a/spec/policies/appeal_policy_spec.rb b/spec/policies/appeal_policy_spec.rb index 1bf8ce0a0da..cdb93bf56cd 100644 --- a/spec/policies/appeal_policy_spec.rb +++ b/spec/policies/appeal_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe AppealPolicy do let(:policy) { described_class } @@ -12,18 +11,18 @@ RSpec.describe AppealPolicy do permissions :index? do context 'with an admin' do it 'permits' do - expect(policy).to permit(admin, Tag) + expect(policy).to permit(admin, Appeal) end end context 'with a non-admin' do it 'denies' do - expect(policy).to_not permit(john, Tag) + expect(policy).to_not permit(john, Appeal) end end end - permissions :reject? do + permissions :reject?, :approve? do context 'with an admin' do context 'with a pending appeal' do before { allow(appeal).to receive(:pending?).and_return(true) } diff --git a/spec/policies/audit_log_policy_spec.rb b/spec/policies/audit_log_policy_spec.rb new file mode 100644 index 00000000000..d9d93594333 --- /dev/null +++ b/spec/policies/audit_log_policy_spec.rb @@ -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 diff --git a/spec/policies/backup_policy_spec.rb b/spec/policies/backup_policy_spec.rb index 28cb65d7890..031021d91df 100644 --- a/spec/policies/backup_policy_spec.rb +++ b/spec/policies/backup_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe BackupPolicy do subject { described_class } diff --git a/spec/policies/canonical_email_block_policy_spec.rb b/spec/policies/canonical_email_block_policy_spec.rb index f5029d9e6b6..b253b439a67 100644 --- a/spec/policies/canonical_email_block_policy_spec.rb +++ b/spec/policies/canonical_email_block_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe CanonicalEmailBlockPolicy do let(:policy) { described_class } @@ -11,13 +10,13 @@ RSpec.describe CanonicalEmailBlockPolicy do permissions :index?, :show?, :test?, :create?, :destroy? do context 'with an admin' do it 'permits' do - expect(policy).to permit(admin, Tag) + expect(policy).to permit(admin, CanonicalEmailBlock) end end context 'with a non-admin' do it 'denies' do - expect(policy).to_not permit(john, Tag) + expect(policy).to_not permit(john, CanonicalEmailBlock) end end end diff --git a/spec/policies/custom_emoji_policy_spec.rb b/spec/policies/custom_emoji_policy_spec.rb index cb869c7d9a7..189885938cf 100644 --- a/spec/policies/custom_emoji_policy_spec.rb +++ b/spec/policies/custom_emoji_policy_spec.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'rails_helper' -require 'pundit/rspec' RSpec.describe CustomEmojiPolicy do subject { described_class } diff --git a/spec/policies/dashboard_policy_spec.rb b/spec/policies/dashboard_policy_spec.rb new file mode 100644 index 00000000000..90c71db381b --- /dev/null +++ b/spec/policies/dashboard_policy_spec.rb @@ -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 diff --git a/spec/policies/delivery_policy_spec.rb b/spec/policies/delivery_policy_spec.rb index bb82389eec8..8bc200159ac 100644 --- a/spec/policies/delivery_policy_spec.rb +++ b/spec/policies/delivery_policy_spec.rb @@ -1,7 +1,6 @@ |