contextually 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-10-16
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,14 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/contextually.rb
6
+ lib/contextually/definition.rb
7
+ lib/contextually/example_extension.rb
8
+ script/console
9
+ script/destroy
10
+ script/generate
11
+ spec/application_controller.rb
12
+ spec/contextually_spec.rb
13
+ spec/spec_helper.rb
14
+ spec/spec_spec.rb
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = contextually
2
+
3
+ * http://github.com/jnicklas/contextually
4
+
5
+ == Description:
6
+
7
+ Contextually is a gem for running Rails controller tests under different user contexts in a convenient way. So you can test your authorization without much effort.
8
+
9
+ == Features/Problems:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == Synopsis:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == Install:
18
+
19
+
20
+
21
+ == License:
22
+
23
+ (The MIT License)
24
+
25
+ Copyright (c) 2009 Jonas Nicklas
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining
28
+ a copy of this software and associated documentation files (the
29
+ 'Software'), to deal in the Software without restriction, including
30
+ without limitation the rights to use, copy, modify, merge, publish,
31
+ distribute, sublicense, and/or sell copies of the Software, and to
32
+ permit persons to whom the Software is furnished to do so, subject to
33
+ the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be
36
+ included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
39
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
41
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
42
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
43
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
44
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+
6
+ $:.unshift(File.expand_path('lib', File.dirname(__FILE__)))
7
+ require 'contextually'
8
+
9
+ Hoe.plugin :newgem
10
+ # Hoe.plugin :website
11
+ # Hoe.plugin :cucumberfeatures
12
+
13
+ # Generate all the Rake tasks
14
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
15
+ $hoe = Hoe.spec 'contextually' do
16
+ self.developer 'Jonas Nicklas', 'jonas.nicklas@gmail.com'
17
+ self.rubyforge_name = self.name # TODO this is default value
18
+ self.extra_deps = [['remarkable_rails','>= 3.1.10']]
19
+ self.version = Contextually::VERSION
20
+ end
21
+
22
+ require 'newgem/tasks'
23
+ Dir['tasks/**/*.rake'].each { |t| load t }
24
+
25
+ # TODO - want other tests/tasks run by default? Add them to the list
26
+ # remove_task :default
27
+ # task :default => [:spec, :features]
@@ -0,0 +1,8 @@
1
+ module Contextually
2
+ VERSION = '0.0.1'
3
+
4
+ class UndefinedContext < StandardError; end
5
+ end
6
+
7
+ require 'contextually/example_extension'
8
+ require 'contextually/definition'
@@ -0,0 +1,57 @@
1
+ module Contextually
2
+ class Role < Struct.new(:name, :before, :deny_access); end
3
+
4
+ class Definition
5
+ def self.define(&block)
6
+ self.new.instance_eval(&block)
7
+ end
8
+
9
+ def roles(*roles)
10
+ Contextually.roles = roles.inject({}) { |hash, role| hash[role] = Contextually::Role.new(role); hash }
11
+ end
12
+
13
+ def group(*roles, &block)
14
+ as = roles.pop[:as]
15
+ Contextually.groups[as] = roles
16
+ end
17
+
18
+ def before(name, &block)
19
+ Contextually.roles[name].before = block
20
+ end
21
+
22
+ def deny_access_to(name, &block)
23
+ Contextually.roles[name].deny_access = block
24
+ end
25
+
26
+ def deny_access(&block)
27
+ Contextually.deny_access_to_all = block
28
+ end
29
+ end
30
+
31
+ class << self
32
+ attr_accessor :roles, :deny_access_to_all
33
+
34
+ def groups
35
+ @groups ||= {}
36
+ end
37
+
38
+ def role(role)
39
+ unless roles and roles.has_key?(role)
40
+ raise Contextually::UndefinedContext, "no role called #{role.inspect} exists"
41
+ end
42
+ roles[role]
43
+ end
44
+
45
+ def before(role)
46
+ role(role).before
47
+ end
48
+
49
+ def deny_access(role)
50
+ role(role).deny_access || deny_access_to_all
51
+ end
52
+
53
+ def define(&block)
54
+ Contextually::Definition.define(&block)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,51 @@
1
+ module Contextually
2
+ module ExampleExtension
3
+ def only_as(*args, &block)
4
+ as(*args, &block)
5
+ roles, params = extract_params(args)
6
+ deny_params = Contextually.roles.keys - roles
7
+ deny_params.push(params)
8
+ deny_access_to(*deny_params)
9
+ end
10
+
11
+ def as(*args, &block)
12
+ roles, params = extract_params(args)
13
+ roles.each do |role|
14
+ context "as #{role}" do
15
+ before(&Contextually.before(role))
16
+ describe(params.dup, &block)
17
+ end
18
+ end
19
+ end
20
+
21
+ def deny_access_to(*args)
22
+ roles, params = extract_params(args)
23
+ roles.each do |role|
24
+ block = Contextually.deny_access(role)
25
+ if block
26
+ context "as #{role}" do
27
+ before(&Contextually.before(role))
28
+ describe(params.dup, &block)
29
+ end
30
+ else
31
+ raise Contextually::UndefinedContext, "don't know how to deny access to a #{role}"
32
+ end
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def extract_params(args)
39
+ if args.blank? or args.last.is_a?(Symbol)
40
+ params = ""
41
+ else
42
+ params = args.pop
43
+ end
44
+ args = args.inject([]) { |array, role| array.push(*Contextually.groups[role] || role); array }
45
+ return args, params
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ Spec::Rails::Example::ControllerExampleGroup.send(:extend, Contextually::ExampleExtension) if defined?(Spec)
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/contextually.rb'}"
9
+ puts "Loading contextually gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ # this file is a workaround for a bug in rspec-rails
2
+ class ApplicationController < ActionController::Base
3
+ end
@@ -0,0 +1,102 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ Contextually.define do
4
+ roles :user, :visitor, :monkey
5
+
6
+ group :user, :monkey, :as => :member
7
+ group :visitor, :monkey, :as => :idiot
8
+
9
+ before :user do
10
+ controller.stub!(:current_user).and_return(:user)
11
+ end
12
+ before :visitor do
13
+ controller.stub!(:current_user).and_return(nil)
14
+ end
15
+ before :monkey do
16
+ controller.stub!(:current_user).and_return(:monkey)
17
+ end
18
+
19
+ deny_access_to :visitor do
20
+ it("should deny access") { should redirect_to(new_session_url) }
21
+ end
22
+ deny_access do
23
+ it("should deny access") { should redirect_to(root_url) }
24
+ end
25
+ end
26
+
27
+ ActionController::Routing::Routes.draw do |map|
28
+ map.root :controller => 'monkey'
29
+ map.new_session 'session', :controller => 'session'
30
+ map.index 'tests', :controller => 'tests', :action => 'index'
31
+ map.index 'test', :controller => 'tests', :action => 'show'
32
+ map.index 'foo', :controller => 'tests', :action => 'foo'
33
+ end
34
+
35
+ class TestsController < ApplicationController
36
+ def index
37
+ if not current_user
38
+ redirect_to new_session_url
39
+ elsif not current_user == :user
40
+ redirect_to root_url
41
+ end
42
+ end
43
+
44
+ def foo
45
+ if not current_user
46
+ redirect_to new_session_url
47
+ end
48
+ end
49
+
50
+ def show
51
+
52
+ end
53
+ end
54
+
55
+ describe TestsController, :type => :controller do
56
+ context "with simple contexts" do
57
+ as :user, :get => :index do
58
+ it { should respond_with(:success) }
59
+ end
60
+
61
+ as :visitor, :get => :index do
62
+ it("should deny access") { should redirect_to(new_session_url) }
63
+ end
64
+ end
65
+
66
+ context "with multiple roles" do
67
+ as :visitor, :monkey, :user, :get => :show do
68
+ it { should respond_with(:success) }
69
+ end
70
+
71
+ as :visitor, :monkey, :user, "GET /test" do
72
+ describe :get => :show do
73
+ it { should respond_with(:success) }
74
+ end
75
+ end
76
+
77
+ as :visitor, :monkey, :user do
78
+ describe :get => :show do
79
+ it { should respond_with(:success) }
80
+ end
81
+ end
82
+ end
83
+
84
+ context "with only_as" do
85
+ only_as :user, :get => :index do
86
+ it { should respond_with(:success) }
87
+ end
88
+ end
89
+
90
+ context "with deny access" do
91
+ deny_access_to :monkey, :visitor, :get => :index
92
+ end
93
+
94
+ context "with groups" do
95
+ deny_access_to :idiot, :get => :index
96
+
97
+ only_as :member, :get => :foo do
98
+ it { should respond_with(:success) }
99
+ end
100
+ end
101
+ end
102
+
@@ -0,0 +1,17 @@
1
+ $:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
2
+ $:.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+
6
+ require 'action_pack'
7
+ require 'action_controller'
8
+ require 'active_support'
9
+ require 'initializer'
10
+
11
+ require 'spec'
12
+ require 'spec/autorun'
13
+ require 'spec/rails'
14
+
15
+ require 'remarkable_rails'
16
+
17
+ require 'contextually'
data/spec/spec_spec.rb ADDED
@@ -0,0 +1,76 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ require 'base64'
4
+
5
+ describe 'running the specs' do
6
+ it "should yield the currect output" do
7
+ out = `spec #{File.expand_path('contextually_spec.rb', File.dirname(__FILE__))} --format specdoc`
8
+ expected = File.read(__FILE__).split(Base64.decode64("X19FTkRfXw==\n")).second.strip
9
+ out.should =~ Regexp.new(Regexp.escape(expected))
10
+ end
11
+ end
12
+
13
+ __END__
14
+
15
+ TestsController with simple contexts as user responding to #GET index
16
+ - should respond with success
17
+
18
+ TestsController with simple contexts as visitor responding to #GET index
19
+ - should deny access
20
+
21
+ TestsController with multiple roles as visitor responding to #GET show
22
+ - should respond with success
23
+
24
+ TestsController with multiple roles as monkey responding to #GET show
25
+ - should respond with success
26
+
27
+ TestsController with multiple roles as user responding to #GET show
28
+ - should respond with success
29
+
30
+ TestsController with multiple roles as visitor GET /test responding to #GET show
31
+ - should respond with success
32
+
33
+ TestsController with multiple roles as monkey GET /test responding to #GET show
34
+ - should respond with success
35
+
36
+ TestsController with multiple roles as user GET /test responding to #GET show
37
+ - should respond with success
38
+
39
+ TestsController with multiple roles as visitor responding to #GET show
40
+ - should respond with success
41
+
42
+ TestsController with multiple roles as monkey responding to #GET show
43
+ - should respond with success
44
+
45
+ TestsController with multiple roles as user responding to #GET show
46
+ - should respond with success
47
+
48
+ TestsController with only_as as user responding to #GET index
49
+ - should respond with success
50
+
51
+ TestsController with only_as as visitor responding to #GET index
52
+ - should deny access
53
+
54
+ TestsController with only_as as monkey responding to #GET index
55
+ - should deny access
56
+
57
+ TestsController with deny access as monkey responding to #GET index
58
+ - should deny access
59
+
60
+ TestsController with deny access as visitor responding to #GET index
61
+ - should deny access
62
+
63
+ TestsController with groups as visitor responding to #GET index
64
+ - should deny access
65
+
66
+ TestsController with groups as monkey responding to #GET index
67
+ - should deny access
68
+
69
+ TestsController with groups as user responding to #GET foo
70
+ - should respond with success
71
+
72
+ TestsController with groups as monkey responding to #GET foo
73
+ - should respond with success
74
+
75
+ TestsController with groups as visitor responding to #GET foo
76
+ - should deny access
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contextually
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Nicklas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-16 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: remarkable_rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 3.1.10
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: hoe
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.3
34
+ version:
35
+ description: Contextually is a gem for running Rails controller tests under different user contexts in a convenient way. So you can test your authorization without much effort.
36
+ email:
37
+ - jonas.nicklas@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ files:
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.rdoc
49
+ - Rakefile
50
+ - lib/contextually.rb
51
+ - lib/contextually/definition.rb
52
+ - lib/contextually/example_extension.rb
53
+ - script/console
54
+ - script/destroy
55
+ - script/generate
56
+ - spec/application_controller.rb
57
+ - spec/contextually_spec.rb
58
+ - spec/spec_helper.rb
59
+ - spec/spec_spec.rb
60
+ has_rdoc: true
61
+ homepage: http://github.com/jnicklas/contextually
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --main
67
+ - README.rdoc
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ requirements: []
83
+
84
+ rubyforge_project: contextually
85
+ rubygems_version: 1.3.5
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Contextually is a gem for running Rails controller tests under different user contexts in a convenient way
89
+ test_files: []
90
+