loyal_devise 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.rdoc +881 -0
- data/CONTRIBUTING.md +12 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +154 -0
- data/MIT-LICENSE +20 -0
- data/README.md +388 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +44 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
- data/app/controllers/devise/passwords_controller.rb +57 -0
- data/app/controllers/devise/registrations_controller.rb +120 -0
- data/app/controllers/devise/sessions_controller.rb +51 -0
- data/app/controllers/devise/unlocks_controller.rb +45 -0
- data/app/controllers/devise_controller.rb +193 -0
- data/app/helpers/devise_helper.rb +26 -0
- data/app/mailers/devise/mailer.rb +16 -0
- data/app/views/devise/_links.erb +3 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +59 -0
- data/devise.gemspec +26 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise/controllers/helpers.rb +273 -0
- data/lib/devise/controllers/rememberable.rb +53 -0
- data/lib/devise/controllers/scoped_views.rb +18 -0
- data/lib/devise/controllers/url_helpers.rb +68 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/failure_app.rb +188 -0
- data/lib/devise/hooks/activatable.rb +12 -0
- data/lib/devise/hooks/forgetable.rb +10 -0
- data/lib/devise/hooks/lockable.rb +8 -0
- data/lib/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/hooks/timeoutable.rb +26 -0
- data/lib/devise/hooks/trackable.rb +10 -0
- data/lib/devise/mailers/helpers.rb +92 -0
- data/lib/devise/mapping.rb +173 -0
- data/lib/devise/models/authenticatable.rb +269 -0
- data/lib/devise/models/confirmable.rb +271 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +194 -0
- data/lib/devise/models/omniauthable.rb +28 -0
- data/lib/devise/models/recoverable.rb +141 -0
- data/lib/devise/models/registerable.rb +26 -0
- data/lib/devise/models/rememberable.rb +126 -0
- data/lib/devise/models/timeoutable.rb +50 -0
- data/lib/devise/models/token_authenticatable.rb +90 -0
- data/lib/devise/models/trackable.rb +36 -0
- data/lib/devise/models/validatable.rb +67 -0
- data/lib/devise/models.rb +129 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +46 -0
- data/lib/devise/omniauth/url_helpers.rb +19 -0
- data/lib/devise/omniauth.rb +29 -0
- data/lib/devise/orm/active_record.rb +4 -0
- data/lib/devise/orm/mongoid.rb +4 -0
- data/lib/devise/param_filter.rb +42 -0
- data/lib/devise/rails/routes.rb +447 -0
- data/lib/devise/rails/warden_compat.rb +44 -0
- data/lib/devise/rails.rb +55 -0
- data/lib/devise/strategies/authenticatable.rb +177 -0
- data/lib/devise/strategies/base.rb +21 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +56 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +132 -0
- data/lib/devise/time_inflector.rb +15 -0
- data/lib/devise/version.rb +4 -0
- data/lib/devise.rb +445 -0
- data/lib/generators/active_record/devise_generator.rb +80 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/active_record/templates/migration_existing.rb +27 -0
- data/lib/generators/devise/devise_generator.rb +25 -0
- data/lib/generators/devise/install_generator.rb +25 -0
- data/lib/generators/devise/orm_helpers.rb +33 -0
- data/lib/generators/devise/views_generator.rb +117 -0
- data/lib/generators/mongoid/devise_generator.rb +58 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +241 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/custom_strategy_test.rb +63 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +111 -0
- data/test/controllers/sessions_controller_test.rb +58 -0
- data/test/controllers/url_helpers_test.rb +60 -0
- data/test/delegator_test.rb +20 -0
- data/test/devise_test.rb +73 -0
- data/test/failure_app_test.rb +222 -0
- data/test/generators/active_record_generator_test.rb +76 -0
- data/test/generators/devise_generator_test.rb +40 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/generators/mongoid_generator_test.rb +24 -0
- data/test/generators/views_generator_test.rb +53 -0
- data/test/helpers/devise_helper_test.rb +52 -0
- data/test/indifferent_hash.rb +34 -0
- data/test/integration/authenticatable_test.rb +634 -0
- data/test/integration/confirmable_test.rb +299 -0
- data/test/integration/database_authenticatable_test.rb +83 -0
- data/test/integration/http_authenticatable_test.rb +98 -0
- data/test/integration/lockable_test.rb +243 -0
- data/test/integration/omniauthable_test.rb +134 -0
- data/test/integration/recoverable_test.rb +307 -0
- data/test/integration/registerable_test.rb +346 -0
- data/test/integration/rememberable_test.rb +159 -0
- data/test/integration/timeoutable_test.rb +141 -0
- data/test/integration/token_authenticatable_test.rb +162 -0
- data/test/integration/trackable_test.rb +93 -0
- data/test/mailers/confirmation_instructions_test.rb +103 -0
- data/test/mailers/reset_password_instructions_test.rb +84 -0
- data/test/mailers/unlock_instructions_test.rb +78 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/authenticatable_test.rb +8 -0
- data/test/models/confirmable_test.rb +392 -0
- data/test/models/database_authenticatable_test.rb +190 -0
- data/test/models/lockable_test.rb +274 -0
- data/test/models/omniauthable_test.rb +8 -0
- data/test/models/recoverable_test.rb +206 -0
- data/test/models/registerable_test.rb +8 -0
- data/test/models/rememberable_test.rb +175 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +47 -0
- data/test/models/token_authenticatable_test.rb +56 -0
- data/test/models/trackable_test.rb +14 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +180 -0
- data/test/omniauth/config_test.rb +58 -0
- data/test/omniauth/url_helpers_test.rb +52 -0
- data/test/orm/active_record.rb +10 -0
- data/test/orm/mongoid.rb +15 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/shim.rb +3 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +12 -0
- data/test/rails_app/app/controllers/application_controller.rb +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +26 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
- data/test/rails_app/app/controllers/users_controller.rb +24 -0
- data/test/rails_app/app/helpers/application_helper.rb +4 -0
- data/test/rails_app/app/mailers/users/mailer.rb +9 -0
- data/test/rails_app/app/mongoid/admin.rb +28 -0
- data/test/rails_app/app/mongoid/shim.rb +25 -0
- data/test/rails_app/app/mongoid/user.rb +43 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config/application.rb +42 -0
- data/test/rails_app/config/boot.rb +9 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +6 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +34 -0
- data/test/rails_app/config/environments/test.rb +34 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
- data/test/rails_app/config/initializers/devise.rb +179 -0
- data/test/rails_app/config/initializers/inflections.rb +3 -0
- data/test/rails_app/config/initializers/secret_token.rb +3 -0
- data/test/rails_app/config/routes.rb +101 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
- data/test/rails_app/db/schema.rb +53 -0
- data/test/rails_app/lib/shared_admin.rb +15 -0
- data/test/rails_app/lib/shared_user.rb +27 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +249 -0
- data/test/support/assertions.rb +41 -0
- data/test/support/helpers.rb +92 -0
- data/test/support/integration.rb +93 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +25 -0
- data/test/test_helper.rb +28 -0
- data/test/test_helpers_test.rb +152 -0
- metadata +407 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
class FailureTest < ActiveSupport::TestCase
|
6
|
+
class RootFailureApp < Devise::FailureApp
|
7
|
+
def fake_app
|
8
|
+
Object.new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.context(name, &block)
|
13
|
+
instance_eval(&block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def call_failure(env_params={})
|
17
|
+
env = {
|
18
|
+
'REQUEST_URI' => 'http://test.host/',
|
19
|
+
'HTTP_HOST' => 'test.host',
|
20
|
+
'REQUEST_METHOD' => 'GET',
|
21
|
+
'warden.options' => { :scope => :user },
|
22
|
+
'rack.session' => {},
|
23
|
+
'action_dispatch.request.formats' => Array(env_params.delete('formats') || Mime::HTML),
|
24
|
+
'rack.input' => "",
|
25
|
+
'warden' => OpenStruct.new(:message => nil)
|
26
|
+
}.merge!(env_params)
|
27
|
+
|
28
|
+
@response = (env.delete(:app) || Devise::FailureApp).call(env).to_a
|
29
|
+
@request = ActionDispatch::Request.new(env)
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'When redirecting' do
|
33
|
+
test 'returns to the default redirect location' do
|
34
|
+
call_failure
|
35
|
+
assert_equal 302, @response.first
|
36
|
+
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
37
|
+
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
38
|
+
end
|
39
|
+
|
40
|
+
test 'returns to the default redirect location for wildcard requests' do
|
41
|
+
call_failure 'action_dispatch.request.formats' => nil, 'HTTP_ACCEPT' => '*/*'
|
42
|
+
assert_equal 302, @response.first
|
43
|
+
assert_equal 'http://test.host/users/sign_in', @response.second['Location']
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'returns to the root path if no session path is available' do
|
47
|
+
swap Devise, :router_name => :fake_app do
|
48
|
+
call_failure :app => RootFailureApp
|
49
|
+
assert_equal 302, @response.first
|
50
|
+
assert_equal 'You need to sign in or sign up before continuing.', @request.flash[:alert]
|
51
|
+
assert_equal 'http://test.host/', @response.second['Location']
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
if Rails.application.config.respond_to?(:relative_url_root)
|
56
|
+
test 'returns to the default redirect location considering the relative url root' do
|
57
|
+
swap Rails.application.config, :relative_url_root => "/sample" do
|
58
|
+
call_failure
|
59
|
+
assert_equal 302, @response.first
|
60
|
+
assert_equal 'http://test.host/sample/users/sign_in', @response.second['Location']
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
test 'uses the proxy failure message as symbol' do
|
66
|
+
call_failure('warden' => OpenStruct.new(:message => :invalid))
|
67
|
+
assert_equal 'Invalid email or password.', @request.flash[:alert]
|
68
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'uses the proxy failure message as string' do
|
72
|
+
call_failure('warden' => OpenStruct.new(:message => 'Hello world'))
|
73
|
+
assert_equal 'Hello world', @request.flash[:alert]
|
74
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
75
|
+
end
|
76
|
+
|
77
|
+
test 'set content type to default text/html' do
|
78
|
+
call_failure
|
79
|
+
assert_equal 'text/html; charset=utf-8', @response.second['Content-Type']
|
80
|
+
end
|
81
|
+
|
82
|
+
test 'setup a default message' do
|
83
|
+
call_failure
|
84
|
+
assert_match /You are being/, @response.last.body
|
85
|
+
assert_match /redirected/, @response.last.body
|
86
|
+
assert_match /users\/sign_in/, @response.last.body
|
87
|
+
end
|
88
|
+
|
89
|
+
test 'works for any navigational format' do
|
90
|
+
swap Devise, :navigational_formats => [:xml] do
|
91
|
+
call_failure('formats' => Mime::XML)
|
92
|
+
assert_equal 302, @response.first
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
test 'redirects the correct format if it is a non-html format request' do
|
97
|
+
swap Devise, :navigational_formats => [:js] do
|
98
|
+
call_failure('formats' => Mime::JS)
|
99
|
+
assert_equal 'http://test.host/users/sign_in.js', @response.second["Location"]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'For HTTP request' do
|
105
|
+
test 'return 401 status' do
|
106
|
+
call_failure('formats' => Mime::XML)
|
107
|
+
assert_equal 401, @response.first
|
108
|
+
end
|
109
|
+
|
110
|
+
test 'return appropriate body for xml' do
|
111
|
+
call_failure('formats' => Mime::XML)
|
112
|
+
result = %(<?xml version="1.0" encoding="UTF-8"?>\n<errors>\n <error>You need to sign in or sign up before continuing.</error>\n</errors>\n)
|
113
|
+
assert_equal result, @response.last.body
|
114
|
+
end
|
115
|
+
|
116
|
+
test 'return appropriate body for json' do
|
117
|
+
call_failure('formats' => Mime::JSON)
|
118
|
+
result = %({"error":"You need to sign in or sign up before continuing."})
|
119
|
+
assert_equal result, @response.last.body
|
120
|
+
end
|
121
|
+
|
122
|
+
test 'return 401 status for unknown formats' do
|
123
|
+
call_failure 'formats' => []
|
124
|
+
assert_equal 401, @response.first
|
125
|
+
end
|
126
|
+
|
127
|
+
test 'return WWW-authenticate headers if model allows' do
|
128
|
+
call_failure('formats' => Mime::XML)
|
129
|
+
assert_equal 'Basic realm="Application"', @response.second["WWW-Authenticate"]
|
130
|
+
end
|
131
|
+
|
132
|
+
test 'does not return WWW-authenticate headers if model does not allow' do
|
133
|
+
swap Devise, :http_authenticatable => false do
|
134
|
+
call_failure('formats' => Mime::XML)
|
135
|
+
assert_nil @response.second["WWW-Authenticate"]
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
test 'works for any non navigational format' do
|
140
|
+
swap Devise, :navigational_formats => [] do
|
141
|
+
call_failure('formats' => Mime::HTML)
|
142
|
+
assert_equal 401, @response.first
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
test 'uses the failure message as response body' do
|
147
|
+
call_failure('formats' => Mime::XML, 'warden' => OpenStruct.new(:message => :invalid))
|
148
|
+
assert_match '<error>Invalid email or password.</error>', @response.third.body
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'on ajax call' do
|
152
|
+
context 'when http_authenticatable_on_xhr is false' do
|
153
|
+
test 'dont return 401 with navigational formats' do
|
154
|
+
swap Devise, :http_authenticatable_on_xhr => false do
|
155
|
+
call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
156
|
+
assert_equal 302, @response.first
|
157
|
+
assert_equal 'http://test.host/users/sign_in', @response.second["Location"]
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
test 'dont return 401 with non navigational formats' do
|
162
|
+
swap Devise, :http_authenticatable_on_xhr => false do
|
163
|
+
call_failure('formats' => Mime::JSON, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
164
|
+
assert_equal 302, @response.first
|
165
|
+
assert_equal 'http://test.host/users/sign_in.json', @response.second["Location"]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when http_authenticatable_on_xhr is true' do
|
171
|
+
test 'return 401' do
|
172
|
+
swap Devise, :http_authenticatable_on_xhr => true do
|
173
|
+
call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
174
|
+
assert_equal 401, @response.first
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
test 'skip WWW-Authenticate header' do
|
179
|
+
swap Devise, :http_authenticatable_on_xhr => true do
|
180
|
+
call_failure('formats' => Mime::HTML, 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest')
|
181
|
+
assert_nil @response.second['WWW-Authenticate']
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context 'With recall' do
|
189
|
+
test 'calls the original controller if invalid email or password' do
|
190
|
+
env = {
|
191
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in" },
|
192
|
+
"devise.mapping" => Devise.mappings[:user],
|
193
|
+
"warden" => stub_everything
|
194
|
+
}
|
195
|
+
call_failure(env)
|
196
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
197
|
+
assert @response.third.body.include?('Invalid email or password.')
|
198
|
+
end
|
199
|
+
|
200
|
+
test 'calls the original controller if not confirmed email' do
|
201
|
+
env = {
|
202
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :unconfirmed },
|
203
|
+
"devise.mapping" => Devise.mappings[:user],
|
204
|
+
"warden" => stub_everything
|
205
|
+
}
|
206
|
+
call_failure(env)
|
207
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
208
|
+
assert @response.third.body.include?('You have to confirm your account before continuing.')
|
209
|
+
end
|
210
|
+
|
211
|
+
test 'calls the original controller if inactive account' do
|
212
|
+
env = {
|
213
|
+
"warden.options" => { :recall => "devise/sessions#new", :attempted_path => "/users/sign_in", :message => :inactive },
|
214
|
+
"devise.mapping" => Devise.mappings[:user],
|
215
|
+
"warden" => stub_everything
|
216
|
+
}
|
217
|
+
call_failure(env)
|
218
|
+
assert @response.third.body.include?('<h2>Sign in</h2>')
|
219
|
+
assert @response.third.body.include?('Your account was not activated yet.')
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
if DEVISE_ORM == :active_record
|
5
|
+
require "generators/active_record/devise_generator"
|
6
|
+
|
7
|
+
class ActiveRecordGeneratorTest < Rails::Generators::TestCase
|
8
|
+
tests ActiveRecord::Generators::DeviseGenerator
|
9
|
+
destination File.expand_path("../../tmp", __FILE__)
|
10
|
+
setup :prepare_destination
|
11
|
+
|
12
|
+
test "all files are properly created with rails31 migration syntax" do
|
13
|
+
run_generator %w(monster)
|
14
|
+
assert_file "app/models/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/
|
15
|
+
assert_migration "db/migrate/devise_create_monsters.rb", /def change/
|
16
|
+
end
|
17
|
+
|
18
|
+
test "all files for namespaced model are properly created" do
|
19
|
+
run_generator %w(admin/monster)
|
20
|
+
assert_file "app/models/admin/monster.rb", /devise/, /attr_accessible (:[a-z_]+(, )?)+/
|
21
|
+
assert_migration "db/migrate/devise_create_admin_monsters.rb", /def change/
|
22
|
+
end
|
23
|
+
|
24
|
+
test "update model migration when model exists" do
|
25
|
+
run_generator %w(monster)
|
26
|
+
assert_file "app/models/monster.rb"
|
27
|
+
run_generator %w(monster)
|
28
|
+
assert_migration "db/migrate/add_devise_to_monsters.rb"
|
29
|
+
end
|
30
|
+
|
31
|
+
test "all files are properly deleted" do
|
32
|
+
run_generator %w(monster)
|
33
|
+
run_generator %w(monster)
|
34
|
+
assert_migration "db/migrate/devise_create_monsters.rb"
|
35
|
+
assert_migration "db/migrate/add_devise_to_monsters.rb"
|
36
|
+
run_generator %w(monster), :behavior => :revoke
|
37
|
+
assert_no_migration "db/migrate/add_devise_to_monsters.rb"
|
38
|
+
assert_migration "db/migrate/devise_create_monsters.rb"
|
39
|
+
run_generator %w(monster), :behavior => :revoke
|
40
|
+
assert_no_file "app/models/monster.rb"
|
41
|
+
assert_no_migration "db/migrate/devise_create_monsters.rb"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module RailsEngine
|
46
|
+
class Engine < Rails::Engine
|
47
|
+
isolate_namespace RailsEngine
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def simulate_inside_engine(engine, namespace)
|
52
|
+
if Rails::Generators.respond_to?(:namespace=)
|
53
|
+
swap Rails::Generators, :namespace => namespace do
|
54
|
+
yield
|
55
|
+
end
|
56
|
+
else
|
57
|
+
swap Rails, :application => engine.instance do
|
58
|
+
yield
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class ActiveRecordEngineGeneratorTest < Rails::Generators::TestCase
|
64
|
+
tests ActiveRecord::Generators::DeviseGenerator
|
65
|
+
destination File.expand_path("../../tmp", __FILE__)
|
66
|
+
setup :prepare_destination
|
67
|
+
|
68
|
+
test "all files are properly created" do
|
69
|
+
simulate_inside_engine(RailsEngine::Engine, RailsEngine) do
|
70
|
+
run_generator ["monster"]
|
71
|
+
|
72
|
+
assert_file "app/models/rails_engine/monster.rb", /devise/,/attr_accessible (:[a-z_]+(, )?)+/
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
require "generators/devise/devise_generator"
|
5
|
+
|
6
|
+
class DeviseGeneratorTest < Rails::Generators::TestCase
|
7
|
+
tests Devise::Generators::DeviseGenerator
|
8
|
+
destination File.expand_path("../../tmp", __FILE__)
|
9
|
+
|
10
|
+
setup do
|
11
|
+
prepare_destination
|
12
|
+
copy_routes
|
13
|
+
end
|
14
|
+
|
15
|
+
test "route generation for simple model names" do
|
16
|
+
run_generator %w(monster name:string)
|
17
|
+
assert_file "config/routes.rb", /devise_for :monsters/
|
18
|
+
end
|
19
|
+
|
20
|
+
test "route generation for namespaced model names" do
|
21
|
+
run_generator %w(monster/goblin name:string)
|
22
|
+
match = /devise_for :goblins, :class_name => "Monster::Goblin"/
|
23
|
+
assert_file "config/routes.rb", match
|
24
|
+
end
|
25
|
+
|
26
|
+
test "route generation with skip routes" do
|
27
|
+
run_generator %w(monster name:string --skip-routes)
|
28
|
+
match = /devise_for :monsters, :skip => :all/
|
29
|
+
assert_file "config/routes.rb", match
|
30
|
+
end
|
31
|
+
|
32
|
+
def copy_routes
|
33
|
+
routes = File.expand_path("../../rails_app/config/routes.rb", __FILE__)
|
34
|
+
destination = File.join(destination_root, "config")
|
35
|
+
|
36
|
+
FileUtils.mkdir_p(destination)
|
37
|
+
FileUtils.cp routes, destination
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
class InstallGeneratorTest < Rails::Generators::TestCase
|
5
|
+
tests Devise::Generators::InstallGenerator
|
6
|
+
destination File.expand_path("../../tmp", __FILE__)
|
7
|
+
setup :prepare_destination
|
8
|
+
|
9
|
+
test "Assert all files are properly created" do
|
10
|
+
run_generator
|
11
|
+
assert_file "config/initializers/devise.rb"
|
12
|
+
assert_file "config/locales/devise.en.yml"
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
if DEVISE_ORM == :mongoid
|
5
|
+
require "generators/mongoid/devise_generator"
|
6
|
+
|
7
|
+
class MongoidGeneratorTest < Rails::Generators::TestCase
|
8
|
+
tests Mongoid::Generators::DeviseGenerator
|
9
|
+
destination File.expand_path("../../tmp", __FILE__)
|
10
|
+
setup :prepare_destination
|
11
|
+
|
12
|
+
test "all files are properly created" do
|
13
|
+
run_generator %w(monster)
|
14
|
+
assert_file "app/models/monster.rb", /devise/
|
15
|
+
end
|
16
|
+
|
17
|
+
test "all files are properly deleted" do
|
18
|
+
run_generator %w(monster)
|
19
|
+
run_generator %w(monster), :behavior => :revoke
|
20
|
+
assert_no_file "app/models/monster.rb"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "test_helper"
|
3
|
+
|
4
|
+
class ViewsGeneratorTest < Rails::Generators::TestCase
|
5
|
+
tests Devise::Generators::ViewsGenerator
|
6
|
+
destination File.expand_path("../../tmp", __FILE__)
|
7
|
+
setup :prepare_destination
|
8
|
+
|
9
|
+
test "Assert all views are properly created with no params" do
|
10
|
+
run_generator
|
11
|
+
assert_files
|
12
|
+
end
|
13
|
+
|
14
|
+
test "Assert all views are properly created with scope param param" do
|
15
|
+
run_generator %w(users)
|
16
|
+
assert_files "users"
|
17
|
+
|
18
|
+
run_generator %w(admins)
|
19
|
+
assert_files "admins"
|
20
|
+
end
|
21
|
+
|
22
|
+
test "Assert views with simple form" do
|
23
|
+
run_generator %w(-b simple_form_for)
|
24
|
+
assert_files
|
25
|
+
assert_file "app/views/devise/confirmations/new.html.erb", /simple_form_for/
|
26
|
+
|
27
|
+
run_generator %w(users -b simple_form_for)
|
28
|
+
assert_files "users"
|
29
|
+
assert_file "app/views/users/confirmations/new.html.erb", /simple_form_for/
|
30
|
+
end
|
31
|
+
|
32
|
+
test "Assert views with markerb" do
|
33
|
+
run_generator %w(--markerb)
|
34
|
+
assert_files nil, :mail_template_engine => "markerb"
|
35
|
+
end
|
36
|
+
|
37
|
+
def assert_files(scope = nil, options={})
|
38
|
+
scope = "devise" if scope.nil?
|
39
|
+
mail_template_engine = options[:mail_template_engine] || "html.erb"
|
40
|
+
|
41
|
+
assert_file "app/views/#{scope}/confirmations/new.html.erb"
|
42
|
+
assert_file "app/views/#{scope}/mailer/confirmation_instructions.#{mail_template_engine}"
|
43
|
+
assert_file "app/views/#{scope}/mailer/reset_password_instructions.#{mail_template_engine}"
|
44
|
+
assert_file "app/views/#{scope}/mailer/unlock_instructions.#{mail_template_engine}"
|
45
|
+
assert_file "app/views/#{scope}/passwords/edit.html.erb"
|
46
|
+
assert_file "app/views/#{scope}/passwords/new.html.erb"
|
47
|
+
assert_file "app/views/#{scope}/registrations/new.html.erb"
|
48
|
+
assert_file "app/views/#{scope}/registrations/edit.html.erb"
|
49
|
+
assert_file "app/views/#{scope}/sessions/new.html.erb"
|
50
|
+
assert_file "app/views/#{scope}/shared/_links.erb"
|
51
|
+
assert_file "app/views/#{scope}/unlocks/new.html.erb"
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class DeviseHelperTest < ActionController::IntegrationTest
|
5
|
+
setup do
|
6
|
+
model_labels = { :models => { :user => "utilisateur" } }
|
7
|
+
|
8
|
+
I18n.backend.store_translations :fr,
|
9
|
+
{
|
10
|
+
:errors => { :messages => { :not_saved => {
|
11
|
+
:one => "Erreur lors de l'enregistrement de '%{resource}': 1 erreur.",
|
12
|
+
:other => "Erreur lors de l'enregistrement de '%{resource}': %{count} erreurs."
|
13
|
+
} } },
|
14
|
+
:activerecord => model_labels,
|
15
|
+
:mongoid => model_labels
|
16
|
+
}
|
17
|
+
|
18
|
+
I18n.locale = 'fr'
|
19
|
+
end
|
20
|
+
|
21
|
+
teardown do
|
22
|
+
I18n.locale = 'en'
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'test errors.messages.not_saved with single error from i18n' do
|
26
|
+
get new_user_registration_path
|
27
|
+
|
28
|
+
fill_in 'password', :with => 'new_user123'
|
29
|
+
fill_in 'password confirmation', :with => 'new_user123'
|
30
|
+
click_button 'Sign up'
|
31
|
+
|
32
|
+
assert_have_selector '#error_explanation'
|
33
|
+
assert_contain "Erreur lors de l'enregistrement de 'utilisateur': 1 erreur"
|
34
|
+
end
|
35
|
+
|
36
|
+
test 'test errors.messages.not_saved with multiple errors from i18n' do
|
37
|
+
# Dirty tracking behavior prevents email validations from being applied:
|
38
|
+
# https://github.com/mongoid/mongoid/issues/756
|
39
|
+
(pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
|
40
|
+
|
41
|
+
get new_user_registration_path
|
42
|
+
|
43
|
+
fill_in 'email', :with => 'invalid_email'
|
44
|
+
fill_in 'password', :with => 'new_user123'
|
45
|
+
fill_in 'password confirmation', :with => 'new_user321'
|
46
|
+
click_button 'Sign up'
|
47
|
+
|
48
|
+
assert_have_selector '#error_explanation'
|
49
|
+
assert_contain "Erreur lors de l'enregistrement de 'utilisateur': 2 erreurs"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class IndifferentHashTest < ActiveSupport::TestCase
|
5
|
+
setup do
|
6
|
+
@hash = Devise::IndifferentHash.new
|
7
|
+
end
|
8
|
+
|
9
|
+
test "it overwrites getter and setter" do
|
10
|
+
@hash[:foo] = "bar"
|
11
|
+
assert_equal "bar", @hash["foo"]
|
12
|
+
assert_equal "bar", @hash[:foo]
|
13
|
+
|
14
|
+
@hash["foo"] = "baz"
|
15
|
+
assert_equal "baz", @hash["foo"]
|
16
|
+
assert_equal "baz", @hash[:foo]
|
17
|
+
end
|
18
|
+
|
19
|
+
test "it overwrites update" do
|
20
|
+
@hash.update :foo => "bar"
|
21
|
+
assert_equal "bar", @hash["foo"]
|
22
|
+
assert_equal "bar", @hash[:foo]
|
23
|
+
|
24
|
+
@hash.update "foo" => "baz"
|
25
|
+
assert_equal "baz", @hash["foo"]
|
26
|
+
assert_equal "baz", @hash[:foo]
|
27
|
+
end
|
28
|
+
|
29
|
+
test "it returns a Hash on to_hash" do
|
30
|
+
@hash[:foo] = "bar"
|
31
|
+
assert_equal Hash["foo", "bar"], @hash.to_hash
|
32
|
+
assert_kind_of Hash, @hash.to_hash
|
33
|
+
end
|
34
|
+
end if defined?(Devise::IndifferentHash)
|