lockdown_vail 1.6.2.1

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 (67) hide show
  1. data/.gitignore +6 -0
  2. data/README.txt +36 -0
  3. data/Rakefile +38 -0
  4. data/VERSION +1 -0
  5. data/lib/lockdown/context.rb +41 -0
  6. data/lib/lockdown/database.rb +41 -0
  7. data/lib/lockdown/errors.rb +11 -0
  8. data/lib/lockdown/frameworks/rails/controller.rb +187 -0
  9. data/lib/lockdown/frameworks/rails/view.rb +50 -0
  10. data/lib/lockdown/frameworks/rails.rb +114 -0
  11. data/lib/lockdown/helper.rb +111 -0
  12. data/lib/lockdown/orms/active_record.rb +68 -0
  13. data/lib/lockdown/permission.rb +222 -0
  14. data/lib/lockdown/references.rb +19 -0
  15. data/lib/lockdown/rspec_helper.rb +114 -0
  16. data/lib/lockdown/rules.rb +372 -0
  17. data/lib/lockdown/session.rb +66 -0
  18. data/lib/lockdown/system.rb +58 -0
  19. data/lib/lockdown.rb +87 -0
  20. data/lockdown.gemspec +118 -0
  21. data/lockdown_vail.gemspec +120 -0
  22. data/rails_generators/lockdown/lockdown_generator.rb +274 -0
  23. data/rails_generators/lockdown/templates/app/controllers/permissions_controller.rb +22 -0
  24. data/rails_generators/lockdown/templates/app/controllers/sessions_controller.rb +39 -0
  25. data/rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb +122 -0
  26. data/rails_generators/lockdown/templates/app/controllers/users_controller.rb +117 -0
  27. data/rails_generators/lockdown/templates/app/helpers/permissions_helper.rb +2 -0
  28. data/rails_generators/lockdown/templates/app/helpers/user_groups_helper.rb +2 -0
  29. data/rails_generators/lockdown/templates/app/helpers/users_helper.rb +2 -0
  30. data/rails_generators/lockdown/templates/app/models/permission.rb +13 -0
  31. data/rails_generators/lockdown/templates/app/models/profile.rb +10 -0
  32. data/rails_generators/lockdown/templates/app/models/user.rb +95 -0
  33. data/rails_generators/lockdown/templates/app/models/user_group.rb +15 -0
  34. data/rails_generators/lockdown/templates/app/views/permissions/index.html.erb +16 -0
  35. data/rails_generators/lockdown/templates/app/views/permissions/show.html.erb +26 -0
  36. data/rails_generators/lockdown/templates/app/views/sessions/new.html.erb +12 -0
  37. data/rails_generators/lockdown/templates/app/views/user_groups/edit.html.erb +33 -0
  38. data/rails_generators/lockdown/templates/app/views/user_groups/index.html.erb +20 -0
  39. data/rails_generators/lockdown/templates/app/views/user_groups/new.html.erb +31 -0
  40. data/rails_generators/lockdown/templates/app/views/user_groups/show.html.erb +29 -0
  41. data/rails_generators/lockdown/templates/app/views/users/edit.html.erb +51 -0
  42. data/rails_generators/lockdown/templates/app/views/users/index.html.erb +22 -0
  43. data/rails_generators/lockdown/templates/app/views/users/new.html.erb +50 -0
  44. data/rails_generators/lockdown/templates/app/views/users/show.html.erb +33 -0
  45. data/rails_generators/lockdown/templates/config/initializers/lockit.rb +1 -0
  46. data/rails_generators/lockdown/templates/db/migrate/create_admin_user.rb +17 -0
  47. data/rails_generators/lockdown/templates/db/migrate/create_permissions.rb +19 -0
  48. data/rails_generators/lockdown/templates/db/migrate/create_profiles.rb +26 -0
  49. data/rails_generators/lockdown/templates/db/migrate/create_user_groups.rb +19 -0
  50. data/rails_generators/lockdown/templates/db/migrate/create_users.rb +17 -0
  51. data/rails_generators/lockdown/templates/lib/lockdown/README +42 -0
  52. data/rails_generators/lockdown/templates/lib/lockdown/init.rb +136 -0
  53. data/spec/lockdown/context_spec.rb +191 -0
  54. data/spec/lockdown/database_spec.rb +66 -0
  55. data/spec/lockdown/frameworks/rails/controller_spec.rb +240 -0
  56. data/spec/lockdown/frameworks/rails/view_spec.rb +87 -0
  57. data/spec/lockdown/frameworks/rails_spec.rb +163 -0
  58. data/spec/lockdown/permission_spec.rb +156 -0
  59. data/spec/lockdown/rspec_helper_spec.rb +41 -0
  60. data/spec/lockdown/rules_spec.rb +245 -0
  61. data/spec/lockdown/session_spec.rb +125 -0
  62. data/spec/lockdown/system_spec.rb +51 -0
  63. data/spec/lockdown_spec.rb +19 -0
  64. data/spec/rcov.opts +5 -0
  65. data/spec/spec.opts +3 -0
  66. data/spec/spec_helper.rb +8 -0
  67. metadata +140 -0
@@ -0,0 +1,42 @@
1
+ #
2
+ # !!!!IMPORTANT!!!!
3
+ #
4
+ #*** MUST define a current_user method that will return the current user object
5
+ #
6
+ #*** MUST add call to add_lockdown_session_values to your login method
7
+ #
8
+ #*** MAY NEED to add call to reset_lockdown_session to your logout method.
9
+ # ** Not needed if your authentication system resets the session
10
+ #
11
+ # Definitely need to use the user_group and permission models. The lockdown
12
+ # generator will provide those for you. Just add the following to your user
13
+ # model:
14
+ # has_and_belongs_to_many :user_groups
15
+ #
16
+ # That's it!
17
+ #
18
+ #
19
+ # ~~~~Method Descriptions~~~~
20
+
21
+ # The Lockdown gem defines these session methods:
22
+ #
23
+ # current_user_id: returns the id of the current_user
24
+ #
25
+ # logged_in? : returns true if current_user_id > 0
26
+ #
27
+ # current_user_is_admin?: returns true if user is assigned
28
+ # administrator rights.
29
+ #
30
+ # reset_lockdown_session: This will nil the following session values:
31
+ # current_user_id
32
+ # access_rights
33
+ # expiry_time
34
+ #
35
+ # current_user_access_in_group?(grp): grp is a symbol referencing a
36
+ # Lockdown::UserGroups method such as :registered_users
37
+ # Will return true if the session[:access_rights] contain at
38
+ # least one match to the access_right list associated to the group
39
+ #
40
+ # If you want access to any of these methods in your view, just add them
41
+ # as helpers in your controller (application controller for global use).
42
+ #
@@ -0,0 +1,136 @@
1
+ Lockdown::System.configure do
2
+
3
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+ # Configuration Options
5
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6
+ # Options with defaults:
7
+ #
8
+ #
9
+ # Set User model:
10
+ # # make sure you use the string "User", not the constant
11
+ # options[:user_model] = "User"
12
+ #
13
+ # Set UserGroup model:
14
+ # # make sure you use the string "UserGroup", not the constant
15
+ # options[:user_group_model] = "UserGroup"
16
+ #
17
+ # Set who_did_it method:
18
+ # This method is used in setting the created_by/updated_by fields and
19
+ # should be accessible to the controller
20
+ # options[:who_did_it] = :current_user_id
21
+ #
22
+ # Set default_who_did_it:
23
+ # When current_user_id returns nil, this is the value to use
24
+ # options[:default_who_did_it] = 1
25
+ #
26
+ # Lockdown version < 0.9.0 set this to:
27
+ # options[:default_who_did_it] = Profile::System
28
+ #
29
+ # Should probably be something like:
30
+ # options[:default_who_did_it] = User::SystemId
31
+ #
32
+ # Set timeout to 1 hour:
33
+ # options[:session_timeout] = (60 * 60)
34
+ #
35
+ # Call method when timeout occurs (method must be callable by controller):
36
+ # options[:session_timeout_method] = :clear_session_values
37
+ #
38
+ # Set system to logout if unauthorized access is attempted:
39
+ # options[:logout_on_access_violation] = false
40
+ #
41
+ # Set redirect to path on unauthorized access attempt:
42
+ # options[:access_denied_path] = "/"
43
+ #
44
+ #
45
+ # Set redirect to path on session timeout if different from unauthorized:
46
+ # options[:session_timeout_path] = "/"
47
+ #
48
+ #
49
+ # Set redirect to path on successful login:
50
+ # options[:successful_login_path] = "/"
51
+ #
52
+ # Set separator on links call
53
+ # options[:links_separator] = "|"
54
+ #
55
+ # If deploying to a subdirectory, set that here. Defaults to nil
56
+ # options[:subdirectory] = "blog"
57
+ # *Notice: Do not add leading or trailing slashes,
58
+ # Lockdown will handle this
59
+ #
60
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
+ # Define permissions
62
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63
+ #
64
+ # set_permission(:product_management).
65
+ # with_controller(:products)
66
+ #
67
+ # :product_management is the name of the permission which is later
68
+ # referenced by the set_user_group method
69
+ #
70
+ # .with_controller(:products) defaults to all action_methods available on that
71
+ # controller. You can change this behaviour by chaining on except_methods or
72
+ # only_methods. (see examples below)
73
+ #
74
+ # ** To define a namespaced controller use two underscores:
75
+ # :admin__products
76
+ #
77
+ # if products is your standard RESTful resource you'll get:
78
+ # ["products/index , "products/show",
79
+ # "products/new", "products/edit",
80
+ # "products/create", "products/update",
81
+ # "products/destroy"]
82
+ #
83
+ # You can chain method calls to restrict the methods for one controller
84
+ # or you can add multiple controllers to one permission.
85
+ #
86
+ # set_permission(:security_management).
87
+ # with_controller(:users).
88
+ # and_controller(:user_groups).
89
+ # and_controller(:permissions)
90
+ #
91
+ # In addition to with_controller(:controller) there are:
92
+ #
93
+ # set_permission(:some_nice_permission_name).
94
+ # with_controller(:some_controller_name).
95
+ # only_methods(:only_method_1, :only_method_2)
96
+ #
97
+ # set_permission(:some_nice_permission_name).
98
+ # with_controller(:some_controller_name).
99
+ # except_methods(:except_method_1, :except_method_2)
100
+ #
101
+ # set_permission(:some_nice_permission_name).
102
+ # with_controller(:some_controller_name).
103
+ # except_methods(:except_method_1, :except_method_2).
104
+ # and_controller(:another_controller_name).
105
+ # and_controller(:yet_another_controller_name)
106
+ #
107
+ # Define your permissions here:
108
+
109
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
+ # Built-in user groups
111
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
+ # You can assign the above permission to one of the built-in user groups
113
+ # by using the following:
114
+ #
115
+ # To allow public access on the permissions :sessions and :home:
116
+ # set_public_access :sessions, :home
117
+ #
118
+ # Restrict :my_account access to only authenticated users:
119
+ # set_protected_access :my_account
120
+ #
121
+ # Define the built-in user groups here:
122
+
123
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124
+ # Define user groups
125
+ #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126
+ #
127
+ # set_user_group(:catalog_management, :category_management,
128
+ # :product_management)
129
+ #
130
+ # :catalog_management is the name of the user group
131
+ # :category_management and :product_management refer to permission names
132
+ #
133
+ #
134
+ # Define your user groups here:
135
+
136
+ end
@@ -0,0 +1,191 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Lockdown::Context do
4
+ before do
5
+ @name = :my_account
6
+ end
7
+
8
+ describe Lockdown::RootContext do
9
+ before do
10
+ @c = Lockdown::RootContext.new(@name)
11
+ end
12
+
13
+ it "should return rootcontext" do
14
+ @c.to_s.should == "Lockdown::RootContext"
15
+ end
16
+
17
+ it "should allow with_controller" do
18
+ @c.allows?('with_controller').should == true
19
+ end
20
+
21
+ it "should allow and_controller" do
22
+ @c.allows?('and_controller').should == true
23
+ end
24
+
25
+ it "should allow to_model" do
26
+ @c.allows?('to_model').should == true
27
+ end
28
+
29
+ it "should not allow only_methods" do
30
+ @c.allows?('only_methods').should == false
31
+ end
32
+
33
+ it "should not allow except_methods" do
34
+ @c.allows?('except_methods').should == false
35
+ end
36
+
37
+ it "should not allow where" do
38
+ @c.allows?('where').should == false
39
+ end
40
+
41
+ it "should not allow is_in" do
42
+ @c.allows?('is_in').should == false
43
+ end
44
+
45
+ it "should not allow includes" do
46
+ @c.allows?('includes').should == false
47
+ end
48
+
49
+ it "should not allow equals" do
50
+ @c.allows?('equals').should == false
51
+ end
52
+ end
53
+
54
+ describe Lockdown::ControllerContext do
55
+ before do
56
+ @c = Lockdown::ControllerContext.new(@name)
57
+ end
58
+
59
+ it "should return rootcontext" do
60
+ @c.to_s.should == "Lockdown::ControllerContext"
61
+ end
62
+
63
+ it "should allow with_controller" do
64
+ @c.allows?('with_controller').should == true
65
+ end
66
+
67
+ it "should allow and_controller" do
68
+ @c.allows?('and_controller').should == true
69
+ end
70
+
71
+ it "should allow to_model" do
72
+ @c.allows?('to_model').should == true
73
+ end
74
+
75
+ it "should allow only_methods" do
76
+ @c.allows?('only_methods').should == true
77
+ end
78
+
79
+ it "should allow except_methods" do
80
+ @c.allows?('except_methods').should == true
81
+ end
82
+
83
+ it "should not allow where" do
84
+ @c.allows?('where').should == false
85
+ end
86
+
87
+ it "should not allow is_in" do
88
+ @c.allows?('is_in').should == false
89
+ end
90
+
91
+ it "should not allow includes" do
92
+ @c.allows?('includes').should == false
93
+ end
94
+
95
+ it "should not allow equals" do
96
+ @c.allows?('equals').should == false
97
+ end
98
+ end
99
+
100
+ describe Lockdown::ModelContext do
101
+ before do
102
+ @c = Lockdown::ModelContext.new(@name)
103
+ end
104
+
105
+ it "should return rootcontext" do
106
+ @c.to_s.should == "Lockdown::ModelContext"
107
+ end
108
+
109
+ it "should not allow with_controller" do
110
+ @c.allows?('with_controller').should == false
111
+ end
112
+
113
+ it "should not allow and_controller" do
114
+ @c.allows?('and_controller').should == false
115
+ end
116
+
117
+ it "should not allow to_model" do
118
+ @c.allows?('to_model').should == false
119
+ end
120
+
121
+ it "should not allow only_methods" do
122
+ @c.allows?('only_methods').should == false
123
+ end
124
+
125
+ it "should not allow except_methods" do
126
+ @c.allows?('except_methods').should == false
127
+ end
128
+
129
+ it "should allow where" do
130
+ @c.allows?('where').should == true
131
+ end
132
+
133
+ it "should not allow is_in" do
134
+ @c.allows?('is_in').should == false
135
+ end
136
+
137
+ it "should not allow includes" do
138
+ @c.allows?('includes').should == false
139
+ end
140
+
141
+ it "should not allow equals" do
142
+ @c.allows?('equals').should == false
143
+ end
144
+ end
145
+
146
+ describe Lockdown::ModelWhereContext do
147
+ before do
148
+ @c = Lockdown::ModelWhereContext.new(@name)
149
+ end
150
+
151
+ it "should return rootcontext" do
152
+ @c.to_s.should == "Lockdown::ModelWhereContext"
153
+ end
154
+
155
+ it "should not allow with_controller" do
156
+ @c.allows?('with_controller').should == false
157
+ end
158
+
159
+ it "should not allow and_controller" do
160
+ @c.allows?('and_controller').should == false
161
+ end
162
+
163
+ it "should not allow to_model" do
164
+ @c.allows?('to_model').should == false
165
+ end
166
+
167
+ it "should not allow only_methods" do
168
+ @c.allows?('only_methods').should == false
169
+ end
170
+
171
+ it "should not allow except_methods" do
172
+ @c.allows?('except_methods').should == false
173
+ end
174
+
175
+ it "should not allow where" do
176
+ @c.allows?('where').should == false
177
+ end
178
+
179
+ it "should allow is_in" do
180
+ @c.allows?('is_in').should == true
181
+ end
182
+
183
+ it "should allow includes" do
184
+ @c.allows?('includes').should == true
185
+ end
186
+
187
+ it "should allow equals" do
188
+ @c.allows?('equals').should == true
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,66 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ class Permission; end;
4
+
5
+ describe Lockdown::Database do
6
+ before do
7
+ Lockdown::System.stub!(:get_permissions).and_return([:permission])
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
+
12
+ end
13
+
14
+ describe "#sync_with_db" do
15
+ it "should call create_new_permissions, delete_extinct_permissions and maintain_user_groups" do
16
+ Permission.stub!(:table_exists?).and_return(true)
17
+ Lockdown::Database.should_receive :maintain_user_groups
18
+
19
+ Lockdown::Database.sync_with_db
20
+ end
21
+ end
22
+
23
+ describe "#maintain_user_groups" do
24
+ before do
25
+ UserGroup = mock('UserGroup') unless defined?(UserGroup)
26
+ end
27
+
28
+ it "should create user group for non-existent user group" do
29
+ @user_group_class.should_receive(:find).and_return(false)
30
+
31
+ Lockdown::Database.should_receive(:create_user_group).
32
+ with("User Group",:user_group)
33
+
34
+ Lockdown::Database.maintain_user_groups
35
+ end
36
+
37
+ it "should sync user group permissions for existing user group" do
38
+ ug = mock('user group')
39
+
40
+ @user_group_class.should_receive(:find).
41
+ with(:first, :conditions => ["name = ?", "User Group"]).
42
+ and_return(ug)
43
+
44
+ Lockdown::Database.maintain_user_groups
45
+ end
46
+ end
47
+
48
+ describe "#create_user_group" do
49
+ it "should create new user group" do
50
+ ug = mock('user group')
51
+ ug.stub!(:id).and_return(123)
52
+
53
+ @user_group_class.should_receive(:create).
54
+ with(:name => "some group").
55
+ and_return(ug)
56
+
57
+ Lockdown::System.stub!(:permissions_for_user_group).
58
+ and_return([:perm])
59
+
60
+ Lockdown::System.stub!(:permission_assigned_automatically?).
61
+ and_return(false)
62
+
63
+ Lockdown::Database.create_user_group("some group", :some_group)
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,240 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. .. .. spec_helper])
2
+
3
+ class TestAController
4
+ extend Lockdown::Frameworks::Rails::Controller
5
+ include Lockdown::Frameworks::Rails::Controller::Lock
6
+ end
7
+
8
+ describe Lockdown::Frameworks::Rails::Controller do
9
+ before do
10
+ @controller = TestAController
11
+
12
+ @actions = %w(posts/index posts/show posts/new posts/edit posts/create posts/update posts/destroy)
13
+
14
+ @lockdown = mock("lockdown")
15
+ end
16
+
17
+ describe "#controller_name" do
18
+ it "should return action_methods" do
19
+ post_controller = mock("PostController")
20
+ post_controller.stub!(:controller_name).and_return("PostController")
21
+
22
+ @controller.controller_name(post_controller).should == "PostController"
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ describe Lockdown::Frameworks::Rails::Controller::Lock do
29
+ before do
30
+ @controller = TestAController.new
31
+
32
+ @actions = %w(posts/index posts/show posts/new posts/edit posts/create posts/update posts/destroy)
33
+
34
+ @session = {:access_rights => @actions}
35
+
36
+ @controller.stub!(:session).and_return(@session)
37
+ end
38
+
39
+ describe "#configure_lockdown" do
40
+ it "should call Lockdown.maybe_parse_init, check_session_expiry and store_location" do
41
+ Lockdown.should_receive(:maybe_parse_init)
42
+ @controller.should_receive(:check_session_expiry)
43
+ @controller.should_receive(:store_location)
44
+
45
+ @controller.configure_lockdown
46
+ end
47
+ end
48
+
49
+ describe "#set_current_user" do
50
+ it "should set who_did_it in Thread.current" do
51
+ Lockdown::System.stub!(:fetch).with(:who_did_it).and_return(:current_user_id)
52
+ @controller.stub!(:logged_in?).and_return(true)
53
+ @controller.stub!(:current_user_id).and_return(1234)
54
+
55
+ @controller.set_current_user
56
+
57
+ Thread.current[:who_did_it].should == 1234
58
+ end
59
+ end
60
+
61
+ describe "#check_request_authorization" do
62
+ it "should raise SecurityError if not authorized" do
63
+ @controller.stub!(:authorized?).and_return(false)
64
+ @controller.stub!(:params).and_return({:p => 1})
65
+
66
+ lambda{@controller.check_request_authorization}.
67
+ should raise_error(SecurityError)
68
+
69
+ end
70
+ end
71
+
72
+ describe "#path_allowed" do
73
+ it "should return false for an invalid path" do
74
+ Lockdown::System.stub!(:public_access).and_return([])
75
+ @controller.stub!(:access_rights_from_session).and_return(["/a/good/path"])
76
+ @controller.send(:path_allowed?,"/no/good").should be_false
77
+ end
78
+ end
79
+
80
+ describe "#check_session_expiry" do
81
+ it "should set expiry if null" do
82
+ Lockdown::System.stub!(:fetch).with(:session_timeout).and_return(10)
83
+ @session[:expiry_time].should be_nil
84
+ @controller.send(:check_session_expiry)
85
+ @session[:expiry_time].should_not be_nil
86
+ end
87
+
88
+ it "should raise an exception if the session has expired" do
89
+ time = Time.now
90
+ Lockdown::System.stub!(:fetch).with(:session_timeout).and_return(10)
91
+ @session[:expiry_time] = time - 10.seconds
92
+ @controller.should_receive(:nil_lockdown_values)
93
+ Lockdown::System.stub!(:call)
94
+ @controller.stub!(:call)
95
+ lambda {@controller.send(:check_session_expiry)}.should(
96
+ raise_error(Lockdown::Frameworks::Rails::Controller::Lock::LockdownSessionExpired, "Authorization failed! \nSession expired."))
97
+ end
98
+ end
99
+
100
+ describe "#session_expired" do
101
+ it "should reset the session if configured to do so on access violation" do
102
+ Lockdown::System.stub!(:fetch).with(:logout_on_access_violation).and_return(true)
103
+ @controller.should_receive(:reset_session)
104
+ @controller.stub!(:respond_to)
105
+ @controller.send(:session_expired, nil)
106
+ end
107
+ end
108
+
109
+ describe "#store_location" do
110
+ it "should set prevpage and thispage" do
111
+ request = mock("request")
112
+ request.stub!(:method).and_return(:get)
113
+ @controller.stub!(:request).and_return(request)
114
+
115
+ @controller.stub!(:sent_from_uri).and_return("/blop")
116
+ @controller.send(:store_location)
117
+
118
+ @session[:prevpage].should == ''
119
+ @session[:thispage].should == '/blop'
120
+ end
121
+ end
122
+
123
+ describe "#sent_from_uri" do
124
+ it "should return request.request_uri" do
125
+ request = mock("request")
126
+ request.stub!(:request_uri).and_return("/blip")
127
+
128
+ @controller.stub!(:request).and_return(request)
129
+
130
+ @controller.send(:sent_from_uri).should == "/blip"
131
+ end
132
+ end
133
+
134
+ describe "#authorized?" do
135
+ before do
136
+ @sample_url = "http://stonean.com/posts/index"
137
+ @a_path = "/a_path"
138
+
139
+ Lockdown::System.stub!(:public_access).and_return([])
140
+ @controller.stub!(:access_rights_from_session).and_return(["/no/good", "posts/index"])
141
+
142
+ request = mock("request")
143
+ request.stub!(:method).and_return(:get)
144
+ Lockdown.stub(:caching?).and_return(true)
145
+ @controller.stub!(:params).and_return({})
146
+ @controller.stub!(:request).and_return(request)
147
+
148
+ stonean_parts = ["http", nil, "stonean.com", nil, nil, "posts/index", nil, nil, nil]
149
+
150
+ a_path_parts = [nil, nil, nil, nil, nil, "/a_path", nil, nil, nil]
151
+
152
+ URI = mock('uri class') unless defined?(URI)
153
+ URI.stub!(:split).with(@sample_url).and_return(stonean_parts)
154
+ URI.stub!(:split).with(@a_path).and_return(a_path_parts)
155
+ end
156
+
157
+ it "should call add_lockdown_session_values unless caching" do
158
+ Lockdown.stub(:caching?).and_return(false)
159
+ @controller.should_receive(:add_lockdown_session_values)
160
+
161
+ @controller.send(:authorized?,nil)
162
+ end
163
+
164
+ it "should return false if url is nil" do
165
+ @controller.send(:authorized?,nil).should be_false
166
+ end
167
+
168
+ it "should return true if current_user_is_admin" do
169
+ @controller.stub!(:current_user_is_admin?).and_return(true)
170
+ @controller.send(:authorized?,@a_path).should be_true
171
+ end
172
+
173
+ it "should return false if path not in access_rights" do
174
+ @controller.send(:authorized?,@a_path).should be_false
175
+ end
176
+
177
+ it "should return true if path is in access_rights" do
178
+ @controller.send(:authorized?,@sample_url).should be_true
179
+ end
180
+
181
+ end
182
+
183
+ describe "#access_denied" do
184
+ end
185
+
186
+ describe "#path_from_hash" do
187
+ it "should return controller/action string" do
188
+ hash = {:controller => "users", :action => "show", :id => "1"}
189
+ @controller.send(:path_from_hash,hash).should == "users/show"
190
+ end
191
+ end
192
+
193
+ describe "#remote_url?" do
194
+ it "should return false if domain is nil" do
195
+ @controller.send(:remote_url?).should be_false
196
+ end
197
+
198
+ it "should return false if domain matches request domain" do
199
+ request = mock("request")
200
+ request.stub!(:host).and_return("stonean.com")
201
+ @controller.stub!(:request).and_return(request)
202
+ @controller.send(:remote_url?,"stonean.com").should be_false
203
+ end
204
+
205
+ it "should return true if subdomain differs" do
206
+ request = mock("request")
207
+ request.stub!(:host).and_return("blog.stonean.com")
208
+ @controller.stub!(:request).and_return(request)
209
+ @controller.send(:remote_url?,"stonean.com").should be_true
210
+ end
211
+
212
+ it "should return true if host doesn't match domain" do
213
+ request = mock("request")
214
+ request.stub!(:host).and_return("stonean.com")
215
+ @controller.stub!(:request).and_return(request)
216
+ @controller.send(:remote_url?,"google.com").should be_true
217
+ end
218
+ end
219
+
220
+ describe "#redirect_back_or_default" do
221
+ it "should redirect to default without session[:prevpage]" do
222
+ @controller.should_receive(:redirect_to).with("/")
223
+ @controller.send :redirect_back_or_default, "/"
224
+ end
225
+
226
+ it "should redirect to session[:prevpage]" do
227
+ path = "/previous"
228
+ path.stub!(:blank?).and_return(false)
229
+ @session[:prevpage] = path
230
+ @controller.should_receive(:redirect_to).with(path)
231
+ @controller.send :redirect_back_or_default, "/"
232
+ end
233
+ end
234
+
235
+ describe "#login_from_basic_auth?" do
236
+ end
237
+
238
+ describe "#get_auth_data" do
239
+ end
240
+ end