rolify 2.2.2 → 3.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.
Files changed (40) hide show
  1. data/.travis.yml +11 -7
  2. data/CHANGELOG.rdoc +6 -0
  3. data/README.rdoc +36 -4
  4. data/UPGRADE.rdoc +22 -0
  5. data/lib/generators/rolify/role/role_generator.rb +5 -6
  6. data/lib/generators/rolify/role/templates/README-active_record +21 -0
  7. data/lib/generators/rolify/role/templates/README-mongoid +17 -0
  8. data/lib/generators/rolify/role/templates/initializer.rb +5 -6
  9. data/lib/generators/rolify/role/templates/{role.rb → role-active_record.rb} +0 -0
  10. data/lib/generators/rolify/role/templates/role-mongoid.rb +8 -0
  11. data/lib/rolify/adapters/active_record.rb +83 -0
  12. data/lib/rolify/adapters/base.rb +53 -0
  13. data/lib/rolify/adapters/mongoid.rb +87 -0
  14. data/lib/rolify/configure.rb +40 -0
  15. data/lib/rolify/dynamic.rb +21 -0
  16. data/lib/rolify/railtie.rb +20 -0
  17. data/lib/rolify/resource.rb +25 -0
  18. data/lib/rolify/role.rb +32 -110
  19. data/lib/rolify/version.rb +1 -1
  20. data/lib/rolify.rb +35 -0
  21. data/rolify.gemspec +3 -2
  22. data/spec/generators/rolify/role/role_generator_spec.rb +75 -16
  23. data/spec/rolify/config_spec.rb +189 -0
  24. data/spec/rolify/custom_spec.rb +15 -0
  25. data/spec/rolify/resource_spec.rb +317 -0
  26. data/spec/rolify/role_spec.rb +8 -495
  27. data/spec/rolify/shared_contexts.rb +74 -0
  28. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +110 -0
  29. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  30. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  31. data/spec/rolify/shared_examples/shared_examples_for_has_no_role.rb +97 -0
  32. data/spec/rolify/shared_examples/shared_examples_for_has_role_getter.rb +135 -0
  33. data/spec/rolify/shared_examples/shared_examples_for_has_role_setter.rb +92 -0
  34. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +80 -0
  35. data/spec/spec_helper.rb +10 -5
  36. data/spec/support/{models.rb → adapters/active_record.rb} +11 -8
  37. data/spec/support/adapters/mongoid.rb +56 -0
  38. data/spec/support/data.rb +14 -5
  39. metadata +76 -20
  40. data/benchmarks/performance.rb +0 -51
@@ -0,0 +1,110 @@
1
+ shared_examples_for Rolify::Dynamic do
2
+ before(:all) do
3
+ Rolify.dynamic_shortcuts = true
4
+ role_class.destroy_all
5
+ user_class.rolify :role_cname => role_class.to_s
6
+ Forum.resourcify :role_cname => role_class.to_s
7
+ Group.resourcify :role_cname => role_class.to_s
8
+ end
9
+
10
+ context "using a global role" do
11
+ subject do
12
+ admin = user_class.first
13
+ admin.has_role "admin"
14
+ admin.has_role "moderator", Forum.first
15
+ admin
16
+ end
17
+
18
+ it { should respond_to(:is_admin?).with(0).arguments }
19
+ it { should respond_to(:is_moderator_of?).with(1).arguments }
20
+ it { should_not respond_to(:is_god?) }
21
+
22
+ it { subject.is_admin?.should be(true) }
23
+ it { subject.is_admin?.should be(true) }
24
+ it { subject.is_admin?.should be(true) }
25
+ end
26
+
27
+ context "using a resource scoped role" do
28
+ subject do
29
+ moderator = user_class.where(:login => "moderator").first
30
+ moderator.has_role "moderator", Forum.first
31
+ moderator
32
+ end
33
+
34
+ it { should respond_to(:is_admin?).with(0).arguments }
35
+ it { should respond_to(:is_moderator?).with(0).arguments }
36
+ it { should respond_to(:is_moderator_of?).with(1).arguments }
37
+ it { should_not respond_to(:is_god?) }
38
+ it { should_not respond_to(:is_god_of?) }
39
+
40
+ it { subject.is_moderator?.should be(false) }
41
+ it { subject.is_moderator_of?(Forum).should be(false) }
42
+ it { subject.is_moderator_of?(Forum.first).should be(true) }
43
+ it { subject.is_moderator_of?(Forum.last).should be(false) }
44
+ it { subject.is_moderator_of?(Group).should be(false) }
45
+ it { subject.is_moderator_of?(Group.first).should be(false) }
46
+ it { subject.is_moderator_of?(Group.last).should be(false) }
47
+ end
48
+
49
+ context "using a class scoped role" do
50
+ subject do
51
+ manager = user_class.where(:login => "god").first
52
+ manager.has_role "manager", Forum
53
+ manager
54
+ 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 }
59
+ it { should respond_to(:is_manager?).with(0).arguments }
60
+ it { should respond_to(:is_manager_of?).with(1).arguments }
61
+ it { should_not respond_to(:is_god?) }
62
+ it { should_not respond_to(:is_god_of?) }
63
+
64
+ it { subject.is_manager?.should be(false) }
65
+ it { subject.is_manager_of?(Forum).should be(true) }
66
+ it { subject.is_manager_of?(Forum.first).should be(true) }
67
+ it { subject.is_manager_of?(Forum.last).should be(true) }
68
+ it { subject.is_manager_of?(Group).should be(false) }
69
+ it { subject.is_manager_of?(Group.first).should be(false) }
70
+ it { subject.is_manager_of?(Group.last).should be(false) }
71
+ end
72
+
73
+ context "if the role doesn't exist in the database" do
74
+
75
+ subject { user_class.first }
76
+
77
+ context "using a global role" do
78
+ before(:all) do
79
+ other_guy = user_class.last
80
+ other_guy.has_role "superman"
81
+ end
82
+
83
+ it { should respond_to(:is_superman?).with(0).arguments }
84
+ it { should_not respond_to(:is_superman_of?) }
85
+ it { should_not respond_to(:is_god?) }
86
+
87
+ it { subject.is_superman?.should be(false) }
88
+ end
89
+
90
+ context "using a resource scope role" do
91
+ before(:all) do
92
+ other_guy = user_class.last
93
+ other_guy.has_role("batman", Forum.first)
94
+ end
95
+
96
+ it { should respond_to(:is_batman?).with(0).arguments }
97
+ it { should respond_to(:is_batman_of?).with(1).arguments }
98
+ it { should_not respond_to(:is_god?) }
99
+ it { should_not respond_to(:is_god_of?) }
100
+
101
+ it { subject.is_batman?.should be(false) }
102
+ it { subject.is_batman_of?(Forum).should be(false) }
103
+ it { subject.is_batman_of?(Forum.first).should be(false) }
104
+ it { subject.is_batman_of?(Forum.last).should be(false) }
105
+ it { subject.is_batman_of?(Group).should be(false) }
106
+ it { subject.is_batman_of?(Group.first).should be(false) }
107
+ it { subject.is_batman_of?(Group.last).should be(false) }
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,71 @@
1
+ shared_examples_for "#has_all_roles?_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "with a global role", :scope => :global do
4
+ context "on global roles only request" do
5
+ it { subject.has_all_roles?("staff".send(param_method)).should be_true }
6
+ it { subject.has_all_roles?("admin".send(param_method), "staff".send(param_method)).should be_true }
7
+ it { subject.has_all_roles?("admin".send(param_method), "dummy".send(param_method)).should be_false }
8
+ it { subject.has_all_roles?("dummy".send(param_method), "dumber".send(param_method)).should be_false }
9
+ end
10
+
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 be_true }
13
+ it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => :any }, { :name => "admin".send(param_method), :resource => Group }).should be_true }
14
+ it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "staff".send(param_method), :resource => Group.last }).should be_true }
15
+ it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "admin".send(param_method), :resource => Forum.last }).should be_true }
16
+ it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should be_false }
17
+ it { subject.has_all_roles?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => :any }).should be_false}
18
+ end
19
+ end
20
+
21
+ context "with a class scoped role", :scope => :class do
22
+ context "on class scoped roles only" do
23
+ it { subject.has_all_roles?({ :name => "player".send(param_method), :resource => Forum }).should be_true }
24
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => Forum }).should be_true }
25
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => Forum }).should be_true }
26
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => :any }).should be_true }
27
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => Forum }).should be_false }
28
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => :any }).should be_false }
29
+ it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum }, { :name => "dumber".send(param_method), :resource => Group }).should be_false }
30
+ end
31
+
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 be_true }
34
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Group }, { :name => "moderator".send(param_method), :resource => Forum.first }).should be_false }
35
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum }).should be_false }
36
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.last }, { :name => "warrior".send(param_method), :resource => Forum.last }).should be_true }
37
+ end
38
+ end
39
+
40
+ context "with a instance scoped role", :scope => :instance do
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 be_true }
43
+ it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => :any }).should be_true }
44
+ it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => :any }, { :name => "anonymous".send(param_method), :resource => Forum }).should be_false }
45
+ it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "anonymous".send(param_method), :resource => Forum.last }).should be_true }
46
+ it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum.last }).should be_false }
47
+ it { subject.has_all_roles?({ :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should be_false }
48
+ it { subject.has_all_roles?({ :name => "dummy".send(param_method), :resource => Forum.first }, { :name => "dumber".send(param_method), :resource => Forum.last }).should be_false }
49
+ end
50
+
51
+ context "on mixed scoped roles" do
52
+ it { subject.has_all_roles?({ :name => "visitor".send(param_method), :resource => Forum.last }).should be_true }
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
+ 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
+ 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) }
56
+ it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => :any }, { :name => "visitor".send(param_method), :resource => :any }).should be(true) }
57
+ it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Group }).should be(false) }
58
+ it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Group.first }).should be(false) }
59
+ it { subject.has_all_roles?({ :name => "soldier".send(param_method), :resource => Forum }, { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Forum }).should be(true) }
60
+ it { subject.has_all_roles?({ :name => "soldier".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Forum }).should be(true) }
61
+ it { subject.has_all_roles?("soldier".send(param_method), { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Forum.first }).should be(true) }
62
+ it { subject.has_all_roles?("dummy".send(param_method), { :name => "dumber".send(param_method), :resource => Forum.last }, { :name => "dumberer".send(param_method), :resource => Forum }).should be(false) }
63
+ it { subject.has_all_roles?("soldier".send(param_method), "dummy".send(param_method), { :name => "dumber".send(param_method), :resource => Forum.last }, { :name => "dumberer".send(param_method), :resource => Forum }).should be(false) }
64
+ it { subject.has_all_roles?({ :name => "manager".send(param_method), :resource => Forum.last }, "dummy".send(param_method), { :name => "dumber".send(param_method), :resource => Forum.last }, { :name => "dumberer".send(param_method), :resource => Forum }).should be(false) }
65
+ it { subject.has_all_roles?("soldier".send(param_method), { :name => "dumber".send(param_method), :resource => Forum.last }, { :name => "manager".send(param_method), :resource => Forum.last }).should be(false) }
66
+ it { subject.has_all_roles?({ :name => "soldier".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum.first }, { :name => "visitor".send(param_method), :resource => Forum.last }).should be(true) }
67
+ it { subject.has_all_roles?({ :name => "soldier".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => :any }, { :name => "visitor".send(param_method), :resource => Forum.last }).should be(true) }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,71 @@
1
+ shared_examples_for "#has_any_role?_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "with a global role", :scope => :global do
4
+ before(:all) do
5
+ subject.has_role "staff".send(param_method)
6
+ end
7
+
8
+ it { subject.has_any_role?("staff".send(param_method)).should be_true }
9
+ it { subject.has_any_role?("admin".send(param_method), "staff".send(param_method)).should be_true }
10
+ it { subject.has_any_role?("admin".send(param_method), "moderator".send(param_method)).should be_true }
11
+ it { subject.has_any_role?("dummy".send(param_method), "dumber".send(param_method)).should be_false }
12
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "admin".send(param_method), :resource => Group }).should be_true }
13
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => :any }, { :name => "admin".send(param_method), :resource => Group }).should be_true }
14
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => Forum }, { :name => "staff".send(param_method), :resource => Group.last }).should be_true }
15
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "admin".send(param_method), :resource => Forum.last }).should be_true }
16
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => Forum.last }).should be_true }
17
+ it { subject.has_any_role?({ :name => "admin".send(param_method), :resource => Forum.first }, { :name => "dummy".send(param_method), :resource => :any }).should be_true }
18
+ it { subject.has_any_role?({ :name => "dummy".send(param_method), :resource => Forum.first }, { :name => "dumber".send(param_method), :resource => :any }).should be_false }
19
+ it { subject.has_any_role?({ :name => "dummy".send(param_method), :resource => :any }, { :name => "dumber".send(param_method), :resource => :any }).should be_false }
20
+ end
21
+
22
+ context "with a class scoped role", :scope => :class do
23
+ before(:all) do
24
+ subject.has_role "player".send(param_method), Forum
25
+ subject.has_role "superhero".send(param_method)
26
+ end
27
+
28
+ it { subject.has_any_role?({ :name => "player".send(param_method), :resource => Forum }).should be_true }
29
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => Forum }).should be_true }
30
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => :any }).should be_true }
31
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "player".send(param_method), :resource => :any }).should be_true }
32
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => :any }, { :name => "player".send(param_method), :resource => :any }).should be_true }
33
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => Forum }).should be_true }
34
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum }, { :name => "dummy".send(param_method), :resource => :any }).should be_true }
35
+ it { subject.has_any_role?({ :name => "dummy".send(param_method), :resource => Forum }, { :name => "dumber".send(param_method), :resource => Group }).should be_false }
36
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "manager".send(param_method), :resource => Forum.last }).should be_true }
37
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Group }, { :name => "moderator".send(param_method), :resource => Forum.first }).should be_false }
38
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum.first }, { :name => "moderator".send(param_method), :resource => Forum }).should be_true }
39
+ it { subject.has_any_role?({ :name => "manager".send(param_method), :resource => Forum.last }, { :name => "warrior".send(param_method), :resource => Forum.last }).should be_true }
40
+ end
41
+
42
+ context "with a instance scoped role", :scope => :instance do
43
+ before(:all) do
44
+ subject.has_role "visitor".send(param_method), Forum.last
45
+ subject.has_role "leader", Group
46
+ subject.has_role "warrior"
47
+ end
48
+
49
+ it { subject.has_any_role?({ :name => "visitor", :resource => Forum.last }).should be_true }
50
+ it { subject.has_any_role?({ :name => "moderator", :resource => Forum.first }, { :name => "visitor", :resource => Forum.last }).should be_true }
51
+ it { subject.has_any_role?({ :name => "moderator", :resource => :any }, { :name => "visitor", :resource => Forum.last }).should be_true }
52
+ it { subject.has_any_role?({ :name => "moderator", :resource => :any }, { :name => "visitor", :resource => :any}).should be_true }
53
+ it { subject.has_any_role?({ :name => "moderator", :resource => Forum }, { :name => "visitor", :resource => :any }).should be_true }
54
+ it { subject.has_any_role?({ :name => "moderator", :resource => Forum.first }, { :name => "moderator", :resource => Forum.last }).should be_true }
55
+ it { subject.has_any_role?({ :name => "moderator", :resource => Forum.first }, { :name => "dummy", :resource => Forum.last }).should be_true }
56
+ it { subject.has_any_role?({ :name => "dummy", :resource => Forum.first }, { :name => "dumber", :resource => Forum.last }).should be_false }
57
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.first }, { :name => "leader", :resource => Group }).should be(true) }
58
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => :any }, { :name => "leader", :resource => Forum }).should be(true) }
59
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.first }, { :name => "leader", :resource => :any }).should be(true) }
60
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => :any }, { :name => "leader", :resource => :any }).should be(true) }
61
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Forum }).should be(true) }
62
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Group }).should be(true) }
63
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Group.first }).should be(true) }
64
+ it { subject.has_any_role?({ :name => "warrior", :resource => Forum }, { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Forum }).should be(true) }
65
+ it { subject.has_any_role?({ :name => "warrior", :resource => Forum.first }, { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Forum }).should be(true) }
66
+ it { subject.has_any_role?("warrior", { :name => "moderator", :resource => Forum.last }, { :name => "leader", :resource => Forum.first }).should be(true) }
67
+ it { subject.has_any_role?("dummy", { :name => "dumber", :resource => Forum.last }, { :name => "dumberer", :resource => Forum }).should be(false) }
68
+ it { subject.has_any_role?({ :name => "leader", :resource => Group.last }, "dummy", { :name => "dumber", :resource => Forum.last }, { :name => "dumberer", :resource => Forum }).should be(true) }
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,97 @@
1
+ shared_examples_for "#has_no_role_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "removing a global role", :scope => :global do
4
+ context "being a global role of the user" do
5
+ it { expect { subject.has_no_role("admin".send(param_method)) }.to change{ subject.roles.size }.by(-1) }
6
+
7
+ it { subject.has_role?("admin".send(param_method)).should be_false }
8
+ it { subject.has_role?("staff".send(param_method)).should be_true }
9
+ it { subject.has_role?("manager".send(param_method), Group).should be_true }
10
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_true }
11
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_true }
12
+ it { subject.has_role?("anonymous".send(param_method), Forum.first).should be_true }
13
+ end
14
+
15
+ context "being a class scoped role to the user" do
16
+ it { expect { subject.has_no_role("manager") }.to change{ subject.roles.size }.by(-1) }
17
+
18
+ it { subject.has_role?("admin".send(param_method)).should be_false }
19
+ it { subject.has_role?("staff".send(param_method)).should be_true }
20
+ it { subject.has_role?("manager".send(param_method), Group).should be_false }
21
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_true }
22
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_true }
23
+ it { subject.has_role?("anonymous".send(param_method), Forum.first).should be_true }
24
+ end
25
+
26
+ context "being instance scoped roles to the user" do
27
+ it { expect { subject.has_no_role("moderator") }.to change{ subject.roles.size }.by(-2) }
28
+
29
+ it { subject.has_role?("admin".send(param_method)).should be_false }
30
+ it { subject.has_role?("staff".send(param_method)).should be_true }
31
+ it { subject.has_role?("manager".send(param_method), Group).should be_false }
32
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_false }
33
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_false }
34
+ it { subject.has_role?("anonymous".send(param_method), Forum.first).should be_true }
35
+ end
36
+
37
+ context "not being a role of the user" do
38
+ it { expect { subject.has_no_role("superhero") }.not_to change{ subject.roles.size } }
39
+ end
40
+ end
41
+
42
+ context "removing a class scoped role", :scope => :class do
43
+ context "being a global role of the user" do
44
+ it { expect { subject.has_no_role("warrior", Forum) }.not_to change{ subject.roles.size } }
45
+ end
46
+
47
+ context "being a class scoped role to the user" do
48
+ it { expect { subject.has_no_role("manager", Forum) }.to change{ subject.roles.size }.by(-1) }
49
+
50
+ it { subject.has_role?("warrior").should be_true }
51
+ it { subject.has_role?("manager", Forum).should be_false }
52
+ it { subject.has_role?("player", Forum).should be_true }
53
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_true }
54
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_true }
55
+ it { subject.has_role?("anonymous".send(param_method), Forum.first).should be_true }
56
+ end
57
+
58
+ context "being instance scoped role to the user" do
59
+ it { expect { subject.has_no_role("moderator", Forum) }.to change{ subject.roles.size }.by(-1) }
60
+
61
+ it { subject.has_role?("warrior").should be_true }
62
+ it { subject.has_role?("manager", Forum).should be_false }
63
+ it { subject.has_role?("player", Forum).should be_true }
64
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_false }
65
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_true }
66
+ it { subject.has_role?("anonymous".send(param_method), Forum.first).should be_true }
67
+ end
68
+
69
+ context "not being a role of the user" do
70
+ it { expect { subject.has_no_role("manager", Group) }.not_to change{ subject.roles.size } }
71
+ end
72
+ end
73
+
74
+ context "removing a instance scoped role", :scope => :instance do
75
+ context "being a global role of the user" do
76
+ it { expect { subject.has_no_role("soldier", Group.first) }.not_to change{ subject.roles.size } }
77
+ end
78
+
79
+ context "being a class scoped role to the user" do
80
+ it { expect { subject.has_no_role("visitor", Forum.first) }.not_to change{ subject.roles.size } }
81
+ end
82
+
83
+ context "being instance scoped role to the user" do
84
+ it { expect { subject.has_no_role("moderator", Forum.first) }.to change{ subject.roles.size }.by(-1) }
85
+
86
+ it { subject.has_role?("soldier").should be_true }
87
+ it { subject.has_role?("visitor", Forum).should be_true }
88
+ it { subject.has_role?("moderator", Forum.first).should be_false }
89
+ it { subject.has_role?("anonymous", Forum.last).should be_true }
90
+ end
91
+
92
+ context "not being a role of the user" do
93
+ it { expect { subject.has_no_role("anonymous", Forum.first) }.not_to change{ subject.roles.size } }
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,135 @@
1
+ shared_examples_for "#has_role?_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "with a global role", :scope => :global do
4
+ it { subject.has_role?("admin".send(param_method)).should be_true }
5
+
6
+ context "on resource request" do
7
+ it { subject.has_role?("admin".send(param_method), Forum.first).should be_true }
8
+ it { subject.has_role?("admin".send(param_method), Forum).should be_true }
9
+ it { subject.has_role?("admin".send(param_method), :any).should be_true }
10
+ end
11
+
12
+ context "with another global role" do
13
+ before(:all) { role_class.create(:name => "global") }
14
+
15
+ it { subject.has_role?("global".send(param_method)).should be_false }
16
+ it { subject.has_role?("global".send(param_method), :any).should be_false }
17
+ end
18
+
19
+ it "should not get an instance scoped role" do
20
+ subject.has_role?("moderator".send(param_method), Group.first).should be_false
21
+ end
22
+
23
+ it "should not get a class scoped role" do
24
+ subject.has_role?("manager".send(param_method), Forum).should be_false
25
+ end
26
+
27
+ context "using inexisting role" do
28
+ it { subject.has_role?("dummy".send(param_method)).should be_false }
29
+ it { subject.has_role?("dumber".send(param_method), Forum.first).should be_false }
30
+ end
31
+ end
32
+
33
+ context "with a class scoped role", :scope => :class do
34
+ context "on class scoped role request" do
35
+ it { subject.has_role?("manager".send(param_method), Forum).should be_true }
36
+ it { subject.has_role?("manager".send(param_method), Forum.first).should be_true }
37
+ it { subject.has_role?("manager".send(param_method), :any).should be_true }
38
+ end
39
+
40
+ it "should not get a scoped role when asking for a global" do
41
+ subject.has_role?("manager".send(param_method)).should be_false
42
+ end
43
+
44
+ it "should not get a global role" do
45
+ role_class.create(:name => "admin")
46
+ subject.has_role?("admin".send(param_method)).should be_false
47
+ end
48
+
49
+ context "with another class scoped role" do
50
+ context "on the same resource but with a different name" do
51
+ before(:all) { role_class.create(:name => "member", :resource_type => "Forum") }
52
+
53
+ it { subject.has_role?("member".send(param_method), Forum).should be_false }
54
+ it { subject.has_role?("member".send(param_method), :any).should be_false }
55
+ end
56
+
57
+ context "on another resource with the same name" do
58
+ before(:all) { role_class.create(:name => "manager", :resource_type => "Group") }
59
+
60
+ it { subject.has_role?("manager".send(param_method), Group).should be_false }
61
+ it { subject.has_role?("manager".send(param_method), :any).should be_true }
62
+ end
63
+
64
+ context "on another resource with another name" do
65
+ before(:all) { role_class.create(:name => "defenders", :resource_type => "Group") }
66
+
67
+ it { subject.has_role?("defenders".send(param_method), Group).should be_false }
68
+ it { subject.has_role?("defenders".send(param_method), :any).should be_false }
69
+ end
70
+ end
71
+
72
+ context "using inexisting role" do
73
+ it { subject.has_role?("dummy".send(param_method), Forum).should be_false }
74
+ it { subject.has_role?("dumber".send(param_method)).should be_false }
75
+ end
76
+ end
77
+
78
+ context "with a instance scoped role", :scope => :instance do
79
+ context "on instance scoped role request" do
80
+ it { subject.has_role?("moderator".send(param_method), Forum.first).should be_true }
81
+ it { subject.has_role?("moderator".send(param_method), :any).should be_true }
82
+ end
83
+
84
+ it "should not get an instance scoped role when asking for a global" do
85
+ subject.has_role?("moderator".send(param_method)).should be_false
86
+ end
87
+
88
+ it "should not get an instance scoped role when asking for a class scoped" do
89
+ subject.has_role?("moderator".send(param_method), Forum).should be_false
90
+ end
91
+
92
+ it "should not get a global role" do
93
+ role_class.create(:name => "admin")
94
+ subject.has_role?("admin".send(param_method)).should be_false
95
+ end
96
+
97
+ context "with another instance scoped role" do
98
+ context "on the same resource but with a different role name" do
99
+ before(:all) { role_class.create(:name => "member", :resource => Forum.first) }
100
+
101
+ it { subject.has_role?("member".send(param_method), Forum.first).should be_false }
102
+ it { subject.has_role?("member".send(param_method), :any).should be_false }
103
+ end
104
+
105
+ context "on another resource of the same type but with the same role name" do
106
+ before(:all) { role_class.create(:name => "moderator", :resource => Forum.last) }
107
+
108
+ it { subject.has_role?("moderator".send(param_method), Forum.last).should be_false }
109
+ it { subject.has_role?("moderator".send(param_method), :any).should be_true }
110
+ end
111
+
112
+ context "on another resource of different type but with the same role name" do
113
+ before(:all) { role_class.create(:name => "moderator", :resource => Group.last) }
114
+
115
+ it { subject.has_role?("moderator".send(param_method), Group.last).should be_false }
116
+ it { subject.has_role?("moderator".send(param_method), :any).should be_true }
117
+ end
118
+
119
+ context "on another resource of the same type and with another role name" do
120
+ before(:all) { role_class.create(:name => "member", :resource => Forum.last) }
121
+
122
+ it { subject.has_role?("member".send(param_method), Forum.last).should be_false }
123
+ it { subject.has_role?("member".send(param_method), :any).should be_false }
124
+ end
125
+
126
+ context "on another resource of different type and with another role name" do
127
+ before(:all) { role_class.create(:name => "member", :resource => Group.first) }
128
+
129
+ it { subject.has_role?("member".send(param_method), Group.first).should be_false }
130
+ it { subject.has_role?("member".send(param_method), :any).should be_false }
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,92 @@
1
+ shared_examples_for "#has_role_examples" do |param_name, param_method|
2
+ context "using #{param_name} as parameter" do
3
+ context "with a global role", :scope => :global do
4
+ it "should add the role to the user" do
5
+ expect { subject.has_role "root".send(param_method) }.to change{ subject.roles.count }.by(1)
6
+ end
7
+
8
+ it "should create a role to the roles table" do
9
+ expect { subject.has_role "moderator".send(param_method) }.to change{ role_class.count }.by(1)
10
+ end
11
+
12
+ context "considering a new global role" do
13
+ subject { role_class.last }
14
+
15
+ its(:name) { should eq("moderator") }
16
+ its(:resource_type) { should be(nil) }
17
+ its(:resource_id) { should be(nil) }
18
+ end
19
+
20
+ context "should not create another role" do
21
+ it "if the role was already assigned to the user" do
22
+ subject.has_role "manager".send(param_method)
23
+ expect { subject.has_role "manager".send(param_method) }.not_to change{ subject.roles.size }
24
+ end
25
+
26
+ it "if the role already exists in the db" do
27
+ role_class.create :name => "god"
28
+ expect { subject.has_role "god".send(param_method) }.not_to change{ role_class.count }
29
+ end
30
+ end
31
+ end
32
+
33
+ context "with a class scoped role", :scope => :class do
34
+ it "should add the role to the user" do
35
+ expect { subject.has_role "supervisor".send(param_method), Forum }.to change{ subject.roles.count }.by(1)
36
+ end
37
+
38
+ it "should create a role in the roles table" do
39
+ expect { subject.has_role "moderator".send(param_method), Forum }.to change{ role_class.count }.by(1)
40
+ end
41
+
42
+ context "considering a new class scoped role" do
43
+ subject { role_class.last }
44
+
45
+ its(:name) { should eq("moderator") }
46
+ its(:resource_type) { should eq(Forum.to_s) }
47
+ its(:resource_id) { should be(nil) }
48
+ end
49
+
50
+ context "should not create another role" do
51
+ it "if the role was already assigned to the user" do
52
+ subject.has_role "warrior".send(param_method), Forum
53
+ expect { subject.has_role "warrior".send(param_method), Forum }.not_to change{ subject.roles.size }
54
+ end
55
+
56
+ it "if already existing in the database" do
57
+ role_class.create :name => "hacker", :resource_type => "Forum"
58
+ expect { subject.has_role "hacker".send(param_method), Forum }.not_to change{ role_class.count }
59
+ end
60
+ end
61
+ end
62
+
63
+ context "with an instance scoped role", :scope => :instance do
64
+ it "should add the role to the user" do
65
+ expect { subject.has_role "visitor".send(param_method), Forum.last }.to change{ subject.roles.size }.by(1)
66
+ end
67
+
68
+ it "should create a role in the roles table" do
69
+ expect { subject.has_role "member".send(param_method), Forum.last }.to change{ role_class.count }.by(1)
70
+ end
71
+
72
+ context "considering a new class scoped role" do
73
+ subject { role_class.last }
74
+
75
+ its(:name) { should eq("member") }
76
+ its(:resource) { should eq(Forum.last) }
77
+ end
78
+
79
+ context "should not create another role" do
80
+ it "if the role was already assigned to the user" do
81
+ subject.has_role "anonymous".send(param_method), Forum.first
82
+ expect { subject.has_role "anonymous".send(param_method), Forum.first }.not_to change{ subject.roles.size }
83
+ end
84
+
85
+ it "if already existing in the database" do
86
+ role_class.create :name => "ghost", :resource_type => "Forum", :resource_id => Forum.first.id
87
+ expect { subject.has_role "ghost".send(param_method), Forum.first }.not_to change{ role_class.count }
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end