monty 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.md +17 -0
- data/Rakefile +55 -0
- data/lib/monty.rb +19 -0
- data/lib/monty/access.rb +82 -0
- data/lib/monty/configuration.rb +34 -0
- data/lib/monty/controller.rb +35 -0
- data/lib/monty/permission.rb +30 -0
- data/lib/monty/watch.rb +54 -0
- data/monty.gemspec +68 -0
- data/test/helper.rb +20 -0
- data/test/monty/test_access.rb +67 -0
- data/test/monty/test_configuration.rb +12 -0
- data/test/monty/test_controller.rb +47 -0
- data/test/monty/test_permission.rb +44 -0
- data/test/monty/test_watch.rb +105 -0
- data/test/rcov.opts +5 -0
- data/test/test_monty.rb +16 -0
- metadata +100 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Andrew Stone
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= monty
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Andrew Stone. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require 'lib/monty.rb'
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "monty"
|
9
|
+
gem.version = Monty.version
|
10
|
+
gem.summary = %Q{Rack based authorization system}
|
11
|
+
gem.description = %Q{Rack based authoriztion system.}
|
12
|
+
gem.email = "andy@stonean.com"
|
13
|
+
gem.homepage = "http://github.com/stonean/monty"
|
14
|
+
gem.authors = ["stonean"]
|
15
|
+
gem.add_development_dependency "mocha", ">= 0.9.8"
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'yard'
|
24
|
+
YARD::Rake::YardocTask.new do |t|
|
25
|
+
t.files = FileList['lib/**/*.rb']
|
26
|
+
t.options = ['-r'] # optional
|
27
|
+
end
|
28
|
+
rescue LoadError
|
29
|
+
task :yard do
|
30
|
+
abort "YARD is not available. In order to run yard, you must: sudo gem install yard"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rake/testtask'
|
35
|
+
Rake::TestTask.new(:test) do |test|
|
36
|
+
test.libs << 'lib' << 'test'
|
37
|
+
test.pattern = 'test/**/test_*.rb'
|
38
|
+
test.verbose = true
|
39
|
+
end
|
40
|
+
|
41
|
+
begin
|
42
|
+
require 'rcov/rcovtask'
|
43
|
+
Rcov::RcovTask.new do |test|
|
44
|
+
test.libs << 'test'
|
45
|
+
test.pattern = 'test/**/test_*.rb'
|
46
|
+
test.verbose = true
|
47
|
+
test.rcov_opts = IO.readlines("test/rcov.opts").map {|l| l.chomp.split " "}.flatten
|
48
|
+
end
|
49
|
+
rescue LoadError
|
50
|
+
task :rcov do
|
51
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
task :default => :test
|
data/lib/monty.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$:.unshift(File.dirname(__FILE__))
|
4
|
+
|
5
|
+
module Monty
|
6
|
+
def self.version
|
7
|
+
'0.1.0'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.authenticated_access
|
11
|
+
Monty::Configuration.public_access + "|" + Monty::Configuration.protected_access
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require 'monty/configuration'
|
16
|
+
require 'monty/watch'
|
17
|
+
require 'monty/controller'
|
18
|
+
require 'monty/permission'
|
19
|
+
require 'monty/access'
|
data/lib/monty/access.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monty
|
4
|
+
module Access
|
5
|
+
# Define permision that defines how your application is accessed.
|
6
|
+
# # All methods on the site controller will be open to users who have
|
7
|
+
# # this permission.
|
8
|
+
# permission :public_pages do
|
9
|
+
# controller :site
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# # Can use multiple controller statements
|
13
|
+
# permission :public_pages do
|
14
|
+
# controller :site
|
15
|
+
# controller :posts
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # Only methods show, edit and update on the users controller will
|
19
|
+
# # be open to users who have this permission.
|
20
|
+
# permission :my_account_pages do
|
21
|
+
# controller :users do
|
22
|
+
# only :show, :edit, :update
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# # All methods except destroy on the users controller will be
|
27
|
+
# # open to users who have this permission.
|
28
|
+
# permission :manage_users do
|
29
|
+
# controller :users do
|
30
|
+
# except :destroy
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# @param [String,Symbol] name permission reference.
|
35
|
+
# @yield [Monty::Permission.new(name)] new permission object
|
36
|
+
def permission(name, &block)
|
37
|
+
permission = Monty::Permission.new(name)
|
38
|
+
if block_given?
|
39
|
+
permission.instance_eval(&block)
|
40
|
+
else
|
41
|
+
permission.controller(permission.name)
|
42
|
+
end
|
43
|
+
|
44
|
+
unless Monty::Configuration.has_permission?(permission)
|
45
|
+
Monty::Configuration.permissions << permission
|
46
|
+
end
|
47
|
+
|
48
|
+
permission
|
49
|
+
end
|
50
|
+
|
51
|
+
# Define which permissions are accessible to everyone
|
52
|
+
# public_access :site, :user_registration
|
53
|
+
#
|
54
|
+
# @param *[String,Symbol] permissions that are accessible to everyone
|
55
|
+
def public_access(*permissions)
|
56
|
+
Monty::Configuration.public_access = regexes(permissions)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Define which permissions are accessible to everyone
|
60
|
+
# protected_access :my_account, :site_administration
|
61
|
+
#
|
62
|
+
# @param *[String,Symbol] permissions that are accessbile to authenticated users
|
63
|
+
def protected_access(*permissions)
|
64
|
+
Monty::Configuration.protected_access = regexes(permissions)
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Method called by Monty::Watch to trigger parsing of class methods
|
69
|
+
def configure
|
70
|
+
true
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def regexes(permissions)
|
76
|
+
permissions.collect!{|p| p.to_s}
|
77
|
+
perms = Monty::Configuration.permissions.select{|p| permissions.include?(p.name)}
|
78
|
+
perms.collect{|p| p.regex_pattern}.join("|")
|
79
|
+
end
|
80
|
+
|
81
|
+
end # Access
|
82
|
+
end # Monty
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monty
|
4
|
+
module Configuration
|
5
|
+
class << self
|
6
|
+
# Path to redirect to if access is denied.
|
7
|
+
# Default: '/'
|
8
|
+
attr_accessor :access_denied_path
|
9
|
+
# Regex string of paths that are publicly accessible.
|
10
|
+
# Default "\/"
|
11
|
+
attr_accessor :public_access
|
12
|
+
# Array of paths that are restricted to an authenticated user.
|
13
|
+
# Default ""
|
14
|
+
attr_accessor :protected_access
|
15
|
+
# Array of permission objects that defines the access to the application.
|
16
|
+
# Default []
|
17
|
+
attr_accessor :permissions
|
18
|
+
|
19
|
+
# Set defaults.
|
20
|
+
def reset
|
21
|
+
@access_denied_path = "/"
|
22
|
+
@public_access = ""
|
23
|
+
@protected_access = ""
|
24
|
+
@permissions = []
|
25
|
+
end
|
26
|
+
|
27
|
+
def has_permission?(permission)
|
28
|
+
permissions.any?{|p| permission.name == p.name}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
self.reset
|
33
|
+
end # Configuration
|
34
|
+
end # Monty
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monty
|
4
|
+
class Controller
|
5
|
+
# Name of the controller
|
6
|
+
attr_accessor :name
|
7
|
+
# Regular expression pattern
|
8
|
+
attr_accessor :regex_pattern
|
9
|
+
# The only methods restricted on the controller
|
10
|
+
attr_accessor :exceptions
|
11
|
+
# The only methods allowed on the controller
|
12
|
+
attr_accessor :inclusions
|
13
|
+
|
14
|
+
|
15
|
+
# @param [String,Symbol] name controller reference.
|
16
|
+
def initialize(name)
|
17
|
+
@name = name.to_s
|
18
|
+
@regex_pattern = "\/#{@name}(\/.*)?"
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param *[String,Symbol] only methods restricted on the controller
|
22
|
+
def except(*methods)
|
23
|
+
return if methods.empty?
|
24
|
+
@exceptions = methods.collect{|m| m.to_s}
|
25
|
+
@regex_pattern = "\/#{@name}(?!\/(#{@exceptions.join('|')}))(\/.*)?"
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param *[String,Symbol] only methods allowed on the controller
|
29
|
+
def only(*methods)
|
30
|
+
return if methods.empty?
|
31
|
+
@inclusions = methods.collect{|m| m.to_s}
|
32
|
+
@regex_pattern = "\/#{@name}\/(#{@inclusions.join('|')})"
|
33
|
+
end
|
34
|
+
end # Controller
|
35
|
+
end # Monty
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monty
|
4
|
+
class Permission
|
5
|
+
# Name of permission
|
6
|
+
attr_accessor :name
|
7
|
+
# Array of controller objects that define the access rights for this permission
|
8
|
+
attr_accessor :controllers
|
9
|
+
|
10
|
+
# @param [String,Symbol] name permission reference.
|
11
|
+
def initialize(name)
|
12
|
+
@name = name.to_s
|
13
|
+
@controllers = []
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param [String,Symbol] name controller reference.
|
17
|
+
# @return new controller
|
18
|
+
def controller(name, &block)
|
19
|
+
controller = Monty::Controller.new(name)
|
20
|
+
controller.instance_eval(&block) if block_given?
|
21
|
+
@controllers << controller
|
22
|
+
controller
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return String representing all controllers defining this permission
|
26
|
+
def regex_pattern
|
27
|
+
controllers.collect{|c| "(#{c.regex_pattern})"}.join("|")
|
28
|
+
end
|
29
|
+
end # Permission
|
30
|
+
end # Monty
|
data/lib/monty/watch.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Monty
|
4
|
+
class Watch
|
5
|
+
def initialize(app, &block)
|
6
|
+
@app = app
|
7
|
+
|
8
|
+
instance_eval(&block) if block_given?
|
9
|
+
end
|
10
|
+
|
11
|
+
# Rack required method. For thread safety, dup self and execute _call
|
12
|
+
def call(env)
|
13
|
+
dup._call(env)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Authorize request. Redirect if access is denied.
|
17
|
+
def _call(env)
|
18
|
+
|
19
|
+
unless allowed?(env)
|
20
|
+
return [302, redirect_headers, []]
|
21
|
+
end
|
22
|
+
|
23
|
+
@app.call(env)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# @return [True,False] created from value of rack.session[:access_rights]
|
29
|
+
def allowed?(env)
|
30
|
+
path = env['PATH_INFO']
|
31
|
+
|
32
|
+
return true if path == '/'
|
33
|
+
|
34
|
+
access_rights(env).match(path)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Regexp] created from value of rack.session[:access_rights]
|
38
|
+
def access_rights(env)
|
39
|
+
begin
|
40
|
+
::Authorization.configure
|
41
|
+
rescue NameError
|
42
|
+
end
|
43
|
+
|
44
|
+
session = env['rack.session'] || {}
|
45
|
+
regex_string = session[:access_rights] || Monty::Configuration.public_access
|
46
|
+
|
47
|
+
Regexp.new(/\A#{regex_string}\z/)
|
48
|
+
end
|
49
|
+
|
50
|
+
def redirect_headers
|
51
|
+
{'Location' => Monty::Configuration.access_denied_path, 'Content-Type' => 'text/html'}
|
52
|
+
end
|
53
|
+
end # Watch
|
54
|
+
end # Monty
|
data/monty.gemspec
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{monty}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["stonean"]
|
12
|
+
s.date = %q{2010-05-05}
|
13
|
+
s.description = %q{Rack based authoriztion system.}
|
14
|
+
s.email = %q{andy@stonean.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README.md",
|
23
|
+
"Rakefile",
|
24
|
+
"lib/monty.rb",
|
25
|
+
"lib/monty/access.rb",
|
26
|
+
"lib/monty/configuration.rb",
|
27
|
+
"lib/monty/controller.rb",
|
28
|
+
"lib/monty/permission.rb",
|
29
|
+
"lib/monty/watch.rb",
|
30
|
+
"monty.gemspec",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/monty/test_access.rb",
|
33
|
+
"test/monty/test_configuration.rb",
|
34
|
+
"test/monty/test_controller.rb",
|
35
|
+
"test/monty/test_permission.rb",
|
36
|
+
"test/monty/test_watch.rb",
|
37
|
+
"test/rcov.opts",
|
38
|
+
"test/test_monty.rb"
|
39
|
+
]
|
40
|
+
s.homepage = %q{http://github.com/stonean/monty}
|
41
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
42
|
+
s.require_paths = ["lib"]
|
43
|
+
s.rubygems_version = %q{1.3.6}
|
44
|
+
s.summary = %q{Rack based authorization system}
|
45
|
+
s.test_files = [
|
46
|
+
"test/helper.rb",
|
47
|
+
"test/monty/test_configuration.rb",
|
48
|
+
"test/monty/test_controller.rb",
|
49
|
+
"test/monty/test_access.rb",
|
50
|
+
"test/monty/test_watch.rb",
|
51
|
+
"test/monty/test_permission.rb",
|
52
|
+
"test/test_monty.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.8"])
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
63
|
+
end
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<mocha>, [">= 0.9.8"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rack/test'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
require 'monty'
|
9
|
+
|
10
|
+
class Test::Unit::TestCase
|
11
|
+
include Rack::Test::Methods
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def mock_framework
|
16
|
+
framework = mock 'framework'
|
17
|
+
framework.stubs :call => [200, {'Content-Type' => 'text/plain'}, 'OK']
|
18
|
+
framework
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMontyAccess < Test::Unit::TestCase
|
4
|
+
include Monty::Access
|
5
|
+
|
6
|
+
def teardown
|
7
|
+
Monty::Configuration.reset
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_model_responds_to_permission
|
11
|
+
assert_respond_to self, :permission
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_permission_with_single_controller
|
15
|
+
perm = permission(:my_perm) do
|
16
|
+
controller :my_controller
|
17
|
+
end
|
18
|
+
|
19
|
+
controller = perm.controllers.first
|
20
|
+
assert_equal 'my_controller', controller.name
|
21
|
+
assert_equal "\/my_controller(\/.*)?", controller.regex_pattern
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_permission_without_block
|
25
|
+
perm = permission(:users)
|
26
|
+
|
27
|
+
controller = perm.controllers.first
|
28
|
+
assert_equal 'users', controller.name
|
29
|
+
assert_equal "\/users(\/.*)?", controller.regex_pattern
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_public_access
|
33
|
+
permission(:site)
|
34
|
+
public_access :site
|
35
|
+
|
36
|
+
assert_equal Monty::Configuration.public_access, "(\/site(\/.*)?)"
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_public_access_with_multiple_permissions
|
40
|
+
permission(:site)
|
41
|
+
permission(:registration)
|
42
|
+
permission(:view_posts)
|
43
|
+
public_access :site, :registration, :view_posts
|
44
|
+
|
45
|
+
assert_equal Monty::Configuration.public_access,
|
46
|
+
"(\/site(\/.*)?)|(\/registration(\/.*)?)|(\/view_posts(\/.*)?)"
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_protected_access
|
50
|
+
permission(:my_account)
|
51
|
+
protected_access :my_account
|
52
|
+
|
53
|
+
assert_equal Monty::Configuration.protected_access, "(\/my_account(\/.*)?)"
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_protected_access_with_multiple_permissions
|
57
|
+
permission(:my_account)
|
58
|
+
permission(:edit_posts)
|
59
|
+
protected_access :my_account, :edit_posts
|
60
|
+
|
61
|
+
assert_equal Monty::Configuration.protected_access,
|
62
|
+
"(\/my_account(\/.*)?)|(\/edit_posts(\/.*)?)"
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMontyConfiguration < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_defaults_are_correct
|
6
|
+
assert_equal Monty::Configuration.access_denied_path, '/'
|
7
|
+
assert_equal Monty::Configuration.public_access, ""
|
8
|
+
assert_equal Monty::Configuration.protected_access, ""
|
9
|
+
assert_equal Monty::Configuration.permissions, []
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMontyController < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@controller = Monty::Controller.new(:users)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_initializer_sets_correct_state
|
10
|
+
assert_equal @controller.name, 'users'
|
11
|
+
assert_equal @controller.regex_pattern, "\/users(\/.*)?"
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_except_sets_correct_regex_pattern
|
15
|
+
@controller.except(:destroy)
|
16
|
+
assert_equal @controller.regex_pattern, "\/users(?!\/(destroy))(\/.*)?"
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_except_with_multiple_params_sets_correct_regex_pattern
|
20
|
+
@controller.except(:index, :destroy)
|
21
|
+
assert_equal @controller.regex_pattern, "\/users(?!\/(index|destroy))(\/.*)?"
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_except_with_no_params_preserves_regex_pattern
|
25
|
+
controller = Monty::Controller.new(:users)
|
26
|
+
controller.except()
|
27
|
+
assert_equal controller.regex_pattern, "\/users(\/.*)?"
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_only_sets_correct_regex_pattern
|
31
|
+
@controller.only(:index)
|
32
|
+
assert_equal @controller.regex_pattern, "\/users\/(index)"
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_only_with_multiple_params_sets_correct_regex_pattern
|
36
|
+
@controller.only(:show, :edit)
|
37
|
+
assert_equal @controller.regex_pattern, "\/users\/(show|edit)"
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_only_with_no_params_preserves_regex_pattern
|
41
|
+
controller = Monty::Controller.new(:users)
|
42
|
+
controller.only()
|
43
|
+
assert_equal controller.regex_pattern, "\/users(\/.*)?"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMontyPermission < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@permission = Monty::Permission.new(:my_account)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_initializer_sets_correct_state
|
10
|
+
assert_equal @permission.name, 'my_account'
|
11
|
+
assert_equal @permission.controllers, []
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_controller
|
15
|
+
@permission.controller(:users)
|
16
|
+
|
17
|
+
controller = @permission.controllers.first
|
18
|
+
assert_equal controller.name, 'users'
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_controller_with_block
|
22
|
+
@permission.controller(:users) do
|
23
|
+
except :destroy
|
24
|
+
end
|
25
|
+
|
26
|
+
controller = @permission.controllers.first
|
27
|
+
assert_equal controller.exceptions, ['destroy']
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def test_regex_pattern
|
32
|
+
@permission.controller(:users)
|
33
|
+
|
34
|
+
assert_equal @permission.regex_pattern, "(\/users(\/.*)?)"
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_regex_pattern_with_multiple_controllers
|
38
|
+
@permission.controller(:users)
|
39
|
+
@permission.controller(:posts)
|
40
|
+
|
41
|
+
assert_equal @permission.regex_pattern, "(\/users(\/.*)?)|(\/posts(\/.*)?)"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class Authorization
|
4
|
+
extend Monty::Access
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestMonty < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
Monty::Configuration.reset
|
11
|
+
end
|
12
|
+
|
13
|
+
def app
|
14
|
+
Monty::Watch.new(mock_framework)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_it_works
|
18
|
+
get '/'
|
19
|
+
assert last_response.ok?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_it_redirects
|
23
|
+
get '/posts'
|
24
|
+
assert last_response.redirect?
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_it_allows_uri_without_ending_slash
|
28
|
+
Authorization.permission :posts
|
29
|
+
Authorization.public_access :posts
|
30
|
+
|
31
|
+
get '/posts', {}
|
32
|
+
assert last_response.ok?
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_it_allows_uri_with_ending_slash
|
36
|
+
Authorization.permission :posts
|
37
|
+
Authorization.public_access :posts
|
38
|
+
|
39
|
+
get '/posts/', {}
|
40
|
+
assert last_response.ok?
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_it_allows_uri_with_action
|
44
|
+
Authorization.permission :posts
|
45
|
+
Authorization.public_access :posts
|
46
|
+
|
47
|
+
get '/posts/new', {}
|
48
|
+
assert last_response.ok?
|
49
|
+
|
50
|
+
get '/posts', {}
|
51
|
+
assert last_response.ok?
|
52
|
+
|
53
|
+
get '/posts/', {}
|
54
|
+
assert last_response.ok?
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_it_allows_uri_access_to_only_show
|
58
|
+
Authorization.permission :posts do
|
59
|
+
controller :posts do
|
60
|
+
only :show
|
61
|
+
end
|
62
|
+
end
|
63
|
+
Authorization.public_access :posts
|
64
|
+
|
65
|
+
get '/posts/show', {}
|
66
|
+
assert last_response.ok?
|
67
|
+
|
68
|
+
get '/postsshow', {}
|
69
|
+
assert last_response.redirect?
|
70
|
+
|
71
|
+
get '/posts', {}
|
72
|
+
assert last_response.redirect?
|
73
|
+
|
74
|
+
get '/posts/', {}
|
75
|
+
assert last_response.redirect?
|
76
|
+
|
77
|
+
get '/posts/edit', {}
|
78
|
+
assert last_response.redirect?
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_it_allows_uri_access_to_all_except_show
|
82
|
+
Authorization.permission :posts do
|
83
|
+
controller :posts do
|
84
|
+
except :show
|
85
|
+
end
|
86
|
+
end
|
87
|
+
Authorization.public_access :posts
|
88
|
+
|
89
|
+
get '/posts/show', {}
|
90
|
+
assert last_response.redirect?
|
91
|
+
|
92
|
+
get '/postsshow', {}
|
93
|
+
assert last_response.redirect?
|
94
|
+
|
95
|
+
get '/posts', {}
|
96
|
+
assert last_response.ok?
|
97
|
+
|
98
|
+
get '/posts/', {}
|
99
|
+
assert last_response.ok?
|
100
|
+
|
101
|
+
get '/posts/edit', {}
|
102
|
+
assert last_response.ok?
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
data/test/rcov.opts
ADDED
data/test/test_monty.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMonty < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_version
|
6
|
+
assert Monty.version, '0.1.0'
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_authenticated_access
|
10
|
+
Monty::Configuration.public_access = "(/static/.*)"
|
11
|
+
Monty::Configuration.protected_access = "(/users/.*)"
|
12
|
+
assert_equal Monty.authenticated_access, "(/static/.*)|(/users/.*)"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- stonean
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-05 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mocha
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 9
|
30
|
+
- 8
|
31
|
+
version: 0.9.8
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Rack based authoriztion system.
|
35
|
+
email: andy@stonean.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.md
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/monty.rb
|
49
|
+
- lib/monty/access.rb
|
50
|
+
- lib/monty/configuration.rb
|
51
|
+
- lib/monty/controller.rb
|
52
|
+
- lib/monty/permission.rb
|
53
|
+
- lib/monty/watch.rb
|
54
|
+
- monty.gemspec
|
55
|
+
- test/helper.rb
|
56
|
+
- test/monty/test_access.rb
|
57
|
+
- test/monty/test_configuration.rb
|
58
|
+
- test/monty/test_controller.rb
|
59
|
+
- test/monty/test_permission.rb
|
60
|
+
- test/monty/test_watch.rb
|
61
|
+
- test/rcov.opts
|
62
|
+
- test/test_monty.rb
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://github.com/stonean/monty
|
65
|
+
licenses: []
|
66
|
+
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --charset=UTF-8
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.3.6
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Rack based authorization system
|
93
|
+
test_files:
|
94
|
+
- test/helper.rb
|
95
|
+
- test/monty/test_configuration.rb
|
96
|
+
- test/monty/test_controller.rb
|
97
|
+
- test/monty/test_access.rb
|
98
|
+
- test/monty/test_watch.rb
|
99
|
+
- test/monty/test_permission.rb
|
100
|
+
- test/test_monty.rb
|