shoulda-matchers-pundit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ Procfile
19
+ Guardfile
20
+ .rvmrc
21
+ .guard_history
22
+ .sass-cache
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0
6
+ - jruby
7
+ - rbx-19mode
8
+ # uncomment this line if your project needs to run something other than `rake`:
9
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,29 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :development do
5
+
6
+ gem 'foreman', '~> 0.62.0'
7
+ gem 'guard', '~> 1.6.2'
8
+ gem 'guard-minitest', '~> 0.5.0'
9
+ gem 'pry'
10
+
11
+ gem 'rspec-web', github: 'dcunited001/rspec-web'
12
+ gem 'minitest-reporters-ws', "~> 0.0.2"
13
+
14
+ if RbConfig::CONFIG['target_os'] =~ /linux/i
15
+ gem 'rb-inotify', require: false
16
+ gem 'ruby_gntp'
17
+ end
18
+
19
+ if RbConfig::CONFIG['target_os'] =~ /darwin/i
20
+ gem 'rb-fsevent', require: false
21
+ gem 'growl'
22
+ end
23
+
24
+ if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
25
+ gem 'wdm', require: false, platforms: [:mswin, :mingw]
26
+ gem 'rb-notifu', platforms: [:mswin, :mingw]
27
+ end
28
+
29
+ end
data/Guardfile.example ADDED
@@ -0,0 +1,10 @@
1
+ interactor guard_rc: '~/.my_guard-rc', history_file: '.guard_history'
2
+ scope groups: [:minitest]
3
+
4
+ group :minitest do
5
+ guard :minitest, :notify => true do
6
+ watch(%r|^spec/(.*)_spec\.rb|)
7
+ watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
8
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
9
+ end
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 David Conner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Procfile.example ADDED
@@ -0,0 +1 @@
1
+ rspecweb: rspec-web
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ [![Build Status](https://travis-ci.org/dcunited001/shoulda-matchers-pundit.png)](https://travis-ci.org/dcunited001/shoulda-matchers-pundit)
2
+
3
+ # Shoulda::Matchers::Pundit
4
+
5
+ TODO: update readme: description, installation, usage
6
+ TODO: Configure Travis
7
+ TODO: Configure Appraisal?
8
+ TODO: file to load matcher for RSpec & integration test & test task
9
+ TODO: file to load matcher for Cucumber & integration test & test task
10
+ TODO: relocate dev dependencies from Gemfile to Gemspec
11
+ TODO: Change gemspec dependencies to work with older versions of shoulda-matchers & pundit
12
+ TODO: allow configurable error messages per suite & per spec file?
13
+ TODO: add aliases to PermittedToMatcher?
14
+ TODO: add ForbidMatcher & ForbiddenToMatcher? (& update integration tests)
15
+ TODO: find a way to remove the constraint that a policy's record must be named record
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ gem 'shoulda-matchers-pundit'
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install shoulda-matchers-pundit
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Contributing
36
+
37
+ 1. Fork it
38
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
39
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
40
+ 4. Push to the branch (`git push origin my-new-feature`)
41
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rake/testtask'
4
+ require "bundler/gem_tasks"
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "spec"
8
+ t.test_files = FileList['spec/**/*_spec.rb']
9
+ t.verbose = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ module Shoulda
2
+ module Matchers
3
+ module Pundit
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,102 @@
1
+ require "shoulda/matchers/pundit/version"
2
+
3
+ module Shoulda
4
+ module Matchers
5
+ module Pundit
6
+
7
+ def permit(action)
8
+ PermitMatcher.new(action)
9
+ end
10
+
11
+ def be_permitted_to(action)
12
+ PermittedToMatcher.new(action)
13
+ end
14
+
15
+ class PermitMatcher
16
+ attr_reader :action, :policy
17
+
18
+ def initialize(action)
19
+ @action = action
20
+ end
21
+
22
+ def matches?(policy)
23
+ @policy = policy
24
+ policy.public_send("#{action}?")
25
+ end
26
+
27
+ def failure_message_for_should
28
+ <<-message
29
+ #{policy.class} does not permit #{action} on #{policy.record}
30
+ for #{policy.user.inspect}.
31
+ message
32
+ end
33
+
34
+ def failure_message_for_should_not
35
+ <<-message
36
+ #{policy.class} does not forbid #{action} on #{policy.record}
37
+ for #{policy.user.inspect}.
38
+ message
39
+ end
40
+
41
+ def description
42
+ "permits #{@action}"
43
+ end
44
+ end
45
+
46
+ class PermittedToMatcher
47
+ attr_reader :action, :record, :policy
48
+
49
+ def initialize(action)
50
+ @action = action
51
+ end
52
+
53
+ def matches?(user)
54
+ @policy ||= infer_policy(user)
55
+ @policy = policy.call(user, record) if @policy.is_a? Proc
56
+ policy.public_send("#{action}?")
57
+ end
58
+
59
+ def on(record)
60
+ @record = record
61
+ self
62
+ end
63
+
64
+ #alias_method :on_a, :on
65
+ #alias_method :a, :on
66
+ #alias_method :to, :on
67
+
68
+ def by(policy)
69
+ @policy = case policy
70
+ when Class then lambda { |user, record| policy.new(user, record) }
71
+ else policy
72
+ end
73
+ self
74
+ end
75
+
76
+ def failure_message_for_should
77
+ <<-message
78
+ User is not permitted to #{action} a #{policy.record} by #{policy.class}.
79
+ #{policy.user.inspect}.
80
+ message
81
+ end
82
+
83
+ def failure_message_for_should_not
84
+ <<-message
85
+ User is not forbidden to #{action} a #{policy.record} by #{policy.class}.
86
+ #{policy.user.inspect}.
87
+ message
88
+ end
89
+
90
+ def description
91
+ "permits #{action} on #{record}"
92
+ end
93
+
94
+ private
95
+
96
+ def infer_policy(user)
97
+ "#{record.class.name}Policy".constantize.new(user, record)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,15 @@
1
+ require 'shoulda/matchers/pundit'
2
+
3
+ if defined?(Pundit)
4
+ Shoulda::Matchers::Pundit.module_eval do
5
+ def self.included(base)
6
+ instance_methods.each do |name|
7
+ base.register_matcher name, name
8
+ end
9
+ end
10
+ end
11
+
12
+ class MiniTest::Spec
13
+ include Shoulda::Matchers::Pundit
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module Shoulda
2
+ module Matchers
3
+ module Pundit
4
+ VERSION = "0.0.1".freeze
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
4
+ require 'shoulda/matchers/pundit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "shoulda-matchers-pundit"
8
+ spec.version = Shoulda::Matchers::Pundit::VERSION.dup
9
+ spec.authors = ["David Conner"]
10
+ spec.email = ["dconner.pro@gmail.com"]
11
+ spec.description = %q{Shoulda Matchers for Pundit & Setup for RSpec/Minitest}
12
+ spec.summary = %q{Shoulda Matchers for Pundit & Setup for RSpec/Minitest}
13
+ spec.homepage = "http://github.com/dcunited001/shoulda-matchers-pundit"
14
+ spec.license = "MIT"
15
+ spec.date = Time.now.strftime("%Y-%m-%d")
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "shoulda-matchers", ">= 1.4.1"
23
+ spec.add_dependency "pundit", "~> 0.2.0"
24
+
25
+ spec.add_development_dependency "rake", ">= 0.9.2"
26
+ spec.add_development_dependency "minitest-matchers", ">= 1.2.0"
27
+
28
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::Pundit::PermitMatcher do
4
+
5
+ describe ArticlePolicy do
6
+
7
+ subject { ArticlePolicy.new(user, article) }
8
+ let(:article) { Article.new(user) }
9
+
10
+ describe 'Guest' do
11
+ let(:user) { nil }
12
+
13
+ wont { permit(:create) }
14
+ must { permit(:show) }
15
+ wont { permit(:update) }
16
+ wont { permit(:destroy) }
17
+
18
+ end
19
+
20
+ describe 'Any User' do
21
+ let(:user) { User.new(0) }
22
+ let(:another_user) { User.new(1) }
23
+ let(:article) { Article.new(another_user) }
24
+
25
+ must { permit(:create) }
26
+ must { permit(:show) }
27
+ wont { permit(:update) }
28
+ wont { permit(:destroy) }
29
+
30
+ end
31
+
32
+ describe 'Article Creater' do
33
+ let(:user) { User.new(1) }
34
+
35
+ must { permit(:create) }
36
+ must { permit(:show) }
37
+ must { permit(:update) }
38
+ wont { permit(:destroy) }
39
+
40
+ end
41
+ end
42
+ end
43
+
44
+ describe Shoulda::Matchers::Pundit::PermittedToMatcher do
45
+
46
+ let(:article) { Article.new(subject) }
47
+
48
+ describe 'Guest' do
49
+ subject { nil }
50
+
51
+ describe ArticlePolicy do
52
+
53
+ wont { be_permitted_to(:create).on(article) }
54
+ must { be_permitted_to(:show).on(article) }
55
+ wont { be_permitted_to(:update).on(article) }
56
+ wont { be_permitted_to(:destroy).on(article) }
57
+
58
+ end
59
+ end
60
+
61
+
62
+ describe 'Any User' do
63
+ subject { User.new(0) }
64
+
65
+ describe ArticlePolicy do
66
+ let(:another_user) { User.new(1) }
67
+ let(:article) { Article.new(another_user) }
68
+
69
+ must { be_permitted_to(:create).on(article) }
70
+ must { be_permitted_to(:show).on(article) }
71
+ wont { be_permitted_to(:update).on(article) }
72
+ wont { be_permitted_to(:destroy).on(article) }
73
+
74
+ end
75
+
76
+ end
77
+
78
+ describe 'Article Creator' do
79
+ subject { User.new(1) }
80
+
81
+ describe ArticlePolicy do
82
+
83
+ must { be_permitted_to(:create).on(article) }
84
+ must { be_permitted_to(:show).on(article) }
85
+ must { be_permitted_to(:update).on(article) }
86
+ wont { be_permitted_to(:destroy).on(article) }
87
+
88
+ end
89
+
90
+ end
91
+ end
@@ -0,0 +1,55 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/matchers'
3
+ require "pry"
4
+
5
+ require "pundit"
6
+ require "active_model/naming"
7
+
8
+ require 'shoulda/matchers/pundit/minitest'
9
+
10
+ if ENV['MINITESTREPORTER']
11
+ require "minitest/reporters"
12
+ require "minitest/reporters/ws"
13
+
14
+ reporter = case ENV['MINITESTREPORTER']
15
+ when 'ws' then MiniTest::Reporters::Ws::Reporter.new
16
+ when 'spec' then MiniTest::Reporters::SpecReporter.new
17
+ else MiniTest::Reporters::ProgressReporter.new
18
+ end
19
+
20
+ Minitest::Reporters.use! reporter
21
+ end
22
+
23
+ class ArticlePolicy < Struct.new(:user, :record)
24
+
25
+ def create?
26
+ true if user
27
+ end
28
+
29
+ def update?
30
+ user && (record.user_id == user.id)
31
+ end
32
+
33
+ def destroy?
34
+ false
35
+ end
36
+
37
+ def show?
38
+ true
39
+ end
40
+
41
+ end
42
+
43
+ class Article
44
+ attr_accessor :user_id
45
+ def initialize(user)
46
+ @user_id = user.id if user
47
+ end
48
+ end
49
+
50
+ class User
51
+ attr_accessor :id
52
+ def initialize(id)
53
+ @id = id
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shoulda-matchers-pundit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Conner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda-matchers
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: pundit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.2.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest-matchers
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 1.2.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.2.0
78
+ description: Shoulda Matchers for Pundit & Setup for RSpec/Minitest
79
+ email:
80
+ - dconner.pro@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - Guardfile.example
89
+ - LICENSE.txt
90
+ - Procfile.example
91
+ - README.md
92
+ - Rakefile
93
+ - lib/shoulda-matchers-pundit.rb
94
+ - lib/shoulda/matchers/pundit.rb
95
+ - lib/shoulda/matchers/pundit/minitest.rb
96
+ - lib/shoulda/matchers/pundit/version.rb
97
+ - shoulda-matchers-pundit.gemspec
98
+ - spec/minitest_matchers_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: http://github.com/dcunited001/shoulda-matchers-pundit
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 1.8.25
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Shoulda Matchers for Pundit & Setup for RSpec/Minitest
125
+ test_files:
126
+ - spec/minitest_matchers_spec.rb
127
+ - spec/spec_helper.rb
128
+ has_rdoc: