upstream-devise 2.1.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +846 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +384 -0
  8. data/Rakefile +34 -0
  9. data/app/controllers/devise/confirmations_controller.rb +43 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
  11. data/app/controllers/devise/passwords_controller.rb +47 -0
  12. data/app/controllers/devise/registrations_controller.rb +107 -0
  13. data/app/controllers/devise/sessions_controller.rb +49 -0
  14. data/app/controllers/devise/unlocks_controller.rb +44 -0
  15. data/app/controllers/devise_controller.rb +177 -0
  16. data/app/helpers/devise_helper.rb +25 -0
  17. data/app/mailers/devise/mailer.rb +15 -0
  18. data/app/views/devise/_links.erb +3 -0
  19. data/app/views/devise/confirmations/new.html.erb +12 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise/passwords/edit.html.erb +16 -0
  24. data/app/views/devise/passwords/new.html.erb +12 -0
  25. data/app/views/devise/registrations/edit.html.erb +25 -0
  26. data/app/views/devise/registrations/new.html.erb +18 -0
  27. data/app/views/devise/sessions/new.html.erb +17 -0
  28. data/app/views/devise/shared/_links.erb +25 -0
  29. data/app/views/devise/unlocks/new.html.erb +12 -0
  30. data/config/locales/en.yml +57 -0
  31. data/devise.gemspec +25 -0
  32. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  33. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  34. data/lib/devise.rb +455 -0
  35. data/lib/devise/controllers/helpers.rb +269 -0
  36. data/lib/devise/controllers/rememberable.rb +52 -0
  37. data/lib/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/controllers/url_helpers.rb +67 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  41. data/lib/devise/encryptors/base.rb +24 -0
  42. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  43. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  44. data/lib/devise/encryptors/sha1.rb +25 -0
  45. data/lib/devise/encryptors/sha512.rb +25 -0
  46. data/lib/devise/failure_app.rb +185 -0
  47. data/lib/devise/hooks/activatable.rb +11 -0
  48. data/lib/devise/hooks/forgetable.rb +9 -0
  49. data/lib/devise/hooks/lockable.rb +7 -0
  50. data/lib/devise/hooks/rememberable.rb +6 -0
  51. data/lib/devise/hooks/timeoutable.rb +22 -0
  52. data/lib/devise/hooks/trackable.rb +9 -0
  53. data/lib/devise/mailers/helpers.rb +86 -0
  54. data/lib/devise/mapping.rb +172 -0
  55. data/lib/devise/models.rb +123 -0
  56. data/lib/devise/models/authenticatable.rb +231 -0
  57. data/lib/devise/models/confirmable.rb +242 -0
  58. data/lib/devise/models/database_authenticatable.rb +126 -0
  59. data/lib/devise/models/encryptable.rb +86 -0
  60. data/lib/devise/models/lockable.rb +185 -0
  61. data/lib/devise/models/omniauthable.rb +27 -0
  62. data/lib/devise/models/recoverable.rb +140 -0
  63. data/lib/devise/models/registerable.rb +25 -0
  64. data/lib/devise/models/rememberable.rb +125 -0
  65. data/lib/devise/models/timeoutable.rb +49 -0
  66. data/lib/devise/models/token_authenticatable.rb +77 -0
  67. data/lib/devise/models/trackable.rb +35 -0
  68. data/lib/devise/models/validatable.rb +66 -0
  69. data/lib/devise/modules.rb +30 -0
  70. data/lib/devise/omniauth.rb +28 -0
  71. data/lib/devise/omniauth/config.rb +45 -0
  72. data/lib/devise/omniauth/url_helpers.rb +33 -0
  73. data/lib/devise/orm/active_record.rb +3 -0
  74. data/lib/devise/orm/mongoid.rb +3 -0
  75. data/lib/devise/param_filter.rb +41 -0
  76. data/lib/devise/rails.rb +54 -0
  77. data/lib/devise/rails/routes.rb +412 -0
  78. data/lib/devise/rails/warden_compat.rb +43 -0
  79. data/lib/devise/strategies/authenticatable.rb +165 -0
  80. data/lib/devise/strategies/base.rb +15 -0
  81. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  82. data/lib/devise/strategies/rememberable.rb +53 -0
  83. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  84. data/lib/devise/test_helpers.rb +130 -0
  85. data/lib/devise/version.rb +3 -0
  86. data/lib/generators/active_record/devise_generator.rb +78 -0
  87. data/lib/generators/active_record/templates/migration.rb +19 -0
  88. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  89. data/lib/generators/devise/devise_generator.rb +24 -0
  90. data/lib/generators/devise/install_generator.rb +24 -0
  91. data/lib/generators/devise/orm_helpers.rb +32 -0
  92. data/lib/generators/devise/views_generator.rb +110 -0
  93. data/lib/generators/mongoid/devise_generator.rb +60 -0
  94. data/lib/generators/templates/README +31 -0
  95. data/lib/generators/templates/devise.rb +216 -0
  96. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  97. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  98. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  99. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  101. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  102. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  103. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  104. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  105. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  106. data/test/controllers/custom_strategy_test.rb +62 -0
  107. data/test/controllers/helpers_test.rb +254 -0
  108. data/test/controllers/internal_helpers_test.rb +97 -0
  109. data/test/controllers/sessions_controller_test.rb +36 -0
  110. data/test/controllers/url_helpers_test.rb +59 -0
  111. data/test/delegator_test.rb +19 -0
  112. data/test/devise_test.rb +72 -0
  113. data/test/encryptors_test.rb +30 -0
  114. data/test/failure_app_test.rb +211 -0
  115. data/test/generators/active_record_generator_test.rb +69 -0
  116. data/test/generators/devise_generator_test.rb +39 -0
  117. data/test/generators/install_generator_test.rb +13 -0
  118. data/test/generators/mongoid_generator_test.rb +23 -0
  119. data/test/generators/views_generator_test.rb +52 -0
  120. data/test/helpers/devise_helper_test.rb +51 -0
  121. data/test/indifferent_hash.rb +33 -0
  122. data/test/integration/authenticatable_test.rb +587 -0
  123. data/test/integration/confirmable_test.rb +255 -0
  124. data/test/integration/database_authenticatable_test.rb +82 -0
  125. data/test/integration/http_authenticatable_test.rb +97 -0
  126. data/test/integration/lockable_test.rb +224 -0
  127. data/test/integration/omniauthable_test.rb +133 -0
  128. data/test/integration/recoverable_test.rb +300 -0
  129. data/test/integration/registerable_test.rb +324 -0
  130. data/test/integration/rememberable_test.rb +158 -0
  131. data/test/integration/timeoutable_test.rb +114 -0
  132. data/test/integration/token_authenticatable_test.rb +161 -0
  133. data/test/integration/trackable_test.rb +92 -0
  134. data/test/mailers/confirmation_instructions_test.rb +95 -0
  135. data/test/mailers/reset_password_instructions_test.rb +83 -0
  136. data/test/mailers/unlock_instructions_test.rb +77 -0
  137. data/test/mapping_test.rb +127 -0
  138. data/test/models/authenticatable_test.rb +7 -0
  139. data/test/models/confirmable_test.rb +357 -0
  140. data/test/models/database_authenticatable_test.rb +189 -0
  141. data/test/models/encryptable_test.rb +73 -0
  142. data/test/models/lockable_test.rb +263 -0
  143. data/test/models/omniauthable_test.rb +7 -0
  144. data/test/models/recoverable_test.rb +205 -0
  145. data/test/models/registerable_test.rb +7 -0
  146. data/test/models/rememberable_test.rb +174 -0
  147. data/test/models/serializable_test.rb +48 -0
  148. data/test/models/timeoutable_test.rb +46 -0
  149. data/test/models/token_authenticatable_test.rb +55 -0
  150. data/test/models/trackable_test.rb +13 -0
  151. data/test/models/validatable_test.rb +117 -0
  152. data/test/models_test.rb +179 -0
  153. data/test/omniauth/config_test.rb +57 -0
  154. data/test/omniauth/url_helpers_test.rb +58 -0
  155. data/test/orm/active_record.rb +9 -0
  156. data/test/orm/mongoid.rb +14 -0
  157. data/test/rails_app/Rakefile +10 -0
  158. data/test/rails_app/app/active_record/admin.rb +6 -0
  159. data/test/rails_app/app/active_record/shim.rb +2 -0
  160. data/test/rails_app/app/active_record/user.rb +6 -0
  161. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  162. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  163. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  164. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  165. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  166. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  167. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  168. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  169. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  170. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  171. data/test/rails_app/app/mongoid/admin.rb +30 -0
  172. data/test/rails_app/app/mongoid/shim.rb +24 -0
  173. data/test/rails_app/app/mongoid/user.rb +45 -0
  174. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  175. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  176. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  177. data/test/rails_app/app/views/home/index.html.erb +1 -0
  178. data/test/rails_app/app/views/home/join.html.erb +1 -0
  179. data/test/rails_app/app/views/home/private.html.erb +1 -0
  180. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  181. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  182. data/test/rails_app/app/views/users/index.html.erb +1 -0
  183. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  184. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  185. data/test/rails_app/config.ru +4 -0
  186. data/test/rails_app/config/application.rb +41 -0
  187. data/test/rails_app/config/boot.rb +8 -0
  188. data/test/rails_app/config/database.yml +18 -0
  189. data/test/rails_app/config/environment.rb +5 -0
  190. data/test/rails_app/config/environments/development.rb +18 -0
  191. data/test/rails_app/config/environments/production.rb +33 -0
  192. data/test/rails_app/config/environments/test.rb +33 -0
  193. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  194. data/test/rails_app/config/initializers/devise.rb +186 -0
  195. data/test/rails_app/config/initializers/inflections.rb +2 -0
  196. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  197. data/test/rails_app/config/routes.rb +90 -0
  198. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +77 -0
  199. data/test/rails_app/db/schema.rb +52 -0
  200. data/test/rails_app/lib/shared_admin.rb +14 -0
  201. data/test/rails_app/lib/shared_user.rb +26 -0
  202. data/test/rails_app/public/404.html +26 -0
  203. data/test/rails_app/public/422.html +26 -0
  204. data/test/rails_app/public/500.html +26 -0
  205. data/test/rails_app/public/favicon.ico +0 -0
  206. data/test/rails_app/script/rails +10 -0
  207. data/test/routes_test.rb +248 -0
  208. data/test/support/assertions.rb +42 -0
  209. data/test/support/helpers.rb +91 -0
  210. data/test/support/integration.rb +90 -0
  211. data/test/support/locale/en.yml +4 -0
  212. data/test/support/webrat/integrations/rails.rb +24 -0
  213. data/test/test_helper.rb +27 -0
  214. data/test/test_helpers_test.rb +134 -0
  215. metadata +451 -0
@@ -0,0 +1,133 @@
1
+ require 'test_helper'
2
+
3
+
4
+ class OmniauthableIntegrationTest < ActionController::IntegrationTest
5
+ FACEBOOK_INFO = {
6
+ "id" => '12345',
7
+ "link" => 'http://facebook.com/josevalim',
8
+ "email" => 'user@example.com',
9
+ "first_name" => 'Jose',
10
+ "last_name" => 'Valim',
11
+ "website" => 'http://blog.plataformatec.com.br'
12
+ }
13
+
14
+ setup do
15
+ OmniAuth.config.test_mode = true
16
+ OmniAuth.config.mock_auth[:facebook] = {
17
+ "uid" => '12345',
18
+ "provider" => 'facebook',
19
+ "user_info" => {"nickname" => 'josevalim'},
20
+ "credentials" => {"token" => 'plataformatec'},
21
+ "extra" => {"user_hash" => FACEBOOK_INFO}
22
+ }
23
+ end
24
+
25
+ teardown do
26
+ OmniAuth.config.test_mode = false
27
+ end
28
+
29
+ def stub_action!(name)
30
+ Users::OmniauthCallbacksController.class_eval do
31
+ alias_method :__old_facebook, :facebook
32
+ alias_method :facebook, name
33
+ end
34
+ yield
35
+ ensure
36
+ Users::OmniauthCallbacksController.class_eval do
37
+ alias_method :facebook, :__old_facebook
38
+ end
39
+ end
40
+
41
+ test "can access omniauth.auth in the env hash" do
42
+ visit "/users/sign_in"
43
+ click_link "Sign in with Facebook"
44
+
45
+ json = ActiveSupport::JSON.decode(response.body)
46
+
47
+ assert_equal "12345", json["uid"]
48
+ assert_equal "facebook", json["provider"]
49
+ assert_equal "josevalim", json["user_info"]["nickname"]
50
+ assert_equal FACEBOOK_INFO, json["extra"]["user_hash"]
51
+ assert_equal "plataformatec", json["credentials"]["token"]
52
+ end
53
+
54
+ test "cleans up session on sign up" do
55
+ assert_no_difference "User.count" do
56
+ visit "/users/sign_in"
57
+ click_link "Sign in with Facebook"
58
+ end
59
+
60
+ assert session["devise.facebook_data"]
61
+
62
+ assert_difference "User.count" do
63
+ visit "/users/sign_up"
64
+ fill_in "Password", :with => "123456"
65
+ fill_in "Password confirmation", :with => "123456"
66
+ click_button "Sign up"
67
+ end
68
+
69
+ assert_current_url "/"
70
+ assert_contain "You have signed up successfully."
71
+ assert_contain "Hello User user@example.com"
72
+ assert_not session["devise.facebook_data"]
73
+ end
74
+
75
+ test "cleans up session on cancel" do
76
+ assert_no_difference "User.count" do
77
+ visit "/users/sign_in"
78
+ click_link "Sign in with Facebook"
79
+ end
80
+
81
+ assert session["devise.facebook_data"]
82
+ visit "/users/cancel"
83
+ assert !session["devise.facebook_data"]
84
+ end
85
+
86
+ test "cleans up session on sign in" do
87
+ assert_no_difference "User.count" do
88
+ visit "/users/sign_in"
89
+ click_link "Sign in with Facebook"
90
+ end
91
+
92
+ assert session["devise.facebook_data"]
93
+ user = sign_in_as_user
94
+ assert !session["devise.facebook_data"]
95
+ end
96
+
97
+ test "sign in and send remember token if configured" do
98
+ visit "/users/sign_in"
99
+ click_link "Sign in with Facebook"
100
+ assert_nil warden.cookies["remember_user_token"]
101
+
102
+ stub_action!(:sign_in_facebook) do
103
+ create_user
104
+ visit "/users/sign_in"
105
+ click_link "Sign in with Facebook"
106
+ assert warden.authenticated?(:user)
107
+ assert warden.cookies["remember_user_token"]
108
+ end
109
+ end
110
+
111
+ test "generates a proper link when SCRIPT_NAME is set" do
112
+ header 'SCRIPT_NAME', '/q'
113
+ visit "/users/sign_in"
114
+ assert_select "a", :href => "/q/users/auth/facebook"
115
+ end
116
+
117
+ test "handles callback error parameter according to the specification" do
118
+ OmniAuth.config.mock_auth[:facebook] = :access_denied
119
+ visit "/users/auth/facebook/callback?error=access_denied"
120
+ assert_current_url "/users/sign_in"
121
+ assert_contain 'Could not authenticate you from Facebook because "Access denied".'
122
+ end
123
+
124
+ test "handles other exceptions from omniauth" do
125
+ OmniAuth.config.mock_auth[:facebook] = :invalid_credentials
126
+
127
+ visit "/users/sign_in"
128
+ click_link "Sign in with Facebook"
129
+
130
+ assert_current_url "/users/sign_in"
131
+ assert_contain 'Could not authenticate you from Facebook because "Invalid credentials".'
132
+ end
133
+ end
@@ -0,0 +1,300 @@
1
+ require 'test_helper'
2
+
3
+ class PasswordTest < ActionController::IntegrationTest
4
+
5
+ def visit_new_password_path
6
+ visit new_user_session_path
7
+ click_link 'Forgot your password?'
8
+ end
9
+
10
+ def request_forgot_password(&block)
11
+ visit_new_password_path
12
+ assert_response :success
13
+ assert_not warden.authenticated?(:user)
14
+
15
+ fill_in 'email', :with => 'user@test.com'
16
+ yield if block_given?
17
+ click_button 'Send me reset password instructions'
18
+ end
19
+
20
+ def reset_password(options={}, &block)
21
+ visit edit_user_password_path(:reset_password_token => options[:reset_password_token]) unless options[:visit] == false
22
+ assert_response :success
23
+
24
+ fill_in 'New password', :with => '987654321'
25
+ fill_in 'Confirm new password', :with => '987654321'
26
+ yield if block_given?
27
+ click_button 'Change my password'
28
+ end
29
+
30
+ test 'reset password with email of different case should succeed when email is in the list of case insensitive keys' do
31
+ create_user(:email => 'Foo@Bar.com')
32
+
33
+ request_forgot_password do
34
+ fill_in 'email', :with => 'foo@bar.com'
35
+ end
36
+
37
+ assert_current_url '/users/sign_in'
38
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
39
+ end
40
+
41
+ test 'reset password with email should send an email from a custom mailer' do
42
+ create_user(:email => 'Foo@Bar.com')
43
+
44
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
45
+ request_forgot_password do
46
+ fill_in 'email', :with => 'foo@bar.com'
47
+ end
48
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.last.from
49
+ end
50
+
51
+ test 'reset password with email of different case should fail when email is NOT the list of case insensitive keys' do
52
+ swap Devise, :case_insensitive_keys => [] do
53
+ create_user(:email => 'Foo@Bar.com')
54
+
55
+ request_forgot_password do
56
+ fill_in 'email', :with => 'foo@bar.com'
57
+ end
58
+
59
+ assert_response :success
60
+ assert_current_url '/users/password'
61
+ assert_have_selector "input[type=email][value='foo@bar.com']"
62
+ assert_contain 'not found'
63
+ end
64
+ end
65
+
66
+ test 'reset password with email with extra whitespace should succeed when email is in the list of strip whitespace keys' do
67
+ create_user(:email => 'foo@bar.com')
68
+
69
+ request_forgot_password do
70
+ fill_in 'email', :with => ' foo@bar.com '
71
+ end
72
+
73
+ assert_current_url '/users/sign_in'
74
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
75
+ end
76
+
77
+ test 'reset password with email with extra whitespace should fail when email is NOT the list of strip whitespace keys' do
78
+ swap Devise, :strip_whitespace_keys => [] do
79
+ create_user(:email => 'foo@bar.com')
80
+
81
+ request_forgot_password do
82
+ fill_in 'email', :with => ' foo@bar.com '
83
+ end
84
+
85
+ assert_response :success
86
+ assert_current_url '/users/password'
87
+ assert_have_selector "input[type=email][value=' foo@bar.com ']"
88
+ assert_contain 'not found'
89
+ end
90
+ end
91
+
92
+ test 'authenticated user should not be able to visit forgot password page' do
93
+ sign_in_as_user
94
+ assert warden.authenticated?(:user)
95
+
96
+ get new_user_password_path
97
+
98
+ assert_response :redirect
99
+ assert_redirected_to root_path
100
+ end
101
+
102
+ test 'not authenticated user should be able to request a forgot password' do
103
+ create_user
104
+ request_forgot_password
105
+
106
+ assert_current_url '/users/sign_in'
107
+ assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
108
+ end
109
+
110
+ test 'not authenticated user with invalid email should receive an error message' do
111
+ request_forgot_password do
112
+ fill_in 'email', :with => 'invalid.test@test.com'
113
+ end
114
+
115
+ assert_response :success
116
+ assert_current_url '/users/password'
117
+ assert_have_selector "input[type=email][value='invalid.test@test.com']"
118
+ assert_contain 'not found'
119
+ end
120
+
121
+ test 'authenticated user should not be able to visit edit password page' do
122
+ sign_in_as_user
123
+ get edit_user_password_path
124
+ assert_response :redirect
125
+ assert_redirected_to root_path
126
+ assert warden.authenticated?(:user)
127
+ end
128
+
129
+ test 'not authenticated user with invalid reset password token should not be able to change his password' do
130
+ user = create_user
131
+ reset_password :reset_password_token => 'invalid_reset_password'
132
+
133
+ assert_response :success
134
+ assert_current_url '/users/password'
135
+ assert_have_selector '#error_explanation'
136
+ assert_contain /Reset password token(.*)invalid/
137
+ assert_not user.reload.valid_password?('987654321')
138
+ end
139
+
140
+ test 'not authenticated user with valid reset password token but invalid password should not be able to change his password' do
141
+ user = create_user
142
+ request_forgot_password
143
+ reset_password :reset_password_token => user.reload.reset_password_token do
144
+ fill_in 'Confirm new password', :with => 'other_password'
145
+ end
146
+
147
+ assert_response :success
148
+ assert_current_url '/users/password'
149
+ assert_have_selector '#error_explanation'
150
+ assert_contain 'Password doesn\'t match confirmation'
151
+ assert_not user.reload.valid_password?('987654321')
152
+ end
153
+
154
+ test 'not authenticated user with valid data should be able to change his password' do
155
+ user = create_user
156
+ request_forgot_password
157
+ reset_password :reset_password_token => user.reload.reset_password_token
158
+
159
+ assert_current_url '/'
160
+ assert_contain 'Your password was changed successfully. You are now signed in.'
161
+ assert user.reload.valid_password?('987654321')
162
+ end
163
+
164
+ test 'after entering invalid data user should still be able to change his password' do
165
+ user = create_user
166
+ request_forgot_password
167
+ reset_password :reset_password_token => user.reload.reset_password_token do
168
+ fill_in 'Confirm new password', :with => 'other_password'
169
+ end
170
+ assert_response :success
171
+ assert_have_selector '#error_explanation'
172
+ assert_not user.reload.valid_password?('987654321')
173
+
174
+ reset_password :reset_password_token => user.reload.reset_password_token, :visit => false
175
+ assert_contain 'Your password was changed successfully.'
176
+ assert user.reload.valid_password?('987654321')
177
+ end
178
+
179
+ test 'sign in user automatically after changing its password' do
180
+ user = create_user
181
+ request_forgot_password
182
+ reset_password :reset_password_token => user.reload.reset_password_token
183
+
184
+ assert warden.authenticated?(:user)
185
+ end
186
+
187
+ test 'does not sign in user automatically after changing its password if it\'s locked' do
188
+ user = create_user(:locked => true)
189
+ request_forgot_password
190
+ reset_password :reset_password_token => user.reload.reset_password_token
191
+
192
+ assert_contain 'Your password was changed successfully.'
193
+ assert_not_contain 'You are now signed in.'
194
+ assert_equal new_user_session_path, @request.path
195
+ assert !warden.authenticated?(:user)
196
+ end
197
+
198
+ test 'sign in user automatically and confirm after changing its password if it\'s not confirmed' do
199
+ user = create_user(:confirm => false)
200
+ request_forgot_password
201
+ reset_password :reset_password_token => user.reload.reset_password_token
202
+
203
+ assert warden.authenticated?(:user)
204
+ assert user.reload.confirmed?
205
+ end
206
+
207
+ test 'reset password request with valid E-Mail in XML format should return valid response' do
208
+ create_user
209
+ post user_password_path(:format => 'xml'), :user => {:email => "user@test.com"}
210
+ assert_response :success
211
+ assert_equal response.body, { }.to_xml
212
+ end
213
+
214
+ test 'reset password request with invalid E-Mail in XML format should return valid response' do
215
+ create_user
216
+ post user_password_path(:format => 'xml'), :user => {:email => "invalid.test@test.com"}
217
+ assert_response :unprocessable_entity
218
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
219
+ end
220
+
221
+ test 'reset password request with invalid E-Mail in XML format should return empty and valid response' do
222
+ swap Devise, :paranoid => true do
223
+ create_user
224
+ post user_password_path(:format => 'xml'), :user => {:email => "invalid@test.com"}
225
+ assert_response :success
226
+ assert_equal response.body, { }.to_xml
227
+ end
228
+ end
229
+
230
+ test 'change password with valid parameters in XML format should return valid response' do
231
+ user = create_user
232
+ request_forgot_password
233
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '987654321', :password_confirmation => '987654321'}
234
+ assert_response :success
235
+ assert warden.authenticated?(:user)
236
+ end
237
+
238
+ test 'change password with invalid token in XML format should return invalid response' do
239
+ user = create_user
240
+ request_forgot_password
241
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => 'invalid.token', :password => '987654321', :password_confirmation => '987654321'}
242
+ assert_response :unprocessable_entity
243
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
244
+ end
245
+
246
+ test 'change password with invalid new password in XML format should return invalid response' do
247
+ user = create_user
248
+ request_forgot_password
249
+ put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '', :password_confirmation => '987654321'}
250
+ assert_response :unprocessable_entity
251
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
252
+ end
253
+
254
+ test "when using json requests to ask a confirmable request, should not return the object" do
255
+ user = create_user(:confirm => false)
256
+
257
+ post user_password_path(:format => :json), :user => { :email => user.email }
258
+
259
+ assert_response :success
260
+ assert_equal response.body, "{}"
261
+ end
262
+
263
+ test "when in paranoid mode and with an invalid e-mail, asking to reset a password should display a message that does not indicates that the e-mail does not exists in the database" do
264
+ swap Devise, :paranoid => true do
265
+ visit_new_password_path
266
+ fill_in "email", :with => "arandomemail@test.com"
267
+ click_button 'Send me reset password instructions'
268
+
269
+ assert_not_contain "1 error prohibited this user from being saved:"
270
+ assert_not_contain "Email not found"
271
+ assert_contain "If your e-mail exists on our database, you will receive a password recovery link on your e-mail"
272
+ assert_current_url "/users/sign_in"
273
+ end
274
+ end
275
+
276
+ test "when in paranoid mode and with a valid e-mail, asking to reset password should display a message that does not indicates that the email exists in the database and redirect to the failure route" do
277
+ swap Devise, :paranoid => true do
278
+ user = create_user
279
+ visit_new_password_path
280
+ fill_in 'email', :with => user.email
281
+ click_button 'Send me reset password instructions'
282
+
283
+ assert_contain "If your e-mail exists on our database, you will receive a password recovery link on your e-mail"
284
+ assert_current_url "/users/sign_in"
285
+ end
286
+ end
287
+
288
+ test "after recovering a password, should set failed attempts to 0" do
289
+ user = create_user
290
+ user.update_attribute(:failed_attempts, 10)
291
+
292
+ assert_equal 10, user.failed_attempts
293
+ request_forgot_password
294
+ reset_password :reset_password_token => user.reload.reset_password_token
295
+
296
+ assert warden.authenticated?(:user)
297
+ user.reload
298
+ assert_equal 0, user.failed_attempts
299
+ end
300
+ end
@@ -0,0 +1,324 @@
1
+ require 'test_helper'
2
+
3
+ class RegistrationTest < ActionController::IntegrationTest
4
+
5
+ test 'a guest admin should be able to sign in successfully' do
6
+ get new_admin_session_path
7
+ click_link 'Sign up'
8
+
9
+ assert_template 'registrations/new'
10
+
11
+ fill_in 'email', :with => 'new_user@test.com'
12
+ fill_in 'password', :with => 'new_user123'
13
+ fill_in 'password confirmation', :with => 'new_user123'
14
+ click_button 'Sign up'
15
+
16
+ assert_contain 'You have signed up successfully'
17
+ assert warden.authenticated?(:admin)
18
+ assert_current_url "/admin_area/home"
19
+
20
+ admin = Admin.last :order => "id"
21
+ assert_equal admin.email, 'new_user@test.com'
22
+ end
23
+
24
+ test 'a guest admin should be able to sign in and be redirected to a custom location' do
25
+ Devise::RegistrationsController.any_instance.stubs(:after_sign_up_path_for).returns("/?custom=1")
26
+ get new_admin_session_path
27
+ click_link 'Sign up'
28
+
29
+ fill_in 'email', :with => 'new_user@test.com'
30
+ fill_in 'password', :with => 'new_user123'
31
+ fill_in 'password confirmation', :with => 'new_user123'
32
+ click_button 'Sign up'
33
+
34
+ assert_contain 'Welcome! You have signed up successfully.'
35
+ assert warden.authenticated?(:admin)
36
+ assert_current_url "/?custom=1"
37
+ end
38
+
39
+ def user_sign_up
40
+ ActionMailer::Base.deliveries.clear
41
+
42
+ get new_user_registration_path
43
+
44
+ fill_in 'email', :with => 'new_user@test.com'
45
+ fill_in 'password', :with => 'new_user123'
46
+ fill_in 'password confirmation', :with => 'new_user123'
47
+ click_button 'Sign up'
48
+ end
49
+
50
+ test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
51
+ user_sign_up
52
+
53
+ assert_contain 'A message with a confirmation link has been sent to your email address. Please open the link to activate your account.'
54
+ assert_not_contain 'You have to confirm your account before continuing'
55
+ assert_current_url "/"
56
+
57
+ assert_not warden.authenticated?(:user)
58
+
59
+ user = User.last :order => "id"
60
+ assert_equal user.email, 'new_user@test.com'
61
+ assert_not user.confirmed?
62
+ end
63
+
64
+ test 'a guest user should receive the confirmation instructions from the default mailer' do
65
+ user_sign_up
66
+ assert_equal ['please-change-me@config-initializers-devise.com'], ActionMailer::Base.deliveries.first.from
67
+ end
68
+
69
+ test 'a guest user should receive the confirmation instructions from a custom mailer' do
70
+ User.any_instance.stubs(:devise_mailer).returns(Users::Mailer)
71
+ user_sign_up
72
+ assert_equal ['custom@example.com'], ActionMailer::Base.deliveries.first.from
73
+ end
74
+
75
+ test 'a guest user should be blocked by confirmation and redirected to a custom path' do
76
+ Devise::RegistrationsController.any_instance.stubs(:after_inactive_sign_up_path_for).returns("/?custom=1")
77
+ get new_user_registration_path
78
+
79
+ fill_in 'email', :with => 'new_user@test.com'
80
+ fill_in 'password', :with => 'new_user123'
81
+ fill_in 'password confirmation', :with => 'new_user123'
82
+ click_button 'Sign up'
83
+
84
+ assert_current_url "/?custom=1"
85
+ assert_not warden.authenticated?(:user)
86
+ end
87
+
88
+ test 'a guest user cannot sign up with invalid information' do
89
+ # Dirty tracking behavior prevents email validations from being applied:
90
+ # https://github.com/mongoid/mongoid/issues/756
91
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
92
+
93
+ get new_user_registration_path
94
+
95
+ fill_in 'email', :with => 'invalid_email'
96
+ fill_in 'password', :with => 'new_user123'
97
+ fill_in 'password confirmation', :with => 'new_user321'
98
+ click_button 'Sign up'
99
+
100
+ assert_template 'registrations/new'
101
+ assert_have_selector '#error_explanation'
102
+ assert_contain "Email is invalid"
103
+ assert_contain "Password doesn't match confirmation"
104
+ assert_contain "2 errors prohibited"
105
+ assert_nil User.first
106
+
107
+ assert_not warden.authenticated?(:user)
108
+ end
109
+
110
+ test 'a guest should not sign up with email/password that already exists' do
111
+ # Dirty tracking behavior prevents email validations from being applied:
112
+ # https://github.com/mongoid/mongoid/issues/756
113
+ (pending "Fails on Mongoid < 2.1"; break) if defined?(Mongoid) && Mongoid::VERSION.to_f < 2.1
114
+
115
+ user = create_user
116
+ get new_user_registration_path
117
+
118
+ fill_in 'email', :with => 'user@test.com'
119
+ fill_in 'password', :with => '123456'
120
+ fill_in 'password confirmation', :with => '123456'
121
+ click_button 'Sign up'
122
+
123
+ assert_current_url '/users'
124
+ assert_contain(/Email.*already.*taken/)
125
+
126
+ assert_not warden.authenticated?(:user)
127
+ end
128
+
129
+ test 'a guest should not be able to change account' do
130
+ get edit_user_registration_path
131
+ assert_redirected_to new_user_session_path
132
+ follow_redirect!
133
+ assert_contain 'You need to sign in or sign up before continuing.'
134
+ end
135
+
136
+ test 'a signed in user should not be able to access sign up' do
137
+ sign_in_as_user
138
+ get new_user_registration_path
139
+ assert_redirected_to root_path
140
+ end
141
+
142
+ test 'a signed in user should be able to edit his account' do
143
+ sign_in_as_user
144
+ get edit_user_registration_path
145
+
146
+ fill_in 'email', :with => 'user.new@example.com'
147
+ fill_in 'current password', :with => '123456'
148
+ click_button 'Update'
149
+
150
+ assert_current_url '/'
151
+ assert_contain 'You updated your account successfully.'
152
+
153
+ assert_equal "user.new@example.com", User.first.email
154
+ end
155
+
156
+ test 'a signed in user should still be able to use the website after changing his password' do
157
+ sign_in_as_user
158
+ get edit_user_registration_path
159
+
160
+ fill_in 'password', :with => '12345678'
161
+ fill_in 'password confirmation', :with => '12345678'
162
+ fill_in 'current password', :with => '123456'
163
+ click_button 'Update'
164
+
165
+ assert_contain 'You updated your account successfully.'
166
+ get users_path
167
+ assert warden.authenticated?(:user)
168
+ end
169
+
170
+ test 'a signed in user should not change his current user with invalid password' do
171
+ sign_in_as_user
172
+ get edit_user_registration_path
173
+
174
+ fill_in 'email', :with => 'user.new@example.com'
175
+ fill_in 'current password', :with => 'invalid'
176
+ click_button 'Update'
177
+
178
+ assert_template 'registrations/edit'
179
+ assert_contain 'user@test.com'
180
+ assert_have_selector 'form input[value="user.new@example.com"]'
181
+
182
+ assert_equal "user@test.com", User.first.email
183
+ end
184
+
185
+ test 'a signed in user should be able to edit his password' do
186
+ sign_in_as_user
187
+ get edit_user_registration_path
188
+
189
+ fill_in 'password', :with => 'pas123'
190
+ fill_in 'password confirmation', :with => 'pas123'
191
+ fill_in 'current password', :with => '123456'
192
+ click_button 'Update'
193
+
194
+ assert_current_url '/'
195
+ assert_contain 'You updated your account successfully.'
196
+
197
+ assert User.first.valid_password?('pas123')
198
+ end
199
+
200
+ test 'a signed in user should not be able to edit his password with invalid confirmation' do
201
+ sign_in_as_user
202
+ get edit_user_registration_path
203
+
204
+ fill_in 'password', :with => 'pas123'
205
+ fill_in 'password confirmation', :with => ''
206
+ fill_in 'current password', :with => '123456'
207
+ click_button 'Update'
208
+
209
+ assert_contain "Password doesn't match confirmation"
210
+ assert_not User.first.valid_password?('pas123')
211
+ end
212
+
213
+ test 'a signed in user should be able to cancel his account' do
214
+ sign_in_as_user
215
+ get edit_user_registration_path
216
+
217
+ click_link "Cancel my account", :method => :delete
218
+ assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
219
+
220
+ assert User.all.empty?
221
+ end
222
+
223
+ test 'a user should be able to cancel sign up by deleting data in the session' do
224
+ get "/set"
225
+ assert_equal "something", @request.session["devise.foo_bar"]
226
+
227
+ get "/users/sign_up"
228
+ assert_equal "something", @request.session["devise.foo_bar"]
229
+
230
+ get "/users/cancel"
231
+ assert_nil @request.session["devise.foo_bar"]
232
+ assert_redirected_to new_user_registration_path
233
+ end
234
+
235
+ test 'a user with XML sign up stub' do
236
+ get new_user_registration_path(:format => 'xml')
237
+ assert_response :success
238
+ assert_match %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>), response.body
239
+ assert_no_match(/<confirmation-token/, response.body)
240
+ end
241
+
242
+ test 'a user with JSON sign up stub' do
243
+ get new_user_registration_path(:format => 'json')
244
+ assert_response :success
245
+ assert_match %({"user":), response.body
246
+ assert_no_match(/"confirmation_token"/, response.body)
247
+ end
248
+
249
+ test 'an admin sign up with valid information in XML format should return valid response' do
250
+ post admin_registration_path(:format => 'xml'), :admin => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
251
+ assert_response :success
252
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<admin>)
253
+
254
+ admin = Admin.last :order => "id"
255
+ assert_equal admin.email, 'new_user@test.com'
256
+ end
257
+
258
+ test 'a user sign up with valid information in XML format should return valid response' do
259
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
260
+ assert_response :success
261
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
262
+
263
+ user = User.last :order => "id"
264
+ assert_equal user.email, 'new_user@test.com'
265
+ end
266
+
267
+ test 'a user sign up with invalid information in XML format should return invalid response' do
268
+ post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'invalid' }
269
+ assert_response :unprocessable_entity
270
+ assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
271
+ end
272
+
273
+ test 'a user update information with valid data in XML format should return valid response' do
274
+ user = sign_in_as_user
275
+ put user_registration_path(:format => 'xml'), :user => { :current_password => '123456', :email => 'user.new@test.com' }
276
+ assert_response :success
277
+ assert_equal user.reload.email, 'user.new@test.com'
278
+ end
279
+
280
+ test 'a user update information with invalid data in XML format should return invalid response' do
281
+ user = sign_in_as_user
282
+ put user_registration_path(:format => 'xml'), :user => { :current_password => 'invalid', :email => 'user.new@test.com' }
283
+ assert_response :unprocessable_entity
284
+ assert_equal user.reload.email, 'user@test.com'
285
+ end
286
+
287
+ test 'a user cancel his account in XML format should return valid response' do
288
+ user = sign_in_as_user
289
+ delete user_registration_path(:format => 'xml')
290
+ assert_response :success
291
+ assert_equal User.count, 0
292
+ end
293
+ end
294
+
295
+ class ReconfirmableRegistrationTest < ActionController::IntegrationTest
296
+ test 'a signed in admin should see a more appropriate flash message when editing his account if reconfirmable is enabled' do
297
+ sign_in_as_admin
298
+ get edit_admin_registration_path
299
+
300
+ fill_in 'email', :with => 'admin.new@example.com'
301
+ fill_in 'current password', :with => '123456'
302
+ click_button 'Update'
303
+
304
+ assert_current_url '/admin_area/home'
305
+ assert_contain 'but we need to verify your new email address'
306
+
307
+ assert_equal "admin.new@example.com", Admin.first.unconfirmed_email
308
+ end
309
+
310
+ test 'a signed in admin should not see a reconfirmation message if they did not change their password' do
311
+ sign_in_as_admin
312
+ get edit_admin_registration_path
313
+
314
+ fill_in 'password', :with => 'pas123'
315
+ fill_in 'password confirmation', :with => 'pas123'
316
+ fill_in 'current password', :with => '123456'
317
+ click_button 'Update'
318
+
319
+ assert_current_url '/admin_area/home'
320
+ assert_contain 'You updated your account successfully.'
321
+
322
+ assert Admin.first.valid_password?('pas123')
323
+ end
324
+ end