Skip to content

Commit

Permalink
policy spec for guests
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Burns committed Jun 19, 2019
1 parent e744979 commit aba829e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ group :development, :test do
gem 'factory_bot_rails'
gem 'rspec-rails'
gem 'rubocop-rails'
gem 'pundit-matchers'
end

group :development do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ GEM
puma (3.12.1)
pundit (2.0.1)
activesupport (>= 3.0.0)
pundit-matchers (1.6.0)
rspec-rails (>= 3.0.0)
rack (2.0.7)
rack-proxy (0.6.5)
rack
Expand Down Expand Up @@ -240,6 +242,7 @@ DEPENDENCIES
pg (>= 0.18, < 2.0)
puma (~> 3.11)
pundit
pundit-matchers
rails (~> 5.2.3)
rspec-rails
rubocop-rails
Expand Down
12 changes: 6 additions & 6 deletions app/policies/publication_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def initialize(user, publication)
@publication = publication
end

def update?
publication.user_id == user.id
def self.update?
@publication.user == @user
end

def edit?
publication.user_id == user.id
def self.edit?
@publication.user == @user
end

def destroy?
publication.user_id == user.id
def self.destroy?
@publication.user == @user
end
end
33 changes: 15 additions & 18 deletions spec/policies/publication_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
require 'rails_helper'

RSpec.describe PublicationPolicy, type: :policy do
let(:user) { User.new }

subject { described_class }
subject { described_class.new(build(:user), attributes_for(:publication, user: build(:user), publication_sub_category: build(:publication_sub_category, publication_category: build(:publication_category))))}

permissions ".scope" do
pending "add some examples to (or delete) #{__FILE__}"
end

permissions :show? do
pending "add some examples to (or delete) #{__FILE__}"
end
context "visitor (unauthenticated user)" do
before(:each) do
pcat = build(:publication_category)
subcat = build(:publication_sub_category, publication_category: pcat)
@user = build(:user)
@pub = build(:publication, publication_sub_category: subcat, user: user)
end

permissions :create? do
pending "add some examples to (or delete) #{__FILE__}"
end

permissions :update? do
pending "add some examples to (or delete) #{__FILE__}"
end
let (:user) { nil }
let (:publication) { @pub }

permissions :destroy? do
pending "add some examples to (or delete) #{__FILE__}"
#it { is_expected.to permit_action(:index) }
#it { is_expected.to permit_action(:show) }
it { is_expected.to forbid_action(:edit) }
it { is_expected.to forbid_action(:update) }
it { is_expected.to forbid_action(:destroy) }
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'support/factory_bot'
require 'pundit/matchers'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
Expand Down

0 comments on commit aba829e

Please sign in to comment.