lockdown 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "lockdown", "helper")
3
3
  module Lockdown
4
4
  extend Lockdown::Helper
5
5
 
6
- VERSION = '1.2.1'
6
+ VERSION = '1.2.2'
7
7
 
8
8
  # Returns the version string for the library.
9
9
  def self.version
@@ -66,12 +66,16 @@ module Lockdown
66
66
  # cache_classes is true in production and testing, need to
67
67
  # modify the ApplicationController
68
68
  def controller_parent
69
- if ::Rails.configuration.cache_classes
69
+ if caching_classes?
70
70
  ApplicationController
71
71
  else
72
72
  ActionController::Base
73
73
  end
74
74
  end
75
+
76
+ def caching_classes?
77
+ ::Rails.configuration.cache_classes
78
+ end
75
79
 
76
80
  # cache_classes is true in production and testing, need to
77
81
  # do an instance eval instead
@@ -97,7 +101,11 @@ module Lockdown
97
101
  include Lockdown::Frameworks::Rails::Controller
98
102
 
99
103
  def skip_sync?
100
- Lockdown::System.fetch(:skip_db_sync_in).include?(ENV['RAILS_ENV'])
104
+ Lockdown::System.fetch(:skip_db_sync_in).include?(framework_environment)
105
+ end
106
+
107
+ def framework_environment
108
+ ::Rails.env
101
109
  end
102
110
  end # System
103
111
  end # Rails
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+
1
3
  module Lockdown
2
4
  module Helper
3
5
  def class_name_from_file(str)
@@ -10,34 +12,42 @@ module Lockdown
10
12
  if str_sym.is_a?(Symbol)
11
13
  titleize(str_sym)
12
14
  else
13
- underscore(str_sym).tr(' ','_').to_sym
15
+ str_sym.underscore.tr(' ','_').to_sym
14
16
  end
15
17
  end
16
18
 
17
19
  def user_group_class
18
- eval(Lockdown::System.fetch(:user_group_model))
20
+ eval(user_group_model_string)
19
21
  end
20
22
 
21
23
  def user_groups_hbtm_reference
22
- underscore(Lockdown::System.fetch(:user_group_model)).pluralize.to_sym
24
+ user_group_model_string.underscore.pluralize.to_sym
23
25
  end
24
26
 
25
27
  def user_group_id_reference
26
- underscore(Lockdown::System.fetch(:user_group_model)) + "_id"
28
+ user_group_model_string.underscore + "_id"
27
29
  end
28
30
 
29
31
  def user_class
30
- eval(Lockdown::System.fetch(:user_model))
32
+ eval(user_model_string)
31
33
  end
32
34
 
33
35
  def users_hbtm_reference
34
- underscore(Lockdown::System.fetch(:user_model)).pluralize.to_sym
36
+ user_model_string.underscore.pluralize.to_sym
35
37
  end
36
38
 
37
39
  def user_id_reference
38
- underscore(Lockdown::System.fetch(:user_model)) + "_id"
40
+ user_model_string.underscore + "_id"
39
41
  end
40
42
 
43
+ def user_group_model_string
44
+ Lockdown::System.fetch(:user_group_model) || "UserGroup"
45
+ end
46
+
47
+ def user_model_string
48
+ Lockdown::System.fetch(:user_model) || "User"
49
+ end
50
+
41
51
  def get_string(value)
42
52
  if value.respond_to?(:name)
43
53
  string_name(value.name)
@@ -18,7 +18,6 @@ module Lockdown
18
18
 
19
19
  @permission_objects = {}
20
20
 
21
- @controller_classes = []
22
21
  @public_access = []
23
22
  @protected_access = []
24
23
 
@@ -47,7 +47,8 @@ class LockdownGenerator < Rails::Generator::Base
47
47
  end
48
48
 
49
49
  def manifest
50
- record do |@m|
50
+ record do |m|
51
+ @m = m
51
52
  # Ensure appropriate folder(s) exists
52
53
  @m.directory @view_path
53
54
  @m.directory @controller_path
@@ -1,13 +1,19 @@
1
1
  require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
2
 
3
+ class Permission; end;
4
+
3
5
  describe Lockdown::Database do
4
- before do
6
+ before do
5
7
  Lockdown::System.stub!(:get_permissions).and_return([:permission])
6
8
  Lockdown::System.stub!(:get_user_groups).and_return([:user_group])
9
+ @user_group_class = mock(:table_exists? => true, :find => false)
10
+ Lockdown.stub!(:user_group_class).and_return @user_group_class
11
+
7
12
  end
8
13
 
9
14
  describe "#sync_with_db" do
10
15
  it "should call create_new_permissions, delete_extinct_permissions and maintain_user_groups" do
16
+ Permission.stub!(:table_exists?).and_return(true)
11
17
  Lockdown::Database.should_receive :create_new_permissions
12
18
  Lockdown::Database.should_receive :delete_extinct_permissions
13
19
  Lockdown::Database.should_receive :maintain_user_groups
@@ -20,7 +26,6 @@ describe Lockdown::Database do
20
26
  it "should create permission from @permissions" do
21
27
  Lockdown::System.stub!(:permission_assigned_automatically?).and_return(false)
22
28
 
23
- Permission = mock('Permission') unless defined?(Permission)
24
29
  Permission.stub!(:find).and_return(false)
25
30
  Permission.should_receive(:create).with(:name => 'Permission')
26
31
 
@@ -52,9 +57,7 @@ describe Lockdown::Database do
52
57
  end
53
58
 
54
59
  it "should create user group for non-existent user group" do
55
- UserGroup.should_receive(:find).
56
- with(:first, :conditions => ["name = ?", "User Group"]).
57
- and_return(false)
60
+ @user_group_class.should_receive(:find).and_return(false)
58
61
 
59
62
  Lockdown::Database.should_receive(:create_user_group).
60
63
  with("User Group",:user_group)
@@ -65,7 +68,7 @@ describe Lockdown::Database do
65
68
  it "should sync user group permissions for existing user group" do
66
69
  ug = mock('user group')
67
70
 
68
- UserGroup.should_receive(:find).
71
+ @user_group_class.should_receive(:find).
69
72
  with(:first, :conditions => ["name = ?", "User Group"]).
70
73
  and_return(ug)
71
74
 
@@ -84,9 +87,7 @@ describe Lockdown::Database do
84
87
  ug = mock('user group')
85
88
  ug.stub!(:id).and_return(123)
86
89
 
87
- UserGroup = mock('UserGroup') unless defined?(UserGroup)
88
-
89
- UserGroup.should_receive(:create).
90
+ @user_group_class.should_receive(:create).
90
91
  with(:name => "some group").
91
92
  and_return(ug)
92
93
 
@@ -14,24 +14,6 @@ describe Lockdown::Frameworks::Rails::Controller do
14
14
  @lockdown = mock("lockdown")
15
15
  end
16
16
 
17
- describe "#available_actions" do
18
- it "should return action_methods" do
19
- post_controller = mock("PostController")
20
- post_controller.stub!(:action_methods).and_return(@actions)
21
-
22
- @controller.available_actions(post_controller).
23
- should == @actions
24
- end
25
-
26
- it "should eql public_instance_methods - hidden_actions unless action_methods" do
27
- post_controller = mock("PostController")
28
- post_controller.stub!(:public_instance_methods).and_return(["m1", "m2", "h1"])
29
- post_controller.stub!(:hidden_actions).and_return(["h1"])
30
- @controller.available_actions(post_controller).
31
- should == ["m1", "m2"]
32
- end
33
- end
34
-
35
17
  describe "#controller_name" do
36
18
  it "should return action_methods" do
37
19
  post_controller = mock("PostController")
@@ -122,7 +104,7 @@ describe Lockdown::Frameworks::Rails::Controller::Lock do
122
104
 
123
105
  @controller.stub!(:request).and_return(request)
124
106
 
125
- @controller.sent(:sent_from_uri).should == "/blip"
107
+ @controller.send(:sent_from_uri).should == "/blip"
126
108
  end
127
109
  end
128
110
 
@@ -204,7 +186,7 @@ describe Lockdown::Frameworks::Rails::Controller::Lock do
204
186
  describe "#redirect_back_or_default" do
205
187
  it "should redirect to default without session[:prevpage]" do
206
188
  @controller.should_receive(:redirect_to).with("/")
207
- @controller.redirect_back_or_default("/")
189
+ @controller.send :redirect_back_or_default, "/"
208
190
  end
209
191
 
210
192
  it "should redirect to session[:prevpage]" do
@@ -212,7 +194,7 @@ describe Lockdown::Frameworks::Rails::Controller::Lock do
212
194
  path.stub!(:blank?).and_return(false)
213
195
  @session[:prevpage] = path
214
196
  @controller.should_receive(:redirect_to).with(path)
215
- @controller.redirect_back_or_default("/")
197
+ @controller.send :redirect_back_or_default, "/"
216
198
  end
217
199
  end
218
200
 
@@ -3,9 +3,10 @@ require File.join(File.dirname(__FILE__), %w[.. .. spec_helper])
3
3
  describe Lockdown::Frameworks::Rails do
4
4
  before do
5
5
  @rails = Lockdown::Frameworks::Rails
6
+
6
7
  @rails.stub!(:use_me?).and_return(true)
7
8
 
8
- @lockdown = mock("lockdown")
9
+ @lockdown = mock("lockdown")
9
10
  end
10
11
 
11
12
 
@@ -36,6 +37,7 @@ describe Lockdown::Frameworks::Rails do
36
37
  ActionController::Base.should_receive(:rescue_from)
37
38
 
38
39
  ActionController::Base.should_receive(:class_eval)
40
+ ActionController::Base.should_receive(:hide_action)
39
41
 
40
42
  Lockdown::System.should_receive(:class_eval)
41
43
 
@@ -46,9 +48,16 @@ describe Lockdown::Frameworks::Rails do
46
48
  end
47
49
  end
48
50
 
51
+ RAILS_ROOT = "/shibby/dibby/do"
52
+
53
+ module ActionController; class Base; end end
54
+
55
+ class ApplicationController; end
56
+
57
+ module ActionView; class Base; end end
58
+
49
59
  describe Lockdown::Frameworks::Rails::Environment do
50
60
 
51
- RAILS_ROOT = "/shibby/dibby/do"
52
61
  before do
53
62
  @env = class Test; extend Lockdown::Frameworks::Rails::Environment; end
54
63
  end
@@ -77,16 +86,20 @@ describe Lockdown::Frameworks::Rails::Environment do
77
86
  end
78
87
 
79
88
  describe "#controller_parent" do
80
- it "should return ActionController::Base" do
81
- module ActionController; class Base; end end
82
-
89
+ it "should return ActionController::Base if not caching classes" do
90
+ @env.should_receive(:caching_classes?).and_return(false)
83
91
  @env.controller_parent.should == ActionController::Base
84
92
  end
93
+
94
+ it "should return ApplicationController if caching classes" do
95
+ @env.should_receive(:caching_classes?).and_return(true)
96
+ @env.controller_parent.should == ApplicationController
97
+ end
98
+
85
99
  end
86
100
 
87
101
  describe "#view_helper" do
88
102
  it "should return ActionView::Base" do
89
- module ActionView; class Base; end end
90
103
 
91
104
  @env.view_helper.should == ActionView::Base
92
105
  end
@@ -96,80 +109,27 @@ end
96
109
  describe Lockdown::Frameworks::Rails::System do
97
110
  class Test
98
111
  extend Lockdown::Frameworks::Rails::System
99
- class << self
100
- attr_accessor :controller_classes
101
- end
102
- end
103
-
104
- module Rails
105
- module VERSION
106
- MAJOR = 2
107
- MINOR = 2
108
- TINY = 2
109
- end
110
112
  end
111
113
 
112
114
  before do
113
115
  @env = Test
114
- @env.controller_classes = {}
115
116
  end
116
117
 
117
118
  describe "#skip_sync?" do
118
- end
119
-
120
- describe "#load_controller_classes" do
121
- end
122
-
123
- describe "#maybe_load_framework_controller_parent" do
124
- it "should call require_or_load with application.rb < 2.3" do
125
- @env.should_receive(:require_or_load).with("application.rb")
126
-
127
- @env.maybe_load_framework_controller_parent
128
- end
129
-
130
- it "should call require_or_load with application_controller.rb >= 2.3" do
131
- module Rails
132
- module VERSION
133
- MINOR = 3
134
- TINY = 0
135
- end
136
- end
137
-
138
- @env.should_receive(:require_or_load).with("application_controller.rb")
139
-
140
- @env.maybe_load_framework_controller_parent
119
+ it "should return true if env == skip sync" do
120
+ Lockdown::System.stub!(:fetch).with(:skip_db_sync_in).and_return(['test'])
121
+ @env.should_receive(:framework_environment).and_return("test")
122
+
123
+ @env.skip_sync?.should == true
141
124
  end
142
- end
143
-
144
- describe "#lockdown_load" do
145
- it "should add class to controller classes" do
146
- @env.stub!(:class_name_from_file).and_return("controller_class")
147
- Lockdown.stub!(:qualified_const_get).and_return(:controller_class)
148
- @env.stub!(:require_or_load)
149
-
150
- @env.lockdown_load("controller_file")
151
125
 
152
- @env.controller_classes["ControllerFile"].should == :controller_class
126
+ it "should return false if env not in skip_sync" do
127
+ Lockdown::System.stub!(:fetch).with(:skip_db_sync_in).and_return(['test', 'ci'])
128
+ @env.should_receive(:framework_environment).and_return("qa")
129
+
130
+ @env.skip_sync?.should == false
153
131
  end
132
+
154
133
  end
155
134
 
156
- describe "#require_or_load" do
157
- it "should use Dependencies if not defined in ActiveSupport" do
158
- module ActiveSupport; end
159
- Dependencies = mock("dependencies") unless defined?(Dependencies)
160
-
161
- Dependencies.should_receive(:require_or_load).with("controller_file")
162
-
163
- @env.require_or_load("controller_file")
164
- end
165
-
166
- it "should use ActiveSupport::Dependencies if defined" do
167
- module ActiveSupport; class Dependencies; end end
168
-
169
- ActiveSupport::Dependencies.should_receive(:require_or_load).
170
- with("controller_file")
171
-
172
- @env.require_or_load("controller_file")
173
- end
174
- end
175
135
  end
@@ -54,6 +54,8 @@ describe Lockdown::Rules do
54
54
 
55
55
  describe "#get_permissions" do
56
56
  it "should return array of permission names as symbols" do
57
+ Lockdown.should_receive(:add_controller_method)
58
+
57
59
  @rules.set_permission(:home_page)
58
60
  @rules.set_permission(:user_management)
59
61
  @rules.process_rules
@@ -64,6 +66,8 @@ describe Lockdown::Rules do
64
66
 
65
67
  describe "#permission_exists?" do
66
68
  it "should return true if permission exists" do
69
+ Lockdown.should_receive(:add_controller_method)
70
+
67
71
  @rules.set_permission(:home_page)
68
72
  @rules.process_rules
69
73
  @rules.permission_exists?(:home_page).should be_true
@@ -99,6 +103,8 @@ describe Lockdown::Rules do
99
103
 
100
104
  describe "#process_rules" do
101
105
  it "should validate user_group permissions" do
106
+ Lockdown.should_receive(:add_controller_method)
107
+
102
108
  @rules.set_user_group(:test_group, :a_perm)
103
109
  error = "User Group: test_group, permission not found: a_perm"
104
110
 
@@ -64,7 +64,8 @@ describe Lockdown::Session do
64
64
  it "should set the access_rights from the user list" do
65
65
  array = ["posts/index", "posts/show"]
66
66
  Lockdown::System.stub!(:access_rights_for_user).and_return(array)
67
- @controller.stub!(:current_user).and_return(:user_object)
67
+ usr = mock(:id => 1234)
68
+ @controller.stub!(:current_user).and_return(usr)
68
69
  @controller.send(:add_lockdown_session_values)
69
70
  @session[:access_rights].should == array
70
71
  end
@@ -7,23 +7,13 @@ describe Lockdown::System do
7
7
  Lockdown::System.options['test'] = "my test"
8
8
  Lockdown::System.fetch('test').should == "my test"
9
9
  end
10
-
11
- it "should fetch the controller class" do
12
- klass = mock("User Controller Class")
13
- Lockdown.stub!(:controller_class_name).and_return(:users)
14
- Lockdown::System.controller_classes = {}
15
- Lockdown::System.controller_classes[:users] = klass
16
- Lockdown::System.fetch_controller_class(:users).should equal(klass)
17
- end
18
-
10
+
19
11
  describe "#configure" do
20
12
  it "should call the methods responsible for defining the rules" do
21
13
  Lockdown::System.stub!(:skip_sync?).and_return(false)
22
14
 
23
15
  Lockdown::System.should_receive :set_defaults
24
16
 
25
- Lockdown::System.should_receive :load_controller_classes
26
-
27
17
  Lockdown::System.should_receive :instance_eval
28
18
 
29
19
  Lockdown::System.should_receive :process_rules
@@ -48,7 +38,7 @@ describe Lockdown::System do
48
38
 
49
39
  it "should build the paths from the controller class if no methods specified" do
50
40
  methods = ["new","edit","create","update"]
51
- Lockdown::System.stub!(:fetch_controller_class)
41
+ Lockdown.stub!(:fetch_controller_class)
52
42
  Lockdown::System.stub!(:available_actions).
53
43
  and_return(methods)
54
44
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lockdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stone
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-08-13 00:00:00 -04:00
12
+ date: 2009-08-16 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency