Last active
August 29, 2015 14:05
-
-
Save nruth/7b242912e3966b1aa1fa to your computer and use it in GitHub Desktop.
role scoped pundit spec for rspec 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding : utf-8 -*- | |
# attempting to get something like http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec again | |
require 'rails_helper' | |
RSpec.describe AdminPolicy, type: :policy do | |
subject { AdminPolicy } | |
def self.permits(action) | |
permissions action do | |
it { expect(subject).to permit(current_admin, admin) } | |
end | |
end | |
def self.doesnt_permit(action) | |
permissions action do | |
it { expect(subject).not_to permit(current_admin, admin) } | |
end | |
end | |
context "for a product owner" do | |
let(:current_admin) { create :product_owner } | |
context "creating a new admin" do | |
let(:admin) { Admin.new } | |
permits :new? | |
permits :create? | |
end | |
context "accessing themselves" do | |
let(:admin) {current_admin} | |
permits :show? | |
permits :edit? | |
permits :update? | |
doesnt_permit :destroy? | |
end | |
context "accessing another admin" do | |
let(:admin) { Admin.new } | |
permits :show? | |
permits :edit? | |
permits :update? | |
doesnt_permit :destroy? | |
end | |
end | |
context "for a question contributor" do | |
let(:current_admin) { create :question_contributor } | |
context "creating a new admin" do | |
let(:admin) { Admin.new } | |
doesnt_permit :new? | |
doesnt_permit :create? | |
end | |
context "accessing themselves" do | |
let(:admin) {current_admin} | |
permits :show? | |
permits :edit? | |
permits :update? | |
doesnt_permit :destroy? | |
end | |
context "accessing another admin" do | |
let(:admin) { Admin.new } | |
doesnt_permit :show? | |
doesnt_permit :edit? | |
doesnt_permit :update? | |
doesnt_permit :destroy? | |
end | |
end | |
context "for a non role" do | |
let(:current_admin) { build :admin } | |
context "creating a new admin" do | |
let(:admin) { Admin.new } | |
doesnt_permit :new? | |
doesnt_permit :create? | |
end | |
context "accessing themselves" do | |
let(:admin) {current_admin} | |
permits :show? | |
doesnt_permit :edit? | |
doesnt_permit :update? | |
doesnt_permit :destroy? | |
end | |
context "accessing another admin" do | |
let(:admin) { Admin.new } | |
doesnt_permit :show? | |
doesnt_permit :edit? | |
doesnt_permit :update? | |
doesnt_permit :destroy? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment