rolify 3.1.0 → 4.0.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.
- checksums.yaml +7 -0
- data/.gitignore +4 -1
- data/.travis.yml +44 -11
- data/CHANGELOG.rdoc +70 -0
- data/CONTRIBUTORS +6 -0
- data/Gemfile +21 -2
- data/README.md +130 -68
- data/Rakefile +29 -4
- data/UPGRADE.rdoc +22 -0
- data/circle.yml +13 -0
- data/gemfiles/Gemfile.rails-3.2 +27 -0
- data/gemfiles/Gemfile.rails-4.0 +33 -0
- data/gemfiles/Gemfile.rails-4.1 +37 -0
- data/lib/generators/active_record/rolify_generator.rb +54 -0
- data/lib/generators/active_record/templates/README +8 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/mongoid/rolify_generator.rb +55 -0
- data/lib/generators/mongoid/templates/README-mongoid +4 -0
- data/lib/generators/rolify/rolify_generator.rb +35 -0
- data/lib/generators/rolify/{role/templates/README-mongoid → templates/README} +1 -5
- data/lib/generators/rolify/templates/initializer.rb +7 -0
- data/lib/generators/rolify/templates/role-active_record.rb +11 -0
- data/lib/generators/rolify/{role/templates → templates}/role-mongoid.rb +9 -9
- data/lib/generators/rolify/user_generator.rb +39 -0
- data/lib/rolify/adapters/active_record/resource_adapter.rb +36 -6
- data/lib/rolify/adapters/active_record/role_adapter.rb +25 -13
- data/lib/rolify/adapters/active_record/scopes.rb +27 -0
- data/lib/rolify/adapters/base.rb +9 -0
- data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -6
- data/lib/rolify/adapters/mongoid/role_adapter.rb +35 -11
- data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
- data/lib/rolify/configure.rb +36 -5
- data/lib/rolify/finders.rb +40 -0
- data/lib/rolify/matchers.rb +31 -0
- data/lib/rolify/railtie.rb +1 -1
- data/lib/rolify/resource.rb +19 -10
- data/lib/rolify/role.rb +33 -13
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +46 -20
- data/rolify.gemspec +18 -24
- data/spec/README.rdoc +24 -0
- data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +165 -0
- data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +113 -0
- data/spec/generators_helper.rb +27 -0
- data/spec/rolify/config_spec.rb +20 -21
- data/spec/rolify/custom_spec.rb +11 -6
- data/spec/rolify/matchers_spec.rb +24 -0
- data/spec/rolify/namespace_spec.rb +24 -0
- data/spec/rolify/resource_spec.rb +135 -37
- data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
- data/spec/rolify/role_spec.rb +8 -9
- data/spec/rolify/shared_contexts.rb +22 -3
- data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +10 -17
- data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
- data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +64 -27
- data/spec/rolify/shared_examples/shared_examples_for_finders.rb +81 -0
- data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +29 -29
- data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +35 -35
- data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +38 -38
- data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
- data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +28 -59
- data/spec/rolify/shared_examples/shared_examples_for_roles.rb +58 -32
- data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
- data/spec/spec_helper.rb +39 -3
- data/spec/support/adapters/active_record.rb +58 -7
- data/spec/support/adapters/mongoid.rb +110 -17
- data/spec/support/adapters/mongoid.yml +6 -0
- data/spec/support/data.rb +18 -15
- data/spec/support/schema.rb +31 -16
- data/spec/support/stream_helpers.rb +18 -0
- metadata +88 -84
- data/lib/generators/rolify/role/role_generator.rb +0 -41
- data/lib/generators/rolify/role/templates/README +0 -11
- data/lib/generators/rolify/role/templates/README-active_record +0 -21
- data/lib/generators/rolify/role/templates/initializer.rb +0 -7
- data/lib/generators/rolify/role/templates/migration.rb +0 -19
- data/lib/generators/rolify/role/templates/role-active_record.rb +0 -4
- data/spec/generators/rolify/role/role_generator_spec.rb +0 -197
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe "Resourcify and rolify on the same model" do
|
|
4
|
+
|
|
5
|
+
before(:all) do
|
|
6
|
+
reset_defaults
|
|
7
|
+
Role.delete_all
|
|
8
|
+
HumanResource.delete_all
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
let!(:user) do
|
|
12
|
+
user = HumanResource.new login: 'Samer'
|
|
13
|
+
user.save
|
|
14
|
+
user
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "should add the role to the user" do
|
|
18
|
+
expect { user.add_role :admin }.to change { user.roles.count }.by(1)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "should create a role to the roles collection" do
|
|
22
|
+
expect { user.add_role :moderator }.to change { Role.count }.by(1)
|
|
23
|
+
end
|
|
24
|
+
end
|
data/spec/rolify/role_spec.rb
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
require "spec_helper"
|
|
2
|
-
require "rolify/shared_examples/shared_examples_for_roles"
|
|
3
|
-
require "rolify/shared_examples/shared_examples_for_dynamic"
|
|
4
2
|
|
|
5
3
|
describe Rolify do
|
|
6
|
-
it_behaves_like Rolify::Role do
|
|
7
|
-
let(:user_class) { User }
|
|
8
|
-
let(:role_class) { Role }
|
|
9
|
-
end
|
|
10
4
|
|
|
11
|
-
|
|
12
|
-
let(:
|
|
13
|
-
|
|
5
|
+
context 'cache' do
|
|
6
|
+
let(:user) { User.first }
|
|
7
|
+
before { user.grant(:zombie) }
|
|
8
|
+
specify do
|
|
9
|
+
expect(user).to have_role(:zombie)
|
|
10
|
+
user.remove_role(:zombie)
|
|
11
|
+
expect(user).to_not have_role(:zombie)
|
|
12
|
+
end
|
|
14
13
|
end
|
|
15
14
|
end
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
shared_context "global role", :scope => :global do
|
|
2
2
|
subject { admin }
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
def admin
|
|
5
|
+
user_class.first
|
|
6
|
+
end
|
|
5
7
|
|
|
6
8
|
before(:all) do
|
|
7
9
|
load_roles
|
|
@@ -29,7 +31,9 @@ shared_context "class scoped role", :scope => :class do
|
|
|
29
31
|
create_other_roles
|
|
30
32
|
end
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
def manager
|
|
35
|
+
user_class.where(:login => "moderator").first
|
|
36
|
+
end
|
|
33
37
|
|
|
34
38
|
def load_roles
|
|
35
39
|
role_class.destroy_all
|
|
@@ -51,7 +55,9 @@ shared_context "instance scoped role", :scope => :instance do
|
|
|
51
55
|
create_other_roles
|
|
52
56
|
end
|
|
53
57
|
|
|
54
|
-
|
|
58
|
+
def moderator
|
|
59
|
+
user_class.where(:login => "god").first
|
|
60
|
+
end
|
|
55
61
|
|
|
56
62
|
def load_roles
|
|
57
63
|
role_class.destroy_all
|
|
@@ -63,6 +69,19 @@ shared_context "instance scoped role", :scope => :instance do
|
|
|
63
69
|
end
|
|
64
70
|
end
|
|
65
71
|
|
|
72
|
+
shared_context "mixed scoped roles", :scope => :mixed do
|
|
73
|
+
subject { user_class }
|
|
74
|
+
|
|
75
|
+
before(:all) do
|
|
76
|
+
role_class.destroy_all
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
let!(:root) { provision_user(user_class.first, [ :admin, :staff, [ :moderator, Group ], [ :visitor, Forum.last ] ]) }
|
|
80
|
+
let!(:modo) { provision_user(user_class.where(:login => "moderator").first, [[ :moderator, Forum ], [ :manager, Group ], [ :visitor, Group.first ]])}
|
|
81
|
+
let!(:visitor) { provision_user(user_class.last, [[ :visitor, Forum.last ]]) }
|
|
82
|
+
let!(:owner) { provision_user(user_class.first, [[:owner, Company.first]]) }
|
|
83
|
+
end
|
|
84
|
+
|
|
66
85
|
def create_other_roles
|
|
67
86
|
role_class.create :name => "superhero"
|
|
68
87
|
role_class.create :name => "admin", :resource_type => "Group"
|
|
@@ -10,11 +10,9 @@ shared_examples_for "#add_role_examples" do |param_name, param_method|
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
context "considering a new global role" do
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
its(:resource_type) { should be(nil) }
|
|
17
|
-
its(:resource_id) { should be(nil) }
|
|
13
|
+
it "creates a new class scoped role" do
|
|
14
|
+
expect(subject.add_role "expert".send(param_method)).to be_the_same_role("expert")
|
|
15
|
+
end
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
context "should not create another role" do
|
|
@@ -40,17 +38,15 @@ shared_examples_for "#add_role_examples" do |param_name, param_method|
|
|
|
40
38
|
end
|
|
41
39
|
|
|
42
40
|
context "considering a new class scoped role" do
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
its(:resource_type) { should eq(Forum.to_s) }
|
|
47
|
-
its(:resource_id) { should be(nil) }
|
|
41
|
+
it "creates a new class scoped role" do
|
|
42
|
+
expect(subject.add_role "boss".send(param_method), Forum).to be_the_same_role("boss", Forum)
|
|
43
|
+
end
|
|
48
44
|
end
|
|
49
45
|
|
|
50
46
|
context "should not create another role" do
|
|
51
47
|
it "if the role was already assigned to the user" do
|
|
52
48
|
subject.add_role "warrior".send(param_method), Forum
|
|
53
|
-
expect { subject.add_role "warrior".send(param_method), Forum }.not_to change { subject.roles.
|
|
49
|
+
expect { subject.add_role "warrior".send(param_method), Forum }.not_to change { subject.roles.count }
|
|
54
50
|
end
|
|
55
51
|
|
|
56
52
|
it "if already existing in the database" do
|
|
@@ -62,18 +58,15 @@ shared_examples_for "#add_role_examples" do |param_name, param_method|
|
|
|
62
58
|
|
|
63
59
|
context "with an instance scoped role", :scope => :instance do
|
|
64
60
|
it "should add the role to the user" do
|
|
65
|
-
expect { subject.add_role "visitor".send(param_method), Forum.last }.to change { subject.roles.
|
|
61
|
+
expect { subject.add_role "visitor".send(param_method), Forum.last }.to change { subject.roles.count }.by(1)
|
|
66
62
|
end
|
|
67
63
|
|
|
68
64
|
it "should create a role in the roles table" do
|
|
69
65
|
expect { subject.add_role "member".send(param_method), Forum.last }.to change { role_class.count }.by(1)
|
|
70
66
|
end
|
|
71
67
|
|
|
72
|
-
|
|
73
|
-
subject
|
|
74
|
-
|
|
75
|
-
its(:name) { should eq("member") }
|
|
76
|
-
its(:resource) { should eq(Forum.last) }
|
|
68
|
+
it "creates a new instance scoped role" do
|
|
69
|
+
expect(subject.add_role "mate".send(param_method), Forum.last).to be_the_same_role("mate", Forum.last)
|
|
77
70
|
end
|
|
78
71
|
|
|
79
72
|
context "should not create another role" do
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
shared_examples_for "Rolify.callbacks" do
|
|
2
|
+
before(:all) do
|
|
3
|
+
reset_defaults
|
|
4
|
+
Rolify.dynamic_shortcuts = false
|
|
5
|
+
role_class.destroy_all
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
after :each do
|
|
9
|
+
@user.roles.destroy_all
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "rolify association callbacks", :if => (Rolify.orm == "active_record") do
|
|
13
|
+
describe "before_add" do
|
|
14
|
+
it "should receive callback" do
|
|
15
|
+
rolify_options = { :role_cname => role_class.to_s, :before_add => :role_callback }
|
|
16
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
17
|
+
silence_warnings { user_class.rolify rolify_options }
|
|
18
|
+
@user = user_class.first
|
|
19
|
+
@user.stub(:role_callback)
|
|
20
|
+
@user.should_receive(:role_callback)
|
|
21
|
+
@user.add_role :admin
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
describe "after_add" do
|
|
26
|
+
it "should receive callback" do
|
|
27
|
+
rolify_options = { :role_cname => role_class.to_s, :after_add => :role_callback }
|
|
28
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
29
|
+
silence_warnings { user_class.rolify rolify_options }
|
|
30
|
+
@user = user_class.first
|
|
31
|
+
@user.stub(:role_callback)
|
|
32
|
+
@user.should_receive(:role_callback)
|
|
33
|
+
@user.add_role :admin
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
describe "before_remove" do
|
|
38
|
+
it "should receive callback" do
|
|
39
|
+
rolify_options = { :role_cname => role_class.to_s, :before_remove => :role_callback }
|
|
40
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
41
|
+
silence_warnings { user_class.rolify rolify_options }
|
|
42
|
+
@user = user_class.first
|
|
43
|
+
@user.add_role :admin
|
|
44
|
+
@user.stub(:role_callback)
|
|
45
|
+
|
|
46
|
+
@user.should_receive(:role_callback)
|
|
47
|
+
@user.remove_role :admin
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
describe "after_remove" do
|
|
52
|
+
it "should receive callback" do
|
|
53
|
+
rolify_options = { :role_cname => role_class.to_s, :after_remove => :role_callback }
|
|
54
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
55
|
+
silence_warnings { user_class.rolify rolify_options }
|
|
56
|
+
@user = user_class.first
|
|
57
|
+
@user.add_role :admin
|
|
58
|
+
@user.stub(:role_callback)
|
|
59
|
+
|
|
60
|
+
@user.should_receive(:role_callback)
|
|
61
|
+
@user.remove_role :admin
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
shared_examples_for Rolify::Dynamic do
|
|
1
|
+
shared_examples_for Rolify::Dynamic do
|
|
2
2
|
before(:all) do
|
|
3
3
|
Rolify.dynamic_shortcuts = true
|
|
4
4
|
role_class.destroy_all
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
rolify_options = { :role_cname => role_class.to_s }
|
|
6
|
+
rolify_options[:role_join_table_name] = join_table if defined? join_table
|
|
7
|
+
silence_warnings { user_class.rolify rolify_options }
|
|
8
|
+
Forum.resourcify :roles, :role_cname => role_class.to_s
|
|
9
|
+
Group.resourcify :roles, :role_cname => role_class.to_s
|
|
8
10
|
end
|
|
9
|
-
|
|
11
|
+
|
|
10
12
|
context "using a global role" do
|
|
11
|
-
subject do
|
|
13
|
+
subject do
|
|
12
14
|
admin = user_class.first
|
|
13
15
|
admin.add_role :admin
|
|
14
16
|
admin.add_role :moderator, Forum.first
|
|
17
|
+
admin.add_role :solo
|
|
15
18
|
admin
|
|
16
19
|
end
|
|
17
20
|
|
|
@@ -22,21 +25,30 @@ shared_examples_for Rolify::Dynamic do
|
|
|
22
25
|
it { subject.is_admin?.should be(true) }
|
|
23
26
|
it { subject.is_admin?.should be(true) }
|
|
24
27
|
it { subject.is_admin?.should be(true) }
|
|
28
|
+
|
|
29
|
+
context "removing the role on the last user having it" do
|
|
30
|
+
before do
|
|
31
|
+
subject.remove_role :solo
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it { should_not respond_to(:is_solo?) }
|
|
35
|
+
it { subject.is_solo?.should be(false) }
|
|
36
|
+
end
|
|
25
37
|
end
|
|
26
|
-
|
|
38
|
+
|
|
27
39
|
context "using a resource scoped role" do
|
|
28
|
-
subject do
|
|
40
|
+
subject do
|
|
29
41
|
moderator = user_class.where(:login => "moderator").first
|
|
30
42
|
moderator.add_role :moderator, Forum.first
|
|
43
|
+
moderator.add_role :sole_mio, Forum.last
|
|
31
44
|
moderator
|
|
32
45
|
end
|
|
33
|
-
|
|
34
|
-
it { should respond_to(:is_admin?).with(0).arguments }
|
|
46
|
+
|
|
35
47
|
it { should respond_to(:is_moderator?).with(0).arguments }
|
|
36
48
|
it { should respond_to(:is_moderator_of?).with(1).arguments }
|
|
37
49
|
it { should_not respond_to(:is_god?) }
|
|
38
50
|
it { should_not respond_to(:is_god_of?) }
|
|
39
|
-
|
|
51
|
+
|
|
40
52
|
it { subject.is_moderator?.should be(false) }
|
|
41
53
|
it { subject.is_moderator_of?(Forum).should be(false) }
|
|
42
54
|
it { subject.is_moderator_of?(Forum.first).should be(true) }
|
|
@@ -44,23 +56,30 @@ shared_examples_for Rolify::Dynamic do
|
|
|
44
56
|
it { subject.is_moderator_of?(Group).should be(false) }
|
|
45
57
|
it { subject.is_moderator_of?(Group.first).should be(false) }
|
|
46
58
|
it { subject.is_moderator_of?(Group.last).should be(false) }
|
|
59
|
+
|
|
60
|
+
context "removing the role on the last user having it" do
|
|
61
|
+
before do
|
|
62
|
+
subject.remove_role :sole_mio, Forum.last
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it { should_not respond_to(:is_sole_mio?) }
|
|
66
|
+
it { subject.is_sole_mio?.should be(false) }
|
|
67
|
+
end
|
|
47
68
|
end
|
|
48
|
-
|
|
69
|
+
|
|
49
70
|
context "using a class scoped role" do
|
|
50
|
-
subject do
|
|
71
|
+
subject do
|
|
51
72
|
manager = user_class.where(:login => "god").first
|
|
52
73
|
manager.add_role :manager, Forum
|
|
74
|
+
manager.add_role :only_me, Forum
|
|
53
75
|
manager
|
|
54
76
|
end
|
|
55
|
-
|
|
56
|
-
it { should respond_to(:is_admin?).with(0).arguments }
|
|
57
|
-
it { should respond_to(:is_moderator?).with(0).arguments }
|
|
58
|
-
it { should respond_to(:is_moderator_of?).with(1).arguments }
|
|
77
|
+
|
|
59
78
|
it { should respond_to(:is_manager?).with(0).arguments }
|
|
60
79
|
it { should respond_to(:is_manager_of?).with(1).arguments }
|
|
61
80
|
it { should_not respond_to(:is_god?) }
|
|
62
81
|
it { should_not respond_to(:is_god_of?) }
|
|
63
|
-
|
|
82
|
+
|
|
64
83
|
it { subject.is_manager?.should be(false) }
|
|
65
84
|
it { subject.is_manager_of?(Forum).should be(true) }
|
|
66
85
|
it { subject.is_manager_of?(Forum.first).should be(true) }
|
|
@@ -68,36 +87,46 @@ shared_examples_for Rolify::Dynamic do
|
|
|
68
87
|
it { subject.is_manager_of?(Group).should be(false) }
|
|
69
88
|
it { subject.is_manager_of?(Group.first).should be(false) }
|
|
70
89
|
it { subject.is_manager_of?(Group.last).should be(false) }
|
|
90
|
+
|
|
91
|
+
context "removing the role on the last user having it" do
|
|
92
|
+
before do
|
|
93
|
+
subject.remove_role :only_me, Forum
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it { should_not respond_to(:is_only_me?) }
|
|
97
|
+
it { subject.is_only_me?.should be(false) }
|
|
98
|
+
end
|
|
71
99
|
end
|
|
72
|
-
|
|
100
|
+
|
|
73
101
|
context "if the role doesn't exist in the database" do
|
|
74
|
-
|
|
102
|
+
|
|
75
103
|
subject { user_class.first }
|
|
76
|
-
|
|
104
|
+
|
|
77
105
|
context "using a global role" do
|
|
78
106
|
before(:all) do
|
|
79
107
|
other_guy = user_class.last
|
|
80
108
|
other_guy.add_role :superman
|
|
81
109
|
end
|
|
82
|
-
|
|
110
|
+
|
|
83
111
|
it { should respond_to(:is_superman?).with(0).arguments }
|
|
84
112
|
it { should_not respond_to(:is_superman_of?) }
|
|
85
113
|
it { should_not respond_to(:is_god?) }
|
|
86
|
-
|
|
114
|
+
|
|
87
115
|
it { subject.is_superman?.should be(false) }
|
|
116
|
+
it { subject.is_god?.should be(false) }
|
|
88
117
|
end
|
|
89
|
-
|
|
118
|
+
|
|
90
119
|
context "using a resource scope role" do
|
|
91
120
|
before(:all) do
|
|
92
121
|
other_guy = user_class.last
|
|
93
122
|
other_guy.add_role :batman, Forum.first
|
|
94
123
|
end
|
|
95
|
-
|
|
124
|
+
|
|
96
125
|
it { should respond_to(:is_batman?).with(0).arguments }
|
|
97
126
|
it { should respond_to(:is_batman_of?).with(1).arguments }
|
|
98
127
|
it { should_not respond_to(:is_god?) }
|
|
99
128
|
it { should_not respond_to(:is_god_of?) }
|
|
100
|
-
|
|
129
|
+
|
|
101
130
|
it { subject.is_batman?.should be(false) }
|
|
102
131
|
it { subject.is_batman_of?(Forum).should be(false) }
|
|
103
132
|
it { subject.is_batman_of?(Forum.first).should be(false) }
|
|
@@ -105,6 +134,14 @@ shared_examples_for Rolify::Dynamic do
|
|
|
105
134
|
it { subject.is_batman_of?(Group).should be(false) }
|
|
106
135
|
it { subject.is_batman_of?(Group.first).should be(false) }
|
|
107
136
|
it { subject.is_batman_of?(Group.last).should be(false) }
|
|
137
|
+
|
|
138
|
+
it { subject.is_god?.should be(false) }
|
|
139
|
+
it { subject.is_god_of?(Forum).should be(false) }
|
|
140
|
+
it { subject.is_god_of?(Forum.first).should be(false) }
|
|
141
|
+
it { subject.is_god_of?(Forum.last).should be(false) }
|
|
142
|
+
it { subject.is_god_of?(Group).should be(false) }
|
|
143
|
+
it { subject.is_god_of?(Group.first).should be(false) }
|
|
144
|
+
it { subject.is_god_of?(Group.last).should be(false) }
|
|
108
145
|
end
|
|
109
146
|
end
|
|
110
|
-
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
shared_examples_for :finders do |param_name, param_method|
|
|
2
|
+
context "using #{param_name} as parameter" do
|
|
3
|
+
describe ".with_role" do
|
|
4
|
+
it { should respond_to(:with_role).with(1).argument }
|
|
5
|
+
it { should respond_to(:with_role).with(2).arguments }
|
|
6
|
+
|
|
7
|
+
context "with a global role" do
|
|
8
|
+
it { subject.with_role("admin".send(param_method)).should eq([ root ]) }
|
|
9
|
+
it { subject.with_role("moderator".send(param_method)).should be_empty }
|
|
10
|
+
it { subject.with_role("visitor".send(param_method)).should be_empty }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context "with a class scoped role" do
|
|
14
|
+
context "on Forum class" do
|
|
15
|
+
it { subject.with_role("admin".send(param_method), Forum).should eq([ root ]) }
|
|
16
|
+
it { subject.with_role("moderator".send(param_method), Forum).should eq([ modo ]) }
|
|
17
|
+
it { subject.with_role("visitor".send(param_method), Forum).should be_empty }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
context "on Group class" do
|
|
21
|
+
it { subject.with_role("admin".send(param_method), Group).should eq([ root ]) }
|
|
22
|
+
it { subject.with_role("moderator".send(param_method), Group).should eq([ root ]) }
|
|
23
|
+
it { subject.with_role("visitor".send(param_method), Group).should be_empty }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "with an instance scoped role" do
|
|
28
|
+
context "on Forum.first instance" do
|
|
29
|
+
it { subject.with_role("admin".send(param_method), Forum.first).should eq([ root ]) }
|
|
30
|
+
it { subject.with_role("moderator".send(param_method), Forum.first).should eq([ modo ]) }
|
|
31
|
+
it { subject.with_role("visitor".send(param_method), Forum.first).should be_empty }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context "on Forum.last instance" do
|
|
35
|
+
it { subject.with_role("admin".send(param_method), Forum.last).should eq([ root ]) }
|
|
36
|
+
it { subject.with_role("moderator".send(param_method), Forum.last).should eq([ modo ]) }
|
|
37
|
+
it { subject.with_role("visitor".send(param_method), Forum.last).should include(root, visitor) } # =~ doesn't pass using mongoid, don't know why...
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context "on Group.first instance" do
|
|
41
|
+
it { subject.with_role("admin".send(param_method), Group.first).should eq([ root ]) }
|
|
42
|
+
it { subject.with_role("moderator".send(param_method), Group.first).should eq([ root ]) }
|
|
43
|
+
it { subject.with_role("visitor".send(param_method), Group.first).should eq([ modo ]) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context "on Company.first_instance" do
|
|
47
|
+
it { subject.with_role("owner".send(param_method), Company.first).should eq([ owner ]) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
describe ".with_all_roles" do
|
|
53
|
+
it { should respond_to(:with_all_roles) }
|
|
54
|
+
|
|
55
|
+
it { subject.with_all_roles("admin".send(param_method), :staff).should eq([ root ]) }
|
|
56
|
+
it { subject.with_all_roles("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
|
|
57
|
+
it { subject.with_all_roles("admin".send(param_method), "moderator".send(param_method)).should be_empty }
|
|
58
|
+
it { subject.with_all_roles("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Forum }).should be_empty }
|
|
59
|
+
it { subject.with_all_roles({ :name => "moderator".send(param_method), :resource => Forum }, { :name => :manager, :resource => Group }).should eq([ modo ]) }
|
|
60
|
+
it { subject.with_all_roles("moderator".send(param_method), :manager).should be_empty }
|
|
61
|
+
it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => Forum.last }, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
|
|
62
|
+
it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => Group.first }, { :name => "moderator".send(param_method), :resource => Forum }).should eq([ modo ]) }
|
|
63
|
+
it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => :any }, { :name => "moderator".send(param_method), :resource => :any }).should =~ [ root, modo ] }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe ".with_any_role" do
|
|
67
|
+
it { should respond_to(:with_any_role) }
|
|
68
|
+
|
|
69
|
+
it { subject.with_any_role("admin".send(param_method), :staff).should eq([ root ]) }
|
|
70
|
+
it { subject.with_any_role("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
|
|
71
|
+
it { subject.with_any_role("admin".send(param_method), "moderator".send(param_method)).should eq([ root ]) }
|
|
72
|
+
it { subject.with_any_role("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Forum }).should =~ [ root, modo ] }
|
|
73
|
+
it { subject.with_any_role({ :name => "moderator".send(param_method), :resource => Forum }, { :name => :manager, :resource => Group }).should eq([ modo ]) }
|
|
74
|
+
it { subject.with_any_role({ :name => "moderator".send(param_method), :resource => Group }, { :name => :manager, :resource => Group }).should =~ [ root, modo ] }
|
|
75
|
+
it { subject.with_any_role("moderator".send(param_method), :manager).should be_empty }
|
|
76
|
+
it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => Forum.last }, { :name => "moderator".send(param_method), :resource => Group }).should =~ [ root, visitor ] }
|
|
77
|
+
it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => Group.first }, { :name => "moderator".send(param_method), :resource => Forum }).should eq([ modo ]) }
|
|
78
|
+
it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => :any }, { :name => "moderator".send(param_method), :resource => :any }).should =~ [ root, modo, visitor ] }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -2,54 +2,54 @@ shared_examples_for "#has_all_roles?_examples" do |param_name, param_method|
|
|
|
2
2
|
context "using #{param_name} as parameter" do
|
|
3
3
|
context "with a global role", :scope => :global do
|
|
4
4
|
context "on global roles only request" do
|
|
5
|
-
it { subject.has_all_roles?("staff".send(param_method)).should
|
|
6
|
-
it { subject.has_all_roles?("admin".send(param_method), "staff".send(param_method)).should
|
|
7
|
-
it { subject.has_all_roles?("admin".send(param_method), "dummy".send(param_method)).should
|
|
8
|
-
it { subject.has_all_roles?("dummy".send(param_method), "dumber".send(param_method)).should
|
|
5
|
+
it { subject.has_all_roles?("staff".send(param_method)).should be_truthy }
|
|
6
|
+
it { subject.has_all_roles?("admin".send(param_method), "staff".send(param_method)).should be_truthy }
|
|
7
|
+
it { subject.has_all_roles?("admin".send(param_method), "dummy".send(param_method)).should be_falsey }
|
|
8
|
+
it { subject.has_all_roles?("dummy".send(param_method), "dumber".send(param_method)).should be_falsey }
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
context "on mixed scoped roles" do
|
|
12
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "admin".send(param_method), :resource => Group }).should
|
|
13
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => :any }, { :name => "admin".send(param_method), :resource => Group }).should
|
|
14
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "staff".send(param_method), :resource => Group.last }).should
|
|
15
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "admin".send(param_method), :resource => Forum.last }).should
|
|
16
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should
|
|
17
|
-
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => :any }).should
|
|
12
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "admin".send(param_method), :resource => Group }).should be_truthy }
|
|
13
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => :any }, { :name => "admin".send(param_method), :resource => Group }).should be_truthy }
|
|
14
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "staff".send(param_method), :resource => Group.last }).should be_truthy }
|
|
15
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "admin".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
16
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should be_falsey }
|
|
17
|
+
it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => :any }).should be_falsey}
|
|
18
18
|
end
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
context "with a class scoped role", :scope => :class do
|
|
22
22
|
context "on class scoped roles only" do
|
|
23
|
-
it { subject.has_all_roles?({ :name => "player".send(param_method), :resource => Forum }).should
|
|
24
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => Forum }).should
|
|
25
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => Forum }).should
|
|
26
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => :any }).should
|
|
27
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => Forum }).should
|
|
28
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => :any }).should
|
|
29
|
-
it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum }, { :name => "dumber".send(param_method), :resource => Group }).should
|
|
23
|
+
it { subject.has_all_roles?({ :name => "player".send(param_method), :resource => Forum }).should be_truthy }
|
|
24
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => Forum }).should be_truthy }
|
|
25
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => Forum }).should be_truthy }
|
|
26
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => :any }).should be_truthy }
|
|
27
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => Forum }).should be_falsey }
|
|
28
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => :any }).should be_falsey }
|
|
29
|
+
it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum }, { :name => "dumber".send(param_method), :resource => Group }).should be_falsey }
|
|
30
30
|
end
|
|
31
31
|
|
|
32
32
|
context "on mixed scoped roles" do
|
|
33
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "manager".send(param_method), :resource => Forum.last }).should
|
|
34
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Group }, { :name => "moderator".send(param_method), :resource => Forum.first }).should
|
|
35
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum }).should
|
|
36
|
-
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.last }, { :name => "warrior".send(param_method), :resource => Forum.last }).should
|
|
33
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "manager".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
34
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Group }, { :name => "moderator".send(param_method), :resource => Forum.first }).should be_falsey }
|
|
35
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum }).should be_falsey }
|
|
36
|
+
it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.last }, { :name => "warrior".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
context "with a instance scoped role", :scope => :instance do
|
|
41
41
|
context "on instance scoped roles only" do
|
|
42
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => Forum.last }).should
|
|
43
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => :any }).should
|
|
44
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => Forum }).should
|
|
45
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "anonymous".send(param_method), :resource => Forum.last }).should
|
|
46
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum.last }).should
|
|
47
|
-
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should
|
|
48
|
-
it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum.first }, { :name => "dumber".send(param_method), :resource => Forum.last }).should
|
|
42
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
43
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => :any }).should be_truthy }
|
|
44
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => Forum }).should be_falsey }
|
|
45
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "anonymous".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
46
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum.last }).should be_falsey }
|
|
47
|
+
it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should be_falsey }
|
|
48
|
+
it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum.first }, { :name => "dumber".send(param_method), :resource => Forum.last }).should be_falsey }
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
context "on mixed scoped roles" do
|
|
52
|
-
it { subject.has_all_roles?({ :name => "visitor".send(param_method), :resource => Forum.last }).should
|
|
52
|
+
it { subject.has_all_roles?({ :name => "visitor".send(param_method), :resource => Forum.last }).should be_truthy }
|
|
53
53
|
it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Forum }).should be(true) }
|
|
54
54
|
it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => Forum.last }, { :name => "visitor".send(param_method), :resource => Forum }).should be(false) }
|
|
55
55
|
it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => :any }, { :name => "visitor".send(param_method), :resource => Forum }).should be(true) }
|