-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.rb
98 lines (91 loc) · 2.44 KB
/
authorization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Shoulda macros
module Authorization
module Shoulda
# options:
# :index
# or :edit, :id => 5
# or 'show category', :action => :index, :id => 5
# or :update, :method => :put, :id => 5
def should_not_be_authorized_for(name, options={})
should "not be authorized for #{name}" do
call_route(name, options)
assert_redirected_to unauthorized_page_path
end
end
# options:
# :index,
# [:update, {:method => :put, :id => 1}],
# ['destroy category', {:action => :destroy, :method => :put, :id => 1}]
def should_not_be_authorized_for_actions(*actions)
actions.each {|a|
if a.kind_of?(Array)
name = a[0]
options = a[1]
else
name = a
end
should_not_be_authorized_for(name, options)
}
end
# options:
# :index
# or :edit, :id => 5
# or 'show category', :action => :index, :id => 5
# or :update, :method => :put, :id => 5
def should_be_authorized_for(name, options={})
should "be authorized for #{name}" do
call_route(name, options)
assert_response :success
end
end
# options:
# :index,
# [:update, {:method => :put, :id => 1}],
# ['destroy category', {:action => :destroy, :method => :put, :id => 1}]
def should_be_authorized_for_actions(*actions)
actions.each {|a|
if a.kind_of?(Array)
name = a[0]
options = a[1]
else
name = a
end
should_be_authorized_for(name, options)
}
end
end
end
module Authorization
module Shoulda
module Helpers
def call_route(name, options={})
unless options.blank?
method = options[:method] || :get
action = options[:action] || name
make_request method, action, options
else
make_request method, name
end
end
def make_request(method, action, options={})
case method
when :post
post action, options
when :put
put action, options
when :delete
delete action, options
else
get action, options
end
end
def unauthorized_page_path
'session/new'
end
end
end
end
class Test::Unit::TestCase
include Authorization::Shoulda::Helpers
end
Test::Unit::TestCase.extend(Authorization::Shoulda)