namxam-devise 1.1.0.win

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.rdoc +455 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +118 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +311 -0
  6. data/Rakefile +55 -0
  7. data/TODO +3 -0
  8. data/app/controllers/devise/confirmations_controller.rb +33 -0
  9. data/app/controllers/devise/passwords_controller.rb +41 -0
  10. data/app/controllers/devise/registrations_controller.rb +57 -0
  11. data/app/controllers/devise/sessions_controller.rb +23 -0
  12. data/app/controllers/devise/unlocks_controller.rb +34 -0
  13. data/app/helpers/devise_helper.rb +17 -0
  14. data/app/mailers/devise/mailer.rb +71 -0
  15. data/app/views/devise/confirmations/new.html.erb +12 -0
  16. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  17. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  18. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  19. data/app/views/devise/passwords/edit.html.erb +16 -0
  20. data/app/views/devise/passwords/new.html.erb +12 -0
  21. data/app/views/devise/registrations/edit.html.erb +25 -0
  22. data/app/views/devise/registrations/new.html.erb +18 -0
  23. data/app/views/devise/sessions/new.html.erb +17 -0
  24. data/app/views/devise/shared/_links.erb +19 -0
  25. data/app/views/devise/unlocks/new.html.erb +12 -0
  26. data/config/locales/en.yml +39 -0
  27. data/lib/devise.rb +290 -0
  28. data/lib/devise/controllers/helpers.rb +231 -0
  29. data/lib/devise/controllers/internal_helpers.rb +98 -0
  30. data/lib/devise/controllers/scoped_views.rb +35 -0
  31. data/lib/devise/controllers/url_helpers.rb +41 -0
  32. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  33. data/lib/devise/encryptors/base.rb +20 -0
  34. data/lib/devise/encryptors/bcrypt.rb +19 -0
  35. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  36. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  37. data/lib/devise/encryptors/sha1.rb +25 -0
  38. data/lib/devise/encryptors/sha512.rb +25 -0
  39. data/lib/devise/failure_app.rb +107 -0
  40. data/lib/devise/hooks/activatable.rb +11 -0
  41. data/lib/devise/hooks/forgetable.rb +11 -0
  42. data/lib/devise/hooks/rememberable.rb +35 -0
  43. data/lib/devise/hooks/timeoutable.rb +22 -0
  44. data/lib/devise/hooks/trackable.rb +9 -0
  45. data/lib/devise/mapping.rb +103 -0
  46. data/lib/devise/models.rb +80 -0
  47. data/lib/devise/models/authenticatable.rb +126 -0
  48. data/lib/devise/models/confirmable.rb +164 -0
  49. data/lib/devise/models/database_authenticatable.rb +110 -0
  50. data/lib/devise/models/lockable.rb +165 -0
  51. data/lib/devise/models/recoverable.rb +81 -0
  52. data/lib/devise/models/registerable.rb +8 -0
  53. data/lib/devise/models/rememberable.rb +104 -0
  54. data/lib/devise/models/timeoutable.rb +26 -0
  55. data/lib/devise/models/token_authenticatable.rb +60 -0
  56. data/lib/devise/models/trackable.rb +30 -0
  57. data/lib/devise/models/validatable.rb +53 -0
  58. data/lib/devise/modules.rb +23 -0
  59. data/lib/devise/orm/active_record.rb +36 -0
  60. data/lib/devise/orm/mongoid.rb +29 -0
  61. data/lib/devise/path_checker.rb +18 -0
  62. data/lib/devise/rails.rb +69 -0
  63. data/lib/devise/rails/routes.rb +248 -0
  64. data/lib/devise/rails/warden_compat.rb +39 -0
  65. data/lib/devise/schema.rb +97 -0
  66. data/lib/devise/strategies/authenticatable.rb +111 -0
  67. data/lib/devise/strategies/base.rb +33 -0
  68. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  69. data/lib/devise/strategies/rememberable.rb +43 -0
  70. data/lib/devise/strategies/token_authenticatable.rb +49 -0
  71. data/lib/devise/test_helpers.rb +90 -0
  72. data/lib/devise/version.rb +3 -0
  73. data/lib/generators/active_record/devise_generator.rb +28 -0
  74. data/lib/generators/active_record/templates/migration.rb +29 -0
  75. data/lib/generators/devise/devise_generator.rb +17 -0
  76. data/lib/generators/devise/install_generator.rb +24 -0
  77. data/lib/generators/devise/orm_helpers.rb +23 -0
  78. data/lib/generators/devise/templates/README +25 -0
  79. data/lib/generators/devise/templates/devise.rb +139 -0
  80. data/lib/generators/devise/views_generator.rb +63 -0
  81. data/lib/generators/devise_install_generator.rb +4 -0
  82. data/lib/generators/devise_views_generator.rb +4 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/test/controllers/helpers_test.rb +213 -0
  85. data/test/controllers/internal_helpers_test.rb +51 -0
  86. data/test/controllers/url_helpers_test.rb +58 -0
  87. data/test/devise_test.rb +65 -0
  88. data/test/encryptors_test.rb +30 -0
  89. data/test/failure_app_test.rb +123 -0
  90. data/test/integration/authenticatable_test.rb +344 -0
  91. data/test/integration/confirmable_test.rb +104 -0
  92. data/test/integration/database_authenticatable_test.rb +38 -0
  93. data/test/integration/http_authenticatable_test.rb +49 -0
  94. data/test/integration/lockable_test.rb +109 -0
  95. data/test/integration/recoverable_test.rb +141 -0
  96. data/test/integration/registerable_test.rb +153 -0
  97. data/test/integration/rememberable_test.rb +91 -0
  98. data/test/integration/timeoutable_test.rb +80 -0
  99. data/test/integration/token_authenticatable_test.rb +88 -0
  100. data/test/integration/trackable_test.rb +64 -0
  101. data/test/mailers/confirmation_instructions_test.rb +80 -0
  102. data/test/mailers/reset_password_instructions_test.rb +68 -0
  103. data/test/mailers/unlock_instructions_test.rb +62 -0
  104. data/test/mapping_test.rb +85 -0
  105. data/test/models/confirmable_test.rb +221 -0
  106. data/test/models/database_authenticatable_test.rb +148 -0
  107. data/test/models/lockable_test.rb +188 -0
  108. data/test/models/recoverable_test.rb +138 -0
  109. data/test/models/rememberable_test.rb +176 -0
  110. data/test/models/timeoutable_test.rb +28 -0
  111. data/test/models/token_authenticatable_test.rb +37 -0
  112. data/test/models/trackable_test.rb +5 -0
  113. data/test/models/validatable_test.rb +99 -0
  114. data/test/models_test.rb +77 -0
  115. data/test/orm/active_record.rb +9 -0
  116. data/test/orm/mongoid.rb +10 -0
  117. data/test/rails_app/app/active_record/admin.rb +3 -0
  118. data/test/rails_app/app/active_record/shim.rb +2 -0
  119. data/test/rails_app/app/active_record/user.rb +7 -0
  120. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  121. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  122. data/test/rails_app/app/controllers/home_controller.rb +7 -0
  123. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  124. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  125. data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
  126. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  127. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  128. data/test/rails_app/app/mongoid/admin.rb +6 -0
  129. data/test/rails_app/app/mongoid/shim.rb +16 -0
  130. data/test/rails_app/app/mongoid/user.rb +10 -0
  131. data/test/rails_app/config/application.rb +35 -0
  132. data/test/rails_app/config/boot.rb +13 -0
  133. data/test/rails_app/config/environment.rb +5 -0
  134. data/test/rails_app/config/environments/development.rb +19 -0
  135. data/test/rails_app/config/environments/production.rb +33 -0
  136. data/test/rails_app/config/environments/test.rb +33 -0
  137. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  138. data/test/rails_app/config/initializers/devise.rb +136 -0
  139. data/test/rails_app/config/initializers/inflections.rb +2 -0
  140. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  141. data/test/rails_app/config/routes.rb +47 -0
  142. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
  143. data/test/rails_app/db/schema.rb +86 -0
  144. data/test/routes_test.rb +146 -0
  145. data/test/support/assertions.rb +24 -0
  146. data/test/support/helpers.rb +54 -0
  147. data/test/support/integration.rb +88 -0
  148. data/test/support/test_silencer.rb +5 -0
  149. data/test/support/webrat/integrations/rails.rb +32 -0
  150. data/test/test_helper.rb +21 -0
  151. data/test/test_helpers_test.rb +72 -0
  152. metadata +230 -0
@@ -0,0 +1,3 @@
1
+ module Devise
2
+ VERSION = "1.1.0".freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/devise/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class DeviseGenerator < ActiveRecord::Generators::Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ include Devise::Generators::OrmHelpers
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def generate_model
13
+ invoke "active_record:model", [name], :migration => false unless model_exists?
14
+ end
15
+
16
+ def copy_devise_migration
17
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
18
+ end
19
+
20
+ def inject_devise_content
21
+ inject_into_class model_path, class_name, model_contents + <<-CONTENT
22
+ # Setup accessible (or protected) attributes for your model
23
+ attr_accessible :email, :password, :password_confirmation, :remember_me
24
+ CONTENT
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.database_authenticatable :null => false
5
+ t.recoverable
6
+ t.rememberable
7
+ t.trackable
8
+
9
+ # t.confirmable
10
+ # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %>
11
+ # t.token_authenticatable
12
+
13
+ <% for attribute in attributes -%>
14
+ t.<%= attribute.type %> :<%= attribute.name %>
15
+ <% end -%>
16
+
17
+ t.timestamps
18
+ end
19
+
20
+ add_index :<%= table_name %>, :email, :unique => true
21
+ add_index :<%= table_name %>, :reset_password_token, :unique => true
22
+ # add_index :<%= table_name %>, :confirmation_token, :unique => true
23
+ # add_index :<%= table_name %>, :unlock_token, :unique => true
24
+ end
25
+
26
+ def self.down
27
+ drop_table :<%= table_name %>
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Devise
2
+ module Generators
3
+ class DeviseGenerator < Rails::Generators::NamedBase
4
+ namespace "devise"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ desc "Generates a model with the given NAME (if one does not exist) with devise " <<
8
+ "configuration plus a migration file and devise routes."
9
+
10
+ hook_for :orm
11
+
12
+ def add_devise_routes
13
+ route "devise_for :#{table_name}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/secure_random'
2
+
3
+ module Devise
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Creates a Devise initializer and copy locale files to your application."
9
+ class_option :orm
10
+
11
+ def copy_initializer
12
+ template "devise.rb", "config/initializers/devise.rb"
13
+ end
14
+
15
+ def copy_locale
16
+ copy_file "../../../../config/locales/en.yml", "config/locales/devise.en.yml"
17
+ end
18
+
19
+ def show_readme
20
+ readme "README"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Devise
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ <<-CONTENT
6
+ # Include default devise modules. Others available are:
7
+ # :token_authenticatable, :confirmable, :lockable and :timeoutable
8
+ devise :database_authenticatable, :registerable,
9
+ :recoverable, :rememberable, :trackable, :validatable
10
+
11
+ CONTENT
12
+ end
13
+
14
+ def model_exists?
15
+ File.exists?(File.join(destination_root, model_path))
16
+ end
17
+
18
+ def model_path
19
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+
2
+ ===============================================================================
3
+
4
+ Some setup you must do manually if you haven't yet:
5
+
6
+ 1. Setup default url options for your specific environment. Here is an
7
+ example of development environment:
8
+
9
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
10
+
11
+ This is a required Rails configuration. In production it must be the
12
+ actual host of your application
13
+
14
+ 2. Ensure you have defined root_url to *something* in your config/routes.rb.
15
+ For example:
16
+
17
+ root :to => "home#index"
18
+
19
+ 3. Ensure you have flash messages in app/views/layouts/application.html.erb.
20
+ For example:
21
+
22
+ <p class="notice"><%= notice %></p>
23
+ <p class="alert"><%= alert %></p>
24
+
25
+ ===============================================================================
@@ -0,0 +1,139 @@
1
+ # Use this hook to configure devise mailer, warden hooks and so forth. The first
2
+ # four configuration values can also be set straight in your models.
3
+ Devise.setup do |config|
4
+ # ==> Mailer Configuration
5
+ # Configure the e-mail address which will be shown in DeviseMailer.
6
+ config.mailer_sender = "please-change-me@config-initializers-devise.com"
7
+
8
+ # Configure the class responsible to send e-mails.
9
+ # config.mailer = "Devise::Mailer"
10
+
11
+ # ==> ORM configuration
12
+ # Load and configure the ORM. Supports :active_record (default) and
13
+ # :mongoid (bson_ext recommended) by default. Other ORMs may be
14
+ # available as additional gems.
15
+ require 'devise/orm/<%= options[:orm] %>'
16
+
17
+ # ==> Configuration for any authentication mechanism
18
+ # Configure which keys are used when authenticating an user. By default is
19
+ # just :email. You can configure it to use [:username, :subdomain], so for
20
+ # authenticating an user, both parameters are required. Remember that those
21
+ # parameters are used only when authenticating and not when retrieving from
22
+ # session. If you need permissions, you should implement that in a before filter.
23
+ # config.authentication_keys = [ :email ]
24
+
25
+ # Tell if authentication through request.params is enabled. True by default.
26
+ # config.params_authenticatable = true
27
+
28
+ # Tell if authentication through HTTP Basic Auth is enabled. True by default.
29
+ # config.http_authenticatable = true
30
+
31
+ # Set this to true to use Basic Auth for AJAX requests. True by default.
32
+ # config.http_authenticatable_on_xhr = true
33
+
34
+ # The realm used in Http Basic Authentication
35
+ # config.http_authentication_realm = "Application"
36
+
37
+ # ==> Configuration for :database_authenticatable
38
+ # For bcrypt, this is the cost for hashing the password and defaults to 10. If
39
+ # using other encryptors, it sets how many times you want the password re-encrypted.
40
+ config.stretches = 20
41
+
42
+ # Define which will be the encryption algorithm. Devise also supports encryptors
43
+ # from others authentication tools as :clearance_sha1, :authlogic_sha512 (then
44
+ # you should set stretches above to 20 for default behavior) and :restful_authentication_sha1
45
+ # (then you should set stretches to 10, and copy REST_AUTH_SITE_KEY to pepper)
46
+ config.encryptor = :authlogic_sha512
47
+
48
+ # Setup a pepper to generate the encrypted password.
49
+ config.pepper = <%= ActiveSupport::SecureRandom.hex(64).inspect %>
50
+
51
+ # ==> Configuration for :confirmable
52
+ # The time you want to give your user to confirm his account. During this time
53
+ # he will be able to access your application without confirming. Default is nil.
54
+ # When confirm_within is zero, the user won't be able to sign in without confirming.
55
+ # You can use this to let your user access some features of your application
56
+ # without confirming the account, but blocking it after a certain period
57
+ # (ie 2 days).
58
+ # config.confirm_within = 2.days
59
+
60
+ # ==> Configuration for :rememberable
61
+ # The time the user will be remembered without asking for credentials again.
62
+ # config.remember_for = 2.weeks
63
+
64
+ # If a valid remember token can be re-used between multiple browsers.
65
+ # config.remember_across_browsers = true
66
+
67
+ # ==> Configuration for :validatable
68
+ # Range for password length
69
+ # config.password_length = 6..20
70
+
71
+ # Regex to use to validate the email address
72
+ # config.email_regexp = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
73
+
74
+ # ==> Configuration for :timeoutable
75
+ # The time you want to timeout the user session without activity. After this
76
+ # time the user will be asked for credentials again.
77
+ # config.timeout_in = 10.minutes
78
+
79
+ # ==> Configuration for :lockable
80
+ # Defines which strategy will be used to lock an account.
81
+ # :failed_attempts = Locks an account after a number of failed attempts to sign in.
82
+ # :none = No lock strategy. You should handle locking by yourself.
83
+ # config.lock_strategy = :failed_attempts
84
+
85
+ # Defines which strategy will be used to unlock an account.
86
+ # :email = Sends an unlock link to the user email
87
+ # :time = Re-enables login after a certain amount of time (see :unlock_in below)
88
+ # :both = Enables both strategies
89
+ # :none = No unlock strategy. You should handle unlocking by yourself.
90
+ # config.unlock_strategy = :both
91
+
92
+ # Number of authentication tries before locking an account if lock_strategy
93
+ # is failed attempts.
94
+ # config.maximum_attempts = 20
95
+
96
+ # Time interval to unlock the account if :time is enabled as unlock_strategy.
97
+ # config.unlock_in = 1.hour
98
+
99
+ # ==> Configuration for :token_authenticatable
100
+ # Defines name of the authentication token params key
101
+ # config.token_authentication_key = :auth_token
102
+
103
+ # ==> Scopes configuration
104
+ # Turn scoped views on. Before rendering "sessions/new", it will first check for
105
+ # "sessions/users/new". It's turned off by default because it's slower if you
106
+ # are using only default views.
107
+ # config.scoped_views = true
108
+
109
+ # Configure the default scope given to Warden. By default it's the first
110
+ # devise role declared in your routes.
111
+ # config.default_scope = :user
112
+
113
+ # Configure sign_out behavior.
114
+ # By default sign_out is scoped (i.e. /users/sign_out affects only :user scope).
115
+ # In case of sign_out_all_scopes set to true any logout action will sign out all active scopes.
116
+ # config.sign_out_all_scopes = false
117
+
118
+ # ==> Navigation configuration
119
+ # Lists the formats that should be treated as navigational. Formats like
120
+ # :html, should redirect to the sign in page when the user does not have
121
+ # access, but formats like :xml or :json, should return 401.
122
+ # If you have any extra navigational formats, like :iphone or :mobile, you
123
+ # should add them to the navigational formats lists. Default is [:html]
124
+ # config.navigational_formats = [:html, :iphone]
125
+
126
+ # ==> Warden configuration
127
+ # If you want to use other strategies, that are not (yet) supported by Devise,
128
+ # you can configure them inside the config.warden block. The example below
129
+ # allows you to setup OAuth, using http://github.com/roman/warden_oauth
130
+ #
131
+ # config.warden do |manager|
132
+ # manager.oauth(:twitter) do |twitter|
133
+ # twitter.consumer_secret = <YOUR CONSUMER SECRET>
134
+ # twitter.consumer_key = <YOUR CONSUMER KEY>
135
+ # twitter.options :site => 'http://twitter.com'
136
+ # end
137
+ # manager.default_strategies(:scope => :user).unshift :twitter_oauth
138
+ # end
139
+ end
@@ -0,0 +1,63 @@
1
+ module Devise
2
+ module Generators
3
+ class ViewsGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../../../app/views", __FILE__)
5
+ desc "Copies all Devise views to your application."
6
+
7
+ argument :scope, :required => false, :default => nil,
8
+ :desc => "The scope to copy views to"
9
+
10
+ class_option :template_engine, :type => :string, :aliases => "-t", :default => "erb",
11
+ :desc => "Template engine for the views. Available options are 'erb' and 'haml'."
12
+
13
+ def copy_views
14
+ case options[:template_engine].to_s
15
+ when "haml"
16
+ verify_haml_existence
17
+ verify_haml_version
18
+ create_and_copy_haml_views
19
+ else
20
+ directory "devise", "app/views/#{scope || :devise}"
21
+ end
22
+ end
23
+
24
+ protected
25
+
26
+ def verify_haml_existence
27
+ begin
28
+ require 'haml'
29
+ rescue LoadError
30
+ say "HAML is not installed, or it is not specified in your Gemfile."
31
+ exit
32
+ end
33
+ end
34
+
35
+ def verify_haml_version
36
+ unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
37
+ say "To generate HAML templates, you need to install HAML 2.3 or above."
38
+ exit
39
+ end
40
+ end
41
+
42
+ def create_and_copy_haml_views
43
+ require 'tmpdir'
44
+ html_root = "#{self.class.source_root}/devise"
45
+
46
+ Dir.mktmpdir("devise-haml.") do |haml_root|
47
+ Dir["#{html_root}/**/*"].each do |path|
48
+ relative_path = path.sub(html_root, "")
49
+ source_path = (haml_root + relative_path).sub(/erb$/, "haml")
50
+
51
+ if File.directory?(path)
52
+ FileUtils.mkdir_p(source_path)
53
+ else
54
+ `html2haml -r #{path} #{source_path}`
55
+ end
56
+ end
57
+
58
+ directory haml_root, "app/views/#{scope || :devise}"
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,4 @@
1
+ # Remove this file after deprecation
2
+ if caller.none? { |l| l =~ %r{lib/rails/generators\.rb:(\d+):in `lookup!'$} }
3
+ warn "[WARNING] `rails g devise_install` is deprecated, please use `rails g devise:install` instead."
4
+ end
@@ -0,0 +1,4 @@
1
+ # Remove this file after deprecation
2
+ if caller.none? { |l| l =~ %r{lib/rails/generators\.rb:(\d+):in `lookup!'$} }
3
+ warn "[WARNING] `rails g devise_views` is deprecated, please use `rails g devise:views` instead."
4
+ end
@@ -0,0 +1,17 @@
1
+ require 'generators/devise/orm_helpers'
2
+
3
+ module Mongoid
4
+ module Generators
5
+ class DeviseGenerator < Rails::Generators::NamedBase
6
+ include Devise::Generators::OrmHelpers
7
+
8
+ def generate_model
9
+ invoke "mongoid:model", [name] unless model_exists?
10
+ end
11
+
12
+ def inject_devise_content
13
+ inject_into_file model_path, model_contents, :after => "include Mongoid::Document\n"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,213 @@
1
+ require 'test_helper'
2
+ require 'ostruct'
3
+
4
+ class MockController < ApplicationController
5
+ attr_accessor :env
6
+
7
+ def request
8
+ self
9
+ end
10
+
11
+ def path
12
+ ''
13
+ end
14
+
15
+ def index
16
+ end
17
+
18
+ def host_with_port
19
+ "test.host:3000"
20
+ end
21
+
22
+ def protocol
23
+ "http"
24
+ end
25
+
26
+ def script_name
27
+ ""
28
+ end
29
+
30
+ def symbolized_path_parameters
31
+ {}
32
+ end
33
+ end
34
+
35
+ class ControllerAuthenticableTest < ActionController::TestCase
36
+ tests MockController
37
+
38
+ def setup
39
+ @mock_warden = OpenStruct.new
40
+ @controller.env = { 'warden' => @mock_warden }
41
+ end
42
+
43
+ test 'setup warden' do
44
+ assert_not_nil @controller.warden
45
+ end
46
+
47
+ test 'provide access to warden instance' do
48
+ assert_equal @controller.warden, @controller.env['warden']
49
+ end
50
+
51
+ test 'proxy signed_in? to authenticated' do
52
+ @mock_warden.expects(:authenticate?).with(:scope => :my_scope)
53
+ @controller.signed_in?(:my_scope)
54
+ end
55
+
56
+ test 'proxy anybody_signed_in? to signed_in?' do
57
+ Devise.mappings.keys.each { |scope| # :user, :admin, :manager
58
+ @controller.expects(:signed_in?).with(scope)
59
+ }
60
+ @controller.anybody_signed_in?
61
+ end
62
+
63
+ test 'proxy current_admin to authenticate with admin scope' do
64
+ @mock_warden.expects(:authenticate).with(:scope => :admin)
65
+ @controller.current_admin
66
+ end
67
+
68
+ test 'proxy current_user to authenticate with user scope' do
69
+ @mock_warden.expects(:authenticate).with(:scope => :user)
70
+ @controller.current_user
71
+ end
72
+
73
+ test 'proxy user_authenticate! to authenticate with user scope' do
74
+ @mock_warden.expects(:authenticate!).with(:scope => :user)
75
+ @controller.authenticate_user!
76
+ end
77
+
78
+ test 'proxy admin_authenticate! to authenticate with admin scope' do
79
+ @mock_warden.expects(:authenticate!).with(:scope => :admin)
80
+ @controller.authenticate_admin!
81
+ end
82
+
83
+ test 'proxy user_signed_in? to authenticate? with user scope' do
84
+ @mock_warden.expects(:authenticate?).with(:scope => :user)
85
+ @controller.user_signed_in?
86
+ end
87
+
88
+ test 'proxy admin_signed_in? to authenticate? with admin scope' do
89
+ @mock_warden.expects(:authenticate?).with(:scope => :admin)
90
+ @controller.admin_signed_in?
91
+ end
92
+
93
+ test 'proxy user_session to session scope in warden' do
94
+ @mock_warden.expects(:authenticate).with(:scope => :user).returns(true)
95
+ @mock_warden.expects(:session).with(:user).returns({})
96
+ @controller.user_session
97
+ end
98
+
99
+ test 'proxy admin_session to session scope in warden' do
100
+ @mock_warden.expects(:authenticate).with(:scope => :admin).returns(true)
101
+ @mock_warden.expects(:session).with(:admin).returns({})
102
+ @controller.admin_session
103
+ end
104
+
105
+ test 'sign in proxy to set_user on warden' do
106
+ user = User.new
107
+ @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
108
+ @controller.sign_in(:user, user)
109
+ end
110
+
111
+ test 'sign in accepts a resource as argument' do
112
+ user = User.new
113
+ @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
114
+ @controller.sign_in(user)
115
+ end
116
+
117
+ test 'sign out proxy to logout on warden' do
118
+ @mock_warden.expects(:user).with(:user).returns(true)
119
+ @mock_warden.expects(:logout).with(:user).returns(true)
120
+ @controller.sign_out(:user)
121
+ end
122
+
123
+ test 'sign out accepts a resource as argument' do
124
+ @mock_warden.expects(:user).with(:user).returns(true)
125
+ @mock_warden.expects(:logout).with(:user).returns(true)
126
+ @controller.sign_out(User.new)
127
+ end
128
+
129
+ test 'sign out everybody proxy to logout on warden' do
130
+ Devise.mappings.keys.each { |scope|
131
+ @mock_warden.expects(:user).with(scope).returns(true)
132
+ }
133
+
134
+ @mock_warden.expects(:logout).with(*Devise.mappings.keys).returns(true)
135
+ @controller.sign_out_all_scopes
136
+ end
137
+
138
+ test 'stored location for returns the location for a given scope' do
139
+ assert_nil @controller.stored_location_for(:user)
140
+ @controller.session[:"user_return_to"] = "/foo.bar"
141
+ assert_equal "/foo.bar", @controller.stored_location_for(:user)
142
+ end
143
+
144
+ test 'stored location for accepts a resource as argument' do
145
+ assert_nil @controller.stored_location_for(:user)
146
+ @controller.session[:"user_return_to"] = "/foo.bar"
147
+ assert_equal "/foo.bar", @controller.stored_location_for(User.new)
148
+ end
149
+
150
+ test 'stored location cleans information after reading' do
151
+ @controller.session[:"user_return_to"] = "/foo.bar"
152
+ assert_equal "/foo.bar", @controller.stored_location_for(:user)
153
+ assert_nil @controller.session[:"user_return_to"]
154
+ end
155
+
156
+ test 'after sign in path defaults to root path if none by was specified for the given scope' do
157
+ assert_equal root_path, @controller.after_sign_in_path_for(:user)
158
+ end
159
+
160
+ test 'after sign in path defaults to the scoped root path' do
161
+ assert_equal admin_root_path, @controller.after_sign_in_path_for(:admin)
162
+ end
163
+
164
+ test 'after update path defaults to root path if none by was specified for the given scope' do
165
+ assert_equal root_path, @controller.after_update_path_for(:user)
166
+ end
167
+
168
+ test 'after update path defaults to the scoped root path' do
169
+ assert_equal admin_root_path, @controller.after_update_path_for(:admin)
170
+ end
171
+
172
+ test 'after sign out path defaults to the root path' do
173
+ assert_equal root_path, @controller.after_sign_out_path_for(:admin)
174
+ assert_equal root_path, @controller.after_sign_out_path_for(:user)
175
+ end
176
+
177
+ test 'sign in and redirect uses the stored location' do
178
+ user = User.new
179
+ @controller.session[:"user_return_to"] = "/foo.bar"
180
+ @mock_warden.expects(:user).with(:user).returns(nil)
181
+ @mock_warden.expects(:set_user).with(user, :scope => :user).returns(true)
182
+ @controller.expects(:redirect_to).with("/foo.bar")
183
+ @controller.sign_in_and_redirect(user)
184
+ end
185
+
186
+ test 'sign in and redirect uses the configured after sign in path' do
187
+ admin = Admin.new
188
+ @mock_warden.expects(:user).with(:admin).returns(nil)
189
+ @mock_warden.expects(:set_user).with(admin, :scope => :admin).returns(true)
190
+ @controller.expects(:redirect_to).with(admin_root_path)
191
+ @controller.sign_in_and_redirect(admin)
192
+ end
193
+
194
+ test 'sign in and redirect does not sign in again if user is already signed' do
195
+ admin = Admin.new
196
+ @mock_warden.expects(:user).with(:admin).returns(admin)
197
+ @mock_warden.expects(:set_user).never
198
+ @controller.expects(:redirect_to).with(admin_root_path)
199
+ @controller.sign_in_and_redirect(admin)
200
+ end
201
+
202
+ test 'sign out and redirect uses the configured after sign out path' do
203
+ @mock_warden.expects(:user).with(:admin).returns(true)
204
+ @mock_warden.expects(:logout).with(:admin).returns(true)
205
+ @controller.expects(:redirect_to).with(admin_root_path)
206
+ @controller.instance_eval "def after_sign_out_path_for(resource); admin_root_path; end"
207
+ @controller.sign_out_and_redirect(:admin)
208
+ end
209
+
210
+ test 'is not a devise controller' do
211
+ assert_not @controller.devise_controller?
212
+ end
213
+ end