third-prestige-rolify 3.3.0.rc5

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 (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +17 -0
  4. data/CHANGELOG.rdoc +157 -0
  5. data/Gemfile +21 -0
  6. data/LICENSE +20 -0
  7. data/README.md +220 -0
  8. data/Rakefile +34 -0
  9. data/UPGRADE.rdoc +44 -0
  10. data/gemfiles/Gemfile.rails-3.2 +21 -0
  11. data/gemfiles/Gemfile.rails-4.0 +27 -0
  12. data/lib/generators/active_record/rolify_generator.rb +50 -0
  13. data/lib/generators/active_record/templates/README +8 -0
  14. data/lib/generators/active_record/templates/migration.rb +19 -0
  15. data/lib/generators/mongoid/rolify_generator.rb +51 -0
  16. data/lib/generators/mongoid/templates/README-mongoid +4 -0
  17. data/lib/generators/rolify/rolify_generator.rb +35 -0
  18. data/lib/generators/rolify/templates/README +13 -0
  19. data/lib/generators/rolify/templates/initializer.rb +8 -0
  20. data/lib/generators/rolify/templates/role-active_record.rb +11 -0
  21. data/lib/generators/rolify/templates/role-mongoid.rb +17 -0
  22. data/lib/generators/rolify/user_generator.rb +39 -0
  23. data/lib/rolify.rb +57 -0
  24. data/lib/rolify/adapters/active_record/resource_adapter.rb +26 -0
  25. data/lib/rolify/adapters/active_record/role_adapter.rb +86 -0
  26. data/lib/rolify/adapters/active_record/scopes.rb +27 -0
  27. data/lib/rolify/adapters/base.rb +60 -0
  28. data/lib/rolify/adapters/mongoid/resource_adapter.rb +27 -0
  29. data/lib/rolify/adapters/mongoid/role_adapter.rb +89 -0
  30. data/lib/rolify/adapters/mongoid/scopes.rb +27 -0
  31. data/lib/rolify/configure.rb +56 -0
  32. data/lib/rolify/dynamic.rb +21 -0
  33. data/lib/rolify/finders.rb +40 -0
  34. data/lib/rolify/matchers.rb +13 -0
  35. data/lib/rolify/railtie.rb +20 -0
  36. data/lib/rolify/resource.rb +31 -0
  37. data/lib/rolify/role.rb +85 -0
  38. data/lib/rolify/utils.rb +10 -0
  39. data/lib/rolify/version.rb +3 -0
  40. data/rolify.gemspec +30 -0
  41. data/spec/README.rdoc +24 -0
  42. data/spec/generators/rolify/rolify_activerecord_generator_spec.rb +163 -0
  43. data/spec/generators/rolify/rolify_mongoid_generator_spec.rb +112 -0
  44. data/spec/generators_helper.rb +21 -0
  45. data/spec/rolify/config_spec.rb +191 -0
  46. data/spec/rolify/custom_spec.rb +20 -0
  47. data/spec/rolify/matchers_spec.rb +24 -0
  48. data/spec/rolify/namespace_spec.rb +24 -0
  49. data/spec/rolify/resource_spec.rb +389 -0
  50. data/spec/rolify/resourcifed_and_rolifed_spec.rb +24 -0
  51. data/spec/rolify/role_spec.rb +20 -0
  52. data/spec/rolify/shared_contexts.rb +92 -0
  53. data/spec/rolify/shared_examples/shared_examples_for_add_role.rb +92 -0
  54. data/spec/rolify/shared_examples/shared_examples_for_callbacks.rb +65 -0
  55. data/spec/rolify/shared_examples/shared_examples_for_dynamic.rb +151 -0
  56. data/spec/rolify/shared_examples/shared_examples_for_finders.rb +77 -0
  57. data/spec/rolify/shared_examples/shared_examples_for_has_all_roles.rb +71 -0
  58. data/spec/rolify/shared_examples/shared_examples_for_has_any_role.rb +71 -0
  59. data/spec/rolify/shared_examples/shared_examples_for_has_role.rb +135 -0
  60. data/spec/rolify/shared_examples/shared_examples_for_only_has_role.rb +174 -0
  61. data/spec/rolify/shared_examples/shared_examples_for_remove_role.rb +121 -0
  62. data/spec/rolify/shared_examples/shared_examples_for_roles.rb +102 -0
  63. data/spec/rolify/shared_examples/shared_examples_for_scopes.rb +38 -0
  64. data/spec/spec_helper.rb +30 -0
  65. data/spec/support/adapters/active_record.rb +76 -0
  66. data/spec/support/adapters/mongoid.rb +143 -0
  67. data/spec/support/adapters/mongoid.yml +6 -0
  68. data/spec/support/data.rb +25 -0
  69. data/spec/support/schema.rb +52 -0
  70. metadata +254 -0
@@ -0,0 +1,77 @@
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
+ end
46
+ end
47
+
48
+ describe ".with_all_roles" do
49
+ it { should respond_to(:with_all_roles) }
50
+
51
+ it { subject.with_all_roles("admin".send(param_method), :staff).should eq([ root ]) }
52
+ it { subject.with_all_roles("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
53
+ it { subject.with_all_roles("admin".send(param_method), "moderator".send(param_method)).should be_empty }
54
+ it { subject.with_all_roles("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Forum }).should be_empty }
55
+ it { subject.with_all_roles({ :name => "moderator".send(param_method), :resource => Forum }, { :name => :manager, :resource => Group }).should eq([ modo ]) }
56
+ it { subject.with_all_roles("moderator".send(param_method), :manager).should be_empty }
57
+ it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => Forum.last }, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
58
+ it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => Group.first }, { :name => "moderator".send(param_method), :resource => Forum }).should eq([ modo ]) }
59
+ it { subject.with_all_roles({ :name => "visitor".send(param_method), :resource => :any }, { :name => "moderator".send(param_method), :resource => :any }).should =~ [ root, modo ] }
60
+ end
61
+
62
+ describe ".with_any_role" do
63
+ it { should respond_to(:with_any_role) }
64
+
65
+ it { subject.with_any_role("admin".send(param_method), :staff).should eq([ root ]) }
66
+ it { subject.with_any_role("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Group }).should eq([ root ]) }
67
+ it { subject.with_any_role("admin".send(param_method), "moderator".send(param_method)).should eq([ root ]) }
68
+ it { subject.with_any_role("admin".send(param_method), :staff, { :name => "moderator".send(param_method), :resource => Forum }).should =~ [ root, modo ] }
69
+ it { subject.with_any_role({ :name => "moderator".send(param_method), :resource => Forum }, { :name => :manager, :resource => Group }).should eq([ modo ]) }
70
+ it { subject.with_any_role({ :name => "moderator".send(param_method), :resource => Group }, { :name => :manager, :resource => Group }).should =~ [ root, modo ] }
71
+ it { subject.with_any_role("moderator".send(param_method), :manager).should be_empty }
72
+ it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => Forum.last }, { :name => "moderator".send(param_method), :resource => Group }).should =~ [ root, visitor ] }
73
+ it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => Group.first }, { :name => "moderator".send(param_method), :resource => Forum }).should eq([ modo ]) }
74
+ it { subject.with_any_role({ :name => "visitor".send(param_method), :resource => :any }, { :name => "moderator".send(param_method), :resource => :any }).should =~ [ root, modo, visitor ] }
75
+ end
76
+ end
77
+ 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 do
5
+ subject.add_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 do
24
+ subject.add_role "player".send(param_method), Forum
25
+ subject.add_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 do
44
+ subject.add_role "visitor".send(param_method), Forum.last
45
+ subject.add_role "leader", Group
46
+ subject.add_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,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,174 @@
1
+ shared_examples_for "#only_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
+ subject do
5
+ user = User.create(:login => "global_user")
6
+ user.add_role "global_role".send(param_method)
7
+ user
8
+ end
9
+
10
+ it { subject.only_has_role?("global_role".send(param_method)).should be_true }
11
+
12
+ context "on resource request" do
13
+ it { subject.only_has_role?("global_role".send(param_method), Forum.first).should be_true }
14
+ it { subject.only_has_role?("global_role".send(param_method), Forum).should be_true }
15
+ it { subject.only_has_role?("global_role".send(param_method), :any).should be_true }
16
+ end
17
+
18
+ context "with another global role" do
19
+ before(:all) { role_class.create(:name => "another_global_role") }
20
+
21
+ it { subject.only_has_role?("another_global_role".send(param_method)).should be_false }
22
+ it { subject.only_has_role?("another_global_role".send(param_method), :any).should be_false }
23
+ end
24
+
25
+ it "should not get an instance scoped role" do
26
+ subject.only_has_role?("moderator".send(param_method), Group.first).should be_false
27
+ end
28
+
29
+ it "should not get a class scoped role" do
30
+ subject.only_has_role?("manager".send(param_method), Forum).should be_false
31
+ end
32
+
33
+ context "using inexisting role" do
34
+ it { subject.only_has_role?("dummy".send(param_method)).should be_false }
35
+ it { subject.only_has_role?("dumber".send(param_method), Forum.first).should be_false }
36
+ end
37
+
38
+ context "with multiple roles" do
39
+ before { subject.add_role "multiple_global_roles".send(param_method) }
40
+
41
+ it { subject.only_has_role?("global_role".send(param_method)).should be_false }
42
+ end
43
+ end
44
+
45
+ context "with a class scoped role", :scope => :class do
46
+ subject do
47
+ user = User.create(:login => "class_user")
48
+ user.add_role "class_role".send(param_method), Forum
49
+ user
50
+ end
51
+
52
+ context "on class scoped role request" do
53
+ it { subject.only_has_role?("class_role".send(param_method), Forum).should be_true }
54
+ it { subject.only_has_role?("class_role".send(param_method), Forum.first).should be_true }
55
+ it { subject.only_has_role?("class_role".send(param_method), :any).should be_true }
56
+ end
57
+
58
+ it "should not get a scoped role when asking for a global" do
59
+ subject.only_has_role?("class_role".send(param_method)).should be_false
60
+ end
61
+
62
+ it "should not get a global role" do
63
+ role_class.create(:name => "global_role")
64
+ subject.only_has_role?("global_role".send(param_method)).should be_false
65
+ end
66
+
67
+ context "with another class scoped role" do
68
+ context "on the same resource but with a different name" do
69
+ before(:all) { role_class.create(:name => "another_class_role", :resource_type => "Forum") }
70
+
71
+ it { subject.only_has_role?("another_class_role".send(param_method), Forum).should be_false }
72
+ it { subject.only_has_role?("another_class_role".send(param_method), :any).should be_false }
73
+ end
74
+
75
+ context "on another resource with the same name" do
76
+ before(:all) { role_class.create(:name => "class_role", :resource_type => "Group") }
77
+
78
+ it { subject.only_has_role?("class_role".send(param_method), Group).should be_false }
79
+ it { subject.only_has_role?("class_role".send(param_method), :any).should be_true }
80
+ end
81
+
82
+ context "on another resource with another name" do
83
+ before(:all) { role_class.create(:name => "another_class_role", :resource_type => "Group") }
84
+
85
+ it { subject.only_has_role?("another_class_role".send(param_method), Group).should be_false }
86
+ it { subject.only_has_role?("another_class_role".send(param_method), :any).should be_false }
87
+ end
88
+ end
89
+
90
+ context "using inexisting role" do
91
+ it { subject.only_has_role?("dummy".send(param_method), Forum).should be_false }
92
+ it { subject.only_has_role?("dumber".send(param_method)).should be_false }
93
+ end
94
+
95
+ context "with multiple roles" do
96
+ before { subject.add_role "multiple_class_roles".send(param_method) }
97
+
98
+ it { subject.only_has_role?("class_role".send(param_method), Forum).should be_false }
99
+ it { subject.only_has_role?("class_role".send(param_method), Forum.first).should be_false }
100
+ it { subject.only_has_role?("class_role".send(param_method), :any).should be_false }
101
+ end
102
+ end
103
+
104
+ context "with a instance scoped role", :scope => :instance do
105
+ subject do
106
+ user = User.create(:login => "instance_user")
107
+ user.add_role "instance_role".send(param_method), Forum.first
108
+ user
109
+ end
110
+
111
+ context "on instance scoped role request" do
112
+ it { subject.only_has_role?("instance_role".send(param_method), Forum.first).should be_true }
113
+ it { subject.only_has_role?("instance_role".send(param_method), :any).should be_true }
114
+ end
115
+
116
+ it "should not get an instance scoped role when asking for a global" do
117
+ subject.only_has_role?("instance_role".send(param_method)).should be_false
118
+ end
119
+
120
+ it "should not get an instance scoped role when asking for a class scoped" do
121
+ subject.only_has_role?("instance_role".send(param_method), Forum).should be_false
122
+ end
123
+
124
+ it "should not get a global role" do
125
+ role_class.create(:name => "global_role")
126
+ subject.only_has_role?("global_role".send(param_method)).should be_false
127
+ end
128
+
129
+ context "with another instance scoped role" do
130
+ context "on the same resource but with a different role name" do
131
+ before(:all) { role_class.create(:name => "another_instance_role", :resource => Forum.first) }
132
+
133
+ it { subject.only_has_role?("another_instance_role".send(param_method), Forum.first).should be_false }
134
+ it { subject.only_has_role?("another_instance_role".send(param_method), :any).should be_false }
135
+ end
136
+
137
+ context "on another resource of the same type but with the same role name" do
138
+ before(:all) { role_class.create(:name => "moderator", :resource => Forum.last) }
139
+
140
+ it { subject.only_has_role?("instance_role".send(param_method), Forum.last).should be_false }
141
+ it { subject.only_has_role?("instance_role".send(param_method), :any).should be_true }
142
+ end
143
+
144
+ context "on another resource of different type but with the same role name" do
145
+ before(:all) { role_class.create(:name => "moderator", :resource => Group.last) }
146
+
147
+ it { subject.only_has_role?("instance_role".send(param_method), Group.last).should be_false }
148
+ it { subject.only_has_role?("instance_role".send(param_method), :any).should be_true }
149
+ end
150
+
151
+ context "on another resource of the same type and with another role name" do
152
+ before(:all) { role_class.create(:name => "another_instance_role", :resource => Forum.last) }
153
+
154
+ it { subject.only_has_role?("another_instance_role".send(param_method), Forum.last).should be_false }
155
+ it { subject.only_has_role?("another_instance_role".send(param_method), :any).should be_false }
156
+ end
157
+
158
+ context "on another resource of different type and with another role name" do
159
+ before(:all) { role_class.create(:name => "another_instance_role", :resource => Group.first) }
160
+
161
+ it { subject.only_has_role?("another_instance_role".send(param_method), Group.first).should be_false }
162
+ it { subject.only_has_role?("another_instance_role".send(param_method), :any).should be_false }
163
+ end
164
+ end
165
+
166
+ context "with multiple roles" do
167
+ before { subject.add_role "multiple_instance_roles".send(param_method), Forum.first }
168
+
169
+ it { subject.only_has_role?("instance_role".send(param_method), Forum.first).should be_false }
170
+ it { subject.only_has_role?("instance_role".send(param_method), :any).should be_false }
171
+ end
172
+ end
173
+ end
174
+ end