I noticed a warning from rspec when this policy spec ran:
Using expect { }.not_to permit_actions could produce
confusing results. Please use `.to forbid_actions` instead. To
clarify, `.not_to permit_actions` will look at all of the actions and
checks if ANY actions fail, not if all actions fail. Therefore, you
could result in something like this:
it { is_expected.to permit_actions([:new, :create, :edit]) }
it { is_expected.not_to permit_actions([:edit, :destroy]) }
In this case, edit would be true and destroy would be false, but both
tests would pass.
I just follow the helpful advice.
29 lines
789 B
Ruby
29 lines
789 B
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe PinnedArticlePolicy, type: :policy do
|
|
subject { described_class.new(user, nil) }
|
|
|
|
context "when user is not signed in" do
|
|
let(:user) { nil }
|
|
|
|
it { within_block_is_expected.to raise_error(Pundit::NotAuthorizedError) }
|
|
end
|
|
|
|
context "when user is signed in as a regular user" do
|
|
let(:user) { build_stubbed(:user) }
|
|
|
|
it { is_expected.to forbid_actions(%i[show update destroy]) }
|
|
end
|
|
|
|
context "when user is signed in as an admin" do
|
|
let(:user) { create(:user, :admin) }
|
|
|
|
it { is_expected.to permit_actions(%i[show update destroy]) }
|
|
end
|
|
|
|
context "when user is signed in as a super_admin" do
|
|
let(:user) { create(:user, :super_admin) }
|
|
|
|
it { is_expected.to permit_actions(%i[show update destroy]) }
|
|
end
|
|
end
|