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,2 @@
1
+ ActiveSupport::Inflector.inflections do |inflect|
2
+ end
@@ -0,0 +1,2 @@
1
+ Rails.application.config.secret_token = 'ea942c41850d502f2c8283e26bdc57829f471bb18224ddff0a192c4f32cdf6cb5aa0d82b3a7a7adbeb640c4b06f3aa1cd5f098162d8240f669b39d6b49680571'
2
+ Rails.application.config.session_store :cookie_store, :key => "_my_app"
@@ -0,0 +1,26 @@
1
+
2
+ Rails.application.routes.draw do
3
+
4
+ resources :users, :only => [:index] do
5
+ get :expire, :on => :member
6
+ get :clear_timeout, :on => :member
7
+ end
8
+
9
+ resources :admins, :only => [:index] do
10
+ get :expire, :on => :member
11
+ end
12
+
13
+ devise_for :users
14
+
15
+ get "/sign_in", :to => "devise/sessions#new"
16
+
17
+ devise_for :admin, :path => "admin_area",
18
+ :controllers => { :sessions => :"admins/sessions" },
19
+ :skip => :passwords
20
+
21
+ get "/anywhere", :to => "foo#bar", :as => :new_admin_password
22
+
23
+ root :to => "home#index"
24
+
25
+ end
26
+
@@ -0,0 +1,47 @@
1
+
2
+ class CreateTables < ActiveRecord::Migration
3
+
4
+ def self.up
5
+ create_table :users do |t|
6
+ t.string :username
7
+ t.string :facebook_token
8
+
9
+ ## Database authenticatable
10
+ t.string :email, :null => false, :default => ""
11
+ t.string :encrypted_password, :null => false, :default => ""
12
+
13
+ ## Rememberable
14
+ t.datetime :remember_created_at
15
+
16
+ ## Token authenticatable
17
+ t.string :authentication_token
18
+
19
+ t.timestamps
20
+ end
21
+
22
+ create_table :admins do |t|
23
+ ## Database authenticatable
24
+ t.string :email, :null => true
25
+ t.string :encrypted_password, :null => true
26
+
27
+ ## Recoverable
28
+ t.string :reset_password_token
29
+ t.datetime :reset_password_sent_at
30
+
31
+ ## Rememberable
32
+ t.datetime :remember_created_at
33
+
34
+ ## Attribute for testing route blocks
35
+ t.boolean :active, :default => false
36
+
37
+ t.timestamps
38
+ end
39
+ end
40
+
41
+ def self.down
42
+ drop_table :users
43
+ drop_table :admins
44
+ end
45
+
46
+ end
47
+
@@ -0,0 +1,25 @@
1
+
2
+ module SharedAdmin
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ devise :database_authenticatable,
9
+ :rememberable,
10
+ :session_expirable,
11
+ :token_authenticatable,
12
+ :recoverable
13
+
14
+ validates_length_of :reset_password_token,
15
+ :minimum => 3,
16
+ :allow_blank => true
17
+
18
+ validates_uniqueness_of :email,
19
+ :allow_blank => true,
20
+ :if => :email_changed?
21
+
22
+ end
23
+
24
+ end
25
+
@@ -0,0 +1,22 @@
1
+
2
+ module SharedUser
3
+
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ devise :database_authenticatable,
9
+ :rememberable,
10
+ :session_expirable,
11
+ :token_authenticatable
12
+
13
+ attr_accessible :username,
14
+ :email,
15
+ :password,
16
+ :password_confirmation,
17
+ :remember_me
18
+
19
+ end
20
+
21
+ end
22
+
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENV_PATH = File.expand_path('../../config/environment', __FILE__)
5
+ BOOT_PATH = File.expand_path('../../config/boot', __FILE__)
6
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
7
+ ROOT_PATH = File.expand_path('../..', __FILE__)
8
+
9
+ require BOOT_PATH
10
+ require 'rails/commands'
@@ -0,0 +1,18 @@
1
+
2
+ require 'active_support/test_case'
3
+
4
+ class ActiveSupport::TestCase
5
+
6
+ def assert_not(assertion)
7
+ assert !assertion
8
+ end
9
+
10
+ def assert_same_content(result, expected)
11
+ assert expected.size == result.size, "the arrays doesn't have the same size"
12
+ expected.each do |element|
13
+ assert result.include?(element), "The array doesn't include '#{element}'."
14
+ end
15
+ end
16
+
17
+ end
18
+
@@ -0,0 +1,68 @@
1
+
2
+ require 'active_support/test_case'
3
+
4
+ class ActiveSupport::TestCase
5
+
6
+ def store_translations(locale, translations, &block)
7
+ begin
8
+ I18n.backend.store_translations(locale, translations)
9
+ yield
10
+ ensure
11
+ I18n.reload!
12
+ end
13
+ end
14
+
15
+ def generate_unique_email
16
+ @@email_count ||= 0
17
+ @@email_count += 1
18
+ "test#{@@email_count}@example.com"
19
+ end
20
+
21
+ def valid_attributes(attributes={})
22
+ { :username => "usertest",
23
+ :email => generate_unique_email,
24
+ :password => '12345678',
25
+ :password_confirmation => '12345678' }.update(attributes)
26
+ end
27
+
28
+ def new_user(attributes={})
29
+ User.new(valid_attributes(attributes))
30
+ end
31
+
32
+ def create_user(attributes={})
33
+ User.create!(valid_attributes(attributes))
34
+ end
35
+
36
+ def create_admin(attributes={})
37
+ valid_attributes = valid_attributes(attributes)
38
+ valid_attributes.delete(:username)
39
+ Admin.create!(valid_attributes)
40
+ end
41
+
42
+ # Execute the block setting the given values and restoring old values after
43
+ # the block is executed.
44
+ def swap(object, new_values)
45
+ old_values = {}
46
+ new_values.each do |key, value|
47
+ old_values[key] = object.send key
48
+ object.send :"#{key}=", value
49
+ end
50
+ clear_cached_variables(new_values)
51
+ yield
52
+ ensure
53
+ clear_cached_variables(new_values)
54
+ old_values.each do |key, value|
55
+ object.send :"#{key}=", value
56
+ end
57
+ end
58
+
59
+ def clear_cached_variables(options)
60
+ if options.key?(:case_insensitive_keys) || options.key?(:strip_whitespace_keys)
61
+ Devise.mappings.each do |_, mapping|
62
+ mapping.to.instance_variable_set(:@devise_param_filter, nil)
63
+ end
64
+ end
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,89 @@
1
+
2
+ require 'action_dispatch/testing/integration'
3
+
4
+ class ActionDispatch::IntegrationTest
5
+
6
+ def warden
7
+ request.env['warden']
8
+ end
9
+
10
+ def create_user(options={})
11
+ @user ||= begin
12
+ user = User.create!(
13
+ :username => 'usertest',
14
+ :email => options[:email] || 'user@test.com',
15
+ :password => options[:password] || '12345678',
16
+ :password_confirmation => options[:password] || '12345678',
17
+ :created_at => Time.now.utc
18
+ )
19
+ user.update_attribute(:confirmation_sent_at, options[:confirmation_sent_at]) if options[:confirmation_sent_at]
20
+ user
21
+ end
22
+ end
23
+
24
+ def create_admin(options={})
25
+ @admin ||= begin
26
+ admin = Admin.create!(
27
+ :email => options[:email] || 'admin@test.com',
28
+ :password => '123456', :password_confirmation => '123456',
29
+ :active => options[:active]
30
+ )
31
+ admin
32
+ end
33
+ end
34
+
35
+ def sign_in_as_user(options={}, &block)
36
+ user = create_user(options)
37
+ visit_with_option options[:visit], new_user_session_path
38
+ fill_in 'email', :with => options[:email] || 'user@test.com'
39
+ fill_in 'password', :with => options[:password] || '12345678'
40
+ check 'remember me' if options[:remember_me] == true
41
+ yield if block_given?
42
+ click_button 'Sign In'
43
+ user
44
+ end
45
+
46
+ def sign_in_as_admin(options={}, &block)
47
+ admin = create_admin(options)
48
+ visit_with_option options[:visit], new_admin_session_path
49
+ fill_in 'email', :with => 'admin@test.com'
50
+ fill_in 'password', :with => '123456'
51
+ yield if block_given?
52
+ click_button 'Sign In'
53
+ admin
54
+ end
55
+
56
+ # Fix assert_redirect_to in integration sessions because they don't take into
57
+ # account Middleware redirects.
58
+ #
59
+ def assert_redirected_to(url)
60
+ assert [301, 302].include?(@integration_session.status),
61
+ "Expected status to be 301 or 302, got #{@integration_session.status}"
62
+
63
+ assert_url url, @integration_session.headers["Location"]
64
+ end
65
+
66
+ def assert_url(expected, actual)
67
+ assert_equal prepend_host(expected), prepend_host(actual)
68
+ end
69
+
70
+ protected
71
+
72
+ def visit_with_option(given, default)
73
+ case given
74
+ when String
75
+ visit given
76
+ when FalseClass
77
+ # Do nothing
78
+ else
79
+ visit default
80
+ end
81
+ end
82
+
83
+ def prepend_host(url)
84
+ url = "http://#{request.host}#{url}" if url[0] == ?/
85
+ url
86
+ end
87
+
88
+ end
89
+
@@ -0,0 +1,28 @@
1
+
2
+ require 'webrat/core/elements/form'
3
+ require 'action_dispatch/testing/integration'
4
+
5
+ module Webrat
6
+
7
+ Form.class_eval do
8
+ def self.parse_rails_request_params(params)
9
+ Rack::Utils.parse_nested_query(params)
10
+ end
11
+ end
12
+
13
+ module Logging
14
+ # Avoid RAILS_DEFAULT_LOGGER deprecation warning
15
+ def logger # :nodoc:
16
+ ::Rails.logger
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ module ActionDispatch #:nodoc:
23
+ IntegrationTest.class_eval do
24
+ include Webrat::Methods
25
+ include Webrat::Matchers
26
+ end
27
+ end
28
+
@@ -0,0 +1,33 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'test/unit'
14
+
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
17
+
18
+ require 'devise_session_expirable'
19
+
20
+ require "rails_app/config/environment"
21
+ require "rails/test_help"
22
+ require "orm/active_record"
23
+
24
+ require 'webrat'
25
+ Webrat.configure do |config|
26
+ config.mode = :rails
27
+ config.open_error_files = false
28
+ end
29
+
30
+ # Add support to load paths so we can overwrite broken webrat setup
31
+ $:.unshift File.expand_path('../support', __FILE__)
32
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
33
+