contextually 0.0.1 → 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/README.rdoc +67 -5
- data/lib/contextually.rb +2 -2
- data/lib/contextually/definition.rb +6 -2
- data/lib/contextually/example_extension.rb +11 -5
- data/spec/contextually_spec.rb +9 -0
- data/spec/spec_spec.rb +7 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -6,17 +6,79 @@
|
|
6
6
|
|
7
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
8
|
|
9
|
-
==
|
9
|
+
== Synopsis:
|
10
10
|
|
11
|
-
|
11
|
+
Somewhere sensible (like your spec helper file), define your contexts like this:
|
12
12
|
|
13
|
-
|
13
|
+
Contextually.define do
|
14
|
+
roles :user, :visitor, :monkey
|
14
15
|
|
15
|
-
|
16
|
+
group :user, :monkey, :as => :member
|
17
|
+
group :visitor, :monkey, :as => :idiot
|
16
18
|
|
17
|
-
|
19
|
+
before :user do
|
20
|
+
controller.stub!(:current_user).and_return(:user)
|
21
|
+
end
|
22
|
+
before :visitor do
|
23
|
+
controller.stub!(:current_user).and_return(nil)
|
24
|
+
end
|
25
|
+
before :monkey do
|
26
|
+
controller.stub!(:current_user).and_return(:monkey)
|
27
|
+
end
|
28
|
+
|
29
|
+
deny_access_to :visitor do
|
30
|
+
it("should deny access") { should redirect_to(new_session_url) }
|
31
|
+
end
|
32
|
+
deny_access do
|
33
|
+
it("should deny access") { should redirect_to(root_url) }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
Then use them in your controller specs like this:
|
38
|
+
|
39
|
+
describe TestsController do
|
40
|
+
as :user, :get => :index do
|
41
|
+
it { should respond_with(:success) }
|
42
|
+
end
|
43
|
+
|
44
|
+
as :visitor, :get => :index do
|
45
|
+
it("should deny access") { should redirect_to(new_session_url) }
|
46
|
+
end
|
47
|
+
|
48
|
+
as :visitor, :monkey, :user, :get => :show do
|
49
|
+
it { should respond_with(:success) }
|
50
|
+
end
|
51
|
+
|
52
|
+
as :visitor, :monkey, :user, "GET /test" do
|
53
|
+
describe :get => :show do
|
54
|
+
it { should respond_with(:success) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
as :visitor, :monkey, :user do
|
59
|
+
describe :get => :show do
|
60
|
+
it { should respond_with(:success) }
|
61
|
+
end
|
62
|
+
end
|
18
63
|
|
64
|
+
only_as :user, :get => :index do
|
65
|
+
it { should respond_with(:success) }
|
66
|
+
end
|
19
67
|
|
68
|
+
deny_access_to :monkey, :visitor, :get => :index
|
69
|
+
|
70
|
+
deny_access_to :idiot, :get => :index
|
71
|
+
|
72
|
+
only_as :member, :get => :foo do
|
73
|
+
it { should respond_with(:success) }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
== Install:
|
78
|
+
|
79
|
+
Contextually is hosted on Gemcutter.org only. Add this to your Rails test environment:
|
80
|
+
|
81
|
+
config.gem "contextually"
|
20
82
|
|
21
83
|
== License:
|
22
84
|
|
data/lib/contextually.rb
CHANGED
@@ -18,6 +18,10 @@ module Contextually
|
|
18
18
|
def before(name, &block)
|
19
19
|
Contextually.roles[name].before = block
|
20
20
|
end
|
21
|
+
|
22
|
+
def before_all(&block)
|
23
|
+
Contextually.before_all = block
|
24
|
+
end
|
21
25
|
|
22
26
|
def deny_access_to(name, &block)
|
23
27
|
Contextually.roles[name].deny_access = block
|
@@ -29,7 +33,7 @@ module Contextually
|
|
29
33
|
end
|
30
34
|
|
31
35
|
class << self
|
32
|
-
attr_accessor :roles, :deny_access_to_all
|
36
|
+
attr_accessor :roles, :deny_access_to_all, :before_all
|
33
37
|
|
34
38
|
def groups
|
35
39
|
@groups ||= {}
|
@@ -54,4 +58,4 @@ module Contextually
|
|
54
58
|
Contextually::Definition.define(&block)
|
55
59
|
end
|
56
60
|
end
|
57
|
-
end
|
61
|
+
end
|
@@ -2,16 +2,14 @@ module Contextually
|
|
2
2
|
module ExampleExtension
|
3
3
|
def only_as(*args, &block)
|
4
4
|
as(*args, &block)
|
5
|
-
|
6
|
-
deny_params = Contextually.roles.keys - roles
|
7
|
-
deny_params.push(params)
|
8
|
-
deny_access_to(*deny_params)
|
5
|
+
only_allow_access_to(*args)
|
9
6
|
end
|
10
7
|
|
11
8
|
def as(*args, &block)
|
12
9
|
roles, params = extract_params(args)
|
13
10
|
roles.each do |role|
|
14
11
|
context "as #{role}" do
|
12
|
+
before(&Contextually.before_all) if Contextually.before_all
|
15
13
|
before(&Contextually.before(role))
|
16
14
|
describe(params.dup, &block)
|
17
15
|
end
|
@@ -24,6 +22,7 @@ module Contextually
|
|
24
22
|
block = Contextually.deny_access(role)
|
25
23
|
if block
|
26
24
|
context "as #{role}" do
|
25
|
+
before(&Contextually.before_all) if Contextually.before_all
|
27
26
|
before(&Contextually.before(role))
|
28
27
|
describe(params.dup, &block)
|
29
28
|
end
|
@@ -32,6 +31,13 @@ module Contextually
|
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
34
|
+
|
35
|
+
def only_allow_access_to(*args)
|
36
|
+
roles, params = extract_params(args)
|
37
|
+
deny_params = Contextually.roles.keys - roles
|
38
|
+
deny_params.push(params)
|
39
|
+
deny_access_to(*deny_params)
|
40
|
+
end
|
35
41
|
|
36
42
|
private
|
37
43
|
|
@@ -48,4 +54,4 @@ module Contextually
|
|
48
54
|
end
|
49
55
|
end
|
50
56
|
|
51
|
-
Spec::Rails::Example::ControllerExampleGroup.send(:extend, Contextually::ExampleExtension) if defined?(Spec)
|
57
|
+
Spec::Rails::Example::ControllerExampleGroup.send(:extend, Contextually::ExampleExtension) if defined?(Spec)
|
data/spec/contextually_spec.rb
CHANGED
@@ -6,6 +6,10 @@ Contextually.define do
|
|
6
6
|
group :user, :monkey, :as => :member
|
7
7
|
group :visitor, :monkey, :as => :idiot
|
8
8
|
|
9
|
+
before_all do
|
10
|
+
controller.stub!(:global_before).and_return(true)
|
11
|
+
end
|
12
|
+
|
9
13
|
before :user do
|
10
14
|
controller.stub!(:current_user).and_return(:user)
|
11
15
|
end
|
@@ -34,6 +38,7 @@ end
|
|
34
38
|
|
35
39
|
class TestsController < ApplicationController
|
36
40
|
def index
|
41
|
+
raise "global before not run" unless global_before
|
37
42
|
if not current_user
|
38
43
|
redirect_to new_session_url
|
39
44
|
elsif not current_user == :user
|
@@ -98,5 +103,9 @@ describe TestsController, :type => :controller do
|
|
98
103
|
it { should respond_with(:success) }
|
99
104
|
end
|
100
105
|
end
|
106
|
+
|
107
|
+
context "with only_allow_access_to" do
|
108
|
+
only_allow_access_to :user, :get => :index
|
109
|
+
end
|
101
110
|
end
|
102
111
|
|
data/spec/spec_spec.rb
CHANGED
@@ -73,4 +73,10 @@ TestsController with groups as monkey responding to #GET foo
|
|
73
73
|
- should respond with success
|
74
74
|
|
75
75
|
TestsController with groups as visitor responding to #GET foo
|
76
|
-
- should deny access
|
76
|
+
- should deny access
|
77
|
+
|
78
|
+
TestsController with only_allow_access_to as visitor responding to #GET index
|
79
|
+
- should deny access
|
80
|
+
|
81
|
+
TestsController with only_allow_access_to as monkey responding to #GET index
|
82
|
+
- should deny access
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contextually
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.1"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Nicklas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-18 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|