devise_session_expirable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/README.rdoc +34 -6
  2. data/VERSION +1 -1
  3. data/devise_session_expirable.gemspec +106 -0
  4. data/lib/devise_session_expirable.rb +18 -0
  5. data/lib/devise_session_expirable/hook.rb +59 -0
  6. data/lib/devise_session_expirable/model.rb +77 -0
  7. data/test/integration/session_expirable_test.rb +152 -0
  8. data/test/models/session_expirable_test.rb +46 -0
  9. data/test/orm/active_record.rb +9 -0
  10. data/test/rails_app/Rakefile +10 -0
  11. data/test/rails_app/app/active_record/admin.rb +6 -0
  12. data/test/rails_app/app/active_record/shim.rb +2 -0
  13. data/test/rails_app/app/active_record/user.rb +6 -0
  14. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  15. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  16. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  17. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  18. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  19. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  20. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  21. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  22. data/test/rails_app/app/views/home/index.html.erb +1 -0
  23. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  24. data/test/rails_app/app/views/users/index.html.erb +1 -0
  25. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  26. data/test/rails_app/config.ru +4 -0
  27. data/test/rails_app/config/application.rb +41 -0
  28. data/test/rails_app/config/boot.rb +8 -0
  29. data/test/rails_app/config/database.yml +18 -0
  30. data/test/rails_app/config/environment.rb +5 -0
  31. data/test/rails_app/config/environments/development.rb +18 -0
  32. data/test/rails_app/config/environments/production.rb +33 -0
  33. data/test/rails_app/config/environments/test.rb +33 -0
  34. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  35. data/test/rails_app/config/initializers/devise.rb +171 -0
  36. data/test/rails_app/config/initializers/inflections.rb +2 -0
  37. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  38. data/test/rails_app/config/routes.rb +26 -0
  39. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +47 -0
  40. data/test/rails_app/lib/shared_admin.rb +25 -0
  41. data/test/rails_app/lib/shared_user.rb +22 -0
  42. data/test/rails_app/public/404.html +26 -0
  43. data/test/rails_app/public/422.html +26 -0
  44. data/test/rails_app/public/500.html +26 -0
  45. data/test/rails_app/public/favicon.ico +0 -0
  46. data/test/rails_app/script/rails +10 -0
  47. data/test/support/assertions.rb +18 -0
  48. data/test/support/helpers.rb +68 -0
  49. data/test/support/integration.rb +89 -0
  50. data/test/support/webrat/integrations/rails.rb +28 -0
  51. data/test/test_helper.rb +33 -0
  52. metadata +52 -3
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class SessionExpirableModelTest < ActiveSupport::TestCase
4
+
5
+ test 'should be expired' do
6
+ assert new_user.session_expired?(31.minutes.ago)
7
+ end
8
+
9
+ test 'should not be expired' do
10
+ assert_not new_user.session_expired?(29.minutes.ago)
11
+ end
12
+
13
+ test 'should be expired when params is nil' do
14
+ assert new_user.session_expired?(nil)
15
+ end
16
+
17
+ test 'should use timeout_in method' do
18
+ user = new_user
19
+ user.instance_eval { def timeout_in; 10.minutes end }
20
+
21
+ assert user.session_expired?(12.minutes.ago)
22
+ assert_not user.session_expired?(8.minutes.ago)
23
+ end
24
+
25
+ test 'should not be expired when timeout_in method returns nil' do
26
+ user = new_user
27
+ user.instance_eval { def timeout_in; nil end }
28
+ assert_not user.session_expired?(10.hours.ago)
29
+ end
30
+
31
+ test 'fallback to Devise config option' do
32
+ swap Devise, :timeout_in => 1.minute do
33
+ user = new_user
34
+ assert user.session_expired?(2.minutes.ago)
35
+ assert_not user.session_expired?(30.seconds.ago)
36
+
37
+ Devise.timeout_in = 5.minutes
38
+ assert_not user.session_expired?(2.minutes.ago)
39
+ assert user.session_expired?(6.minutes.ago)
40
+ end
41
+ end
42
+
43
+ test 'required_fields should contain the fields that Devise uses' do
44
+ assert_same_content Devise::Models::SessionExpirable.required_fields(User), []
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Migration.verbose = false
2
+ ActiveRecord::Base.logger = Logger.new(nil)
3
+
4
+ ActiveRecord::Migrator.migrate(File.expand_path("../../rails_app/db/migrate/", __FILE__))
5
+
6
+ class ActiveSupport::TestCase
7
+ self.use_transactional_fixtures = true
8
+ self.use_instantiated_fixtures = false
9
+ end
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ Rails.application.load_tasks
@@ -0,0 +1,6 @@
1
+ require 'shared_admin'
2
+
3
+ class Admin < ActiveRecord::Base
4
+ include Shim
5
+ include SharedAdmin
6
+ end
@@ -0,0 +1,2 @@
1
+ module Shim
2
+ end
@@ -0,0 +1,6 @@
1
+ require 'shared_user'
2
+
3
+ class User < ActiveRecord::Base
4
+ include Shim
5
+ include SharedUser
6
+ end
@@ -0,0 +1,6 @@
1
+ class Admins::SessionsController < Devise::SessionsController
2
+ def new
3
+ flash[:special] = "Welcome to #{controller_path.inspect} controller!"
4
+ super
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ class AdminsController < ApplicationController
2
+ before_filter :authenticate_admin!
3
+
4
+ def index
5
+ end
6
+
7
+ def expire
8
+ admin_session['last_request_at'] = 31.minutes.ago.utc
9
+ render :text => 'Admin will be expired on next request'
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ protect_from_forgery
6
+ before_filter :current_user, :unless => :devise_controller?
7
+ before_filter :authenticate_user!, :if => :devise_controller?
8
+ respond_to *Mime::SET.map(&:to_sym)
9
+ end
@@ -0,0 +1,4 @@
1
+ class HomeController < ApplicationController
2
+ def index
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+
2
+ class UsersController < ApplicationController
3
+
4
+ before_filter :authenticate_user!
5
+ respond_to :html, :xml
6
+
7
+ def index
8
+ user_session[:cart] = "Cart"
9
+ respond_with(current_user)
10
+ end
11
+
12
+ def expire
13
+ user_session['last_request_at'] = 31.minutes.ago.utc
14
+ render :text => 'User will be expired on next request'
15
+ end
16
+
17
+ def clear_timeout
18
+ user_session['last_request_at'] = nil
19
+ render :text => 'User will be expired on next request'
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1 @@
1
+ Welcome Admin!
@@ -0,0 +1,2 @@
1
+ Welcome to "sessions/new" view!
2
+ <%= render :file => "devise/sessions/new" %>
@@ -0,0 +1,24 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html>
4
+ <head>
5
+ <title>Devise Test App</title>
6
+ </head>
7
+ <body>
8
+ <div id="container">
9
+ <%- flash.each do |name, msg| -%>
10
+ <%= content_tag :div, msg, :id => "flash_#{name}" %>
11
+ <%- end -%>
12
+
13
+ <% if user_signed_in? -%>
14
+ <p>Hello User <%= current_user.email %>! You are signed in!</p>
15
+ <% end -%>
16
+
17
+ <% if admin_signed_in? -%>
18
+ <p>Hello Admin <%= current_admin.email %>! You are signed in!</p>
19
+ <% end -%>
20
+
21
+ <%= yield %>
22
+ </div>
23
+ </body>
24
+ </html>
@@ -0,0 +1 @@
1
+ Welcome User #<%= current_user.id %>!
@@ -0,0 +1 @@
1
+ Special user view
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run RailsApp::Application
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "action_controller/railtie"
4
+ require "action_mailer/railtie"
5
+ require "active_resource/railtie"
6
+ require "rails/test_unit/railtie"
7
+
8
+ Bundler.require :default, DEVISE_ORM
9
+
10
+ begin
11
+ require "#{DEVISE_ORM}/railtie"
12
+ rescue LoadError
13
+ end
14
+
15
+ require "devise"
16
+
17
+ module RailsApp
18
+ class Application < Rails::Application
19
+ # Add additional load paths for your own custom dirs
20
+ config.autoload_paths.reject!{ |p| p =~ /\/app\/(\w+)$/ && !%w(controllers helpers views).include?($1) }
21
+ config.autoload_paths += [ "#{config.root}/app/#{DEVISE_ORM}" ]
22
+
23
+ # Configure generators values. Many other options are available, be sure to check the documentation.
24
+ # config.generators do |g|
25
+ # g.orm :active_record
26
+ # g.template_engine :erb
27
+ # g.test_framework :test_unit, :fixture => true
28
+ # end
29
+
30
+ # Configure sensitive parameters which will be filtered from the log file.
31
+ config.filter_parameters << :password
32
+ config.assets.enabled = false
33
+
34
+ config.action_mailer.default_url_options = { :host => "localhost:3000" }
35
+
36
+ # This was used to break devise in some situations
37
+ config.to_prepare do
38
+ Devise::SessionsController.layout "application"
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ unless defined?(DEVISE_ORM)
2
+ DEVISE_ORM = (ENV["DEVISE_ORM"] || :active_record).to_sym
3
+ end
4
+
5
+ require 'rubygems'
6
+ require 'bundler/setup'
7
+
8
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,18 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: ":memory:"
15
+
16
+ production:
17
+ adapter: sqlite3
18
+ database: ":memory:"
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ RailsApp::Application.initialize!
@@ -0,0 +1,18 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
18
+ end
@@ -0,0 +1,33 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # See everything in the log (default is :info)
13
+ # config.log_level = :debug
14
+
15
+ # Use a different logger for distributed setups
16
+ # config.logger = SyslogLogger.new
17
+
18
+ # Use a different cache store in production
19
+ # config.cache_store = :mem_cache_store
20
+
21
+ # Disable Rails's static asset server
22
+ # In production, Apache or nginx will already do this
23
+ config.serve_static_assets = false
24
+
25
+ # Enable serving of images, stylesheets, and javascripts from an asset server
26
+ # config.action_controller.asset_host = "http://assets.example.com"
27
+
28
+ # Disable delivery errors, bad email addresses will be ignored
29
+ # config.action_mailer.raise_delivery_errors = false
30
+
31
+ # Enable threaded mode
32
+ # config.threadsafe!
33
+ end
@@ -0,0 +1,33 @@
1
+ RailsApp::Application.configure do
2
+ # Settings specified here will take precedence over those in config/environment.rb
3
+
4
+ # The test environment is used exclusively to run your application's
5
+ # test suite. You never need to work with it otherwise. Remember that
6
+ # your test database is "scratch space" for the test suite and is wiped
7
+ # and recreated between test runs. Don't rely on the data there!
8
+ config.cache_classes = true
9
+
10
+ # Log error messages when you accidentally call methods on nil.
11
+ config.whiny_nils = true
12
+
13
+ # Show full error reports and disable caching
14
+ config.consider_all_requests_local = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Disable request forgery protection in test environment
18
+ config.action_controller.allow_forgery_protection = false
19
+
20
+ # Tell Action Mailer not to deliver emails to the real world.
21
+ # The :test delivery method accumulates sent emails in the
22
+ # ActionMailer::Base.deliveries array.
23
+ config.action_mailer.delivery_method = :test
24
+
25
+ # Use SQL instead of Active Record's schema dumper when creating the test database.
26
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
27
+ # like if you have constraints or database-specific column types
28
+ # config.active_record.schema_format = :sql
29
+
30
+ config.action_dispatch.show_exceptions = false
31
+
32
+ config.active_support.deprecation = :stderr
33
+ end
@@ -0,0 +1,7 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
4
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
5
+
6
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
7
+ Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,171 @@
1
+
2
+ # Use this hook to configure devise mailer, warden hooks and so forth. The first
3
+ # four configuration values can also be set straight in your models.
4
+ Devise.setup do |config|
5
+ # ==> Mailer Configuration
6
+ # Configure the e-mail address which will be shown in Devise::Mailer,
7
+ # note that it will be overwritten if you use your own mailer class with default "from" parameter.
8
+ config.mailer_sender = "please-change-me@config-initializers-devise.com"
9
+
10
+ # Configure the class responsible to send e-mails.
11
+ # config.mailer = "Devise::Mailer"
12
+
13
+ # ==> ORM configuration
14
+ # Load and configure the ORM. Supports :active_record (default) and
15
+ # :mongoid (bson_ext recommended) by default. Other ORMs may be
16
+ # available as additional gems.
17
+ require "devise/orm/active_record"
18
+
19
+ # ==> Configuration for any authentication mechanism
20
+ # Configure which keys are used when authenticating a user. By default is
21
+ # just :email. You can configure it to use [:username, :subdomain], so for
22
+ # authenticating a user, both parameters are required. Remember that those
23
+ # parameters are used only when authenticating and not when retrieving from
24
+ # session. If you need permissions, you should implement that in a before filter.
25
+ # You can also supply hash where the value is a boolean expliciting if authentication
26
+ # should be aborted or not if the value is not present. By default is empty.
27
+ # config.authentication_keys = [ :email ]
28
+
29
+ # Configure parameters from the request object used for authentication. Each entry
30
+ # given should be a request method and it will automatically be passed to
31
+ # find_for_authentication method and considered in your model lookup. For instance,
32
+ # if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
33
+ # The same considerations mentioned for authentication_keys also apply to request_keys.
34
+ # config.request_keys = []
35
+
36
+ # Configure which authentication keys should be case-insensitive.
37
+ # These keys will be downcased upon creating or modifying a user and when used
38
+ # to authenticate or find a user. Default is :email.
39
+ config.case_insensitive_keys = [ :email ]
40
+
41
+ # Configure which authentication keys should have whitespace stripped.
42
+ # These keys will have whitespace before and after removed upon creating or
43
+ # modifying a user and when used to authenticate or find a user. Default is :email.
44
+ config.strip_whitespace_keys = [ :email ]
45
+
46
+ # Tell if authentication through request.params is enabled. True by default.
47
+ # config.params_authenticatable = true
48
+
49
+ # Tell if authentication through HTTP Basic Auth is enabled. False by default.
50
+ config.http_authenticatable = true
51
+
52
+ # If http headers should be returned for AJAX requests. True by default.
53
+ # config.http_authenticatable_on_xhr = true
54
+
55
+ # The realm used in Http Basic Authentication. "Application" by default.
56
+ # config.http_authentication_realm = "Application"
57
+
58
+ # ==> Configuration for :database_authenticatable
59
+ # For bcrypt, this is the cost for hashing the password and defaults to 10. If
60
+ # using other encryptors, it sets how many times you want the password re-encrypted.
61
+ config.stretches = Rails.env.test? ? 1 : 10
62
+
63
+ # ==> Configuration for :confirmable
64
+ # The time you want to give your user to confirm his account. During this time
65
+ # he will be able to access your application without confirming. Default is nil.
66
+ # When allow_unconfirmed_access_for is zero, the user won't be able to sign in without confirming.
67
+ # You can use this to let your user access some features of your application
68
+ # without confirming the account, but blocking it after a certain period
69
+ # (ie 2 days).
70
+ # config.allow_unconfirmed_access_for = 2.days
71
+
72
+ # Defines which key will be used when confirming an account
73
+ # config.confirmation_keys = [ :email ]
74
+
75
+ # ==> Configuration for :rememberable
76
+ # The time the user will be remembered without asking for credentials again.
77
+ # config.remember_for = 2.weeks
78
+
79
+ # If true, a valid remember token can be re-used between multiple browsers.
80
+ # config.remember_across_browsers = true
81
+
82
+ # If true, extends the user's remember period when remembered via cookie.
83
+ # config.extend_remember_period = false
84
+
85
+ # ==> Configuration for :validatable
86
+ # Range for password length. Default is 8..128.
87
+ # config.password_length = 8..128
88
+
89
+ # Regex to use to validate the email address
90
+ # config.email_regexp = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
91
+
92
+ # ==> Configuration for :timeoutable
93
+ # The time you want to timeout the user session without activity. After this
94
+ # time the user will be asked for credentials again. Default is 30 minutes.
95
+ # config.timeout_in = 30.minutes
96
+
97
+ # ==> Configuration for :lockable
98
+ # Defines which strategy will be used to lock an account.
99
+ # :failed_attempts = Locks an account after a number of failed attempts to sign in.
100
+ # :none = No lock strategy. You should handle locking by yourself.
101
+ # config.lock_strategy = :failed_attempts
102
+
103
+ # Defines which key will be used when locking and unlocking an account
104
+ # config.unlock_keys = [ :email ]
105
+
106
+ # Defines which strategy will be used to unlock an account.
107
+ # :email = Sends an unlock link to the user email
108
+ # :time = Re-enables login after a certain amount of time (see :unlock_in below)
109
+ # :both = Enables both strategies
110
+ # :none = No unlock strategy. You should handle unlocking by yourself.
111
+ # config.unlock_strategy = :both
112
+
113
+ # Number of authentication tries before locking an account if lock_strategy
114
+ # is failed attempts.
115
+ # config.maximum_attempts = 20
116
+
117
+ # Time interval to unlock the account if :time is enabled as unlock_strategy.
118
+ # config.unlock_in = 1.hour
119
+
120
+ # ==> Configuration for :recoverable
121
+ #
122
+ # Defines which key will be used when recovering the password for an account
123
+ # config.reset_password_keys = [ :email ]
124
+
125
+ # Time interval you can reset your password with a reset password key.
126
+ # Don't put a too small interval or your users won't have the time to
127
+ # change their passwords.
128
+ config.reset_password_within = 2.hours
129
+
130
+ # Setup a pepper to generate the encrypted password.
131
+ config.pepper = "d142367154e5beacca404b1a6a4f8bc52c6fdcfa3ccc3cf8eb49f3458a688ee6ac3b9fae488432a3bfca863b8a90008368a9f3a3dfbe5a962e64b6ab8f3a3a1a"
132
+
133
+ # ==> Configuration for :token_authenticatable
134
+ # Defines name of the authentication token params key
135
+ # config.token_authentication_key = :auth_token
136
+
137
+ # ==> Scopes configuration
138
+ # Turn scoped views on. Before rendering "sessions/new", it will first check for
139
+ # "users/sessions/new". It's turned off by default because it's slower if you
140
+ # are using only default views.
141
+ # config.scoped_views = false
142
+
143
+ # Configure the default scope given to Warden. By default it's the first
144
+ # devise role declared in your routes (usually :user).
145
+ # config.default_scope = :user
146
+
147
+ # Configure sign_out behavior.
148
+ # Sign_out action can be scoped (i.e. /users/sign_out affects only :user scope).
149
+ # The default is true, which means any logout action will sign out all active scopes.
150
+ # config.sign_out_all_scopes = true
151
+
152
+ # ==> Navigation configuration
153
+ # Lists the formats that should be treated as navigational. Formats like
154
+ # :html, should redirect to the sign in page when the user does not have
155
+ # access, but formats like :xml or :json, should return 401.
156
+ # If you have any extra navigational formats, like :iphone or :mobile, you
157
+ # should add them to the navigational formats lists. Default is [:html]
158
+ # config.navigational_formats = [:html, :iphone]
159
+
160
+ # The default HTTP method used to sign out a resource. Default is :get.
161
+ # config.sign_out_via = :get
162
+
163
+ # ==> Warden configuration
164
+ # If you want to use other strategies, that are not supported by Devise, or
165
+ # change the failure app, you can configure them inside the config.warden block.
166
+ #
167
+ # config.warden do |manager|
168
+ # manager.failure_app = AnotherApp
169
+ # manager.default_strategies(:scope => :user).unshift :some_external_strategy
170
+ # end
171
+ end