spec/pundit_spec.rb in pundit-2.1.0 vs spec/pundit_spec.rb in pundit-2.1.1
- old
+ new
@@ -1,10 +1,10 @@
# frozen_string_literal: true
require "spec_helper"
-describe Pundit do
+RSpec.describe Pundit do
let(:user) { double }
let(:post) { Post.new(user) }
let(:customer_post) { Customer::Post.new(user) }
let(:post_four_five_six) { PostFourFiveSix.new(user) }
let(:comment) { Comment.new }
@@ -23,10 +23,30 @@
describe ".authorize" do
it "infers the policy and authorizes based on it" do
expect(Pundit.authorize(user, post, :update?)).to be_truthy
end
+ it "returns the record on successful authorization" do
+ expect(Pundit.authorize(user, post, :update?)).to eq(post)
+ end
+
+ it "returns the record when passed record with namespace " do
+ expect(Pundit.authorize(user, [:project, comment], :update?)).to eq(comment)
+ end
+
+ it "returns the record when passed record with nested namespace " do
+ expect(Pundit.authorize(user, [:project, :admin, comment], :update?)).to eq(comment)
+ end
+
+ it "returns the policy name symbol when passed record with headless policy" do
+ expect(Pundit.authorize(user, :publication, :create?)).to eq(:publication)
+ end
+
+ it "returns the class when passed record not a particular instance" do
+ expect(Pundit.authorize(user, Post, :show?)).to eq(Post)
+ end
+
it "can be given a different policy class" do
expect(Pundit.authorize(user, post, :create?, policy_class: PublicationPolicy)).to be_truthy
end
it "works with anonymous class policies" do
@@ -408,10 +428,26 @@
it "infers the policy name and authorizes based on it" do
expect(controller.authorize(post)).to be_truthy
end
it "returns the record on successful authorization" do
- expect(controller.authorize(post)).to be(post)
+ expect(controller.authorize(post)).to eq(post)
+ end
+
+ it "returns the record when passed record with namespace " do
+ expect(controller.authorize([:project, comment], :update?)).to eq(comment)
+ end
+
+ it "returns the record when passed record with nested namespace " do
+ expect(controller.authorize([:project, :admin, comment], :update?)).to eq(comment)
+ end
+
+ it "returns the policy name symbol when passed record with headless policy" do
+ expect(controller.authorize(:publication, :create?)).to eq(:publication)
+ end
+
+ it "returns the class when passed record not a particular instance" do
+ expect(controller.authorize(Post, :show?)).to eq(Post)
end
it "can be given a different permission to check" do
expect(controller.authorize(post, :show?)).to be_truthy
expect { controller.authorize(post, :destroy?) }.to raise_error(Pundit::NotAuthorizedError)