PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/integration/registerable_test.rb

https://github.com/FundingCircle/devise
Ruby | 268 lines | 232 code | 36 blank | 0 comment | 4 complexity | 7bd06106dcc15e05c0e1fcd1072bc63a MD5 | raw file
  1. require 'test_helper'
  2. class RegistrationTest < ActionController::IntegrationTest
  3. test 'a guest admin should be able to sign in successfully' do
  4. get new_admin_session_path
  5. click_link 'Sign up'
  6. assert_template 'registrations/new'
  7. fill_in 'email', :with => 'new_user@test.com'
  8. fill_in 'password', :with => 'new_user123'
  9. fill_in 'password confirmation', :with => 'new_user123'
  10. click_button 'Sign up'
  11. assert_contain 'Welcome! You have signed up successfully.'
  12. assert warden.authenticated?(:admin)
  13. assert_current_url "/admin_area/home"
  14. admin = Admin.last :order => "id"
  15. assert_equal admin.email, 'new_user@test.com'
  16. end
  17. test 'a guest admin should be able to sign in and be redirected to a custom location' do
  18. Devise::RegistrationsController.any_instance.stubs(:after_sign_up_path_for).returns("/?custom=1")
  19. get new_admin_session_path
  20. click_link 'Sign up'
  21. fill_in 'email', :with => 'new_user@test.com'
  22. fill_in 'password', :with => 'new_user123'
  23. fill_in 'password confirmation', :with => 'new_user123'
  24. click_button 'Sign up'
  25. assert_contain 'Welcome! You have signed up successfully.'
  26. assert warden.authenticated?(:admin)
  27. assert_current_url "/?custom=1"
  28. end
  29. test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
  30. get new_user_registration_path
  31. fill_in 'email', :with => 'new_user@test.com'
  32. fill_in 'password', :with => 'new_user123'
  33. fill_in 'password confirmation', :with => 'new_user123'
  34. click_button 'Sign up'
  35. assert_contain 'You have signed up successfully. However, we could not sign you in because your account is unconfirmed.'
  36. assert_not_contain 'You have to confirm your account before continuing'
  37. assert_current_url "/"
  38. assert_not warden.authenticated?(:user)
  39. user = User.last :order => "id"
  40. assert_equal user.email, 'new_user@test.com'
  41. assert_not user.confirmed?
  42. end
  43. test 'a guest user should be blocked by confirmation and redirected to a custom path' do
  44. Devise::RegistrationsController.any_instance.stubs(:after_inactive_sign_up_path_for).returns("/?custom=1")
  45. get new_user_registration_path
  46. fill_in 'email', :with => 'new_user@test.com'
  47. fill_in 'password', :with => 'new_user123'
  48. fill_in 'password confirmation', :with => 'new_user123'
  49. click_button 'Sign up'
  50. assert_current_url "/?custom=1"
  51. assert_not warden.authenticated?(:user)
  52. end
  53. test 'a guest user cannot sign up with invalid information' do
  54. get new_user_registration_path
  55. fill_in 'email', :with => 'invalid_email'
  56. fill_in 'password', :with => 'new_user123'
  57. fill_in 'password confirmation', :with => 'new_user321'
  58. click_button 'Sign up'
  59. assert_template 'registrations/new'
  60. assert_have_selector '#error_explanation'
  61. assert_contain "Email is invalid"
  62. assert_contain "Password doesn't match confirmation"
  63. assert_contain "2 errors prohibited"
  64. assert_nil User.first
  65. assert_not warden.authenticated?(:user)
  66. end
  67. test 'a guest should not sign up with email/password that already exists' do
  68. user = create_user
  69. get new_user_registration_path
  70. fill_in 'email', :with => 'user@test.com'
  71. fill_in 'password', :with => '123456'
  72. fill_in 'password confirmation', :with => '123456'
  73. click_button 'Sign up'
  74. assert_current_url '/users'
  75. assert_contain(/Email.*already.*taken/)
  76. assert_not warden.authenticated?(:user)
  77. end
  78. test 'a guest should not be able to change account' do
  79. get edit_user_registration_path
  80. assert_redirected_to new_user_session_path
  81. follow_redirect!
  82. assert_contain 'You need to sign in or sign up before continuing.'
  83. end
  84. test 'a signed in user should not be able to access sign up' do
  85. sign_in_as_user
  86. get new_user_registration_path
  87. assert_redirected_to root_path
  88. end
  89. test 'a signed in user should be able to edit his account' do
  90. sign_in_as_user
  91. get edit_user_registration_path
  92. fill_in 'email', :with => 'user.new@example.com'
  93. fill_in 'current password', :with => '123456'
  94. click_button 'Update'
  95. assert_current_url '/'
  96. assert_contain 'You updated your account successfully.'
  97. assert_equal "user.new@example.com", User.first.email
  98. end
  99. test 'a signed in user should still be able to use the website after changing his password' do
  100. sign_in_as_user
  101. get edit_user_registration_path
  102. fill_in 'password', :with => '12345678'
  103. fill_in 'password confirmation', :with => '12345678'
  104. fill_in 'current password', :with => '123456'
  105. click_button 'Update'
  106. assert_contain 'You updated your account successfully.'
  107. get users_path
  108. assert warden.authenticated?(:user)
  109. end
  110. test 'a signed in user should not change his current user with invalid password' do
  111. sign_in_as_user
  112. get edit_user_registration_path
  113. fill_in 'email', :with => 'user.new@example.com'
  114. fill_in 'current password', :with => 'invalid'
  115. click_button 'Update'
  116. assert_template 'registrations/edit'
  117. assert_contain 'user@test.com'
  118. assert_have_selector 'form input[value="user.new@example.com"]'
  119. assert_equal "user@test.com", User.first.email
  120. end
  121. test 'a signed in user should be able to edit his password' do
  122. sign_in_as_user
  123. get edit_user_registration_path
  124. fill_in 'password', :with => 'pas123'
  125. fill_in 'password confirmation', :with => 'pas123'
  126. fill_in 'current password', :with => '123456'
  127. click_button 'Update'
  128. assert_current_url '/'
  129. assert_contain 'You updated your account successfully.'
  130. assert User.first.valid_password?('pas123')
  131. end
  132. test 'a signed in user should not be able to edit his password with invalid confirmation' do
  133. sign_in_as_user
  134. get edit_user_registration_path
  135. fill_in 'password', :with => 'pas123'
  136. fill_in 'password confirmation', :with => ''
  137. fill_in 'current password', :with => '123456'
  138. click_button 'Update'
  139. assert_contain "Password doesn't match confirmation"
  140. assert_not User.first.valid_password?('pas123')
  141. end
  142. test 'a signed in user should be able to cancel his account' do
  143. sign_in_as_user
  144. get edit_user_registration_path
  145. click_link "Cancel my account", :method => :delete
  146. assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
  147. assert User.all.empty?
  148. end
  149. test 'a user should be able to cancel sign up by deleting data in the session' do
  150. get "/set"
  151. assert_equal "something", @request.session["devise.foo_bar"]
  152. get "/users/sign_up"
  153. assert_equal "something", @request.session["devise.foo_bar"]
  154. get "/users/cancel"
  155. assert_nil @request.session["devise.foo_bar"]
  156. assert_redirected_to new_user_registration_path
  157. end
  158. test 'a user with XML sign up stub' do
  159. get new_user_registration_path(:format => 'xml')
  160. assert_response :success
  161. assert_match %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>), response.body
  162. assert_no_match(/<confirmation-token/, response.body) if DEVISE_ORM == :active_record
  163. end
  164. test 'a user with JSON sign up stub' do
  165. get new_user_registration_path(:format => 'json')
  166. assert_response :success
  167. assert_match %({"user":), response.body
  168. assert_no_match(/"confirmation_token"/, response.body) if DEVISE_ORM == :active_record
  169. end
  170. test 'an admin sign up with valid information in XML format should return valid response' do
  171. post admin_registration_path(:format => 'xml'), :admin => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
  172. assert_response :success
  173. assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<admin>)
  174. admin = Admin.last :order => "id"
  175. assert_equal admin.email, 'new_user@test.com'
  176. end
  177. test 'a user sign up with valid information in XML format should return valid response' do
  178. post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'new_user123' }
  179. assert_response :success
  180. assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
  181. user = User.last :order => "id"
  182. assert_equal user.email, 'new_user@test.com'
  183. end
  184. test 'a user sign up with invalid information in XML format should return invalid response' do
  185. post user_registration_path(:format => 'xml'), :user => { :email => 'new_user@test.com', :password => 'new_user123', :password_confirmation => 'invalid' }
  186. assert_response :unprocessable_entity
  187. assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
  188. end
  189. test 'a user update information with valid data in XML format should return valid response' do
  190. user = sign_in_as_user
  191. put user_registration_path(:format => 'xml'), :user => { :current_password => '123456', :email => 'user.new@test.com' }
  192. assert_response :success
  193. assert_equal user.reload.email, 'user.new@test.com'
  194. end
  195. test 'a user update information with invalid data in XML format should return invalid response' do
  196. user = sign_in_as_user
  197. put user_registration_path(:format => 'xml'), :user => { :current_password => 'invalid', :email => 'user.new@test.com' }
  198. assert_response :unprocessable_entity
  199. assert_equal user.reload.email, 'user@test.com'
  200. end
  201. test 'a user cancel his account in XML format should return valid response' do
  202. user = sign_in_as_user
  203. delete user_registration_path(:format => 'xml')
  204. assert_response :success
  205. assert_equal User.count, 0
  206. end
  207. end