trust 0.5.0
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/MIT-LICENSE +23 -0
- data/README.md +244 -0
- data/Rakefile +37 -0
- data/lib/tasks/trust_tasks.rake +42 -0
- data/lib/trust/active_record.rb +65 -0
- data/lib/trust/authorization.rb +85 -0
- data/lib/trust/controller/properties.rb +134 -0
- data/lib/trust/controller/resource.rb +306 -0
- data/lib/trust/controller.rb +197 -0
- data/lib/trust/exceptions.rb +45 -0
- data/lib/trust/inheritable_attribute.rb +91 -0
- data/lib/trust/permissions.rb +268 -0
- data/lib/trust/test_helper.rb +56 -0
- data/lib/trust/version.rb +27 -0
- data/lib/trust.rb +39 -0
- data/test/dummy/README.rdoc +261 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/assets/javascripts/accounts.js +2 -0
- data/test/dummy/app/assets/javascripts/application.js +15 -0
- data/test/dummy/app/assets/javascripts/clients.js +2 -0
- data/test/dummy/app/assets/javascripts/users.js +2 -0
- data/test/dummy/app/assets/stylesheets/accounts.css +4 -0
- data/test/dummy/app/assets/stylesheets/application.css +13 -0
- data/test/dummy/app/assets/stylesheets/clients.css +4 -0
- data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
- data/test/dummy/app/assets/stylesheets/users.css +4 -0
- data/test/dummy/app/controllers/accounts_controller.rb +100 -0
- data/test/dummy/app/controllers/application_controller.rb +31 -0
- data/test/dummy/app/controllers/clients_controller.rb +107 -0
- data/test/dummy/app/controllers/savings_accounts_controller.rb +27 -0
- data/test/dummy/app/controllers/settlements_controller.rb +26 -0
- data/test/dummy/app/controllers/users_controller.rb +107 -0
- data/test/dummy/app/helpers/accounts_helper.rb +26 -0
- data/test/dummy/app/helpers/application_helper.rb +26 -0
- data/test/dummy/app/helpers/clients_helper.rb +26 -0
- data/test/dummy/app/helpers/users_helper.rb +26 -0
- data/test/dummy/app/models/account/credit.rb +26 -0
- data/test/dummy/app/models/account.rb +35 -0
- data/test/dummy/app/models/client.rb +35 -0
- data/test/dummy/app/models/permissions.rb +68 -0
- data/test/dummy/app/models/savings_account.rb +26 -0
- data/test/dummy/app/models/user.rb +40 -0
- data/test/dummy/app/views/accounts/_form.html.erb +46 -0
- data/test/dummy/app/views/accounts/edit.html.erb +31 -0
- data/test/dummy/app/views/accounts/index.html.erb +48 -0
- data/test/dummy/app/views/accounts/new.html.erb +30 -0
- data/test/dummy/app/views/accounts/show.html.erb +35 -0
- data/test/dummy/app/views/clients/_form.html.erb +46 -0
- data/test/dummy/app/views/clients/edit.html.erb +31 -0
- data/test/dummy/app/views/clients/index.html.erb +48 -0
- data/test/dummy/app/views/clients/new.html.erb +30 -0
- data/test/dummy/app/views/clients/show.html.erb +35 -0
- data/test/dummy/app/views/layouts/application.html.erb +39 -0
- data/test/dummy/app/views/users/_form.html.erb +46 -0
- data/test/dummy/app/views/users/edit.html.erb +31 -0
- data/test/dummy/app/views/users/index.html.erb +48 -0
- data/test/dummy/app/views/users/new.html.erb +30 -0
- data/test/dummy/app/views/users/show.html.erb +35 -0
- data/test/dummy/config/application.rb +56 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +67 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +15 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +38 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/db/migrate/20120522115011_create_accounts.rb +36 -0
- data/test/dummy/db/migrate/20120522130322_create_users.rb +33 -0
- data/test/dummy/db/migrate/20120523144144_create_clients.rb +34 -0
- data/test/dummy/db/schema.rb +38 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +25 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/dummy/test/fixtures/accounts.yml +7 -0
- data/test/dummy/test/fixtures/clients.yml +7 -0
- data/test/dummy/test/fixtures/users.yml +7 -0
- data/test/dummy/test/functional/accounts_controller_test.rb +123 -0
- data/test/dummy/test/functional/clients_controller_test.rb +74 -0
- data/test/dummy/test/functional/users_controller_test.rb +74 -0
- data/test/dummy/test/unit/account_test.rb +31 -0
- data/test/dummy/test/unit/client_test.rb +31 -0
- data/test/dummy/test/unit/helpers/accounts_helper_test.rb +28 -0
- data/test/dummy/test/unit/helpers/clients_helper_test.rb +28 -0
- data/test/dummy/test/unit/helpers/users_helper_test.rb +28 -0
- data/test/dummy/test/unit/permissions_test.rb +171 -0
- data/test/dummy/test/unit/user_test.rb +31 -0
- data/test/test_helper.rb +45 -0
- data/test/trust_test.rb +31 -0
- data/test/unit/trust/active_record_test.rb +56 -0
- data/test/unit/trust/authorization_test.rb +108 -0
- data/test/unit/trust/controller/properties_test.rb +132 -0
- data/test/unit/trust/controller/resource_test.rb +251 -0
- data/test/unit/trust/controller_test.rb +160 -0
- data/test/unit/trust/inheritable_attribute_test.rb +65 -0
- data/test/unit/trust/permissions_test.rb +258 -0
- metadata +280 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
|
4
|
+
# Copyright (c) 2012 Patrick Hanevold
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
# a copy of this software and associated documentation files (the
|
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
# the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be
|
|
15
|
+
# included in all copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
|
|
25
|
+
require 'test_helper'
|
|
26
|
+
|
|
27
|
+
class Trust::Controller::ResourceTest < ActiveSupport::TestCase
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
setup do
|
|
31
|
+
module ::NameSpacedResource
|
|
32
|
+
|
|
33
|
+
class MyEntity
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
class Person
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class ::Parent
|
|
41
|
+
end
|
|
42
|
+
class ::Child < Parent; end
|
|
43
|
+
class ::Baluba < Parent; end
|
|
44
|
+
class ::GrandChild < Child; end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
context 'Info classes' do
|
|
49
|
+
context 'Instance' do
|
|
50
|
+
setup do
|
|
51
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('name_spaced_resource/my_entities', {:name_spaced_resource_my_entity => 'cool'})
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
should 'resolve name' do
|
|
55
|
+
assert_equal :name_spaced_resource_my_entity, @res.name
|
|
56
|
+
end
|
|
57
|
+
should 'resolve plural name' do
|
|
58
|
+
assert_equal :name_spaced_resource_my_entities, @res.plural_name
|
|
59
|
+
end
|
|
60
|
+
should 'resolve path' do
|
|
61
|
+
assert_equal 'name_spaced_resource/my_entities', @res.path
|
|
62
|
+
end
|
|
63
|
+
should 'resolve class' do
|
|
64
|
+
assert_equal NameSpacedResource::MyEntity, @res.klass
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
should 'resolve parameter' do
|
|
68
|
+
assert_equal 'cool', @res.params
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
context 'Irregular Instance' do
|
|
73
|
+
setup do
|
|
74
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('name_spaced_resource/people', {:name_spaced_resource_person => 'cool' })
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
should 'resolve name' do
|
|
78
|
+
assert_equal :name_spaced_resource_person, @res.name
|
|
79
|
+
end
|
|
80
|
+
should 'resolve plural name' do
|
|
81
|
+
assert_equal :name_spaced_resource_people, @res.plural_name
|
|
82
|
+
end
|
|
83
|
+
should 'resolve path' do
|
|
84
|
+
assert_equal 'name_spaced_resource/people', @res.path
|
|
85
|
+
end
|
|
86
|
+
should 'resolve class' do
|
|
87
|
+
assert_equal NameSpacedResource::Person, @res.klass
|
|
88
|
+
end
|
|
89
|
+
should 'resolve parameter' do
|
|
90
|
+
assert_equal 'cool', @res.params
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
context 'Inheritable instance' do
|
|
95
|
+
should 'detect params for children' do
|
|
96
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('parents', {:child => 'cool' })
|
|
97
|
+
assert_equal 'cool', @res.params
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
should 'detect params for grandchilds' do
|
|
101
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('parents', {:grand_child => 'cool' })
|
|
102
|
+
assert_equal 'cool', @res.params
|
|
103
|
+
end
|
|
104
|
+
should 'detect params for base' do
|
|
105
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('parents', {:parent => 'cool' })
|
|
106
|
+
assert_equal 'cool', @res.params
|
|
107
|
+
end
|
|
108
|
+
should 'resolve class' do
|
|
109
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('parents', {:grand_child => 'cool' })
|
|
110
|
+
assert_equal Parent, @res.klass
|
|
111
|
+
assert_equal GrandChild, @res.real_class
|
|
112
|
+
@res = Trust::Controller::Resource::ResourceInfo.new('parents', {:parent => 'cool' })
|
|
113
|
+
assert_equal Parent, @res.klass
|
|
114
|
+
assert_equal Parent, @res.real_class
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
context 'Parent resource' do
|
|
119
|
+
setup do
|
|
120
|
+
@request = Object.new
|
|
121
|
+
@resources = [NameSpacedResource::Person, :child]
|
|
122
|
+
end
|
|
123
|
+
context 'when found' do
|
|
124
|
+
should 'return object for namespaced resource' do
|
|
125
|
+
@request.stubs(:symbolized_path_parameters).returns({:name_spaced_resource_person_id => 2 })
|
|
126
|
+
NameSpacedResource::Person.expects(:find).with(2).returns(@object = NameSpacedResource::Person.new)
|
|
127
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
128
|
+
assert_equal @object, @res.object
|
|
129
|
+
end
|
|
130
|
+
should 'return object for regular resource' do
|
|
131
|
+
@request.stubs(:symbolized_path_parameters).returns({:child_id => 2 })
|
|
132
|
+
Child.expects(:find).with(2).returns(@object = Child.new)
|
|
133
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
134
|
+
assert_equal @object, @res.object
|
|
135
|
+
end
|
|
136
|
+
context 'the attributes' do
|
|
137
|
+
setup do
|
|
138
|
+
@request.stubs(:symbolized_path_parameters).returns({:child_id => 2 })
|
|
139
|
+
Child.expects(:find).with(2).returns(@object = Child.new)
|
|
140
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {:child => 'tie'}, @request)
|
|
141
|
+
end
|
|
142
|
+
should 'return class for object' do
|
|
143
|
+
assert_equal @object, @res.object
|
|
144
|
+
assert @res.object?
|
|
145
|
+
end
|
|
146
|
+
should 'respond to object?' do
|
|
147
|
+
assert @res.object?
|
|
148
|
+
end
|
|
149
|
+
should 'return name for class' do
|
|
150
|
+
assert_equal :child, @res.name
|
|
151
|
+
end
|
|
152
|
+
should 'return parameters' do
|
|
153
|
+
assert_equal 'tie', @res.params
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
should 'return nil for object if not found' do
|
|
158
|
+
@request.stubs(:symbolized_path_parameters).returns({:child_id => 2 })
|
|
159
|
+
Child.expects(:find).with(2).returns(nil)
|
|
160
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
161
|
+
assert_nil @res.object
|
|
162
|
+
assert !@res.object?
|
|
163
|
+
end
|
|
164
|
+
should 'return nil for object if not specified' do
|
|
165
|
+
@request.stubs(:symbolized_path_parameters).returns({})
|
|
166
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
167
|
+
assert_nil @res.object
|
|
168
|
+
assert !@res.object?
|
|
169
|
+
end
|
|
170
|
+
should 'return nil for klass when not found' do
|
|
171
|
+
@request.stubs(:symbolized_path_parameters).returns({})
|
|
172
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
173
|
+
assert_nil @res.klass
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
context 'Parent resource with inheritance' do
|
|
178
|
+
setup do
|
|
179
|
+
@request = Object.new
|
|
180
|
+
@resources = [:parent]
|
|
181
|
+
@request.stubs(:symbolized_path_parameters).returns({:child_id => 2 })
|
|
182
|
+
Parent.expects(:find).with(2).returns(@object = Child.new)
|
|
183
|
+
@res = Trust::Controller::Resource::ParentInfo.new(@resources, {}, @request)
|
|
184
|
+
end
|
|
185
|
+
should 'resolve descendants' do
|
|
186
|
+
assert_equal @object, @res.object
|
|
187
|
+
end
|
|
188
|
+
should 'have correct name' do
|
|
189
|
+
assert_equal :child, @res.name
|
|
190
|
+
end
|
|
191
|
+
should 'have correct class' do
|
|
192
|
+
assert_equal Parent, @res.klass
|
|
193
|
+
end
|
|
194
|
+
should 'have real class' do
|
|
195
|
+
assert_equal Child, @res.real_class
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
context 'Resource' do
|
|
202
|
+
setup do
|
|
203
|
+
@controller = stub('Controller', :controller_path => :controller)
|
|
204
|
+
@properties = Trust::Controller::Properties.new(@controller, nil)
|
|
205
|
+
@properties.model :child
|
|
206
|
+
@properties.belongs_to :parent
|
|
207
|
+
@resource_info = stub('ResourceInfo')
|
|
208
|
+
@parent_info = stub(:object => 6, :name => :parent)
|
|
209
|
+
@resource_info.expects(:relation).with(@parent_info).returns(Child)
|
|
210
|
+
@resource_info.stubs(:name).returns(:child)
|
|
211
|
+
Trust::Controller::Resource.any_instance.expects(:extract_resource_info).with(:child, {}).returns(@resource_info)
|
|
212
|
+
Trust::Controller::Resource.any_instance.expects(:extract_parent_info).with({:parent => nil}, {}, @request).returns(@parent_info)
|
|
213
|
+
end
|
|
214
|
+
should 'instantiate properly' do
|
|
215
|
+
@resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{}, @request)
|
|
216
|
+
end
|
|
217
|
+
should 'discover variable names' do
|
|
218
|
+
@resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{}, @request)
|
|
219
|
+
@resource_info.expects(:plural_name).returns(:children)
|
|
220
|
+
assert_equal :child, @resource.send(:instance_name)
|
|
221
|
+
assert_equal :parent, @resource.send(:parent_name)
|
|
222
|
+
assert_equal :children, @resource.send(:plural_instance_name)
|
|
223
|
+
end
|
|
224
|
+
should 'have access to instances' do
|
|
225
|
+
@resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{}, @request)
|
|
226
|
+
@resource.expects(:plural_instance_name).twice.returns(:children)
|
|
227
|
+
@resource.instances = [1]
|
|
228
|
+
assert_equal [1], @resource.instances
|
|
229
|
+
assert_equal [1], @controller.instance_variable_get(:@children)
|
|
230
|
+
end
|
|
231
|
+
should 'have access to instantiated' do
|
|
232
|
+
@resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{}, @request)
|
|
233
|
+
@resource.expects(:instances).returns(1)
|
|
234
|
+
assert_equal 1, @resource.instantiated
|
|
235
|
+
@resource.expects(:instances).returns(nil)
|
|
236
|
+
@resource.expects(:instance).returns(2)
|
|
237
|
+
assert_equal 2, @resource.instantiated
|
|
238
|
+
end
|
|
239
|
+
should 'load as expected' do
|
|
240
|
+
@resource = Trust::Controller::Resource.new(@controller, @properties, 'new',{}, @request)
|
|
241
|
+
@resource_info.stubs(:params).returns({})
|
|
242
|
+
@controller.expects(:respond_to?).with(:build).returns(false)
|
|
243
|
+
@resource.load
|
|
244
|
+
assert_equal 6, @controller.instance_variable_get(:@parent)
|
|
245
|
+
assert_equal 6, @resource.parent
|
|
246
|
+
assert @controller.instance_variable_get(:@child).is_a?(Child)
|
|
247
|
+
assert @resource.instance.is_a?(Child)
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
end
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
|
4
|
+
# Copyright (c) 2012 Patrick Hanevold
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
# a copy of this software and associated documentation files (the
|
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
# the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be
|
|
15
|
+
# included in all copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
|
|
25
|
+
require 'test_helper'
|
|
26
|
+
|
|
27
|
+
class Trust::ControllerTest < ActiveSupport::TestCase
|
|
28
|
+
setup do
|
|
29
|
+
class Controller < ActionController::Base
|
|
30
|
+
trustee
|
|
31
|
+
end
|
|
32
|
+
class DerivedController < Controller
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
context 'class method' do
|
|
36
|
+
should 'instantiate properties' do
|
|
37
|
+
assert_kind_of Trust::Controller::Properties, Controller.properties
|
|
38
|
+
end
|
|
39
|
+
should 'trustee set filers' do
|
|
40
|
+
options = {:hello => :there}
|
|
41
|
+
Controller.expects(:before_filter).with(:set_user, options)
|
|
42
|
+
Controller.expects(:before_filter).with(:load_resource, options)
|
|
43
|
+
Controller.expects(:before_filter).with(:access_control, options)
|
|
44
|
+
Controller.trustee options
|
|
45
|
+
end
|
|
46
|
+
should 'delegate to resource' do
|
|
47
|
+
Controller.properties.expects(:belongs_to)
|
|
48
|
+
Controller.properties.expects(:actions)
|
|
49
|
+
Controller.properties.expects(:model)
|
|
50
|
+
Controller.belongs_to
|
|
51
|
+
Controller.actions
|
|
52
|
+
Controller.model
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
context 'callbacks' do
|
|
56
|
+
should 'be set_user, load_resource, access_control' do
|
|
57
|
+
%w(set_user load_resource access_control).map(&:to_sym).each do |callback|
|
|
58
|
+
assert Controller.respond_to?(callback), "'#{callback}' not included"
|
|
59
|
+
Controller.expects(:_filter_setting).with(callback, 'gurba')
|
|
60
|
+
Controller.send(callback, 'gurba')
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context '_filter_setting' do
|
|
66
|
+
should 'setup correct instance method callback' do
|
|
67
|
+
Controller.expects(:skip_before_filter).with(:access_control).times(3)
|
|
68
|
+
Controller.expects(:before_filter).with(:access_control,{})
|
|
69
|
+
Controller.access_control
|
|
70
|
+
Controller.expects(:before_filter).with(:access_control,{:only => :index})
|
|
71
|
+
Controller.access_control :only => :index
|
|
72
|
+
Controller.expects(:before_filter).never
|
|
73
|
+
Controller.access_control :off
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
end
|
|
78
|
+
context 'instance methods' do
|
|
79
|
+
setup do
|
|
80
|
+
@controller = Controller.new
|
|
81
|
+
end
|
|
82
|
+
should 'set user' do
|
|
83
|
+
user = stub('user')
|
|
84
|
+
@controller.expects(:current_user).returns(user)
|
|
85
|
+
Trust::Authorization.expects(:user=).with(user)
|
|
86
|
+
@controller.set_user
|
|
87
|
+
end
|
|
88
|
+
should 'load resource' do
|
|
89
|
+
@controller.expects(:resource).returns(stub(:load => true))
|
|
90
|
+
@controller.load_resource
|
|
91
|
+
end
|
|
92
|
+
should 'expose resource as helper' do
|
|
93
|
+
assert @controller.class._helper_methods.include?(:resource)
|
|
94
|
+
end
|
|
95
|
+
should 'provide access control' do
|
|
96
|
+
resource = stub('resource')
|
|
97
|
+
instance = stub('resource instance')
|
|
98
|
+
klass = stub('resource klass')
|
|
99
|
+
parent = stub('resource parent')
|
|
100
|
+
|
|
101
|
+
resource.expects(:instance).returns(instance)
|
|
102
|
+
resource.expects(:parent).returns(parent)
|
|
103
|
+
@controller.expects(:resource).returns(resource).twice
|
|
104
|
+
Trust::Authorization.expects(:authorize!).with(nil,instance,parent)
|
|
105
|
+
@controller.access_control
|
|
106
|
+
|
|
107
|
+
resource.expects(:instance).returns(nil)
|
|
108
|
+
resource.expects(:parent).returns(parent)
|
|
109
|
+
resource.expects(:klass).returns(klass)
|
|
110
|
+
@controller.expects(:resource).returns(resource).times(3)
|
|
111
|
+
Trust::Authorization.expects(:authorize!).with(nil,klass,parent)
|
|
112
|
+
@controller.access_control
|
|
113
|
+
end
|
|
114
|
+
context 'can?' do
|
|
115
|
+
should 'call authorized?' do
|
|
116
|
+
user = User.new
|
|
117
|
+
account = Account.new
|
|
118
|
+
resource = stub('Resource')
|
|
119
|
+
resource.expects(:parent).returns(nil)
|
|
120
|
+
@controller.expects(:resource).returns(resource)
|
|
121
|
+
Trust::Authorization.expects(:authorized?).with(:manage,account,nil).returns(true)
|
|
122
|
+
@controller.can? :manage, account
|
|
123
|
+
end
|
|
124
|
+
should 'should have default parameters' do
|
|
125
|
+
resource = stub('Resource')
|
|
126
|
+
@controller.expects(:resource).returns(resource).at_least_once
|
|
127
|
+
resource.expects(:instance).returns(:instance)
|
|
128
|
+
resource.expects(:parent).returns(:parent)
|
|
129
|
+
Trust::Authorization.expects(:authorized?).with(:manage,:instance,:parent)
|
|
130
|
+
@controller.can? :manage
|
|
131
|
+
resource.expects(:instance).returns(nil)
|
|
132
|
+
resource.expects(:klass).returns(:klass)
|
|
133
|
+
resource.expects(:parent).returns(:parent)
|
|
134
|
+
Trust::Authorization.expects(:authorized?).with(:manage,:klass,:parent)
|
|
135
|
+
@controller.can? :manage
|
|
136
|
+
end
|
|
137
|
+
should 'be exposed as helper' do
|
|
138
|
+
assert @controller.class._helper_methods.include?(:can?)
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
context 'derived controller' do
|
|
143
|
+
should 'instantiate its properties' do
|
|
144
|
+
DerivedController.instance_variable_set('@properties',nil)
|
|
145
|
+
Trust::Controller::Properties.expects(:instantiate).with(DerivedController)
|
|
146
|
+
DerivedController.properties
|
|
147
|
+
end
|
|
148
|
+
should 'instantiate its own properties' do
|
|
149
|
+
assert_not_equal Controller.properties, DerivedController.properties
|
|
150
|
+
end
|
|
151
|
+
should 'delegate to its own resource' do
|
|
152
|
+
DerivedController.properties.expects(:belongs_to)
|
|
153
|
+
DerivedController.properties.expects(:actions)
|
|
154
|
+
DerivedController.properties.expects(:model)
|
|
155
|
+
DerivedController.belongs_to
|
|
156
|
+
DerivedController.actions
|
|
157
|
+
DerivedController.model
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# Copyright (c) 2012 Bingo Entreprenøren AS
|
|
2
|
+
# Copyright (c) 2012 Teknobingo Scandinavia AS
|
|
3
|
+
# Copyright (c) 2012 Knut I. Stenmark
|
|
4
|
+
# Copyright (c) 2012 Patrick Hanevold
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
# a copy of this software and associated documentation files (the
|
|
8
|
+
# "Software"), to deal in the Software without restriction, including
|
|
9
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
# the following conditions:
|
|
13
|
+
#
|
|
14
|
+
# The above copyright notice and this permission notice shall be
|
|
15
|
+
# included in all copies or substantial portions of the Software.
|
|
16
|
+
#
|
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
20
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
21
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
22
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
23
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24
|
+
|
|
25
|
+
require 'test_helper'
|
|
26
|
+
|
|
27
|
+
class Trust::InheritableAttributeTest < ActiveSupport::TestCase
|
|
28
|
+
setup do
|
|
29
|
+
class Cat
|
|
30
|
+
include Trust::InheritableAttribute
|
|
31
|
+
inheritable_attr :drinks
|
|
32
|
+
inheritable_attr :food, :instance_writer => false, :instance_reader => false
|
|
33
|
+
self.drinks = ["Becks"]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
should 'have only one element in Cat and have two elements in Garfield' do
|
|
37
|
+
class Garfield < Cat
|
|
38
|
+
self.drinks << "Fireman's 4"
|
|
39
|
+
end
|
|
40
|
+
assert_equal ["Becks"], Cat.drinks
|
|
41
|
+
assert_equal ["Becks", "Fireman's 4"], Garfield.drinks
|
|
42
|
+
end
|
|
43
|
+
should 'have instance method' do
|
|
44
|
+
assert_equal ["Becks"], Cat.new.drinks
|
|
45
|
+
Cat.new.drinks << "Bud"
|
|
46
|
+
assert_equal ["Becks", "Bud"], Cat.drinks
|
|
47
|
+
assert_equal ["Becks", "Bud"], Cat.new.drinks
|
|
48
|
+
assert_raises NoMethodError do
|
|
49
|
+
Cat.new.food
|
|
50
|
+
end
|
|
51
|
+
assert_raises NoMethodError do
|
|
52
|
+
Cat.new.food = 'x'
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
context 'inheritance' do
|
|
56
|
+
should 'deep copy inheritable attributes' do
|
|
57
|
+
Cat.new.drinks << ['Corona', { :booze => [ 'Liquor', 'Spirit'] }]
|
|
58
|
+
class Sylvester < Cat
|
|
59
|
+
self.drinks.last << 'Wiskey'
|
|
60
|
+
end
|
|
61
|
+
assert_equal ['Becks', ['Corona', { :booze => [ 'Liquor', 'Spirit'] }]], Cat.drinks
|
|
62
|
+
assert_equal ['Becks', ['Corona', { :booze => [ 'Liquor', 'Spirit'] }, 'Wiskey']], Sylvester.drinks
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|