the_role 2.1.1 → 2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +27 -3
  3. data/.ruby-gemset.example +1 -0
  4. data/.ruby-version.example +1 -0
  5. data/.rvmrc.example +1 -1
  6. data/.travis.yml +2 -2
  7. data/README.md +46 -47
  8. data/app/assets/javascripts/the_role_editinplace.js.coffee +32 -0
  9. data/app/controllers/admin/role_sections_controller.rb +2 -2
  10. data/app/controllers/admin/roles_controller.rb +2 -2
  11. data/app/controllers/concerns/controller.rb +23 -0
  12. data/{lib/generators/the_role/templates → app/models/_templates_}/role.rb +1 -1
  13. data/app/models/concerns/base.rb +37 -0
  14. data/app/models/concerns/role.rb +134 -0
  15. data/app/models/concerns/user.rb +57 -0
  16. data/app/views/admin/roles/_role.html.haml +90 -69
  17. data/app/views/admin/roles/_sidebar.html.haml +16 -9
  18. data/app/views/admin/roles/new.html.haml +11 -10
  19. data/config/locales/en.yml +1 -0
  20. data/config/locales/es.yml +28 -28
  21. data/config/locales/ru.yml +1 -1
  22. data/db/migrate/20111025025129_create_roles.rb +7 -1
  23. data/docs/2.1.1.png +0 -0
  24. data/lib/generators/the_role/USAGE +1 -1
  25. data/lib/generators/the_role/templates/the_role.rb +7 -2
  26. data/lib/generators/the_role/the_role_generator.rb +8 -5
  27. data/lib/tasks/roles.rake +1 -1
  28. data/lib/the_role/activerecord.rb +11 -0
  29. data/lib/the_role/config.rb +7 -0
  30. data/lib/the_role/version.rb +1 -1
  31. data/lib/the_role.rb +19 -5
  32. data/spec/dummy_app/.gitignore +18 -0
  33. data/spec/dummy_app/.rspec +1 -0
  34. data/spec/dummy_app/Gemfile +32 -0
  35. data/spec/dummy_app/README.md +39 -0
  36. data/spec/dummy_app/Rakefile +6 -0
  37. data/spec/dummy_app/app/assets/images/.keep +0 -0
  38. data/spec/dummy_app/app/assets/javascripts/application.js +17 -0
  39. data/spec/dummy_app/app/assets/stylesheets/app/style.css.scss +23 -0
  40. data/spec/dummy_app/app/assets/stylesheets/application.css +3 -0
  41. data/spec/dummy_app/app/controllers/application_controller.rb +38 -0
  42. data/spec/dummy_app/app/controllers/concerns/.keep +0 -0
  43. data/spec/dummy_app/app/controllers/pages_controller.rb +75 -0
  44. data/spec/dummy_app/app/controllers/users_controller.rb +32 -0
  45. data/spec/dummy_app/app/controllers/welcome_controller.rb +13 -0
  46. data/spec/dummy_app/app/helpers/application_helper.rb +2 -0
  47. data/spec/dummy_app/app/mailers/.keep +0 -0
  48. data/spec/dummy_app/app/models/.keep +0 -0
  49. data/spec/dummy_app/app/models/concerns/.keep +0 -0
  50. data/spec/dummy_app/app/models/page.rb +24 -0
  51. data/spec/dummy_app/app/models/role.rb +4 -0
  52. data/spec/dummy_app/app/models/user.rb +21 -0
  53. data/spec/dummy_app/app/views/layouts/_current_user_info.html.haml +17 -0
  54. data/spec/dummy_app/app/views/layouts/application.html.haml +68 -0
  55. data/spec/dummy_app/app/views/pages/_form.html.haml +19 -0
  56. data/spec/dummy_app/app/views/pages/edit.html.haml +7 -0
  57. data/spec/dummy_app/app/views/pages/index.html.haml +27 -0
  58. data/spec/dummy_app/app/views/pages/manage.html.haml +23 -0
  59. data/spec/dummy_app/app/views/pages/my.html.haml +10 -0
  60. data/spec/dummy_app/app/views/pages/new.html.haml +5 -0
  61. data/spec/dummy_app/app/views/pages/show.html.haml +15 -0
  62. data/spec/dummy_app/app/views/users/edit.html.haml +34 -0
  63. data/spec/dummy_app/app/views/welcome/index.html.haml +38 -0
  64. data/spec/dummy_app/app/views/welcome/profile.html.haml +2 -0
  65. data/spec/dummy_app/bin/bundle +3 -0
  66. data/spec/dummy_app/bin/rails +4 -0
  67. data/spec/dummy_app/bin/rake +4 -0
  68. data/spec/dummy_app/config/application.rb +23 -0
  69. data/spec/dummy_app/config/boot.rb +4 -0
  70. data/spec/dummy_app/config/database.yml +17 -0
  71. data/spec/dummy_app/config/environment.rb +5 -0
  72. data/spec/dummy_app/config/environments/development.rb +29 -0
  73. data/spec/dummy_app/config/environments/production.rb +80 -0
  74. data/spec/dummy_app/config/environments/test.rb +36 -0
  75. data/spec/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
  76. data/spec/dummy_app/config/initializers/devise.rb +227 -0
  77. data/spec/dummy_app/config/initializers/filter_parameter_logging.rb +4 -0
  78. data/spec/dummy_app/config/initializers/inflections.rb +16 -0
  79. data/spec/dummy_app/config/initializers/mime_types.rb +5 -0
  80. data/spec/dummy_app/config/initializers/secret_token.rb +12 -0
  81. data/spec/dummy_app/config/initializers/session_store.rb +3 -0
  82. data/spec/dummy_app/config/initializers/the_role.rb +6 -0
  83. data/spec/dummy_app/config/initializers/wrap_parameters.rb +14 -0
  84. data/spec/dummy_app/config/locales/devise.en.yml +57 -0
  85. data/spec/dummy_app/config/locales/en.yml +7 -0
  86. data/spec/dummy_app/config/locales/ru.yml +4 -0
  87. data/spec/dummy_app/config/routes.rb +20 -0
  88. data/spec/dummy_app/config.ru +4 -0
  89. data/spec/dummy_app/db/migrate/20120212061952_devise_create_users.rb +63 -0
  90. data/spec/dummy_app/db/migrate/20120212063249_create_roles.rb +18 -0
  91. data/spec/dummy_app/db/migrate/20120314061307_create_pages.rb +14 -0
  92. data/spec/dummy_app/db/schema.rb +59 -0
  93. data/spec/dummy_app/db/seeds.rb +85 -0
  94. data/spec/dummy_app/lib/assets/.keep +0 -0
  95. data/spec/dummy_app/lib/tasks/.keep +0 -0
  96. data/spec/dummy_app/lib/tasks/assets.rake +15 -0
  97. data/spec/dummy_app/lib/tasks/db_bootstrap.rake +16 -0
  98. data/spec/dummy_app/log/.keep +0 -0
  99. data/spec/dummy_app/public/404.html +58 -0
  100. data/spec/dummy_app/public/422.html +58 -0
  101. data/spec/dummy_app/public/500.html +57 -0
  102. data/spec/dummy_app/public/favicon.ico +0 -0
  103. data/spec/dummy_app/public/robots.txt +5 -0
  104. data/spec/dummy_app/spec/controllers/admin_roles_controller_spec.rb +52 -0
  105. data/spec/dummy_app/spec/controllers/pages_controller_spec.rb +141 -0
  106. data/spec/dummy_app/spec/controllers/welcome_controller_spec.rb +66 -0
  107. data/spec/dummy_app/spec/factories/page.rb +6 -0
  108. data/spec/dummy_app/spec/factories/role.rb +55 -0
  109. data/spec/dummy_app/spec/factories/user.rb +11 -0
  110. data/spec/dummy_app/spec/models/hash_spec.rb +272 -0
  111. data/spec/dummy_app/spec/models/param_process_spec.rb +44 -0
  112. data/spec/dummy_app/spec/models/role_spec.rb +218 -0
  113. data/spec/dummy_app/spec/models/user_spec.rb +164 -0
  114. data/spec/dummy_app/spec/routing/admin_roles_routing_spec.rb +41 -0
  115. data/spec/dummy_app/spec/routing/pages_routing_spec.rb +35 -0
  116. data/spec/dummy_app/spec/spec_helper.rb +68 -0
  117. data/spec/dummy_app/vendor/assets/javascripts/.keep +0 -0
  118. data/spec/dummy_app/vendor/assets/stylesheets/.keep +0 -0
  119. data/the_role.gemspec +2 -1
  120. data/the_role.yml.teamocil.example +11 -0
  121. metadata +213 -16
  122. data/app/assets/javascripts/the_role.js.coffee +0 -31
  123. data/app/assets/stylesheets/the_role/bootstrap_sass.css.scss +0 -20
  124. data/app/assets/stylesheets/the_role.css.scss +0 -47
  125. data/app/controllers/the_role_controller.rb +0 -18
  126. data/app/models/concerns/role_model.rb +0 -125
  127. data/app/models/concerns/the_role_base.rb +0 -35
  128. data/app/models/concerns/the_role_user_model.rb +0 -54
@@ -0,0 +1,11 @@
1
+ FactoryGirl.define do
2
+ factory :user, class: User do
3
+ sequence(:email) { Faker::Internet.email }
4
+ sequence(:name) { Faker::Name.name }
5
+ sequence(:company){ Faker::Company.name }
6
+ sequence(:address){ Faker::Address.street_address }
7
+
8
+ password 'qwerty'
9
+ password_confirmation { |u| u.password }
10
+ end
11
+ end
@@ -0,0 +1,272 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+ context "METHODS" do
5
+ it "underscorify_keys" do
6
+ Hash.new.respond_to?(:underscorify_keys).should eq true
7
+ end
8
+
9
+ it "underscorify_keys!" do
10
+ Hash.new.respond_to?(:underscorify_keys!).should eq true
11
+ end
12
+
13
+ it "deep_reset" do
14
+ Hash.new.respond_to?(:deep_reset).should eq true
15
+ end
16
+
17
+ it "deep_reset!" do
18
+ Hash.new.respond_to?(:deep_reset!).should eq true
19
+ end
20
+
21
+ it "deep_merge" do
22
+ Hash.new.respond_to?(:deep_merge).should eq true
23
+ end
24
+
25
+ it "deep_merge!" do
26
+ Hash.new.respond_to?(:deep_merge!).should eq true
27
+ end
28
+ end
29
+
30
+ context "deep HASH.underscorify_keys" do
31
+ it "UNDERSCORIFY hash keys" do
32
+ hash = {
33
+ 'a b' => 1,
34
+ "x y" => {
35
+ :hello => 1,
36
+ :'hello-world' => 1,
37
+ 'good MorninG mom!' => 1
38
+ }
39
+ }
40
+ result = {
41
+ 'a_b' => 1,
42
+ 'x_y' => {
43
+ 'hello' => 1,
44
+ 'hello_world' => 1,
45
+ 'good_morning_mom' => 1
46
+ }
47
+ }
48
+ hash.underscorify_keys.should eq result
49
+ hash.should_not eq result
50
+ end
51
+
52
+ it "UNDERSCORIFY! hash keys" do
53
+ hash = {
54
+ 'a b' => 1,
55
+ "x y" => {
56
+ 'hello' => 1
57
+ }
58
+ }
59
+ result = {
60
+ 'a_b' => 1,
61
+ 'x_y' => {
62
+ 'hello' => 1
63
+ }
64
+ }
65
+ hash.underscorify_keys!.should eq result
66
+ hash.should eq result
67
+ end
68
+ end
69
+
70
+ context "HASH.deep_reset" do
71
+ before(:each) do
72
+ @hash = {
73
+ :pages => {
74
+ :index => true,
75
+ :show => true,
76
+ :new => false,
77
+ :edit => false,
78
+ :update => false,
79
+ :destroy => false
80
+ },
81
+ :articles => {
82
+ :index => true,
83
+ :show => true
84
+ }
85
+ }
86
+ end
87
+
88
+ it "DEEP RESET hash values to nil" do
89
+ result = {
90
+ :pages => {
91
+ :index => nil,
92
+ :show => nil,
93
+ :new => nil,
94
+ :edit => nil,
95
+ :update => nil,
96
+ :destroy => nil
97
+ },
98
+ :articles => {
99
+ :index => nil,
100
+ :show => nil
101
+ }
102
+ }
103
+ hash = @hash.deep_reset
104
+ hash.should eq result
105
+ end
106
+
107
+ it "DEEP RESET hash values to nil :test" do
108
+ result = {
109
+ :pages => {
110
+ :index => :test,
111
+ :show => :test,
112
+ :new => :test,
113
+ :edit => :test,
114
+ :update => :test,
115
+ :destroy => :test
116
+ },
117
+ :articles => {
118
+ :index => :test,
119
+ :show => :test
120
+ }
121
+ }
122
+ hash = @hash.deep_reset(:test)
123
+ hash.should eq result
124
+ end
125
+
126
+ it "DEEP RESET! hash values to nil" do
127
+ result = {
128
+ :pages => {
129
+ :index => :test,
130
+ :show => :test,
131
+ :new => :test,
132
+ :edit => :test,
133
+ :update => :test,
134
+ :destroy => :test
135
+ },
136
+ :articles => {
137
+ :index => :test,
138
+ :show => :test
139
+ }
140
+ }
141
+ hash = @hash.deep_reset(:test)
142
+ hash.should eq result
143
+ @hash.should_not eq result
144
+ end
145
+ end
146
+
147
+ context "DEEP RESET! with different keys types" do
148
+ it "work fine" do
149
+ hash = {
150
+ 'pages' => {
151
+ :index => true,
152
+ 'edit' => true
153
+ }
154
+ }
155
+ result = {
156
+ 'pages' => {
157
+ :index => nil,
158
+ 'edit' => nil
159
+ }
160
+ }
161
+ hash.deep_reset!(nil).should eq result
162
+ end
163
+ end
164
+
165
+ context "DEEP MERGE" do
166
+ it " => hashes" do
167
+ first_hash = {
168
+ 'pages' => {
169
+ 'index' => false,
170
+ 'edit' => false
171
+ }
172
+ }
173
+
174
+ second_hash = {
175
+ 'pages' => {
176
+ 'index' => true,
177
+ 'edit' => false,
178
+ 'destroy' => true
179
+ }
180
+ }
181
+
182
+ result = {
183
+ 'pages' => {
184
+ 'index' => true,
185
+ 'edit' => false,
186
+ 'destroy' => true
187
+ }
188
+ }
189
+
190
+ first_hash.deep_merge!(second_hash).should eq result
191
+ end
192
+
193
+ it "should merge 2 hash (1st 3-lavels, 2nd - 2 levels => 2 level)" do
194
+ first_hash = {
195
+ 'pages' => {
196
+ 'index' => false,
197
+ 'edit' => {
198
+ 'one' => 1,
199
+ 'two' => 2
200
+ }
201
+ }
202
+ }
203
+
204
+ second_hash = {
205
+ 'pages' => {
206
+ 'index' => true,
207
+ 'edit' => false,
208
+ 'destroy' => true
209
+ }
210
+ }
211
+
212
+ result = {
213
+ 'pages' => {
214
+ 'index' => true,
215
+ 'edit' => false,
216
+ 'destroy' => true
217
+ }
218
+ }
219
+
220
+ first_hash.deep_merge!(second_hash).should eq result
221
+ end
222
+ end
223
+
224
+ context "DEEP MERGE!" do
225
+ before(:each) do
226
+ @incoming_hash = {
227
+ 'pages' => {
228
+ 'edit' => true,
229
+ 'manage' => true,
230
+ 'destroy' => true
231
+ }
232
+ }
233
+ @base_hash = {
234
+ 'pages' => {
235
+ 'index' => true,
236
+ 'edit' => false,
237
+ 'destroy' => false,
238
+ 'banners' => false
239
+ }
240
+ }
241
+ end
242
+
243
+ it 'should reset base hash to false' do
244
+ result = {
245
+ 'pages' => {
246
+ 'index' => nil,
247
+ 'edit' => nil,
248
+ 'destroy' => nil,
249
+ 'banners' => nil
250
+ }
251
+ }
252
+ @base_hash.deep_reset!
253
+ @base_hash.should == result
254
+ end
255
+
256
+ it 'should merge 2 hashes' do
257
+ result = {
258
+ 'pages' => {
259
+ 'index' => false,
260
+ 'edit' => true,
261
+ 'manage' => true,
262
+ 'destroy' => true,
263
+ 'banners' => false
264
+ }
265
+ }
266
+
267
+ @base_hash.deep_reset!(false)
268
+ @base_hash.deep_merge! @incoming_hash
269
+ @base_hash.should == result
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe TheRoleParam do
6
+ it 'module TheRoleParam should be defined' do
7
+ TheRoleParam.class.should be Module
8
+ end
9
+
10
+ it 'string process 1' do
11
+ TheRoleParam.process('hello world!').should eq 'hello_world'
12
+ end
13
+
14
+ it 'string process 2' do
15
+ TheRoleParam.process(:hello_world!).should eq 'hello_world'
16
+ end
17
+
18
+ it 'string process 3' do
19
+ TheRoleParam.process("hello ! world").should eq 'hello_world'
20
+ end
21
+
22
+ it 'string process 4' do
23
+ TheRoleParam.process("HELLO $!= WorlD").should eq 'hello_world'
24
+ end
25
+
26
+ it 'string process 5' do
27
+ TheRoleParam.process("HELLO---WorlD").should eq 'hello_world'
28
+ end
29
+
30
+ it "should work with Controller Name" do
31
+ ctrl = PagesController.new
32
+ ctrl.controller_path
33
+
34
+ TheRoleParam.process(ctrl.controller_path).should eq 'pages'
35
+ end
36
+
37
+ it "should work with Nested Controller Name" do
38
+ class Admin::PagesController < ApplicationController; end
39
+ ctrl = Admin::PagesController.new
40
+ ctrl.controller_path
41
+
42
+ TheRoleParam.process(ctrl.controller_path).should eq 'admin_pages'
43
+ end
44
+ end
@@ -0,0 +1,218 @@
1
+ require 'spec_helper'
2
+
3
+ describe Role do
4
+ context "Role *Create* methods" do
5
+ it "New/Create invalid" do
6
+ role = Role.new
7
+ role.save.should be_false
8
+
9
+ role.should have(1).error_on(:name)
10
+ role.should have(1).error_on(:title)
11
+ role.should have(1).error_on(:description)
12
+ end
13
+
14
+ it "New/Create valid, without Role hash" do
15
+ role = Role.new
16
+
17
+ role.name = :user
18
+ role.title = :user_title
19
+ role.description = :role_description
20
+
21
+ role.save.should be_true
22
+ end
23
+
24
+ it "New/Create valid, without Role hash (2)" do
25
+ Role.new(
26
+ name: :user,
27
+ title: :user_title,
28
+ description: :role_description
29
+ ).save.should be_true
30
+ end
31
+
32
+ it "New/Create, without Role hash, default Role hash value" do
33
+ role = FactoryGirl.create :role_without_rules
34
+ role.the_role.should == "{}"
35
+ role.to_hash.should == {}
36
+ end
37
+
38
+ it "New/Create, role methods result types" do
39
+ role = FactoryGirl.create :role_without_rules
40
+ role.the_role.should be_an_instance_of String
41
+ role.to_hash.should be_an_instance_of Hash
42
+ end
43
+ end
44
+
45
+ context "Role *Section/Rule create* methods" do
46
+ before(:each) do
47
+ @role = FactoryGirl.create :role_without_rules
48
+ end
49
+
50
+ it "should have empty Role hash" do
51
+ @role.to_hash.should == {}
52
+ end
53
+
54
+ it "Create section" do
55
+ @role.create_section(:articles)
56
+ @role.to_hash.should == { "articles" => {} }
57
+ end
58
+
59
+ it "Create rule (with section)" do
60
+ @role.create_section(:articles)
61
+ @role.create_rule(:articles, :index)
62
+ @role.to_hash.should == { "articles" => { "index" => false } }
63
+ end
64
+
65
+ it "Create rule (without section)" do
66
+ @role.create_rule(:articles, :index)
67
+ @role.to_hash.should == { "articles" => { "index" => false } }
68
+ end
69
+ end
70
+
71
+ context "*has?* is aliace of *has_role?*" do
72
+ before(:each) do
73
+ @role = FactoryGirl.create :role_user
74
+ end
75
+
76
+ it "aliace methods" do
77
+ @role.has?(:pages, :index).should be_true
78
+ @role.has_role?(:pages, :index).should be_true
79
+
80
+ @role.has?(:pages, :secret).should be_false
81
+ @role.has_role?(:pages, :secret).should be_false
82
+ end
83
+ end
84
+
85
+ context "Rule *On/Off* methods" do
86
+ before(:each) do
87
+ @role = FactoryGirl.create :role_user
88
+ end
89
+
90
+ it "has access to pages/index" do
91
+ @role.has_role?(:pages, :index).should be_true
92
+ end
93
+
94
+ it "set pages/index on false" do
95
+ @role.rule_off(:pages, :index)
96
+ @role.has_role?(:pages, :index).should be_false
97
+ end
98
+
99
+ it "has no access to pages/secret" do
100
+ @role.has_role?(:pages, :secret).should be_false
101
+ end
102
+
103
+ it "set pages/secret on true" do
104
+ @role.rule_on(:pages, :secret)
105
+ @role.has_role?(:pages, :secret).should be_true
106
+ end
107
+ end
108
+
109
+ context "Class Methods" do
110
+ before(:each) do
111
+ @role = FactoryGirl.create :role_user
112
+ end
113
+
114
+ it "Role.with_name(:name) method" do
115
+ Role.with_name(:user).should be_an_instance_of Role
116
+ Role.with_name('user').should be_an_instance_of Role
117
+
118
+ Role.with_name(:moderator).should be_nil
119
+ Role.with_name('moderator').should be_nil
120
+ end
121
+ end
122
+
123
+ context "*Delete* methods" do
124
+ before(:each) do
125
+ @role = FactoryGirl.create :role_user
126
+ end
127
+
128
+ it "*has_section?* method" do
129
+ @role.has_section?(:pages).should be_true
130
+ @role.has_section?(:articles).should be_false
131
+ end
132
+
133
+ it "has pages section" do
134
+ @role.to_hash['pages'].should be_an_instance_of Hash
135
+ end
136
+
137
+ it "has pages/index value" do
138
+ @role.to_hash['pages']['index'].should be_true
139
+ end
140
+
141
+ it "delete rule pages/index" do
142
+ @role.delete_rule(:pages, :index)
143
+ @role.to_hash['pages']['index'].should be_nil
144
+ end
145
+
146
+ it "delete section pages" do
147
+ @role.delete_section(:pages)
148
+ @role.to_hash['pages'].should be_nil
149
+ end
150
+ end
151
+
152
+ context "*helper* methods" do
153
+ before(:each) do
154
+ @role = FactoryGirl.create :role_without_rules
155
+ end
156
+
157
+ it "to_hash on empty rules set" do
158
+ @role.to_hash.should == {}
159
+ end
160
+
161
+ it "to_json on empty rules set" do
162
+ @role.to_json.should == "{}"
163
+ end
164
+ end
165
+
166
+ context "Update method" do
167
+ before(:each) do
168
+ @role = FactoryGirl.create :role_user
169
+ end
170
+
171
+ it "should has true rules" do
172
+ @role.has?(:pages, :index).should be_true
173
+ @role.has?(:pages, :edit).should be_true
174
+ @role.has?(:pages, :update).should be_true
175
+ @role.has?(:pages, :secret).should be_false
176
+
177
+ @role.has?(:articles, :index).should be_false
178
+ end
179
+
180
+ it "should has true rules" do
181
+ @role.update_role({ articles: { index: true } })
182
+
183
+ @role.has?(:pages, :index).should be_false
184
+ @role.has?(:pages, :edit).should be_false
185
+ @role.has?(:pages, :update).should be_false
186
+ @role.has?(:pages, :secret).should be_false
187
+
188
+ @role.has?(:articles, :index).should be_true
189
+ end
190
+
191
+ it "should has any rules 1" do
192
+ @role.has?(:pages, :index).should be_true
193
+ @role.has?(:pages, :update).should be_true
194
+
195
+ @role.any?({ pages: :index }).should be_true
196
+ @role.any?({ pages: :update }).should be_true
197
+ @role.any?({ pages: :index, pages: :update}).should be_true
198
+ end
199
+
200
+ it "should has any rules 2" do
201
+ @role.has?(:pages, :index).should be_true
202
+ @role.has?(:articles, :index).should be_false
203
+
204
+ @role.any?({ pages: :index }).should be_true
205
+ @role.any?({ articles: :index }).should be_false
206
+
207
+ @role.any?({ articles: :index }).should be_false
208
+ @role.any?({ pages: :index, articles: :index}).should be_true
209
+ @role.any?({ pages: :index, pages: :update}).should be_true
210
+ end
211
+
212
+ it "should has any rules 3, easy syntaxis" do
213
+ @role.any?(articles: :index).should be_false
214
+ @role.any?(pages: :index, articles: :index).should be_true
215
+ @role.any?(pages: :index, pages: :update).should be_true
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,164 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe User do
6
+ describe "Owner check for User and object" do
7
+ context "Regular user" do
8
+ before(:each) do
9
+ @user_1 = FactoryGirl.create(:user)
10
+ @user_2 = FactoryGirl.create(:user)
11
+
12
+ @user_1_page = FactoryGirl.create(:page, user: @user_1)
13
+ @user_2_page = FactoryGirl.create(:page, user: @user_2)
14
+ end
15
+
16
+ it 'should have test varaibles' do
17
+ @user_1.should be_instance_of User
18
+ @user_2.should be_instance_of User
19
+
20
+ @user_1_page.should be_instance_of Page
21
+ @user_2_page.should be_instance_of Page
22
+ end
23
+
24
+ it 'should be owner of page' do
25
+ @user_1.owner?(@user_1_page).should be_true
26
+ @user_2.owner?(@user_2_page).should be_true
27
+ end
28
+
29
+ it 'should not be owner of page' do
30
+ @user_1.owner?(@user_2_page).should be_false
31
+ @user_2.owner?(@user_1_page).should be_false
32
+ end
33
+ end
34
+
35
+ context "Moderator" do
36
+ before(:each) do
37
+ mrole = FactoryGirl.create(:role_moderator)
38
+
39
+ @moderator = FactoryGirl.create(:user, role: mrole)
40
+ @user = FactoryGirl.create(:user)
41
+
42
+ @moderator_page = FactoryGirl.create(:page, user: @moderator)
43
+ @user_page = FactoryGirl.create(:page, user: @user)
44
+ end
45
+
46
+ it 'Moderator is owner of any Page' do
47
+ @moderator.owner?(@moderator_page).should be_true
48
+ @moderator.owner?(@user_page).should be_true
49
+ end
50
+
51
+ it 'User is owner of his Pages' do
52
+ @user.owner?(@user_page).should be_true
53
+ @user.owner?(@moderator_page).should be_false
54
+ end
55
+ end
56
+
57
+ context "Admin" do
58
+ # not important. to implement later
59
+ end
60
+
61
+ # context "Custom Page relation to User" do
62
+ # before(:each) do
63
+ # Page.class_eval do
64
+ # belongs_to :user, class_name: User, foreign_key: 'person_id'
65
+ # end
66
+
67
+ # @user = FactoryGirl.create(:user)
68
+ # @page = FactoryGirl.create(:page, user: @user)
69
+ # end
70
+
71
+ # it 'relation via person_id' do
72
+ # @page.user_id.should eq @user.id
73
+ # @page.person_id.should eq @user.id
74
+ # end
75
+ # end
76
+ end
77
+
78
+ describe "Create user without any Role" do
79
+ before(:each) do
80
+ FactoryGirl.create(:user)
81
+ @user = User.first
82
+ end
83
+
84
+ it "Create test user" do
85
+ User.count.should be 1
86
+ end
87
+
88
+ it "User have not any role" do
89
+ @user.role.should be_nil
90
+ end
91
+
92
+ it "User should gives false on any request" do
93
+ @user.has_role?(:pages, :index).should be_false
94
+ @user.has_role?(:moderator, :pages).should be_false
95
+ end
96
+ end
97
+
98
+ describe "Create user with default Role" do
99
+ before(:each) do
100
+ TheRole.config.default_user_role = :user
101
+ FactoryGirl.create(:role_user)
102
+ FactoryGirl.create(:user)
103
+ @user = User.first
104
+ end
105
+
106
+ it "User and Role should exists" do
107
+ Role.count.should be 1
108
+ User.count.should be 1
109
+ end
110
+
111
+ it "Role should nave name :user" do
112
+ Role.first.name.should eq 'user'
113
+ end
114
+
115
+ it "User should have default Role" do
116
+ @user.role.should_not be_nil
117
+ end
118
+
119
+ it "User has Role for Pages" do
120
+ @user.has_role?(:pages, :index).should be_true
121
+ @user.has_role?(:pages, :destroy).should be_true
122
+ end
123
+
124
+ it "User has disabled rule" do
125
+ @user.has_role?(:pages, :secret).should be_false
126
+ end
127
+
128
+ it "User try to have access to undefined rule" do
129
+ @user.has_role?(:pages, :wrong_name).should be_false
130
+ end
131
+
132
+ it "User has not Role for Atricles" do
133
+ @user.has_role?(:articles, :index).should be_false
134
+ end
135
+
136
+ # Any
137
+ it "should has any rules 1" do
138
+ @user.has_role?(:pages, :index).should be_true
139
+ @user.has_role?(:pages, :update).should be_true
140
+
141
+ @user.any_role?({ pages: :index }).should be_true
142
+ @user.any_role?({ pages: :update }).should be_true
143
+ @user.any_role?({ pages: :index, pages: :update}).should be_true
144
+ end
145
+
146
+ it "should has any rules 2" do
147
+ @user.has_role?(:pages, :index).should be_true
148
+ @user.has_role?(:articles, :index).should be_false
149
+
150
+ @user.any_role?({ pages: :index }).should be_true
151
+ @user.any_role?({ articles: :index }).should be_false
152
+
153
+ @user.any_role?({ articles: :index }).should be_false
154
+ @user.any_role?({ pages: :index, articles: :index}).should be_true
155
+ @user.any_role?({ pages: :index, pages: :update}).should be_true
156
+ end
157
+
158
+ it "should has any rules 3, easy syntaxis" do
159
+ @user.any_role?(articles: :index).should be_false
160
+ @user.any_role?(pages: :index, articles: :index).should be_true
161
+ @user.any_role?(pages: :index, pages: :update).should be_true
162
+ end
163
+ end
164
+ end