Skip to content

Commit

Permalink
test: add more clear specs for clear context
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanural committed Oct 22, 2024
1 parent cd0ad40 commit 9515c99
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions spec/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,35 +273,46 @@ def to_params(*args, **kwargs, &block)
end

describe "#pundit_reset!" do
let(:new_user) { double }
it "allows authorize to react to a user change" do
expect(controller.authorize(post)).to be_truthy
controller.current_user = double
controller.pundit_reset!
expect { controller.authorize(post) }.to raise_error(Pundit::NotAuthorizedError)
end

it "clears the current user" do
expect(controller.pundit.user).to eq controller.current_user
it "allows policy scope to react to a user change" do
expect(controller.policy_scope(Post)).to eq :published
expect { controller.verify_policy_scoped }.not_to raise_error
controller.current_user = double
controller.pundit_reset!
expect { controller.verify_policy_scoped }.to raise_error(Pundit::PolicyScopingNotPerformedError)
end

controller.current_user = new_user
expect(controller.pundit.user).not_to eq controller.current_user
it "clears the pundit context user" do
expect(controller.pundit.user).to be(user)

controller.pundit_reset!
expect(controller.pundit.user).to eq controller.current_user
new_user = double
controller.current_user = new_user
expect { controller.pundit_reset! }.to change { controller.pundit.user }.from(user).to(new_user)
end

it "clears the policy cache" do
controller.authorize(post)
expect(controller.policies).not_to be_empty
it "clears pundit_policy_authorized? flag" do
expect(controller.pundit_policy_authorized?).to be false

controller.skip_authorization
expect(controller.pundit_policy_authorized?).to be true

controller.pundit_reset!
expect(controller.policies).to be_empty
expect(controller.pundit_policy_authorized?).to be false
end

it "clears the policy scope cache" do
controller.policy_scope(Post)
expect(controller.policy_scopes).not_to be_empty
it "clears pundit_policy_scoped? flag" do
expect(controller.pundit_policy_scoped?).to be false

controller.skip_policy_scope
expect(controller.pundit_policy_scoped?).to be true

controller.pundit_reset!
expect(controller.policy_scopes).to be_empty
expect(controller.pundit_policy_scoped?).to be false
end
end
Expand Down

0 comments on commit 9515c99

Please sign in to comment.