cbac 0.6.5 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  <div class="cbac">
2
2
 
3
3
  <h2>Subset:</h2>
4
- <form action="<%= request.request_uri %>" method="get" name="subset_view_form">
4
+ <form action="<%= request.url %>" method="get" name="subset_view_form">
5
5
  <b>Privilege set</b> starts with: <input type="text" name="priv_substr" value="<%= params[:priv_substr] %>" /><br />
6
6
  <b>Role</b> starts with: <input type="text" name="role_substr" value="<%= params[:role_substr] %>" /><br/>
7
7
  <input type="submit" value="Submit" />
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'cbac'
3
+
4
+ require_relative './fixtures/controllers/dating/daughter_controller'
5
+
6
+ # create a fake controller with some actions
7
+ describe Cbac do
8
+ describe :authorization_check do
9
+ include Cbac
10
+
11
+ before :all do
12
+ @controller = Dating::DaughterController.new
13
+
14
+ # define a set of privileges
15
+ Cbac::PrivilegeSet.add :go_out_with_daughter, "Allows users to perform the actions nested in this privilege set"
16
+ # add some privileges to the given set
17
+ Privilege.resource :go_out_with_daughter, "dating/daughter_controller/take_to_dinner", :post
18
+ Privilege.resource :go_out_with_daughter, "dating/daughter_controller/bring_home", :post
19
+
20
+ # define a context role that can be evaluated when one of the privileges is invoked
21
+ ContextRole.add :suitable_boyfriend do |context|
22
+ context.send(:candidate).brought_flowers?
23
+ end
24
+
25
+ # allow any 'suitable_boyfriend' to invoke Privileges in the 'go_out_with_daughter' PrivilegeSet
26
+ Cbac::Permission.create(
27
+ :context_role => 'suitable_boyfriend',
28
+ :privilege_set_id => Cbac::PrivilegeSetRecord.where(
29
+ :name => 'go_out_with_daughter'
30
+ ).first.id
31
+ )
32
+ end
33
+
34
+ context "when a user attempts to invoke the action" do
35
+ before :each do
36
+ @controller.request = ActionDispatch::TestRequest.new
37
+ @controller.request.request_method = 'POST'
38
+
39
+ @controller.params = {
40
+ :controller => "dating/daughter_controller",
41
+ :action => "take_to_dinner"
42
+ }
43
+ end
44
+
45
+ context "and the contextual requirements are fulfilled" do
46
+ before :each do
47
+ ideal_son_in_law = mock('user', :brought_flowers? => true)
48
+ @controller.stub(:candidate).and_return(ideal_son_in_law)
49
+ end
50
+
51
+ specify "the action is invoked" do
52
+ @controller.authorize.should == true
53
+ end
54
+ end
55
+
56
+ context "and the contextual requirements are not fulfilled" do
57
+ before :each do
58
+ some_punk = mock('user', :brought_flowers? => false)
59
+ @controller.stub(:candidate).and_return(some_punk)
60
+ end
61
+
62
+ specify "the action is blocked" do
63
+ @controller.should_receive(:unauthorized)
64
+
65
+ @controller.authorize
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,18 +1,13 @@
1
- require 'spec'
2
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
3
- require 'cbac/cbac_pristine/pristine'
4
- require 'cbac/cbac_pristine/pristine_permission'
5
- require 'cbac/cbac_pristine/pristine_role'
6
- require 'cbac/cbac_pristine/pristine_file'
1
+ require 'spec_helper'
2
+
7
3
  include Cbac::CbacPristine
8
4
 
9
5
  describe "CbacPristineFile" do
10
6
  before(:each) do
11
- @pristine_file = PristineFile.new("cbac.pristine")
7
+ @pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
12
8
  end
13
9
 
14
10
  describe "indicate if a line looks like a pristine line" do
15
-
16
11
  it "should indicate that a ruby style comment line is not a pristine line" do
17
12
  comment_line = "#this is a comment line in Ruby"
18
13
 
@@ -68,7 +63,7 @@ describe "CbacPristineFile" do
68
63
  privilege_set_name = "chat"
69
64
  line = "0:+:PrivilegeSet(#{privilege_set_name})Admin()"
70
65
 
71
- @pristine_file.parse_privilege_set_name(line, 0).should == privilege_set_name
66
+ @pristine_file.parse_privilege_set_name(line, 0).should == privilege_set_name
72
67
  end
73
68
 
74
69
  it "should fail if an invalid line is provided" do
@@ -142,7 +137,7 @@ describe "CbacPristineFile" do
142
137
 
143
138
 
144
139
  it "should return a generic role if a generic pristine file is used" do
145
- @pristine_file = GenericPristineFile.new("cbac.pristine")
140
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
146
141
  line = "0:+:PrivilegeSet(chat)GenericRole(group_admins)"
147
142
 
148
143
  @pristine_file.parse_role(line, 0).role_type.should == PristineRole.ROLE_TYPES[:generic]
@@ -150,7 +145,7 @@ describe "CbacPristineFile" do
150
145
 
151
146
  it "should return an existing generic role if use_db is not specified" do
152
147
  generic_role_name = 'group_admins'
153
- @pristine_file = GenericPristineFile.new("cbac.pristine")
148
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
154
149
  line = "0:+:PrivilegeSet(chat)GenericRole(#{generic_role_name})"
155
150
  existing_role = PristineRole.create(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:generic], :name => generic_role_name)
156
151
 
@@ -159,7 +154,7 @@ describe "CbacPristineFile" do
159
154
 
160
155
  it "should not use an existing role if use_db is set to false" do
161
156
  generic_role_name = 'group_admins'
162
- @pristine_file = GenericPristineFile.new("cbac.pristine")
157
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
163
158
  line = "0:+:PrivilegeSet(chat)GenericRole(#{generic_role_name})"
164
159
  existing_role = PristineRole.create(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:generic], :name => generic_role_name)
165
160
 
@@ -167,7 +162,7 @@ describe "CbacPristineFile" do
167
162
  end
168
163
 
169
164
  it "should fail if an Admin role is used in a generic pristine file" do
170
- @pristine_file = GenericPristineFile.new("cbac.pristine")
165
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
171
166
  line = "0:+:PrivilegeSet(chat)Admin()"
172
167
 
173
168
  proc{
@@ -176,7 +171,7 @@ describe "CbacPristineFile" do
176
171
  end
177
172
 
178
173
  it "should fail if an context role is used in a generic pristine file" do
179
- @pristine_file = GenericPristineFile.new("cbac.pristine")
174
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
180
175
  line = "0:+:PrivilegeSet(chat)ContextRole(logged_in_user)"
181
176
 
182
177
  proc{
@@ -185,7 +180,7 @@ describe "CbacPristineFile" do
185
180
  end
186
181
 
187
182
  it "should fail if an invalid line is provided in a generic pristine file" do
188
- @pristine_file = GenericPristineFile.new("cbac.pristine")
183
+ @pristine_file = GenericPristineFile.new(:file_name =>"cbac.pristine")
189
184
  line = "0:+:PrivilegeSet(toeteraars)"
190
185
 
191
186
  proc{
@@ -195,14 +190,13 @@ describe "CbacPristineFile" do
195
190
  end
196
191
 
197
192
  describe "parsing a cbac_pristine file" do
198
-
199
193
  it "should fail if a row number is used twice" do
200
194
  pristine_file_lines = ["0:+:PrivilegeSet(chat)ContextRole(logged_in_user)"]
201
195
  pristine_file_lines.push("0:+:PrivilegeSet(log_in)ContextRole(everybody)")
202
-
196
+
203
197
  File.stub!(:open).and_return(pristine_file_lines)
204
198
 
205
- pristine_file = PristineFile.new("cbac.pristine")
199
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
206
200
 
207
201
  proc{
208
202
  pristine_file.parse
@@ -216,7 +210,7 @@ describe "CbacPristineFile" do
216
210
 
217
211
  File.stub!(:open).and_return(pristine_file_lines)
218
212
 
219
- pristine_file = PristineFile.new("cbac.pristine")
213
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
220
214
  pristine_file.parse
221
215
 
222
216
  pristine_file.permissions.length.should == pristine_file_lines.length
@@ -229,7 +223,7 @@ describe "CbacPristineFile" do
229
223
 
230
224
  File.stub!(:open).and_return(pristine_file_lines)
231
225
 
232
- pristine_file = PristineFile.new("cbac.pristine")
226
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
233
227
  pristine_file.parse
234
228
 
235
229
  pristine_file.permissions.length.should == 2
@@ -242,7 +236,7 @@ describe "CbacPristineFile" do
242
236
 
243
237
  File.stub!(:open).and_return(pristine_file_lines)
244
238
 
245
- pristine_file = PristineFile.new("cbac.pristine")
239
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
246
240
  pristine_file.parse
247
241
 
248
242
  pristine_file.permissions.length.should == 3
@@ -256,7 +250,7 @@ describe "CbacPristineFile" do
256
250
 
257
251
  File.stub!(:open).and_return(pristine_file_lines)
258
252
 
259
- pristine_file = PristineFile.new("cbac.pristine")
253
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
260
254
  proc{
261
255
  pristine_file.parse
262
256
  }.should raise_error(SyntaxError)
@@ -266,7 +260,7 @@ describe "CbacPristineFile" do
266
260
  pristine_file_lines = ["0:x:PrivilegeSet(chat)ContextRole(logged_in_user)"]
267
261
  File.stub!(:open).and_return(pristine_file_lines)
268
262
 
269
- pristine_file = PristineFile.new("cbac.pristine")
263
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
270
264
  proc{
271
265
  pristine_file.parse
272
266
  }.should raise_error(NotImplementedError)
@@ -276,7 +270,7 @@ describe "CbacPristineFile" do
276
270
  pristine_file_lines = ["0:=>:PrivilegeSet(chat)ContextRole(logged_in_user)"]
277
271
  File.stub!(:open).and_return(pristine_file_lines)
278
272
 
279
- pristine_file = PristineFile.new("cbac.pristine")
273
+ pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
280
274
  proc{
281
275
  pristine_file.parse
282
276
  }.should raise_error(NotImplementedError)
@@ -287,7 +281,7 @@ describe "CbacPristineFile" do
287
281
  before(:each) do
288
282
  @context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "logged_in_user")
289
283
  @admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin],:name => "administrator")
290
- @pristine_file = PristineFile.new("cbac.pristine")
284
+ @pristine_file = PristineFile.new(:file_name =>"cbac.pristine")
291
285
  end
292
286
 
293
287
  it "should filter out the permissions which were revoked" do
@@ -324,7 +318,6 @@ describe "CbacPristineFile" do
324
318
  proc {
325
319
  @pristine_file.permission_set
326
320
  }.should raise_error(ArgumentError)
327
-
328
321
  end
329
322
  end
330
- end
323
+ end
@@ -1,75 +1,67 @@
1
-
2
- require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
3
- require 'spec'
4
- require '../lib/cbac/cbac_pristine/pristine'
5
- require '../lib/cbac/cbac_pristine/pristine_role'
6
- require '../lib/cbac/cbac_pristine/pristine_permission'
1
+ require 'spec_helper'
7
2
 
8
3
  include Cbac::CbacPristine
9
4
 
10
5
  describe "CbacPristinePermission" do
11
-
12
-
13
6
  describe "convert pristine line to a yml fixture" do
14
7
  before(:each) do
15
- @context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "logged_in_user")
16
8
  @admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin], :name => "administrator")
9
+ @context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "chat_starter")
17
10
  end
18
11
 
19
-
20
12
  it "should raise an error if the pristine line has no role" do
21
- pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => 'log_in', :pristine_role => nil)
22
- lambda{
13
+ pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => 'log_in', :pristine_role => nil)
14
+ lambda {
23
15
  pristine_permission.to_yml_fixture
24
16
  }.should raise_error(ArgumentError)
25
17
  end
26
18
 
27
19
  it "should raise an error if the pristine line has no privilege_set_name" do
28
- pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "", :pristine_role => @context_role)
29
- lambda{
20
+ pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "", :pristine_role => @context_role)
21
+ lambda {
30
22
  pristine_permission.to_yml_fixture
31
23
  }.should raise_error(ArgumentError)
32
24
  end
33
25
 
34
26
  it "should return a yml string starting with cbac_permission_ " do
35
- pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "chat", :pristine_role => @context_role)
27
+ pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "chat", :pristine_role => @context_role)
36
28
 
37
29
  pristine_permission.to_yml_fixture.should match(/\Acbac_permission_/)
38
30
  end
39
31
 
40
32
  it "should return a yml string containing the line number of the pristine line" do
41
33
  line_number= 100
42
- pristine_permission = PristinePermission.new(:line_number => line_number, :privilege_set_name => "chat", :pristine_role => @context_role)
34
+ pristine_permission = PristinePermission.new(:line_number => line_number, :privilege_set_name => "chat", :pristine_role => @context_role)
43
35
 
44
36
  pristine_permission.to_yml_fixture.should match(/id: #{line_number}/)
45
37
  end
46
38
 
47
39
  it "should return a yml string containing a generic role id of 0 if a context_role is used" do
48
- pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
40
+ pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
49
41
 
50
42
  pristine_permission.to_yml_fixture.should match(/generic_role_id: 0/)
51
43
  end
52
44
 
53
45
  it "should return a yml string containing the name of the context role if a context_role is used" do
54
- pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
46
+ pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
55
47
 
56
48
  pristine_permission.to_yml_fixture.should match(/context_role: #{@context_role.name}/)
57
49
  end
58
50
 
59
51
  it "should return a yml string containing the id of the generic role if a generic role is used" do
60
- pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @admin_role)
52
+ pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @admin_role)
61
53
 
62
54
  pristine_permission.to_yml_fixture.should match(/generic_role_id: #{@admin_role.id.to_s}/)
63
55
  end
64
56
 
65
57
  it "should return a yml string containing ruby code to find the privilege set by name" do
66
- pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
58
+ pristine_permission = PristinePermission.new(:line_number => 150, :privilege_set_name => "chat", :pristine_role => @context_role)
67
59
 
68
60
  pristine_permission.to_yml_fixture.should match(/privilege_set_id: \<%= Cbac::PrivilegeSetRecord.find\(:first, :conditions => \{:name => '#{pristine_permission.privilege_set_name}'\}\)\.id %>/)
69
61
  end
70
62
 
71
63
  it "should return a yml string containing created_at and updated_at" do
72
- pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "chat", :pristine_role => @context_role)
64
+ pristine_permission = PristinePermission.new(:line_number => 1, :privilege_set_name => "chat", :pristine_role => @context_role)
73
65
  pristine_permission.to_yml_fixture.should match(/created_at:.+updated_at:/m)
74
66
  end
75
67
  end
@@ -85,8 +77,8 @@ describe "CbacPristinePermission" do
85
77
 
86
78
  it "should return true if the pristine permission exists as generic cbac permission in the database" do
87
79
  Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => @admin_role.id)
88
-
89
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
80
+
81
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
90
82
 
91
83
  pristine_permission.cbac_permission_exists?.should be_true
92
84
  end
@@ -94,19 +86,19 @@ describe "CbacPristinePermission" do
94
86
  it "should return true if the pristine permission exists as context cbac permission in the database" do
95
87
  Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
96
88
 
97
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
89
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
98
90
 
99
91
  pristine_permission.cbac_permission_exists?.should be_true
100
92
  end
101
93
 
102
94
  it "should return false if the pristine permission does not exist as context cbac permission in the database" do
103
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
95
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
104
96
 
105
97
  pristine_permission.cbac_permission_exists?.should be_false
106
98
  end
107
99
 
108
100
  it "should return false if the pristine permission does not exist as a generic cbac permission in the database" do
109
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
101
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
110
102
 
111
103
  pristine_permission.cbac_permission_exists?.should be_false
112
104
  end
@@ -115,7 +107,7 @@ describe "CbacPristinePermission" do
115
107
  group_admin = Cbac::GenericRole.create(:name => "group_administrator")
116
108
  Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => group_admin.id)
117
109
 
118
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
110
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
119
111
 
120
112
  pristine_permission.cbac_permission_exists?.should be_false
121
113
  end
@@ -123,7 +115,7 @@ describe "CbacPristinePermission" do
123
115
  it "should return false if a similar pristine permission exist as a context cbac permission in the database, but for another context role" do
124
116
  Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => 0, :context_role => "group_owner")
125
117
 
126
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
118
+ pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
127
119
 
128
120
  pristine_permission.cbac_permission_exists?.should be_false
129
121
  end
@@ -131,13 +123,12 @@ describe "CbacPristinePermission" do
131
123
 
132
124
  describe "check if a known permission exists for this pristine permission" do
133
125
  before(:each) do
134
-
135
126
  @pristine_context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "logged_in_user")
136
127
  @pristine_admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin], :name => "administrator")
137
128
  end
138
129
 
139
130
  it "should return true if the pristine permission exists as a known permission in the database" do
140
- pristine_permission = PristinePermission.new(:pristine_role => @pristine_admin_role, :line_number => 4, :privilege_set_name => "not relevant")
131
+ pristine_permission = PristinePermission.new(:pristine_role => @pristine_admin_role, :line_number => 4, :privilege_set_name => "not relevant")
141
132
 
142
133
  Cbac::KnownPermission.create(:permission_number => pristine_permission.line_number, :permission_type => Cbac::KnownPermission.PERMISSION_TYPES[:context])
143
134
 
@@ -145,125 +136,173 @@ describe "CbacPristinePermission" do
145
136
  end
146
137
  end
147
138
 
148
- describe "apply the permission" do
139
+ describe "registering the change" do
149
140
  before(:each) do
150
141
  @privilege_set = Cbac::PrivilegeSetRecord.create(:name => "login")
151
- @admin_role = Cbac::GenericRole.create(:name => "administrator")
152
142
 
153
- @pristine_context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "logged_in_user")
143
+ @admin_role = Cbac::GenericRole.create(:name => "administrator")
154
144
  @pristine_admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin], :name => @admin_role.name)
155
- end
156
-
157
145
 
158
- it "should add the context permission to the database if operation + is used" do
159
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
160
- pristine_permission.operation = '+'
146
+ @pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
147
+ @pristine_permission.operation = '+'
148
+ @pristine_permission.line_number = rand
161
149
 
162
- proc {
163
- pristine_permission.accept
164
- }.should change(Cbac::Permission, :count).by(1)
165
- end
166
-
167
- it "should create a generic permission if operation + is used" do
168
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
169
- pristine_permission.operation = '+'
170
-
171
- proc {
172
- pristine_permission.accept
173
- }.should change(Cbac::Permission, :count).by(1)
174
- end
175
-
176
- it "should delete the pristine permission since it was accepted" do
177
- pristine_permission = PristinePermission.create(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role, :operation => '+')
178
-
179
- proc {
180
- pristine_permission.accept
181
- }.should change(PristinePermission, :count).by(-1)
182
- end
183
-
184
- it "should create a generic role if it doesn't exist in yet" do
185
- cbac_privilege_set = Cbac::PrivilegeSetRecord.create(:name => "cbac_administration")
186
-
187
- cbac_admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:generic], :name => "cbac_administrator")
188
- pristine_permission = PristinePermission.new(:privilege_set_name => cbac_privilege_set.name, :pristine_role => cbac_admin_role)
189
- pristine_permission.operation = '+'
190
-
191
- proc {
192
- pristine_permission.accept
193
- }.should change(Cbac::GenericRole, :count).by(1)
194
- end
195
-
196
- it "should use an existing role if possible" do
197
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_admin_role)
198
- pristine_permission.operation = '+'
199
-
200
- pristine_permission.accept
201
- # test smell: depends on a clean database
202
- cbac_permission = Cbac::Permission.first
203
-
204
- cbac_permission.generic_role.should == @admin_role
205
- end
206
-
207
- it "should remove an existing permission if operation - is used" do
208
- Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
209
-
210
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
211
- pristine_permission.operation = '-'
212
-
213
- proc {
214
- pristine_permission.accept
215
- }.should change(Cbac::Permission, :count).by(-1)
216
- end
217
-
218
- it "should raise an error if operation - is used and the permission does not exist" do
219
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
220
- pristine_permission.operation = '-'
221
-
222
- proc {
223
- pristine_permission.accept
224
- }.should raise_error(ArgumentError)
150
+ @pristine_file = mock('pristine_file', :permissions => [ @pristine_permission ])
151
+ @pristine_permission.stub(:pristine_file).and_return @pristine_file
225
152
  end
226
153
 
227
154
  it "should create a known permission to record a change" do
228
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
229
- pristine_permission.operation = '+'
230
-
231
155
  proc {
232
- pristine_permission.accept
156
+ @pristine_permission.accept
233
157
  }.should change(Cbac::KnownPermission, :count).by(1)
234
158
  end
235
159
 
236
160
  it "should create a known permission with specified permission identifier" do
237
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
238
- pristine_permission.operation = '+'
239
-
240
- pristine_permission.accept
161
+ @pristine_permission.accept
241
162
 
242
163
  known_permission = Cbac::KnownPermission.last
243
164
 
244
- known_permission.permission_number.should == pristine_permission.line_number
165
+ known_permission.permission_number.should == @pristine_permission.line_number
245
166
  end
246
167
 
247
168
  it "should create a known permission with specified role type" do
248
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
249
- pristine_permission.operation = '+'
250
-
251
- pristine_permission.accept
169
+ @pristine_permission.accept
252
170
 
253
171
  known_permission = Cbac::KnownPermission.last
254
172
 
255
173
  known_permission.permission_type.should == Cbac::KnownPermission.PERMISSION_TYPES[:context]
256
174
  end
257
175
 
258
- it "should also create a known permission if operation - is used to revoke a permission" do
259
- Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
176
+ context "if the operation is '-'" do
177
+ before :each do
178
+ Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => @pristine_admin_role.role_id, :context_role => @pristine_admin_role.name)
260
179
 
261
- pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name, :pristine_role => @pristine_context_role)
262
- pristine_permission.operation = '-'
180
+ @pristine_permission.operation = '-'
181
+ end
263
182
 
264
- proc {
265
- pristine_permission.accept
266
- }.should change(Cbac::KnownPermission, :count).by(1)
183
+ it "should still create a known permission" do
184
+ proc {
185
+ @pristine_permission.accept
186
+ }.should change(Cbac::KnownPermission, :count).by(1)
187
+ end
188
+ end
189
+ end
190
+
191
+ describe "apply the permission" do
192
+ before(:each) do
193
+ @privilege_set = Cbac::PrivilegeSetRecord.create(:name => "login")
194
+ @admin_role = Cbac::GenericRole.create(:name => "administrator")
195
+
196
+ @pristine_context_role = PristineRole.new(:role_id => 0, :role_type => PristineRole.ROLE_TYPES[:context], :name => "logged_in_user")
197
+ @pristine_admin_role = PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin], :name => @admin_role.name)
198
+
199
+ @pristine_permission = PristinePermission.new(:privilege_set_name => @privilege_set.name)
200
+ @pristine_permission.stub(:register_change)
201
+ end
202
+
203
+ context "if operation '+' is used" do
204
+ before :each do
205
+ @pristine_permission.operation = '+'
206
+ end
207
+
208
+ context "if the role is a context role" do
209
+ before :each do
210
+ @pristine_permission.pristine_role = @pristine_context_role
211
+ @pristine_permission.save!
212
+ end
213
+
214
+ it "should delete the pristine permission since it was accepted" do
215
+ proc {
216
+ @pristine_permission.accept
217
+ }.should change(PristinePermission, :count).by(-1)
218
+ end
219
+
220
+ it "should register the change" do
221
+ @pristine_permission.should_receive(:register_change)
222
+
223
+ @pristine_permission.accept
224
+ end
225
+
226
+ it "should add the context permission to the database" do
227
+ proc {
228
+ @pristine_permission.accept
229
+ }.should change(Cbac::Permission, :count).by(1)
230
+ end
231
+ end
232
+
233
+ context "if the role is a generic role" do
234
+ before :each do
235
+ @pristine_permission.pristine_role = @pristine_admin_role
236
+ @pristine_permission.save!
237
+ end
238
+
239
+ it "should delete the pristine permission since it was accepted" do
240
+ proc {
241
+ @pristine_permission.accept
242
+ }.should change(PristinePermission, :count).by(-1)
243
+ end
244
+
245
+ it "should register the change" do
246
+ @pristine_permission.should_receive(:register_change)
247
+
248
+ @pristine_permission.accept
249
+ end
250
+
251
+ it "should create a generic permission" do
252
+ proc {
253
+ @pristine_permission.accept
254
+ }.should change(Cbac::Permission, :count).by(1)
255
+ end
256
+
257
+ context "and the given role already exists" do
258
+ it "should use the existing role" do
259
+ @pristine_permission.pristine_role = @pristine_admin_role
260
+
261
+ @pristine_permission.accept
262
+
263
+ Cbac::Permission.last.generic_role.should == @admin_role
264
+ end
265
+ end
266
+
267
+ context "but no role with that name exists" do
268
+ before :each do
269
+ Cbac::GenericRole.delete_all
270
+ end
271
+
272
+ it "should create a generic role if it doesn't exist in yet" do
273
+ proc {
274
+ @pristine_permission.accept
275
+ }.should change(Cbac::GenericRole, :count).by(1)
276
+ end
277
+ end
278
+ end
279
+ end
280
+
281
+ context "if operation '-' is used" do
282
+ before :each do
283
+ @pristine_permission.operation = '-'
284
+ @pristine_permission.pristine_role = @pristine_context_role
285
+ end
286
+
287
+ it "should remove an existing permission" do
288
+ Cbac::Permission.create(:privilege_set_id => @privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
289
+
290
+ proc {
291
+ @pristine_permission.accept
292
+ }.should change(Cbac::Permission, :count).by(-1)
293
+ end
294
+
295
+ context "if the permission specified does not exist" do
296
+ before :each do
297
+ Cbac::Permission.delete_all
298
+ end
299
+
300
+ it "should raise an error" do
301
+ proc {
302
+ @pristine_permission.accept
303
+ }.should raise_error(ArgumentError)
304
+ end
305
+ end
267
306
  end
268
307
  end
269
308
 
@@ -274,7 +313,7 @@ describe "CbacPristinePermission" do
274
313
  end
275
314
 
276
315
  it "should persist the pristine permission to the database" do
277
- pristine_permission = PristinePermission.new(:privilege_set_name => "login", :pristine_role => @pristine_context_role, :operation => '+')
316
+ pristine_permission = PristinePermission.new(:privilege_set_name => "login", :pristine_role => @pristine_context_role, :operation => '+')
278
317
 
279
318
  proc {
280
319
  pristine_permission.stage
@@ -283,7 +322,7 @@ describe "CbacPristinePermission" do
283
322
  end
284
323
 
285
324
  it "should persist the associated role if it doesn't exist yet" do
286
- pristine_permission = PristinePermission.new(:privilege_set_name => "login", :pristine_role => @pristine_context_role, :operation => '+')
325
+ pristine_permission = PristinePermission.new(:privilege_set_name => "login", :pristine_role => @pristine_context_role, :operation => '+')
287
326
 
288
327
  proc {
289
328
  pristine_permission.stage
@@ -294,7 +333,7 @@ describe "CbacPristinePermission" do
294
333
  privilege_set = Cbac::PrivilegeSetRecord.create(:name => "login")
295
334
  Cbac::Permission.create(:privilege_set_id => privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
296
335
 
297
- pristine_permission = PristinePermission.new(:operation => '+', :privilege_set_name => privilege_set.name, :pristine_role => @pristine_context_role)
336
+ pristine_permission = PristinePermission.new(:operation => '+', :privilege_set_name => privilege_set.name, :pristine_role => @pristine_context_role)
298
337
  proc {
299
338
  pristine_permission.stage
300
339
  }.should_not change(Cbac::CbacPristine::PristinePermission, :count)
@@ -304,7 +343,7 @@ describe "CbacPristinePermission" do
304
343
  privilege_set = Cbac::PrivilegeSetRecord.create(:name => "login")
305
344
  Cbac::Permission.create(:privilege_set_id => privilege_set.id, :generic_role_id => 0, :context_role => @pristine_context_role.name)
306
345
 
307
- pristine_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set.name, :pristine_role => @pristine_context_role)
346
+ pristine_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set.name, :pristine_role => @pristine_context_role)
308
347
  proc {
309
348
  pristine_permission.stage
310
349
  }.should change(Cbac::CbacPristine::PristinePermission, :count).by(1)
@@ -313,7 +352,7 @@ describe "CbacPristinePermission" do
313
352
  it "should not create a new pristine permission if a staged add permission exists and this pristine permission wants to revoke" do
314
353
  privilege_set_name = "chat"
315
354
  PristinePermission.new(:operation => '+', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
316
- pristine_revoke_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
355
+ pristine_revoke_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
317
356
 
318
357
  proc {
319
358
  pristine_revoke_permission.stage
@@ -323,7 +362,7 @@ describe "CbacPristinePermission" do
323
362
  it "should delete a staged add permission if the pristine permission wants to revoke the same permission" do
324
363
  privilege_set_name = "chat"
325
364
  PristinePermission.create(:operation => '+', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
326
- pristine_revoke_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
365
+ pristine_revoke_permission = PristinePermission.new(:operation => '-', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role)
327
366
 
328
367
  proc {
329
368
  pristine_revoke_permission.stage
@@ -332,7 +371,7 @@ describe "CbacPristinePermission" do
332
371
 
333
372
  it "should not create a new pristine permission if a cbac known permission exists" do
334
373
  known_number = 1
335
- pristine_permission = PristinePermission.new(:line_number => known_number, :privilege_set_name => "name not relevant", :pristine_role => @pristine_context_role)
374
+ pristine_permission = PristinePermission.new(:line_number => known_number, :privilege_set_name => "name not relevant", :pristine_role => @pristine_context_role)
336
375
  Cbac::KnownPermission.create(:permission_number => known_number, :permission_type => Cbac::KnownPermission.PERMISSION_TYPES[:context])
337
376
 
338
377
  proc {
@@ -344,15 +383,12 @@ describe "CbacPristinePermission" do
344
383
  it "should raise an error if the same pristine permission is staged twice" do
345
384
  privilege_set_name = "chat"
346
385
  PristinePermission.create(:operation => '+', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role, :line_number => 2)
347
- pristine_permission = PristinePermission.new(:operation => '+', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role, :line_number => 3)
386
+ pristine_permission = PristinePermission.new(:operation => '+', :privilege_set_name => privilege_set_name, :pristine_role => @pristine_context_role, :line_number => 3)
348
387
 
349
388
  proc {
350
389
  pristine_permission.stage
351
390
  }.should raise_error(ArgumentError)
352
391
  end
353
-
354
-
355
392
  end
356
-
357
393
  end
358
394