acl9 0.12.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +21 -7
  3. data/.travis.yml +19 -0
  4. data/Appraisals +8 -0
  5. data/CONTRIBUTING.md +58 -0
  6. data/Gemfile +2 -0
  7. data/Gemfile.lock +88 -32
  8. data/LICENSE +9 -0
  9. data/README.md +156 -0
  10. data/Rakefile +6 -3
  11. data/acl9.gemspec +10 -13
  12. data/gemfiles/rails_4.0.gemfile +8 -0
  13. data/gemfiles/rails_4.1.gemfile +8 -0
  14. data/lib/acl9/model_extensions/for_subject.rb +5 -1
  15. data/lib/acl9/model_extensions.rb +3 -24
  16. data/lib/acl9/version.rb +1 -1
  17. data/lib/acl9.rb +1 -1
  18. data/test/controller_extensions/actions_test.rb +167 -0
  19. data/test/controller_extensions/anon_test.rb +39 -0
  20. data/test/controller_extensions/base.rb +96 -0
  21. data/test/controller_extensions/basics_test.rb +44 -0
  22. data/test/controller_extensions/conditions_test.rb +48 -0
  23. data/test/controller_extensions/method_test.rb +50 -0
  24. data/test/controller_extensions/multi_match_test.rb +142 -0
  25. data/test/controller_extensions/multiple_role_arguments_test.rb +135 -0
  26. data/test/controller_extensions/prepositions_test.rb +99 -0
  27. data/test/controller_extensions/pseudo_role_test.rb +26 -0
  28. data/test/controller_extensions/role_test.rb +75 -0
  29. data/test/controllers/acl_action_override_test.rb +24 -0
  30. data/test/controllers/acl_arguments_test.rb +5 -0
  31. data/test/controllers/acl_block_test.rb +5 -0
  32. data/test/controllers/acl_boolean_method_test.rb +5 -0
  33. data/test/controllers/acl_helper_method_test.rb +26 -0
  34. data/test/controllers/acl_ivars_test.rb +15 -0
  35. data/test/controllers/acl_method2_test.rb +6 -0
  36. data/test/controllers/acl_method_test.rb +6 -0
  37. data/test/controllers/acl_object_hash_test.rb +18 -0
  38. data/test/controllers/acl_query_method_named_test.rb +9 -0
  39. data/test/controllers/acl_query_method_test.rb +9 -0
  40. data/test/controllers/acl_query_method_with_lambda_test.rb +9 -0
  41. data/test/controllers/acl_query_mixin.rb +51 -0
  42. data/test/controllers/acl_subject_method_test.rb +15 -0
  43. data/test/controllers/arguments_checking_test.rb +43 -0
  44. data/test/dummy/app/controllers/acl_action_override.rb +15 -0
  45. data/test/dummy/app/controllers/acl_arguments.rb +10 -0
  46. data/test/dummy/app/controllers/acl_block.rb +6 -0
  47. data/test/dummy/app/controllers/acl_boolean_method.rb +23 -0
  48. data/test/dummy/app/controllers/acl_helper_method.rb +11 -0
  49. data/test/dummy/app/controllers/acl_ivars.rb +17 -0
  50. data/test/dummy/app/controllers/acl_method.rb +6 -0
  51. data/test/dummy/app/controllers/acl_method2.rb +6 -0
  52. data/test/dummy/app/controllers/acl_objects_hash.rb +10 -0
  53. data/test/dummy/app/controllers/acl_query_method.rb +9 -0
  54. data/test/dummy/app/controllers/acl_query_method_named.rb +13 -0
  55. data/test/dummy/app/controllers/acl_query_method_with_lambda.rb +9 -0
  56. data/test/dummy/app/controllers/acl_subject_method.rb +16 -0
  57. data/test/dummy/app/controllers/application_controller.rb +7 -0
  58. data/test/dummy/app/controllers/empty_controller.rb +5 -0
  59. data/test/dummy/app/helpers/application_helper.rb +2 -0
  60. data/test/dummy/app/helpers/some_helper.rb +8 -0
  61. data/test/dummy/app/models/.keep +0 -0
  62. data/test/dummy/app/models/access.rb +3 -0
  63. data/test/dummy/app/models/account.rb +3 -0
  64. data/test/dummy/app/models/bar.rb +3 -0
  65. data/test/dummy/app/models/concerns/.keep +0 -0
  66. data/test/dummy/app/models/foo.rb +3 -0
  67. data/test/dummy/app/models/foo_bar.rb +3 -0
  68. data/test/dummy/app/models/other/foo.rb +5 -0
  69. data/test/dummy/app/models/other/role.rb +5 -0
  70. data/test/dummy/app/models/other/user.rb +5 -0
  71. data/test/dummy/app/models/role.rb +3 -0
  72. data/test/dummy/app/models/user.rb +3 -0
  73. data/test/dummy/app/models/uuid.rb +4 -0
  74. data/test/dummy/config/application.rb +23 -0
  75. data/test/dummy/config/boot.rb +4 -0
  76. data/test/dummy/config/database.yml +25 -0
  77. data/test/dummy/config/environment.rb +5 -0
  78. data/test/dummy/config/environments/development.rb +37 -0
  79. data/test/dummy/config/environments/production.rb +78 -0
  80. data/test/dummy/config/environments/test.rb +39 -0
  81. data/test/dummy/config/initializers/assets.rb +8 -0
  82. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  83. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  84. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  85. data/test/dummy/config/initializers/inflections.rb +16 -0
  86. data/test/dummy/config/initializers/mime_types.rb +4 -0
  87. data/test/dummy/config/initializers/secrets.rb +1 -0
  88. data/test/dummy/config/initializers/session_store.rb +3 -0
  89. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  90. data/test/dummy/config/locales/en.yml +23 -0
  91. data/test/dummy/config/routes.rb +3 -0
  92. data/test/dummy/config.ru +4 -0
  93. data/test/dummy/db/migrate/20141117132218_create_tables.rb +102 -0
  94. data/test/helpers/helper_test.rb +89 -0
  95. data/test/models/roles_test.rb +251 -0
  96. data/test/models/roles_with_custom_association_names_test.rb +28 -0
  97. data/test/models/roles_with_custom_class_names_test.rb +28 -0
  98. data/test/models/system_roles_test.rb +16 -0
  99. data/test/models/users_roles_and_subjects_with_namespaced_class_names_test.rb +30 -0
  100. data/test/test_helper.rb +76 -23
  101. data/test/version_test.rb +2 -2
  102. metadata +190 -74
  103. data/README.textile +0 -921
  104. data/VERSION.yml +0 -5
  105. data/init.rb +0 -1
  106. data/test/access_control_test.rb +0 -338
  107. data/test/dsl_base_test.rb +0 -795
  108. data/test/helpers_test.rb +0 -133
  109. data/test/roles_test.rb +0 -370
  110. data/test/support/controllers.rb +0 -207
  111. data/test/support/models.rb +0 -59
  112. data/test/support/schema.rb +0 -93
@@ -0,0 +1,135 @@
1
+ require_relative 'base'
2
+
3
+ module ControllerExtensions
4
+ class MultipleRoleArgumentsTest < Base
5
+ test "#allow should be able to receive a role list (global roles)" do
6
+ assert ( bzz = User.create ).has_role! :bzz
7
+ assert ( whoa = User.create ).has_role! :whoa
8
+
9
+ @tester.acl_block! do
10
+ allow :bzz, :whoa
11
+ end
12
+ assert_permitted bzz
13
+ assert_permitted whoa
14
+ assert_forbidden nil
15
+ assert_forbidden User.create
16
+ end
17
+
18
+ test "#allow should be able to receive a role list (object roles)" do
19
+ assert foo = Foo.create
20
+ assert foo_too = Foo.create
21
+
22
+ assert ( maker = User.create ).has_role! :maker, foo
23
+ assert ( faker = User.create ).has_role! :faker, foo_too
24
+
25
+ @tester.acl_block! do
26
+ allow :maker, :faker, :of => :foo
27
+ end
28
+
29
+ assert_permitted maker, :foo => foo
30
+ assert_forbidden maker, :foo => foo_too
31
+ assert_permitted faker, :foo => foo_too
32
+ assert_forbidden faker, :foo => foo
33
+
34
+ assert other = User.create
35
+ assert_forbidden other, :foo => foo
36
+ assert_forbidden other, :foo => foo_too
37
+ assert_forbidden nil
38
+ end
39
+
40
+ test "#allow should be able to receive a role list (class roles)" do
41
+ assert ( frooble = User.create ).has_role! :frooble, Foo
42
+ assert ( oombigle = User.create ).has_role! :oombigle, Foo
43
+ assert ( lame_frooble = User.create ).has_role! :frooble
44
+
45
+ @tester.acl_block! do
46
+ allow :frooble, :oombigle, :by => Foo
47
+ end
48
+ assert_permitted frooble
49
+ assert_permitted oombigle
50
+ assert_forbidden lame_frooble
51
+ assert_forbidden nil
52
+ end
53
+
54
+ test "#deny should be able to receive a role list (global roles)" do
55
+ assert ( bzz = User.create ).has_role! :bzz
56
+ assert ( whoa = User.create ).has_role! :whoa
57
+
58
+ @tester.acl_block! do
59
+ default :allow
60
+ deny :bzz, :whoa
61
+ end
62
+
63
+ assert_forbidden bzz
64
+ assert_forbidden whoa
65
+ assert_permitted nil
66
+ assert_permitted User.create
67
+ end
68
+
69
+ test "#deny should be able to receive a role list (object roles)" do
70
+ assert foo = Foo.create
71
+ assert foo_too = Foo.create
72
+
73
+ assert ( maker = User.create ).has_role! :maker, foo
74
+ assert ( faker = User.create ).has_role! :faker, foo_too
75
+
76
+ @tester.acl_block! do
77
+ default :allow
78
+ deny :maker, :faker, :of => :foo
79
+ end
80
+
81
+ assert_forbidden maker, :foo => foo
82
+ assert_permitted maker, :foo => foo_too
83
+ assert_forbidden faker, :foo => foo_too
84
+ assert_permitted faker, :foo => foo
85
+
86
+ assert other = User.create
87
+ assert_permitted other, :foo => foo
88
+ assert_permitted other, :foo => foo_too
89
+ assert_permitted nil
90
+ end
91
+
92
+ test "#deny should be able to receive a role list (class roles)" do
93
+ assert ( frooble = User.create ).has_role! :frooble, Foo
94
+ assert ( oombigle = User.create ).has_role! :oombigle, Foo
95
+ assert ( lame_frooble = User.create ).has_role! :frooble
96
+
97
+ @tester.acl_block! do
98
+ default :allow
99
+ deny :frooble, :oombigle, :by => Foo
100
+ end
101
+
102
+ assert_forbidden frooble
103
+ assert_forbidden oombigle
104
+ assert_permitted lame_frooble
105
+ assert_permitted nil
106
+ end
107
+
108
+ test "should also respect :to and :except" do
109
+ assert foo = Foo.create
110
+
111
+ assert ( foo = User.create ).has_role! :foo
112
+ assert ( joo = User.create ).has_role! :joo, foo
113
+ assert ( qoo = User.create ).has_role! :qoo, Bar
114
+
115
+ @tester.acl_block! do
116
+ allow :foo, :boo, :to => [:index, :show]
117
+ allow :zoo, :joo, :by => :foo, :to => [:edit, :update]
118
+ allow :qoo, :woo, :of => Bar
119
+ deny :qoo, :woo, :of => Bar, :except => [:delete, :destroy]
120
+ end
121
+
122
+ assert_permitted foo, 'index'
123
+ assert_permitted foo, 'show'
124
+ assert_forbidden foo, 'edit'
125
+ assert_permitted joo, 'edit', :foo => foo
126
+ assert_permitted joo, 'update', :foo => foo
127
+ assert_forbidden joo, 'show', :foo => foo
128
+ assert_forbidden joo, 'show'
129
+ assert_permitted qoo, 'delete'
130
+ assert_permitted qoo, 'destroy'
131
+ assert_forbidden qoo, 'edit'
132
+ assert_forbidden qoo, 'show'
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,99 @@
1
+ require_relative 'base'
2
+
3
+ module ControllerExtensions
4
+ class PrepositionsTest < Base
5
+
6
+ %i[of for in on at by].each do |prep|
7
+ test "allow :#{prep} => :foo checks @foo" do
8
+ assert @foo = Foo.first_or_create
9
+ assert ( user = User.create ).has_role! :manager, @foo
10
+
11
+ @tester.acl_block! do
12
+ allow :manager, prep => :foo
13
+ end
14
+
15
+ assert other_foo = Foo.create
16
+
17
+ assert_permitted user, :foo => @foo
18
+ assert_forbidden user, :foo => other_foo
19
+ assert_forbidden user, :foo => Foo
20
+ assert_forbidden nil, :foo => @foo
21
+ assert_forbidden User.create, :foo => @foo
22
+ end
23
+
24
+ test "invalid allow :#{prep} arg raises ArgumentError" do
25
+ assert_raise ArgumentError do
26
+ @tester.acl_block! { allow :hom, :by => 1 }
27
+ end
28
+ end
29
+ end
30
+
31
+ test "allow class role allowed" do
32
+ assert ( user = User.create ).has_role! :owner, Foo
33
+
34
+ @tester.acl_block! do
35
+ allow :owner, :of => Foo
36
+ end
37
+
38
+ assert_permitted user
39
+ assert_forbidden nil
40
+ assert_forbidden User.create
41
+ end
42
+
43
+ %i[of for in on at by].each do |prep|
44
+ test "deny :#{prep} => :foo checks @foo" do
45
+ assert @foo = Foo.first_or_create
46
+ assert ( user = User.create ).has_role! :thief, @foo
47
+
48
+ @tester.acl_block! do
49
+ default :allow
50
+ deny :thief, prep => :foo
51
+ end
52
+
53
+ assert_forbidden user, :foo => @foo
54
+ assert_permitted user, :foo => Foo.create
55
+ assert_permitted user, :foo => Foo
56
+ assert_permitted nil, :foo => @foo
57
+ assert_permitted User.create, :foo => @foo
58
+ end
59
+
60
+ test "invalid deny :#{prep} arg raises ArgumentError" do
61
+ assert_raise ArgumentError do
62
+ @tester.acl_block! { deny :her, :for => "him" }
63
+ end
64
+ end
65
+ end
66
+
67
+ test "deny class role denied" do
68
+ assert ( user = User.create ).has_role! :ignorant, Foo
69
+
70
+ @tester.acl_block! do
71
+ default :allow
72
+ deny :ignorant, :of => Foo
73
+ end
74
+
75
+ assert_forbidden user, Foo
76
+ assert_permitted nil
77
+ assert_permitted User.create
78
+ end
79
+
80
+ test "> 1 allow prepositions raises ArgumentError" do
81
+ assert_raise ArgumentError do
82
+ @tester.acl_block! { allow :some, :by => :one, :for => :another }
83
+ end
84
+ end
85
+
86
+ test "> 1 deny prepositions raises ArgumentError" do
87
+ assert_raise ArgumentError do
88
+ @tester.acl_block! { deny :some, :in => :here, :on => :today }
89
+ end
90
+ end
91
+
92
+ test "should raise an ArgumentError when both :to and :except are specified" do
93
+ assert_raise ArgumentError do
94
+ @tester.acl_block! { allow all, :to => :index, :except => ['show', 'edit'] }
95
+ end
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'base'
2
+
3
+ module ControllerExtensions
4
+ class PseudoRoleTest < Base
5
+ %i[all everyone everybody anyone].each do |pseudorole|
6
+ test "allow #{pseudorole} allows all" do
7
+ @tester.acl_block! do
8
+ allow send pseudorole
9
+ end
10
+
11
+ assert_equal :deny, @tester.default_action
12
+ assert_all_permitted
13
+ end
14
+
15
+ test "deny #{pseudorole} denies all" do
16
+ @tester.acl_block! do
17
+ default :allow
18
+ deny send pseudorole
19
+ end
20
+
21
+ assert_equal :allow, @tester.default_action
22
+ assert_all_forbidden
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,75 @@
1
+ require_relative 'base'
2
+
3
+ module ControllerExtensions
4
+ class RoleTest < Base
5
+ test "allows admin implicit default" do
6
+ @tester.acl_block! { allow :admin }
7
+
8
+ assert_admins_permitted
9
+ assert_forbidden nil
10
+
11
+ assert ( user = User.create ).has_role! :cool
12
+ assert_forbidden user
13
+ end
14
+
15
+ test "allow plural admins implicit default" do
16
+ @tester.acl_block! do
17
+ allow :admins
18
+ end
19
+
20
+ assert_admins_permitted
21
+ assert_forbidden nil
22
+
23
+ assert ( user = User.create ).has_role! :cool
24
+ assert_forbidden user
25
+ end
26
+
27
+ test "allow with several roles" do
28
+ assert ( cool1_user = User.create ).has_role! :cool
29
+ assert ( cool2_user = User.create ).has_role! :cool
30
+ assert ( super_user = User.create ).has_role! :super
31
+
32
+ @tester.acl_block! do
33
+ allow :admin
34
+ allow :cool
35
+ end
36
+
37
+ assert_admins_permitted
38
+
39
+ assert_permitted cool1_user
40
+ assert_permitted cool2_user
41
+
42
+ assert_forbidden nil
43
+ assert_forbidden super_user
44
+ end
45
+
46
+ test "deny plural admins" do
47
+ @tester.acl_block! do
48
+ default :allow
49
+ deny :admins
50
+ end
51
+
52
+ assert_permitted nil
53
+ assert_permitted User.create
54
+ assert_admins_forbidden
55
+ end
56
+
57
+ test "deny several roles" do
58
+ assert ( cool1_user = User.create ).has_role! :cool
59
+ assert ( cool2_user = User.create ).has_role! :cool
60
+ assert ( super_user = User.create ).has_role! :super
61
+
62
+ @tester.acl_block! do
63
+ default :allow
64
+ deny :admin
65
+ deny :cool
66
+ end
67
+
68
+ assert_permitted nil
69
+ assert_admins_forbidden
70
+ assert_forbidden cool1_user
71
+ assert_forbidden cool2_user
72
+ assert_permitted super_user
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class ACLActionOverrideTest < ActionController::TestCase
4
+ test "anon can index" do
5
+ assert get :check_allow, :_action => :index
6
+ assert_response :ok
7
+ end
8
+
9
+ test "anon can't show" do
10
+ assert get :check_allow, :_action => :show
11
+ assert_response :unauthorized
12
+ end
13
+
14
+ test "normal user can't edit" do
15
+ assert get :check_allow_with_foo, :_action => :edit, :user_id => User.create.id
16
+ assert_response :unauthorized
17
+ end
18
+
19
+ test "foo owner can edit" do
20
+ assert ( user = User.create ).has_role! :owner, Foo.first_or_create
21
+ assert get :check_allow_with_foo, :_action => :edit, :user_id => user.id
22
+ assert_response :ok
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class ACLArgumentsTest < ActionController::TestCase
4
+ include BaseTests
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class ACLBlockTest < ActionController::TestCase
4
+ include BaseTests
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_helper'
2
+
3
+ class ACLBooleanMethodTest < ActionController::TestCase
4
+ include BaseTests
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class ACLHelperMethodTest < ActionController::TestCase
4
+ setup do
5
+ assert @user = User.create
6
+ end
7
+
8
+ test "foo owner allowed" do
9
+ assert @user.has_role! :owner, Foo.first_or_create
10
+
11
+ assert get :allow, :user_id => @user.id
12
+ assert_select 'div', 'OK'
13
+ end
14
+
15
+ test "another user denied" do
16
+ assert @user.has_role! :owner
17
+
18
+ assert get :allow, :user_id => @user.id
19
+ assert_select 'div', 'OK'
20
+ end
21
+
22
+ test "anon denied" do
23
+ assert get :allow
24
+ assert_select 'div', 'AccessDenied'
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ACLIvarsTest < ActionController::TestCase
4
+ test "owner of foo destroys" do
5
+ assert ( user = User.create ).has_role! :owner, Bar
6
+ assert delete :destroy, :id => 1, :user_id => user.id
7
+ assert_response :ok
8
+ end
9
+
10
+ test "bartender at Foo destroys" do
11
+ assert ( user = User.create ).has_role! :bartender, Foo
12
+ assert delete :destroy, :id => 1, :user_id => user.id
13
+ assert_response :ok
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ class ACLMethod2Test < ActionController::TestCase
4
+ include BaseTests
5
+ include ShouldRespondToAcl
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+
3
+ class ACLMethodTest < ActionController::TestCase
4
+ include BaseTests
5
+ include ShouldRespondToAcl
6
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class ACLObjectsHashTest < ActionController::TestCase
4
+ setup do
5
+ assert @user = User.create
6
+ assert @user.has_role! :owner, Foo.first_or_create
7
+ end
8
+
9
+ test "objects hash preferred to @ivar" do
10
+ assert get :allow, :user_id => @user.id
11
+ assert_response :ok
12
+ end
13
+
14
+ test "unauthed for no user" do
15
+ assert get :allow
16
+ assert_response :unauthorized
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'acl_query_mixin'
2
+
3
+ class ACLQueryMethodNamedTest < ActionController::TestCase
4
+ test "should respond to :allow_ay" do
5
+ assert @controller.respond_to? :allow_ay
6
+ end
7
+
8
+ include ACLQueryMixin
9
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'acl_query_mixin'
2
+
3
+ class ACLQueryMethodTest < ActionController::TestCase
4
+ test "should respond to :acl?" do
5
+ assert @controller.respond_to? :acl?
6
+ end
7
+
8
+ include ACLQueryMixin
9
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'acl_query_mixin'
2
+
3
+ class ACLQueryMethodWithLambdaTest < ActionController::TestCase
4
+ test "should respond to :acl?" do
5
+ assert @controller.respond_to? :acl?
6
+ end
7
+
8
+ include ACLQueryMixin
9
+ end
@@ -0,0 +1,51 @@
1
+ require 'test_helper'
2
+
3
+ module ACLQueryMixin
4
+ def self.included base
5
+ base.class_eval do
6
+ setup do
7
+ ( @editor = User.create ).has_role! :editor
8
+ ( @viewer = User.create ).has_role! :viewer
9
+ ( @owneroffoo = User.create ).has_role! :owner, Foo.first_or_create
10
+ end
11
+
12
+ %i[edit update destroy].each do |meth|
13
+ test "should return true for editor/#{meth}" do
14
+ assert @controller.current_user = @editor
15
+ assert @controller.acl? meth
16
+ assert @controller.acl? meth.to_s
17
+ end
18
+
19
+ test "should return false for viewer/#{meth}" do
20
+ assert @controller.current_user = @viewer
21
+ refute @controller.acl? meth
22
+ refute @controller.acl? meth.to_s
23
+ end
24
+ end
25
+
26
+ %i[index show].each do |meth|
27
+ test "should return false for editor/#{meth}" do
28
+ assert @controller.current_user = @editor
29
+ refute @controller.acl? meth
30
+ refute @controller.acl? meth.to_s
31
+ end
32
+
33
+ test "should return true for viewer/#{meth}" do
34
+ assert @controller.current_user = @viewer
35
+ assert @controller.acl? meth
36
+ assert @controller.acl? meth.to_s
37
+ end
38
+ end
39
+
40
+ test "should return false for editor/fooize" do
41
+ assert @controller.current_user = @editor
42
+ refute @controller.acl? :fooize
43
+ end
44
+
45
+ test "should return true for foo owner" do
46
+ assert @controller.current_user = @owneroffoo
47
+ assert @controller.acl? :fooize, :foo => Foo.first
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ACLSubjectMethodTest < ActionController::TestCase
4
+ test "allow the only user to index" do
5
+ assert ( user = User.create ).has_role! :the_only_one
6
+ assert get :index, :user_id => user.id
7
+ assert_response :ok
8
+ end
9
+
10
+ test "deny anonymous to index" do
11
+ assert_raises Acl9::AccessDenied do
12
+ assert get :index
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class ArgumentsCheckingTest < ActionController::TestCase
4
+ test "raise ArgumentError without a block" do
5
+ assert_raise ArgumentError do
6
+ class FailureController < ApplicationController
7
+ access_control
8
+ end
9
+ end
10
+ end
11
+
12
+ test "raise ArgumentError with 1st argument which is not a symbol" do
13
+ assert_raise ArgumentError do
14
+ class FailureController < ApplicationController
15
+ access_control 123 do end
16
+ end
17
+ end
18
+ end
19
+
20
+ test "raise ArgumentError with more than 1 positional argument" do
21
+ assert_raise ArgumentError do
22
+ class FailureController < ApplicationController
23
+ access_control :foo, :bar do end
24
+ end
25
+ end
26
+ end
27
+
28
+ test "raise ArgumentError with :helper => true and no method name" do
29
+ assert_raise ArgumentError do
30
+ class FailureController < ApplicationController
31
+ access_control :helper => true do end
32
+ end
33
+ end
34
+ end
35
+
36
+ test "raise ArgumentError with :helper => :method and a method name" do
37
+ assert_raise ArgumentError do
38
+ class FailureController < ApplicationController
39
+ access_control :meth, :helper => :another_meth do end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,15 @@
1
+ class ACLActionOverride < ApplicationController
2
+ access_control :allowed?, :filter => false do
3
+ allow all, :to => :index
4
+ deny all, :to => :show
5
+ allow :owner, :of => :foo, :to => :edit
6
+ end
7
+
8
+ def check_allow
9
+ head allowed?(params[:_action]) ? :ok : :unauthorized
10
+ end
11
+
12
+ def check_allow_with_foo
13
+ head allowed?(params[:_action], :foo => Foo.first) ? :ok : :unauthorized
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ class ACLArguments < EmptyController
2
+ access_control :except => [:index, :show] do
3
+ allow :admin, :if => :true_meth, :unless => :false_meth
4
+ end
5
+
6
+ private
7
+
8
+ def true_meth; true end
9
+ def false_meth; false end
10
+ end
@@ -0,0 +1,6 @@
1
+ class ACLBlock < EmptyController
2
+ access_control :debug => true do
3
+ allow all, :to => [:index, :show]
4
+ allow :admin
5
+ end
6
+ end
@@ -0,0 +1,23 @@
1
+ class ACLBooleanMethod < EmptyController
2
+ access_control :acl, :filter => false do
3
+ allow all, :to => [:index, :show], :if => :true_meth
4
+ allow :admin, :unless => :false_meth
5
+ allow all, :if => :false_meth
6
+ allow all, :unless => :true_meth
7
+ end
8
+
9
+ before_filter :check_acl
10
+
11
+ def check_acl
12
+ if self.acl
13
+ true
14
+ else
15
+ raise Acl9::AccessDenied
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def true_meth; true end
22
+ def false_meth; false end
23
+ end
@@ -0,0 +1,11 @@
1
+ class ACLHelperMethod < ApplicationController
2
+ access_control :helper => :foo? do
3
+ allow :owner, :of => :foo
4
+ end
5
+
6
+ def allow
7
+ @foo = Foo.first
8
+
9
+ render inline: "<div><%= foo? ? 'OK' : 'AccessDenied' %></div>"
10
+ end
11
+ end