Skip to content

Commit 8a9a7a6

Browse files
committed
add a suite of tests for Pundit
1 parent 859384e commit 8a9a7a6

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed

CHANGELOG.textile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
h1. CHANGELOG
22

3+
h3. 0.3.4 June 14, 2014
4+
5+
* add a suite of tests for Pundit
6+
37
h3. 0.3.3 June 7, 2014
48

59
* eliminate deprecated 'should' syntax for OmniAuth tests

lib/generators/testing/configure/configure_generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ def configure_framework
4545
copy_file 'spec/omniauth/controllers/sessions_controller_spec.rb', 'spec/controllers/sessions_controller_spec.rb'
4646
copy_file 'spec/omniauth/factories/users.rb', 'spec/factories/users.rb'
4747
copy_file 'spec/omniauth/models/user_spec.rb', 'spec/models/user_spec.rb'
48+
when 'pundit'
49+
copy_file 'spec/pundit/factories/users.rb', 'spec/factories/users.rb'
50+
copy_file 'spec/pundit/features/users/user_index_spec.rb', 'spec/features/users/user_index_spec.rb'
51+
copy_file 'spec/pundit/policies/user_policy_spec.rb', 'spec/policies/user_policy_spec.rb'
52+
copy_file 'spec/pundit/support/pundit.rb', 'spec/support/pundit.rb'
4853
end
4954
end
5055

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FactoryGirl.define do
2+
factory :user do
3+
name "Test User"
4+
5+
password "please123"
6+
7+
trait :admin do
8+
role 'admin'
9+
end
10+
11+
end
12+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
include Warden::Test::Helpers
2+
Warden.test_mode!
3+
4+
# Feature: User index page
5+
# As a user
6+
# I want to see a list of users
7+
# So I can see who has registered
8+
feature 'User index page', :devise do
9+
10+
after(:each) do
11+
Warden.test_reset!
12+
end
13+
14+
# Scenario: User listed on index page
15+
# Given I am signed in
16+
# When I visit the user index page
17+
# Then I see my own email address
18+
scenario 'user sees own email address' do
19+
user = FactoryGirl.create(:user, :admin)
20+
login_as(user, scope: :user)
21+
visit users_path
22+
expect(page).to have_content user.email
23+
end
24+
25+
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
describe UserPolicy do
2+
subject { UserPolicy }
3+
4+
let (:current_user) { FactoryGirl.build_stubbed :user }
5+
let (:other_user) { FactoryGirl.build_stubbed :user }
6+
let (:admin) { FactoryGirl.build_stubbed :user, :admin }
7+
8+
permissions :index? do
9+
it "denies access if not an admin" do
10+
expect(UserPolicy).not_to permit(current_user)
11+
end
12+
it "allows access for an admin" do
13+
expect(UserPolicy).to permit(admin)
14+
end
15+
end
16+
17+
permissions :show? do
18+
it "prevents other users from seeing your profile" do
19+
expect(subject).not_to permit(current_user, other_user)
20+
end
21+
it "allows you to see your own profile" do
22+
expect(subject).to permit(current_user, current_user)
23+
end
24+
it "allows an admin to see any profile" do
25+
expect(subject).to permit(admin)
26+
end
27+
end
28+
29+
permissions :update? do
30+
it "prevents updates if not an admin" do
31+
expect(subject).not_to permit(current_user)
32+
end
33+
it "allows an admin to make updates" do
34+
expect(subject).to permit(admin)
35+
end
36+
end
37+
38+
permissions :destroy? do
39+
it "prevents deleting yourself" do
40+
expect(subject).not_to permit(current_user, current_user)
41+
end
42+
it "allows an admin to delete any user" do
43+
expect(subject).to permit(admin, other_user)
44+
end
45+
end
46+
47+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'pundit/rspec'

lib/rails_apps_testing/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsAppsTesting
2-
VERSION = "0.3.3"
2+
VERSION = "0.3.4"
33
end

0 commit comments

Comments
 (0)