troles 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/.rspec +1 -0
- data/Gemfile +26 -0
- data/Gemfile.lock +161 -0
- data/MIT-LICENSE +20 -0
- data/README.textile +686 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/config/database.yml +4 -0
- data/development.sqlite3 +0 -0
- data/lib/trole/adapters/active_record/config.rb +23 -0
- data/lib/trole/adapters/active_record/storage.rb +0 -0
- data/lib/trole/adapters/active_record/strategy.rb +0 -0
- data/lib/trole/adapters/active_record.rb +8 -0
- data/lib/trole/adapters/mongoid/config.rb +34 -0
- data/lib/trole/adapters/mongoid/storage.rb +0 -0
- data/lib/trole/adapters/mongoid/strategy.rb +0 -0
- data/lib/trole/adapters/mongoid.rb +0 -0
- data/lib/trole/api/cache.rb +9 -0
- data/lib/trole/api/config.rb +12 -0
- data/lib/trole/api/core.rb +20 -0
- data/lib/trole/api/event.rb +9 -0
- data/lib/trole/api/read.rb +14 -0
- data/lib/trole/api/validation.rb +9 -0
- data/lib/trole/api/write.rb +25 -0
- data/lib/trole/api.rb +41 -0
- data/lib/trole/config.rb +15 -0
- data/lib/trole/macros.rb +5 -0
- data/lib/trole/operations/read.rb +12 -0
- data/lib/trole/operations/write.rb +11 -0
- data/lib/trole/operations.rb +34 -0
- data/lib/trole/storage/base_one.rb +46 -0
- data/lib/trole/storage/bit_one.rb +43 -0
- data/lib/trole/storage/embed_one.rb +36 -0
- data/lib/trole/storage/ref_one.rb +39 -0
- data/lib/trole/storage/string_one.rb +50 -0
- data/lib/trole/storage.rb +14 -0
- data/lib/trole/strategy.rb +35 -0
- data/lib/trole.rb +10 -0
- data/lib/troles/adapters/active_record/Design Notes.textile +4 -0
- data/lib/troles/adapters/active_record/config.rb +51 -0
- data/lib/troles/adapters/active_record/storage/embed_many.rb +8 -0
- data/lib/troles/adapters/active_record/storage.rb +5 -0
- data/lib/troles/adapters/active_record/strategy.rb +11 -0
- data/lib/troles/adapters/active_record.rb +8 -0
- data/lib/troles/adapters/mongoid/Design Notes.textile +3 -0
- data/lib/troles/adapters/mongoid/config.rb +45 -0
- data/lib/troles/adapters/mongoid.rb +8 -0
- data/lib/troles/api/cache.rb +4 -0
- data/lib/troles/api/config.rb +9 -0
- data/lib/troles/api/core.rb +9 -0
- data/lib/troles/api/event.rb +4 -0
- data/lib/troles/api/read.rb +4 -0
- data/lib/troles/api/validation.rb +4 -0
- data/lib/troles/api/write.rb +4 -0
- data/lib/troles/api.rb +40 -0
- data/lib/troles/common/api/cache.rb +12 -0
- data/lib/troles/common/api/config.rb +4 -0
- data/lib/troles/common/api/core.rb +52 -0
- data/lib/troles/common/api/event.rb +39 -0
- data/lib/troles/common/api/read.rb +44 -0
- data/lib/troles/common/api/validation.rb +44 -0
- data/lib/troles/common/api/write.rb +76 -0
- data/lib/troles/common/api.rb +28 -0
- data/lib/troles/common/config/schema.rb +72 -0
- data/lib/troles/common/config/schema_helpers.rb +95 -0
- data/lib/troles/common/config/static_roles.rb +14 -0
- data/lib/troles/common/config/valid_roles.rb +21 -0
- data/lib/troles/common/config.rb +96 -0
- data/lib/troles/common/dependencies.rb +9 -0
- data/lib/troles/common/event_manager.rb +40 -0
- data/lib/troles/common/macros/configuration/base_loader.rb +40 -0
- data/lib/troles/common/macros/configuration/config_loader.rb +19 -0
- data/lib/troles/common/macros/configuration/storage_loader.rb +20 -0
- data/lib/troles/common/macros/configuration/strategy_loader.rb +38 -0
- data/lib/troles/common/macros/configuration.rb +89 -0
- data/lib/troles/common/macros/static_roles.rb +9 -0
- data/lib/troles/common/macros/strategy_options.rb +21 -0
- data/lib/troles/common/macros.rb +38 -0
- data/lib/troles/common/marshaller/bitmask.rb +43 -0
- data/lib/troles/common/marshaller/generic.rb +24 -0
- data/lib/troles/common/marshaller.rb +14 -0
- data/lib/troles/common/operations/read.rb +28 -0
- data/lib/troles/common/operations/write.rb +42 -0
- data/lib/troles/common/operations.rb +33 -0
- data/lib/troles/common/storage.rb +73 -0
- data/lib/troles/common.rb +17 -0
- data/lib/troles/config.rb +15 -0
- data/lib/troles/macros.rb +7 -0
- data/lib/troles/meta.rb +5 -0
- data/lib/troles/operations/read.rb +6 -0
- data/lib/troles/operations/write.rb +6 -0
- data/lib/troles/operations.rb +12 -0
- data/lib/troles/storage/base_many.rb +25 -0
- data/lib/troles/storage/bit_many.rb +56 -0
- data/lib/troles/storage/embed_many.rb +58 -0
- data/lib/troles/storage/ref_many.rb +44 -0
- data/lib/troles/storage/string_many.rb +41 -0
- data/lib/troles/storage.rb +13 -0
- data/lib/troles/strategy.rb +34 -0
- data/lib/troles.rb +11 -0
- data/playbox/old_rake +25 -0
- data/spec/Guide to running specs.textile +16 -0
- data/spec/active_record/migrations/many/bit_many.rb +16 -0
- data/spec/active_record/migrations/many/ref_many.rb +31 -0
- data/spec/active_record/migrations/many/string_many.rb +16 -0
- data/spec/active_record/migrations/one/bit_one.rb +14 -0
- data/spec/active_record/migrations/one/ref_one.rb +20 -0
- data/spec/active_record/migrations/one/string_one.rb +14 -0
- data/spec/active_record/models/ref_many.rb +10 -0
- data/spec/active_record/models/ref_one.rb +10 -0
- data/spec/active_record/models/role.rb +2 -0
- data/spec/active_record/models/user.rb +5 -0
- data/spec/active_record/models.rb +2 -0
- data/spec/active_record/strategies/many/bit_many_spec.rb +41 -0
- data/spec/active_record/strategies/many/ref_many_spec.rb +45 -0
- data/spec/active_record/strategies/many/string_many_spec.rb +39 -0
- data/spec/active_record/strategies/one/bit_one_spec.rb +35 -0
- data/spec/active_record/strategies/one/ref_one_spec.rb +41 -0
- data/spec/active_record/strategies/one/string_one_spec.rb +35 -0
- data/spec/active_record/strategy_helper.rb +4 -0
- data/spec/active_record_helper.rb +50 -0
- data/spec/db/database.yml +4 -0
- data/spec/dummy/Gemfile.lock +108 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/images/rails.png +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +9 -0
- data/spec/dummy/app/assets/stylesheets/application.css +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/main_controller.rb +16 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/ref_many_user.rb +7 -0
- data/spec/dummy/app/models/ref_one_user.rb +3 -0
- data/spec/dummy/app/models/role.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/main/index.html.erb +3 -0
- data/spec/dummy/config/application.rb +54 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +24 -0
- data/spec/dummy/config/environments/production.rb +52 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/troles.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +60 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/01_create_roles.rb +14 -0
- data/spec/dummy/db/migrate/02_create_ref_many_users.rb +20 -0
- data/spec/dummy/db/schema.rb +32 -0
- data/spec/dummy/db/seeds.rb +4 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy_spec_helper.rb +33 -0
- data/spec/factories.rb +8 -0
- data/spec/generic/models/accounts/admin_account.rb +7 -0
- data/spec/generic/models/accounts/blogger_account.rb +7 -0
- data/spec/generic/models/accounts/user_account.rb +7 -0
- data/spec/generic/models/accounts.rb +1 -0
- data/spec/generic/models/base_user.rb +27 -0
- data/spec/generic/models/role.rb +37 -0
- data/spec/generic/models/user.rb +7 -0
- data/spec/generic/models.rb +3 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/integration/troles/Running dummy tests.textile +35 -0
- data/spec/integration/troles/navigation_spec.rb +49 -0
- data/spec/mongoid/models/ref_many.rb +15 -0
- data/spec/mongoid/models/ref_one.rb +15 -0
- data/spec/mongoid/models/role.rb +5 -0
- data/spec/mongoid/models/user.rb +9 -0
- data/spec/mongoid/models.rb +2 -0
- data/spec/mongoid/strategies/many/bit_many_spec.rb +35 -0
- data/spec/mongoid/strategies/many/ref_many_spec.rb +35 -0
- data/spec/mongoid/strategies/many/string_many_spec.rb +30 -0
- data/spec/mongoid/strategies/one/bit_one_spec.rb +26 -0
- data/spec/mongoid/strategies/one/ref_one_spec.rb +31 -0
- data/spec/mongoid/strategies/one/string_one_spec.rb +26 -0
- data/spec/mongoid/strategy_helper.rb +4 -0
- data/spec/mongoid_helper.rb +19 -0
- data/spec/playbox/rspec_examples.rb +381 -0
- data/spec/support/shared_examples.rb +1 -0
- data/spec/trole/Trole Design.textile +4 -0
- data/spec/trole/api/cache_api_spec.rb +2 -0
- data/spec/trole/api/core_api_spec.rb +4 -0
- data/spec/trole/api/event_api.rb +2 -0
- data/spec/trole/api/operations_api_spec.rb +2 -0
- data/spec/trole/api/read_api_spec.rb +5 -0
- data/spec/trole/api/validation_api_spec.rb +2 -0
- data/spec/trole/api/write_api_spec.rb +2 -0
- data/spec/trole/api_spec.rb +60 -0
- data/spec/trole/multi_roles_spec.rb +163 -0
- data/spec/trole/operations/read_spec.rb +18 -0
- data/spec/trole/operations/write_spec.rb +0 -0
- data/spec/trole/playbox/shared_examples.rb +107 -0
- data/spec/trole/strategies/bit_one_spec.rb +22 -0
- data/spec/trole/strategies/embed_one_spec.rb +32 -0
- data/spec/trole/strategies/ref_one_spec.rb +29 -0
- data/spec/trole/strategies/string_one_spec.rb +26 -0
- data/spec/trole/strategy_helper.rb +3 -0
- data/spec/trole/two_roles_spec.rb +76 -0
- data/spec/trole_spec.rb +12 -0
- data/spec/trole_spec_helper.rb +20 -0
- data/spec/troles/api/cache_api_spec.rb +2 -0
- data/spec/troles/api/core_api_spec.rb +4 -0
- data/spec/troles/api/event_api.rb +2 -0
- data/spec/troles/api/read_api_spec.rb +2 -0
- data/spec/troles/api/validation_api_spec.rb +2 -0
- data/spec/troles/api/write_api_spec.rb +2 -0
- data/spec/troles/api_spec.rb +41 -0
- data/spec/troles/common/api/cache_api_spec.rb +31 -0
- data/spec/troles/common/api/config_api.rb +0 -0
- data/spec/troles/common/api/core_api_spec.rb +14 -0
- data/spec/troles/common/api/event_api_spec.rb +9 -0
- data/spec/troles/common/api/operations_api_spec.rb +55 -0
- data/spec/troles/common/api/read_api_spec.rb +23 -0
- data/spec/troles/common/api/validation_api_spec.rb +46 -0
- data/spec/troles/common/api/write_api_spec.rb +81 -0
- data/spec/troles/common/api_spec.rb +101 -0
- data/spec/troles/common/config_spec.rb +11 -0
- data/spec/troles/common/multi_roles_spec.rb +142 -0
- data/spec/troles/marshaller/bitmask_spec.rb +14 -0
- data/spec/troles/operations/read_ops_spec.rb +0 -0
- data/spec/troles/operations/write_ops_spec.rb +0 -0
- data/spec/troles/playbox/shared_examples.rb +68 -0
- data/spec/troles/strategies/bit_many_spec.rb +30 -0
- data/spec/troles/strategies/embed_many_spec.rb +35 -0
- data/spec/troles/strategies/ref_many_spec.rb +36 -0
- data/spec/troles/strategies/string_many_spec.rb +32 -0
- data/spec/troles/strategy_helper.rb +3 -0
- data/spec/troles_spec.rb +10 -0
- data/troles.gemspec +325 -0
- metadata +469 -0
@@ -0,0 +1,60 @@
|
|
1
|
+
require_all File.dirname(__FILE__) + '/api'
|
2
|
+
|
3
|
+
require 'trole/two_roles_spec'
|
4
|
+
require 'trole/multi_roles_spec'
|
5
|
+
|
6
|
+
shared_examples_for "Trole API" do
|
7
|
+
include UserSetup
|
8
|
+
|
9
|
+
it_behaves_like "Trole Core API" do
|
10
|
+
define_users
|
11
|
+
end
|
12
|
+
|
13
|
+
it_behaves_like "Trole Cache API" do
|
14
|
+
define_users
|
15
|
+
end
|
16
|
+
|
17
|
+
it_behaves_like "Trole Event API" do
|
18
|
+
define_users
|
19
|
+
end
|
20
|
+
|
21
|
+
it_behaves_like "Trole Read API" do
|
22
|
+
define_users
|
23
|
+
end
|
24
|
+
|
25
|
+
it_behaves_like "Trole Validation API" do
|
26
|
+
define_users
|
27
|
+
end
|
28
|
+
|
29
|
+
it_behaves_like "Trole Write API" do
|
30
|
+
define_users
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# All strategies EXCEPT :bit_one strategy
|
35
|
+
|
36
|
+
shared_examples_for "Trole API for multiple roles" do
|
37
|
+
include UserSetup
|
38
|
+
|
39
|
+
it_behaves_like "Trole Write API for multiple roles" do
|
40
|
+
define_users
|
41
|
+
end
|
42
|
+
|
43
|
+
it_behaves_like "Trole Operations API for multiple roles" do
|
44
|
+
define_users
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Specs for :bit_one strategy
|
49
|
+
|
50
|
+
shared_examples_for "Trole API for two roles :bit_one" do
|
51
|
+
include UserSetup
|
52
|
+
|
53
|
+
it_behaves_like "Trole Write API for two roles :bit_one" do
|
54
|
+
define_users
|
55
|
+
end
|
56
|
+
|
57
|
+
it_behaves_like "Trole Operations API for two roles :bit_one" do
|
58
|
+
define_users
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
shared_examples_for "Trole Write API for multiple roles" do
|
2
|
+
describe '#add_roles' do
|
3
|
+
it "should be that after adding :admin role, he has that role" do
|
4
|
+
lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
5
|
+
user.has_role?(:admin).should be_false
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should be that after adding :admin role, he has that role" do
|
9
|
+
user.clear_roles!
|
10
|
+
expect { user.add_roles(:admin) }.to change{ user.role_value }
|
11
|
+
user.has_role?(:admin).should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should invalidate the role list after roles are changed' do
|
15
|
+
# expect roles changed event
|
16
|
+
lambda { user.set_roles(:blip) }.should raise_error # invalid role
|
17
|
+
user.set_role :editor # editor should be a valid role
|
18
|
+
expect { user.role_list }.to change{user.instance_variable_get "@role_list"}
|
19
|
+
user.role_name.should == :editor
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#clear_roles!' do
|
24
|
+
it "should clear all roles and invalidate roles cache" do
|
25
|
+
user.set_roles :admin, :editor, :blogger
|
26
|
+
user.clear_roles!
|
27
|
+
expect { user.role_list }.to change{user.role_list_value }
|
28
|
+
user.role_list.should be_empty
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#set_roles' do
|
33
|
+
it "should set multiple valid roles" do
|
34
|
+
user.set_roles :admin, :editor, :blogger
|
35
|
+
user.has_role?(:admin).should be_true
|
36
|
+
user.has_role?(:user).should be_false
|
37
|
+
user.has_any_role?(:admin, :editor).should be_true
|
38
|
+
user.has_any_role?(:admin, :user).should be_false
|
39
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should only set valid roles" do
|
43
|
+
user.set_roles :admin, nil, :hello, :editor, :blogger, :admin, nil, 'hello'
|
44
|
+
user.has_role?(:admin).should be_true
|
45
|
+
user.has_role?(:user).should be_false
|
46
|
+
user.has_any_role?(:admin, :editor).should be_true
|
47
|
+
user.has_any_role?(:admin, :user).should be_false
|
48
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe '#remove_roles' do
|
53
|
+
it "should remove multiple valid roles and invalidate roles cache" do
|
54
|
+
user.set_roles :admin, :editor, :blogger, :user
|
55
|
+
user.remove_roles :admin, :blogger
|
56
|
+
expect { user.role_list }.to change{user.role_list_value}
|
57
|
+
|
58
|
+
user.has_any_role?(:admin, :blogger).should be_false
|
59
|
+
user.has_any_role?(:admin, :editor).should be_true
|
60
|
+
user.has_any_role?(:blogger, :user).should be_true
|
61
|
+
|
62
|
+
user.has_all_roles?(:admin, :blogger).should be_false
|
63
|
+
user.has_all_roles?(:editor).should be_true
|
64
|
+
user.has_role?(:editor).should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should not remove :admin role twice if duplicated in roles argument" do
|
68
|
+
user.set_roles :admin, :editor, :blogger
|
69
|
+
user.remove_roles :admin, :admin
|
70
|
+
expect { user.role_list }.to change{user.role_list_value}
|
71
|
+
user.has_role?(:admin).should be_false
|
72
|
+
user.roles.list.should == [:editor, :blogger]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#static_role!' do
|
77
|
+
pending 'TODO'
|
78
|
+
|
79
|
+
it "should set set roles to :user only" do
|
80
|
+
user.static_role!(:guest)
|
81
|
+
user.has_role?(:guest).should be_true
|
82
|
+
user.valid_roles.should include(:guest)
|
83
|
+
lambda { user.set_roles(:admin) }.should raise_error
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
shared_examples_for "Trole Operations API for multiple roles" do
|
89
|
+
describe '#clear!' do
|
90
|
+
it "should clear all roles and invalidate roles cache" do
|
91
|
+
user.set_roles :admin
|
92
|
+
user.roles << [:admin, :editor, :blogger]
|
93
|
+
user.roles.list.should == [:admin, :editor, :blogger]
|
94
|
+
user.roles.clear!
|
95
|
+
expect { user.role_list }.to change{user.role_list_value }
|
96
|
+
user.role_list.should be_empty
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#+' do
|
101
|
+
it "should add multiple valid roles" do
|
102
|
+
user.set_roles :admin
|
103
|
+
user.roles + [:admin, :editor, :blogger]
|
104
|
+
|
105
|
+
user.roles.list.should == [:admin, :editor, :blogger]
|
106
|
+
user.has_role?(:admin).should be_true
|
107
|
+
user.has_role?(:user).should be_false
|
108
|
+
|
109
|
+
user.has_any_role?(:admin, :editor).should be_true
|
110
|
+
|
111
|
+
user.has_all_roles?(:admin, :user).should be_false
|
112
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should only set valid roles" do
|
116
|
+
user.set_roles :admin
|
117
|
+
user.roles + [:admin, nil, :hello, :editor, :blogger, :admin, nil, 'hello']
|
118
|
+
|
119
|
+
user.has_role?(:hello).should be_false
|
120
|
+
user.has_role?(:admin).should be_true
|
121
|
+
user.has_role?(:user).should be_false
|
122
|
+
|
123
|
+
user.has_any_role?(:admin, :editor).should be_true
|
124
|
+
user.has_any_role?(:admin, :user).should be_false
|
125
|
+
|
126
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe '#-' do
|
131
|
+
it "should remove multiple valid roles and invalidate roles cache" do
|
132
|
+
user.set_roles :admin, :blogger, :editor, :user
|
133
|
+
user.roles - [:admin, :blogger]
|
134
|
+
expect { user.role_list }.to change{user.role_list_value}
|
135
|
+
user.has_any_role?(:admin, :blogger).should be_false
|
136
|
+
user.has_any_role?(:admin, :editor).should be_true
|
137
|
+
user.has_any_role?(:blogger, :user).should be_true
|
138
|
+
|
139
|
+
user.has_all_roles?(:admin, :blogger).should be_false
|
140
|
+
user.has_all_roles?(:editor).should be_true
|
141
|
+
user.has_role?(:editor).should be_true
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should not remove :admin role twice if duplicated in roles argument" do
|
145
|
+
user.set_roles :admin, :editor, :blogger
|
146
|
+
user.remove_roles :admin, :editor, :admin, nil, :blip
|
147
|
+
expect { user.role_list }.to change{user.role_list_value}
|
148
|
+
user.has_role?(:admin).should be_false
|
149
|
+
user.roles.list.should == [:blogger]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe '#static_role!' do
|
154
|
+
pending 'TODO'
|
155
|
+
|
156
|
+
it "should set set roles to :user only" do
|
157
|
+
user.static_role!(:guest)
|
158
|
+
user.has_role?(:guest).should be_true
|
159
|
+
user.valid_roles.should include(:guest)
|
160
|
+
lambda { user.set_roles(:admin) }.should raise_error
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# module Troles
|
2
|
+
# module ReadOperations
|
3
|
+
# # check if the roles list has the given role
|
4
|
+
# def contains? role
|
5
|
+
# list.include? role
|
6
|
+
# end
|
7
|
+
# alias_method :includes?, :contains?
|
8
|
+
#
|
9
|
+
# # symbol list
|
10
|
+
# def list
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# # filter list of symbols
|
14
|
+
# # on whether they are present as roles
|
15
|
+
# def get *roles
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
# end
|
File without changes
|
@@ -0,0 +1,107 @@
|
|
1
|
+
shared_examples_for "Using a :single role strategy for Kris" do
|
2
|
+
subject { kris }
|
3
|
+
its(:role) { should be_a Trole::Operations }
|
4
|
+
|
5
|
+
#
|
6
|
+
# specify { kris.has_role?(:user).should be_true }
|
7
|
+
# specify { kris.is_role?(:user).should be_true }
|
8
|
+
#
|
9
|
+
# specify { kris.has_all_roles?(:user).should be_true }
|
10
|
+
# specify { kris.has_all_roles?(:user, :admin).should be_false }
|
11
|
+
#
|
12
|
+
# specify { kris.has_any_role?(:user, :admin).should be_true }
|
13
|
+
# specify { kris.has_any_role?(:admin).should be_false }
|
14
|
+
|
15
|
+
# # Store Api
|
16
|
+
# specify do
|
17
|
+
# kris.set_role(:admin)
|
18
|
+
# kris.has_role?(:admin).should be_true
|
19
|
+
# kris.has_role?(:user).should be_false
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# specify do
|
23
|
+
# kris.set_roles(:admin)
|
24
|
+
# kris.has_role?(:admin).should be_true
|
25
|
+
# kris.has_role?(:user).should be_false
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# specify do
|
29
|
+
# expect { kris.clear_roles! }.to change{kris.instance_variable_get "@trole"}
|
30
|
+
# kris.role_list.should be_empty
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# # Write Api
|
34
|
+
# specify do
|
35
|
+
# expect { kris.add_roles(:admin) }.to change{kris.instance_variable_get "@trole"}
|
36
|
+
# expect { kris.remove_roles(:user) }.to change{kris.instance_variable_get "@trole"}
|
37
|
+
# kris.has_role?(:admin).should be_true
|
38
|
+
# kris.has_role?(:user).should be_false
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# # # roles - operator
|
42
|
+
# specify { lambda { kris.add_roles(:admin, :blogger) }.should raise_error }
|
43
|
+
#
|
44
|
+
# specify do
|
45
|
+
# expect { kris.add_role(:admin) }.to change{kris.instance_variable_get "@trole"}
|
46
|
+
# expect { kris.remove_role(:user) }.to change{kris.instance_variable_get "@trole"}
|
47
|
+
# kris.has_role?(:admin).should be_true
|
48
|
+
# kris.has_role?(:user).should be_false
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# # # roles Operations object
|
52
|
+
# specify do
|
53
|
+
# expect { kris.roles.clear! }.to change{kris.instance_variable_get "@trole"}
|
54
|
+
# kris.roles.list.should be_empty
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# specify do
|
58
|
+
# expect { kris.role.clear! }.to change{kris.instance_variable_get "@trole"}
|
59
|
+
# kris.roles.list.should be_empty
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# # # roles + operator
|
63
|
+
# specify do
|
64
|
+
# expect { kris.roles + :admin }.to change{kris.instance_variable_get "@trole"}
|
65
|
+
# kris.roles.contains?(:admin).should be_true
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# # # roles + operator
|
69
|
+
# specify do
|
70
|
+
# expect { kris.roles - :admin }.to change{kris.instance_variable_get "@trole"}
|
71
|
+
# kris.roles.contains?(:admin).should be_true
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# specify do
|
75
|
+
# expect { kris.roles - [:admin, :blogger] }.to change{kris.instance_variable_get "@trole"}
|
76
|
+
# kris.roles.contains?(:admin).should be_true
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# # # roles << operator
|
80
|
+
# specify do
|
81
|
+
# kris.clear_roles!
|
82
|
+
# expect { kris.roles << :admin }.to change{kris.instance_variable_get "@trole"}
|
83
|
+
# kris.roles.contains?(:admin).should be_true
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# specify do
|
87
|
+
# kris.clear_roles!
|
88
|
+
# expect { kris.roles << [:admin, :blogger] }.to change{kris.instance_variable_get "@trole"}
|
89
|
+
# kris.roles.contains?(:admin).should be_true
|
90
|
+
# end
|
91
|
+
end
|
92
|
+
|
93
|
+
shared_examples_for "a Many Single for Susan" do
|
94
|
+
subject { susan }
|
95
|
+
its(:name) { should == 'susan' }
|
96
|
+
|
97
|
+
# Core API
|
98
|
+
its(:role_list) { should include(:admin) }
|
99
|
+
end
|
100
|
+
|
101
|
+
shared_examples_for "a Many strategy for Jack" do
|
102
|
+
subject { jack }
|
103
|
+
its(:name) { should == 'jack' }
|
104
|
+
|
105
|
+
# Core API
|
106
|
+
its(:role_list) { should include(:user, :admin) }
|
107
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'trole/strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :bit_one, :generic => true do |c|
|
4
|
+
# c.valid_roles = [:user, :admin]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
module UserSetup
|
8
|
+
def create_user
|
9
|
+
Factory.create :user, :name => 'user', :trole => false
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_admin_user
|
13
|
+
Factory.create :user, :name => 'admin', :trole => true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'Trole strategy bit_one' do
|
18
|
+
it_behaves_like "Common API"
|
19
|
+
it_behaves_like "Trole API for two roles :bit_one"
|
20
|
+
|
21
|
+
it_behaves_like "Trole API"
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :embed_one do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :editor, :blogger]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
Config.add_roles [:user, :admin, :editor, :blogger]
|
8
|
+
|
9
|
+
module UserSetup
|
10
|
+
def create_role name
|
11
|
+
Role.create(name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_no_roles
|
15
|
+
Factory.create :user, :name => 'no roles', :trole => [ ]
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_user
|
19
|
+
Factory.create :user, :name => 'user', :trole => [ create_role(:user) ]
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_admin_user
|
23
|
+
Factory.create :user, :name => 'admin', :trole => [ create_role(:admin) ]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'Trole strategy :embed_one' do
|
28
|
+
it_behaves_like "Common API"
|
29
|
+
it_behaves_like "Common API for multiple roles"
|
30
|
+
|
31
|
+
it_behaves_like "Trole API"
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :ref_one do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :editor, :blogger]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
Config.add_roles [:user, :admin, :editor, :blogger]
|
8
|
+
|
9
|
+
module UserSetup
|
10
|
+
def create_no_roles
|
11
|
+
Factory.create :user, :name => 'no roles', :trole => [ ]
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_user
|
15
|
+
Factory.create :user, :name => 'user', :trole => [ Role.where(:name => :user) ]
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_admin_user
|
19
|
+
Factory.create :user, :name => 'admin', :trole => [ Role.where(:name => :admin) ]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'Trole strategy ref_one' do
|
24
|
+
it_behaves_like "Common API"
|
25
|
+
it_behaves_like "Common API for multiple roles"
|
26
|
+
|
27
|
+
it_behaves_like "Trole API"
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :string_one do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :editor, :blogger]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
module UserSetup
|
8
|
+
def create_no_roles
|
9
|
+
Factory.create :user, :name => 'no roles', :trole => ''
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_user
|
13
|
+
Factory.create :user, :name => 'user', :trole => 'user'
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_admin_user
|
17
|
+
Factory.create :user, :name => 'admin', :trole => 'admin'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'Trole strategy string_one' do
|
22
|
+
it_behaves_like "Common API"
|
23
|
+
it_behaves_like "Common API for multiple roles"
|
24
|
+
|
25
|
+
it_behaves_like "Trole API"
|
26
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
shared_examples_for "Trole Write API for two roles :bit_one" do
|
2
|
+
describe '#add_role' do
|
3
|
+
it "should be that after adding :admin role, he has that role" do
|
4
|
+
# lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
5
|
+
user.add_role(:admin)
|
6
|
+
user.has_role?(:admin).should be_false
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#add_roles' do
|
11
|
+
it "should be that after adding :admin role, he has that role" do
|
12
|
+
# lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
13
|
+
user.add_roles(:admin)
|
14
|
+
user.has_role?(:admin).should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be that after adding :admin role, he has that role" do
|
18
|
+
user.clear_roles!
|
19
|
+
expect { user.add_roles(:admin) }.to_not change{ user.role_value }
|
20
|
+
user.has_role?(:admin).should be_false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#set_role' do
|
25
|
+
it 'should invalidate the role list after roles are changed' do
|
26
|
+
user.set_role :admin # editor should be a valid role
|
27
|
+
expect { user.role_list }.to change{user.instance_variable_get "@role_list"}
|
28
|
+
user.role_name.should == :admin
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#set_roles' do
|
33
|
+
it 'should invalidate the role list after roles are removed' do
|
34
|
+
user.set_role :admin # editor should be a valid role
|
35
|
+
user.clear_roles!
|
36
|
+
expect { user.role_list }.to change{user.instance_variable_get "@role_list"}
|
37
|
+
user.role_name.should == :user
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#set_roles' do
|
42
|
+
it 'should invalidate the role list after roles are changed' do
|
43
|
+
user.set_roles :admin # editor should be a valid role
|
44
|
+
expect { user.role_list }.to change{user.instance_variable_get "@role_list"}
|
45
|
+
user.role_name.should == :admin
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
shared_examples_for "Trole Operations API for two roles :bit_one" do
|
51
|
+
describe '#+' do
|
52
|
+
it "should be that trying to add :admin role, he will not be given that role" do
|
53
|
+
# lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
54
|
+
user.roles + [:admin]
|
55
|
+
user.has_role?(:admin).should be_false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#-' do
|
60
|
+
it "should be that removing :admin role (true) sets him back to :user role (false)" do
|
61
|
+
# lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
62
|
+
user.set_roles :admin
|
63
|
+
user.roles - [:admin]
|
64
|
+
user.has_role?(:admin).should be_false
|
65
|
+
user.has_role?(:user).should be_true
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be that removing :user role (true) from user with that role, sets him back to :user role (false) anyways!" do
|
69
|
+
# lambda { user.add_roles(:admin) }.should raise_error # since it already has :user role and only allows one role!
|
70
|
+
user.set_roles :user
|
71
|
+
user.roles - [:user]
|
72
|
+
user.has_role?(:admin).should be_false
|
73
|
+
user.has_role?(:user).should be_true
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/trole_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'trole'
|
2
|
+
require 'trole/macros'
|
3
|
+
require 'troles'
|
4
|
+
|
5
|
+
require 'troles/common/api_spec' # Common API examples
|
6
|
+
require 'trole/api_spec' # Trole API examples
|
7
|
+
|
8
|
+
describe Trole do
|
9
|
+
it "should be a module" do
|
10
|
+
Trole.should be_a(Module)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'require_all'
|
3
|
+
require 'factory_girl'
|
4
|
+
require 'mocha'
|
5
|
+
require 'factories'
|
6
|
+
require 'support/shared_examples'
|
7
|
+
|
8
|
+
module Config
|
9
|
+
def self.create_role name
|
10
|
+
Role.create(:name => name.to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.add_roles *names
|
14
|
+
names.flatten.each {|n| create_role(n) }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
RSpec.configure do |config|
|
19
|
+
config.mock_with :mocha
|
20
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_all File.dirname(__FILE__) + '/api'
|
2
|
+
|
3
|
+
shared_examples_for "Troles API" do
|
4
|
+
include UserSetup
|
5
|
+
|
6
|
+
it_behaves_like "Troles Core API" do
|
7
|
+
let(:no_roles_user) { create_no_roles_user }
|
8
|
+
let(:user) { create_user }
|
9
|
+
let(:admin_user) { create_admin_user }
|
10
|
+
end
|
11
|
+
|
12
|
+
it_behaves_like "Troles Cache API" do
|
13
|
+
let(:no_roles_user) { create_no_roles_user }
|
14
|
+
let(:user) { create_user }
|
15
|
+
let(:admin_user) { create_admin_user }
|
16
|
+
end
|
17
|
+
|
18
|
+
it_behaves_like "Troles Event API" do
|
19
|
+
let(:no_roles_user) { create_no_roles_user }
|
20
|
+
let(:user) { create_user }
|
21
|
+
let(:admin_user) { create_admin_user }
|
22
|
+
end
|
23
|
+
|
24
|
+
it_behaves_like "Troles Read API" do
|
25
|
+
let(:no_roles_user) { create_no_roles_user }
|
26
|
+
let(:user) { create_user }
|
27
|
+
let(:admin_user) { create_admin_user }
|
28
|
+
end
|
29
|
+
|
30
|
+
it_behaves_like "Troles Validation API" do
|
31
|
+
let(:no_roles_user) { create_no_roles_user }
|
32
|
+
let(:user) { create_user }
|
33
|
+
let(:admin_user) { create_admin_user }
|
34
|
+
end
|
35
|
+
|
36
|
+
it_behaves_like "Troles Write API" do
|
37
|
+
let(:no_roles_user) { create_no_roles_user }
|
38
|
+
let(:user) { create_user }
|
39
|
+
let(:admin_user) { create_admin_user }
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
shared_examples_for "Common Cache API" do
|
2
|
+
describe '#invalidate_role_cache!' do
|
3
|
+
before :each do
|
4
|
+
user.role_list
|
5
|
+
user.invalidate_role_cache!
|
6
|
+
end
|
7
|
+
|
8
|
+
# should invalidate role cache, forcing update of @role_list instance var
|
9
|
+
expect { user.role_list }.to change { user.instance_variable_get "@role_list" }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# specify { user.role_list.should == [:admin, :blogger] }
|
14
|
+
# it { should be_valid }
|
15
|
+
# its(:errors) { should be_empty }
|
16
|
+
# ################################################################################
|
17
|
+
# # specify (generated descriptions)
|
18
|
+
# # instead of repeating expectation with: it "should not be published"...)
|
19
|
+
# specify { blog_post.should_not be_published }
|
20
|
+
#
|
21
|
+
# ################################################################################
|
22
|
+
# # subject (delegate should/should_not to subject)
|
23
|
+
# subject { BlogPost.new :title => 'foo', :body => 'bar' }
|
24
|
+
# it "sets published timestamp" do
|
25
|
+
# subject.publish!
|
26
|
+
# subject.should be_published
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# it { should be_valid }
|
30
|
+
# its(:errors) { should be_empty }
|
31
|
+
# its(:title) { should == 'foo' }
|
File without changes
|