Skip to content

Instantly share code, notes, and snippets.

@netzfisch
Created June 26, 2014 13:51
Show Gist options
  • Save netzfisch/acc249f828884c739848 to your computer and use it in GitHub Desktop.
Save netzfisch/acc249f828884c739848 to your computer and use it in GitHub Desktop.
RSpec example for rails authorisation with Pundit's policy_scope method
describe PostPolicy do
let(:scope) { Post.where(:published => true }
subject(:policy_scope) { PostPolicy::Scope.new(user, scope).resolve }
permissions ".scope" do
context "for an ordinary user"
let(:user) { User.new(:admin => false) }
it "hides unpublished post" do
post = Post.create(:published => false)
expect(policy_scope).to eq []
end
it "shows published post" do
post = Post.create(:published => true)
expect(policy_scope).to eq [post]
end
end
context "for an admin user"
let(:user) { User.new(:admin => true) }
it "shows unpublished post" do
post = Post.create(:published => false)
expect(policy_scope).to eq [post]
end
it "shows published post" do
post = Post.create(:published => true)
expect(policy_scope).to eq [post]
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment