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,14 @@
|
|
1
|
+
shared_examples_for "Common Core API" do
|
2
|
+
# Core API
|
3
|
+
specify { lambda { user.role_field }.should raise_error } # no, role_field is a class method
|
4
|
+
specify { User.role_field.should_not be_nil } # yes, role_field is a class method
|
5
|
+
|
6
|
+
subject { user }
|
7
|
+
its(:role_list) { should include(:user) }
|
8
|
+
its(:roles) { should be_a Troles::Operations }
|
9
|
+
|
10
|
+
specify { user.static_roles?.should be_false }
|
11
|
+
specify { User.static_roles?.should be_false }
|
12
|
+
|
13
|
+
# TODO: Add examples with other users?
|
14
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
shared_examples_for "Common Event API" do
|
2
|
+
# Event API
|
3
|
+
subject { user }
|
4
|
+
its(:event_manager) { should == Troles::Common::EventManager }
|
5
|
+
|
6
|
+
specify { user.role_field_changed?(:roles).should be_false } # using dummy method on User
|
7
|
+
specify { user.publish_change(:roles).should be_true }
|
8
|
+
specify { user.update_roles.should be_false } # ?
|
9
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
shared_examples_for "Common Operations API" do
|
2
|
+
describe '#list' do
|
3
|
+
it 'should clear the roles on clear!' do
|
4
|
+
user.clear_roles!
|
5
|
+
user.add_roles(:admin, :blogger)
|
6
|
+
user.role_list.should include(:admin, :blogger)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#clear!' do
|
11
|
+
it 'should clear the roles on clear!' do
|
12
|
+
user.roles.clear!
|
13
|
+
expect { user.role_list }.to change{user.role_list_value }
|
14
|
+
user.role_list.should be_empty
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#- operator' do
|
19
|
+
it 'should remove the :admin role' do
|
20
|
+
user.clear_roles!
|
21
|
+
user.add_roles(:admin, :blogger)
|
22
|
+
user.roles - :admin
|
23
|
+
expect { user.role_list }.to change{user.role_list_value }
|
24
|
+
|
25
|
+
user.has_role?(:blogger).should be_true
|
26
|
+
user.has_role?(:admin).should be_false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#+ operator' do
|
31
|
+
it 'should add the :admin role' do
|
32
|
+
user.clear_roles!
|
33
|
+
user.roles + :admin
|
34
|
+
expect { user.role_list }.to change{user.role_list_value }
|
35
|
+
user.has_role?(:admin).should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#<< operator' do
|
40
|
+
it 'should add the :admin role' do
|
41
|
+
user.clear_roles!
|
42
|
+
user.roles << :admin
|
43
|
+
expect { user.role_list }.to change{user.role_list_value }
|
44
|
+
user.has_role?(:admin).should be_true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'acts as Enumerable' do
|
49
|
+
it 'should enable include?' do
|
50
|
+
user.clear_roles!
|
51
|
+
user.roles << :admin
|
52
|
+
user.roles.include?(:admin).should be_true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
shared_examples_for "Common Read API" do
|
2
|
+
specify { user.has_role?(:user).should be_true }
|
3
|
+
specify { user.is_role?(:user).should be_true }
|
4
|
+
|
5
|
+
specify { user.has_roles?(:user).should be_true }
|
6
|
+
specify { user.has_roles?(:user, :admin).should be_false }
|
7
|
+
|
8
|
+
specify { user.has_any_role?(:user, :admin).should be_true }
|
9
|
+
specify { user.has_any_role?(:admin).should be_false }
|
10
|
+
|
11
|
+
subject { user }
|
12
|
+
its(:role_list) { should include(:user) }
|
13
|
+
|
14
|
+
it 'should cache the role list' do
|
15
|
+
# use mock expectation : don't expect strategy to be called!
|
16
|
+
user.role_list.should include(:user)
|
17
|
+
user.expects(:strategy).never
|
18
|
+
|
19
|
+
# calling role_list multiple times should NOT invalidate the cache :)
|
20
|
+
expect { user.role_list }.to_not change{user.instance_variable_get "@role_list"}
|
21
|
+
user.role_list.should include(:user)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
shared_examples_for "Common Validation API" do
|
2
|
+
describe '#check_valid_role?' do
|
3
|
+
it 'should return valid role' do
|
4
|
+
user.check_valid_role?(:admin).should == :admin
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
# @return [Array<Symbol>] returns the valid roles or empty list if no valid roles
|
9
|
+
describe '#check_valid_roles?' do
|
10
|
+
it 'should return valid roles' do
|
11
|
+
user.check_valid_roles?(:admin, :editor).should include(:admin)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# @return [Symbol, false, Error] a valid role name, false if invalid, or Error on some error
|
16
|
+
describe '#make_valid_role?' do
|
17
|
+
it 'should return valid role' do
|
18
|
+
user.send :make_valid_role, :admin
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Ensures the role are valid
|
23
|
+
# @param [Symbol] list of roles
|
24
|
+
# @return [Array<Symbol>] the valid roles from the list of roles given
|
25
|
+
describe '#make_valid_roles?' do
|
26
|
+
it 'should return valid roles' do
|
27
|
+
user.send :make_valid_roles, [:admin, :editor]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# @return [Array<Symbol>] the valid roles of the role subject
|
32
|
+
describe '#valid_roles' do
|
33
|
+
it 'should get valid roles of class' do
|
34
|
+
user.send(:valid_roles).should include(:admin)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'Class Methods' do
|
39
|
+
# @return [Array<Symbol>] the valid roles of the role subject
|
40
|
+
describe '#valid_roles' do
|
41
|
+
it 'should set not valid roles again!' do
|
42
|
+
user.class.valid_roles.should include(:admin)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
shared_examples_for "Common Write API" do
|
2
|
+
describe '#clear_roles!' do
|
3
|
+
it "should clear roles and invalidate roles cache" do
|
4
|
+
user.set_roles :admin
|
5
|
+
user.clear_roles!
|
6
|
+
expect { user.role_list }.to change{user.role_list_value }
|
7
|
+
user.role_list.should be_empty
|
8
|
+
end
|
9
|
+
|
10
|
+
it "successive clear roles should not invalidate roles cache" do
|
11
|
+
user.set_roles :admin
|
12
|
+
user.clear_roles!
|
13
|
+
expect { user.role_list }.to change{user.role_list_value }
|
14
|
+
user.clear_roles!
|
15
|
+
expect { user.role_list }.to_not change{user.role_list_value }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#set_roles' do
|
20
|
+
it "should set roles to :admin only" do
|
21
|
+
user.set_roles(:admin)
|
22
|
+
user.has_role?(:admin).should be_true
|
23
|
+
user.has_role?(:user).should be_false
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should invalidate the role list after roles are changed' do
|
27
|
+
user.clear_roles!
|
28
|
+
user.set_roles :admin
|
29
|
+
expect { user.role_list }.to change{user.role_list_value}
|
30
|
+
user.has_role?(:admin).should be_true
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should not set invalid role and thus not invalidate the role list' do
|
34
|
+
# lambda { user.set_roles(:blip) }.should raise_error # invalid role
|
35
|
+
user.set_roles(:blip).should be_false
|
36
|
+
expect { user.role_list }.to_not change{user.role_list_value}
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should set roles to one :admin only if duplicated in roles argument" do
|
40
|
+
user.set_roles(:admin, :admin)
|
41
|
+
expect { user.role_list }.to change{user.role_list_value}
|
42
|
+
user.has_role?(:admin).should be_false
|
43
|
+
user.role_list.should == [:admin]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
describe '#remove_roles' do
|
49
|
+
it "should not remove :blip role and thus not invalidate roles cache" do
|
50
|
+
user.remove_roles :blip
|
51
|
+
expect { user.role_list }.to_not change{user.role_list_value}
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should not remove :admin role twice if duplicated in roles argument" do
|
55
|
+
user.remove_roles :admin, :admin
|
56
|
+
expect { user.role_list }.to change{user.role_list_value}
|
57
|
+
user.has_role?(:admin).should be_false
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should remove :admin role and invalidate roles cache" do
|
61
|
+
user.set_roles :admin
|
62
|
+
expect { user.role_list }.to change{user.role_list_value}
|
63
|
+
expect { user.role_list }.to_not change{user.role_list_value}
|
64
|
+
|
65
|
+
user.remove_roles(:admin).should be_false
|
66
|
+
expect { user.role_list }.to change{user.role_list_value}
|
67
|
+
user.has_role?(:admin).should be_false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#static_role!' do
|
72
|
+
pending 'TODO'
|
73
|
+
|
74
|
+
it "should set set roles to :user only" do
|
75
|
+
user.static_role!(:guest)
|
76
|
+
user.has_role?(:guest).should be_true
|
77
|
+
user.valid_roles.should include(:guest)
|
78
|
+
lambda { user.set_roles(:admin) }.should raise_error
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# See http://blog.davidchelimsky.net/2010/11/07/specifying-mixins-with-shared-example-groups-in-rspec-2/
|
2
|
+
# And: http://relishapp.com/rspec/rspec-core/v/2-6/dir/example-groups/shared-examples
|
3
|
+
# Also: http://stackoverflow.com/questions/6152359/dynamically-generating-shared-examples-in-rspec-2
|
4
|
+
|
5
|
+
require_all File.dirname(__FILE__) + '/api'
|
6
|
+
|
7
|
+
require 'troles/common/multi_roles_spec'
|
8
|
+
|
9
|
+
def define_users
|
10
|
+
let(:no_roles_user) { create_no_roles_user }
|
11
|
+
let(:user) { create_user }
|
12
|
+
let(:admin_user) { create_admin_user }
|
13
|
+
end
|
14
|
+
|
15
|
+
shared_examples_for "Common API" do
|
16
|
+
include UserSetup
|
17
|
+
|
18
|
+
it_behaves_like "Common Core API" do
|
19
|
+
define_users
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# it_behaves_like "Common Event API" do
|
24
|
+
# define_users
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# it_behaves_like "Common Read API" do
|
28
|
+
# define_users
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# it_behaves_like "Common Validation API" do
|
32
|
+
# define_users
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# it_behaves_like "Common Write API" do
|
36
|
+
# define_users
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# it_behaves_like "Common Operations API" do
|
40
|
+
# define_users
|
41
|
+
# end
|
42
|
+
end
|
43
|
+
|
44
|
+
shared_examples_for "Common API for multiple roles" do
|
45
|
+
include UserSetup
|
46
|
+
|
47
|
+
it_behaves_like "Common Write API for multiple roles" do
|
48
|
+
define_users
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Customizing shared example groups
|
53
|
+
#
|
54
|
+
# Here are three techniques for customizing shared groups:
|
55
|
+
# Parameterization
|
56
|
+
#
|
57
|
+
# describe Host do
|
58
|
+
# it_should_behave_like M, Host.new
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# Here, the result of Host.new is passed to the shared group as a block parameter, making that value available at the group level (each example group is a class), and the instance level (each example runs in an instance of that class). So …
|
62
|
+
#
|
63
|
+
# shared_examples_for M do |host|
|
64
|
+
# it "can access #{host} in the docstring" do
|
65
|
+
# host.do_something # it can access the host _in_ the example
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
#
|
69
|
+
# Methods defined in host group
|
70
|
+
#
|
71
|
+
# describe Host do
|
72
|
+
# let(:foo) { Host.new }
|
73
|
+
# it_should_behave_like M
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# In this case, the foo() method defined by let() is inherited by the generated nested group, and available within any of the examples defined in the shared group.
|
77
|
+
#
|
78
|
+
# shared_examples_for M do
|
79
|
+
# it "does something" do
|
80
|
+
# foo
|
81
|
+
# end
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# NOTE that instance methods that are inherited like this are not available in the class scope of the generated example group, and are therefore not available for use in docstings:
|
85
|
+
#
|
86
|
+
# shared_examples_for M do
|
87
|
+
# it "does some #{foo}" do # this would raise an error
|
88
|
+
# # ...
|
89
|
+
# end
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# Methods defined in an extension block
|
93
|
+
#
|
94
|
+
# describe Host do
|
95
|
+
# it_should_behave_like M do
|
96
|
+
# let(:foo) { Host.new }
|
97
|
+
# end
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# The block passed to it_should_behave_like() is eval’d after the shared group is eval’d, allowing you to define default implementations of methods in the shared group. This means we can define groups that programmatically enforce rules for the host groups.
|
101
|
+
#
|
@@ -0,0 +1,11 @@
|
|
1
|
+
shared_examples_for "Troles Common Config" do
|
2
|
+
describe 'config settings' do
|
3
|
+
describe '#default_orm' do
|
4
|
+
before :each do
|
5
|
+
user.troles_config.orm.should == nil
|
6
|
+
Troles.Config.default_orm = :mongoid
|
7
|
+
user.troles_config.orm.should == :mongoid
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
shared_examples_for "Common Write API for multiple roles" do
|
2
|
+
describe '#clear_roles!' do
|
3
|
+
it "should clear all roles and invalidate roles cache" do
|
4
|
+
user.set_roles :admin, :editor, :blogger
|
5
|
+
user.clear_roles!
|
6
|
+
expect { user.role_list }.to change{user.role_list_value }
|
7
|
+
user.role_list.should be_empty
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#set_roles' do
|
12
|
+
it "should set multiple valid roles" do
|
13
|
+
user.set_roles :admin, :editor, :blogger
|
14
|
+
user.has_role?(:admin).should be_true
|
15
|
+
user.has_role?(:user).should be_false
|
16
|
+
user.has_any_role?(:admin, :editor).should be_true
|
17
|
+
user.has_any_role?(:admin, :user).should be_false
|
18
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should only set valid roles" do
|
22
|
+
user.set_roles :admin, nil, :hello, :editor, :blogger, :admin, nil, 'hello'
|
23
|
+
user.has_role?(:admin).should be_true
|
24
|
+
user.has_role?(:user).should be_false
|
25
|
+
user.has_any_role?(:admin, :editor).should be_true
|
26
|
+
user.has_any_role?(:admin, :user).should be_false
|
27
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#remove_roles' do
|
32
|
+
it "should remove multiple valid roles and invalidate roles cache" do
|
33
|
+
user.set_roles :admin, :editor, :blogger, :user
|
34
|
+
user.remove_roles :admin, :blogger
|
35
|
+
expect { user.role_list }.to change{user.role_list_value}
|
36
|
+
|
37
|
+
user.has_any_role?(:admin, :blogger).should be_false
|
38
|
+
user.has_any_role?(:admin, :editor).should be_true
|
39
|
+
user.has_any_role?(:blogger, :user).should be_true
|
40
|
+
|
41
|
+
user.has_all_roles?(:admin, :blogger).should be_false
|
42
|
+
user.has_all_roles?(:editor).should be_true
|
43
|
+
user.has_role?(:editor).should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not remove :admin role twice if duplicated in roles argument" do
|
47
|
+
user.set_roles :admin, :editor, :blogger
|
48
|
+
user.remove_roles :admin, :admin
|
49
|
+
expect { user.role_list }.to change{user.role_list_value}
|
50
|
+
user.has_role?(:admin).should be_false
|
51
|
+
user.roles.list.should == [:editor, :blogger]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#static_role!' do
|
56
|
+
pending 'TODO'
|
57
|
+
|
58
|
+
it "should set set roles to :user only" do
|
59
|
+
user.static_role!(:guest)
|
60
|
+
user.has_role?(:guest).should be_true
|
61
|
+
user.valid_roles.should include(:guest)
|
62
|
+
lambda { user.set_roles(:admin) }.should raise_error
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
shared_examples_for "Common Operations API for multiple roles" do
|
68
|
+
describe '#clear!' do
|
69
|
+
it "should clear all roles and invalidate roles cache" do
|
70
|
+
user.set_roles :admin
|
71
|
+
user.roles << [:admin, :editor, :blogger]
|
72
|
+
user.roles.list.should == [:admin, :editor, :blogger]
|
73
|
+
user.roles.clear!
|
74
|
+
expect { user.role_list }.to change{user.role_list_value }
|
75
|
+
user.role_list.should be_empty
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#+' do
|
80
|
+
it "should add multiple valid roles" do
|
81
|
+
user.set_roles :admin
|
82
|
+
user.roles + [:admin, :editor, :blogger]
|
83
|
+
|
84
|
+
user.roles.list.should == [:admin, :editor, :blogger]
|
85
|
+
user.has_role?(:admin).should be_true
|
86
|
+
user.has_role?(:user).should be_false
|
87
|
+
|
88
|
+
user.has_any_role?(:admin, :editor).should be_true
|
89
|
+
|
90
|
+
user.has_all_roles?(:admin, :user).should be_false
|
91
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should only set valid roles" do
|
95
|
+
user.set_roles :admin
|
96
|
+
user.roles + [:admin, nil, :hello, :editor, :blogger, :admin, nil, 'hello']
|
97
|
+
|
98
|
+
user.has_role?(:hello).should be_false
|
99
|
+
user.has_role?(:admin).should be_true
|
100
|
+
user.has_role?(:user).should be_false
|
101
|
+
|
102
|
+
user.has_any_role?(:admin, :editor).should be_true
|
103
|
+
user.has_any_role?(:admin, :user).should be_false
|
104
|
+
|
105
|
+
user.has_all_roles?(:admin, :editor, :blogger).should be_true
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#-' do
|
110
|
+
it "should remove multiple valid roles and invalidate roles cache" do
|
111
|
+
user.set_roles :admin, :blogger, :editor, :user
|
112
|
+
user.roles - [:admin, :blogger]
|
113
|
+
expect { user.role_list }.to change{user.role_list_value}
|
114
|
+
user.has_any_role?(:admin, :blogger).should be_false
|
115
|
+
user.has_any_role?(:admin, :editor).should be_true
|
116
|
+
user.has_any_role?(:blogger, :user).should be_true
|
117
|
+
|
118
|
+
user.has_all_roles?(:admin, :blogger).should be_false
|
119
|
+
user.has_all_roles?(:editor).should be_true
|
120
|
+
user.has_role?(:editor).should be_true
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should not remove :admin role twice if duplicated in roles argument" do
|
124
|
+
user.set_roles :admin, :editor, :blogger
|
125
|
+
user.remove_roles :admin, :editor, :admin, nil, :blip
|
126
|
+
expect { user.role_list }.to change{user.role_list_value}
|
127
|
+
user.has_role?(:admin).should be_false
|
128
|
+
user.roles.list.should == [:blogger]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#static_role!' do
|
133
|
+
pending 'TODO'
|
134
|
+
|
135
|
+
it "should set set roles to :user only" do
|
136
|
+
user.static_role!(:guest)
|
137
|
+
user.has_role?(:guest).should be_true
|
138
|
+
user.valid_roles.should include(:guest)
|
139
|
+
lambda { user.set_roles(:admin) }.should raise_error
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'troles_spec'
|
2
|
+
User.troles_strategy :bit_many
|
3
|
+
User.valid_roles = [:user, :admin, :blogger]
|
4
|
+
|
5
|
+
describe Troles::Marshaller::Bitmask do
|
6
|
+
let(:kris) { Factory.create :user, :troles => 4}
|
7
|
+
let(:bitmask) { Troles::Marshaller::Bitmask.new kris }
|
8
|
+
|
9
|
+
subject { bitmask }
|
10
|
+
specify { bitmask.read.should == [:blogger] }
|
11
|
+
specify { bitmask.write(:admin).should == 2 }
|
12
|
+
specify { bitmask.write(:user, :admin).should == 3 }
|
13
|
+
|
14
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,68 @@
|
|
1
|
+
shared_examples_for "a Many strategy for Kris" do
|
2
|
+
subject { kris }
|
3
|
+
its(:name) { should == 'kris' }
|
4
|
+
|
5
|
+
# Core API
|
6
|
+
specify { lambda { kris.role_field }.should raise_error } # is a class method
|
7
|
+
specify { User.role_field.should == :troles }
|
8
|
+
|
9
|
+
its(:roles) { should be_a Troles::Operations }
|
10
|
+
|
11
|
+
specify { kris.static_roles?.should be_false }
|
12
|
+
specify { User.static_roles?.should be_false }
|
13
|
+
|
14
|
+
# Event API
|
15
|
+
its(:event_manager) { should == Troles::EventManager }
|
16
|
+
specify { lambda { kris.role_field_changed?(:roles) }.should raise_error } # since not using rails here!
|
17
|
+
specify { kris.publish_change(:roles).should be_true }
|
18
|
+
specify { kris.update_roles.should be_false } # since not using rails
|
19
|
+
|
20
|
+
# Read API
|
21
|
+
its(:role_list) { should include(:user) }
|
22
|
+
|
23
|
+
it 'should cache the role list' do
|
24
|
+
# use mock expectation : don't expect strategy to be called!
|
25
|
+
kris.role_list.should include(:user)
|
26
|
+
kris.expects(:strategy).never
|
27
|
+
# calling role_list multiple times should NOT invalidate the cache :)
|
28
|
+
expect { kris.role_list }.to_not change{kris.instance_variable_get "@role_list"}
|
29
|
+
kris.role_list.should include(:user)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should invalidate the role list after roles are changed' do
|
33
|
+
# expect roles changed event
|
34
|
+
kris.expects(:invalidate_role_cache!)
|
35
|
+
kris.set_roles(:admin)
|
36
|
+
# expect cached role_list instance variable (cache) to be changed
|
37
|
+
expect { kris.role_list }.to change{kris.instance_variable_get "@role_list"}
|
38
|
+
kris.role_list.should include(:admin)
|
39
|
+
end
|
40
|
+
|
41
|
+
specify { kris.has_role?(:user).should be_true }
|
42
|
+
specify { kris.is_role?(:user).should be_true }
|
43
|
+
|
44
|
+
specify { kris.has_roles?(:user).should be_true }
|
45
|
+
specify { kris.has_roles?(:user, :admin).should be_false }
|
46
|
+
|
47
|
+
specify { kris.has_any_role?(:user, :admin).should be_true }
|
48
|
+
specify { kris.has_any_role?(:admin).should be_false }
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
shared_examples_for "a Many strategy for Susan" do
|
55
|
+
subject { susan }
|
56
|
+
its(:name) { should == 'susan' }
|
57
|
+
|
58
|
+
# Core API
|
59
|
+
its(:role_list) { should include(:admin) }
|
60
|
+
end
|
61
|
+
|
62
|
+
shared_examples_for "a Many strategy for Jack" do
|
63
|
+
subject { jack }
|
64
|
+
its(:name) { should == 'jack' }
|
65
|
+
|
66
|
+
# Core API
|
67
|
+
its(:role_list) { should include(:user, :admin) }
|
68
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :bit_many do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :blogger, :editor]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
module UserSetup
|
8
|
+
def create_no_roles_user
|
9
|
+
Factory.create :user, :name => 'no roles', :troles => 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_user
|
13
|
+
Factory.create :user, :name => 'normal', :troles => 1
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_admin_user
|
17
|
+
Factory.create :user, :name => 'admin', :troles => 2
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_complex_user
|
21
|
+
Factory.create :user, :name => 'user and admin', :troles => 3
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe 'Troles strategy bit_many' do
|
26
|
+
it_behaves_like "Common API"
|
27
|
+
it_behaves_like "Common API for multiple roles"
|
28
|
+
|
29
|
+
it_behaves_like "Troles API"
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :embed_many do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :blogger, :editor]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
|
8
|
+
module UserSetup
|
9
|
+
def create_role name
|
10
|
+
Role.create(name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_no_roles_user
|
14
|
+
Factory.create :user, :name => 'no roles', :troles => []
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_user
|
18
|
+
Factory.create :user, :name => 'normal', :troles => [ create_role(:user) ]
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_admin_user
|
22
|
+
Factory.create :user, :name => 'admin', :troles => [ create_role(:admin) ]
|
23
|
+
end
|
24
|
+
|
25
|
+
def create_complex_user
|
26
|
+
Factory.create :user, :name => 'user and admin', :troles => [ create_role(:name => :user), create_role(:name => :admin) ]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'Troles strategy embed_many' do
|
31
|
+
it_behaves_like "Common API"
|
32
|
+
it_behaves_like "Common API for multiple roles"
|
33
|
+
|
34
|
+
it_behaves_like "Troles API"
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'strategy_helper'
|
2
|
+
|
3
|
+
User.troles_strategy :ref_many do |c|
|
4
|
+
c.valid_roles = [:user, :admin, :blogger, :editor]
|
5
|
+
end.configure!
|
6
|
+
|
7
|
+
Config.add_roles [:user, :admin, :editor, :blogger]
|
8
|
+
|
9
|
+
module UserSetup
|
10
|
+
def find_role name
|
11
|
+
Role.where :name => name.to_sym
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_no_roles_user
|
15
|
+
Factory.create :user, :name => 'no roles', :troles => []
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_user
|
19
|
+
Factory.create :user, :name => 'normal', :troles => [ find_role(:user) ]
|
20
|
+
end
|
21
|
+
|
22
|
+
def create_admin_user
|
23
|
+
Factory.create :user, :name => 'admin', :troles => [ find_role(:admin) ]
|
24
|
+
end
|
25
|
+
|
26
|
+
def create_complex_user
|
27
|
+
Factory.create :user, :name => 'user and admin', :troles => [ find_role(:user), find_role(:admin) ]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'Troles strategy ref_many' do
|
32
|
+
it_behaves_like "Common API"
|
33
|
+
it_behaves_like "Common API for multiple roles"
|
34
|
+
|
35
|
+
it_behaves_like "Troles API"
|
36
|
+
end
|