PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/test/functional/user_controller_test.rb

https://github.com/jasnow/railsspace
Ruby | 286 lines | 210 code | 31 blank | 45 comment | 1 complexity | 1561d0841cc72bab027f2fa32b0b39a4 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../test_helper'
  2. require 'user_controller'
  3. # Re-raise errors caught by the controller.
  4. class UserController; def rescue_action(e) raise e end; end
  5. class UserControllerTest < Test::Unit::TestCase
  6. include ApplicationHelper
  7. fixtures :users
  8. def setup
  9. @controller = UserController.new
  10. @request = ActionController::TestRequest.new
  11. @response = ActionController::TestResponse.new
  12. # This user is initially valid, but we may change its attributes.
  13. @valid_user = users(:valid_user)
  14. end
  15. # Make sure the registration page responds with the proper form.
  16. def test_registration_page
  17. get :register
  18. title = assigns(:title)
  19. assert_equal "Register", title
  20. assert_response :success
  21. assert_template "register"
  22. # Test the form and all its tags.
  23. assert_form_tag "/user/register"
  24. assert_screen_name_field
  25. assert_email_field
  26. assert_password_field
  27. assert_password_field "password_confirmation"
  28. assert_submit_button "Register!"
  29. end
  30. # Test a valid registration.
  31. def test_registration_success
  32. post :register, :user => { :screen_name => "new_screen_name",
  33. :email => "valid@example.com",
  34. :password => "long_enough_password" }
  35. # Test assignment of user.
  36. user = assigns(:user)
  37. assert_not_nil user
  38. # Test new user in database.
  39. new_user = User.find_by_screen_name_and_password(user.screen_name,
  40. user.password)
  41. assert_equal new_user, user
  42. # Test flash and redirect.
  43. assert_equal "User #{new_user.screen_name} created!", flash[:notice]
  44. assert_redirected_to :action => "index"
  45. # Make sure user is logged in properly.
  46. assert logged_in?
  47. assert_equal user.id, session[:user_id]
  48. end
  49. # Test an invalid registration
  50. def test_registration_failure
  51. post :register, :user => { :screen_name => "aa/noyes",
  52. :email => "anoyes@example,com",
  53. :password => "sun" }
  54. assert_response :success
  55. assert_template "register"
  56. # Test display of error messages.
  57. assert_tag "div", :attributes => { :id => "errorExplanation",
  58. :class => "errorExplanation" }
  59. # Assert that each form field has at least one error displayed.
  60. assert_tag "li", :content => /Screen name/
  61. assert_tag "li", :content => /Email/
  62. assert_tag "li", :content => /Password/
  63. # Test to see that the input fields are being wrapped with the correct div.
  64. error_div_user = { :tag => "div", :attributes => { :class => "fieldWithErrors" } }
  65. assert_tag "input",
  66. :attributes => { :name => "user[screen_name]",
  67. :value => "aa/noyes" },
  68. :parent => error_div_user
  69. assert_tag "input",
  70. :attributes => { :name => "user[email]",
  71. :value => "anoyes@example,com" },
  72. :parent => error_div_user
  73. assert_tag "input",
  74. :attributes => { :name => "user[password]",
  75. :value => nil },
  76. :parent => error_div_user
  77. end
  78. # Make sure the login page works and has the right fields.
  79. def test_login_page
  80. get :login
  81. title = assigns(:title)
  82. assert_equal "Log in to RailsSpace", title
  83. assert_response :success
  84. assert_template "login"
  85. assert_tag "form", :attributes => { :action => "/user/login",
  86. :method => "post" }
  87. assert_tag "input",
  88. :attributes => { :name => "user[screen_name]",
  89. :type => "text",
  90. :size => User::SCREEN_NAME_SIZE,
  91. :maxlength => User::SCREEN_NAME_MAX_LENGTH }
  92. assert_tag "input", :attributes => { :name => "user[remember_me]",
  93. :type => "checkbox" }
  94. assert_tag "input", :attributes => { :type => "submit",
  95. :value => "Login!" }
  96. end
  97. # Test a valid login.
  98. def test_login_success
  99. try_to_login @valid_user, :remember_me => "0"
  100. assert logged_in?
  101. assert_equal @valid_user.id, session[:user_id]
  102. assert_equal "User #{@valid_user.screen_name} logged in!", flash[:notice]
  103. assert_response :redirect
  104. assert_redirected_to :action => "index"
  105. # Verify that we're not remembering the user.
  106. user = assigns(:user)
  107. assert user.remember_me != "1"
  108. # There should be no cookies set.
  109. assert_nil cookie_value(:remember_me)
  110. assert_nil cookie_value(:authorization_token)
  111. end
  112. # Test a login with invalid screen name.
  113. def test_login_failure_with_nonexistent_screen_name
  114. invalid_user = @valid_user
  115. invalid_user.screen_name = "no such user"
  116. try_to_login invalid_user
  117. assert_template "login"
  118. assert_equal "Invalid screen name/password combination", flash[:notice]
  119. # Make sure screen_name will be redisplayed, but not the password.
  120. user = assigns(:user)
  121. assert_equal invalid_user.screen_name, user.screen_name
  122. assert_nil user.password
  123. end
  124. # Test a login with invalid password.
  125. def test_login_failure_with_wrong_password
  126. invalid_user = @valid_user
  127. # Construct an invalid password.
  128. invalid_user.password += "baz"
  129. try_to_login invalid_user
  130. assert_template "login"
  131. assert_equal "Invalid screen name/password combination", flash[:notice]
  132. # Make sure screen_name will be redisplayed, but not the password.
  133. user = assigns(:user)
  134. assert_equal invalid_user.screen_name, user.screen_name
  135. assert_nil user.password
  136. end
  137. # Test the logout function.
  138. def test_logout
  139. try_to_login @valid_user
  140. assert logged_in?
  141. get :logout
  142. assert_response :redirect
  143. assert_redirected_to :action => "index", :controller => "site"
  144. assert_equal "Logged out", flash[:notice]
  145. assert !logged_in?
  146. end
  147. # Test the navigation menu after login.
  148. def test_navigation_logged_in
  149. authorize @valid_user
  150. get :index
  151. assert_tag "a", :content => /Logout/,
  152. :attributes => { :href => "/user/logout" }
  153. assert_no_tag "a", :content => /Register/
  154. assert_no_tag "a", :content => /Login/
  155. end
  156. # Test index page for unauthorized user.
  157. def test_index_unauthorized
  158. # Make sure the before_filter is working.
  159. get :index
  160. assert_response :redirect
  161. assert_redirected_to :action => "login"
  162. assert_equal "Please log in first", flash[:notice]
  163. end
  164. # Test index page for authorized user.
  165. def test_index_authorized
  166. authorize @valid_user
  167. get :index
  168. assert_response :success
  169. assert_template "index"
  170. end
  171. # Test forward back to protected page after login.
  172. def test_login_friendly_url_forwarding
  173. user = { :screen_name => @valid_user.screen_name,
  174. :password => @valid_user.password }
  175. friendly_url_forwarding_aux(:login, :index, user)
  176. end
  177. # Test forward back to protected page after register.
  178. def test_register_friendly_url_forwarding
  179. user = { :screen_name => "new_screen_name",
  180. :email => "valid@example.com",
  181. :password => "long_enough_password" }
  182. friendly_url_forwarding_aux(:register, :index, user)
  183. end
  184. # Test the edit page.
  185. def test_edit_page
  186. authorize @valid_user
  187. get :edit
  188. title = assigns(:title)
  189. assert_equal "Edit basic info", title
  190. assert_response :success
  191. assert_template "edit"
  192. # Test the form and all its tags.
  193. assert_form_tag "/user/edit"
  194. assert_email_field @valid_user.email
  195. assert_password_field "current_password"
  196. assert_password_field
  197. assert_password_field "password_confirmation"
  198. assert_submit_button "Update"
  199. end
  200. private
  201. # Try to log a user in using the login action.
  202. # Pass :remember_me => "0" or :remember_me => "1" in options
  203. # to invoke the remember me machinery.
  204. def try_to_login(user, options = {})
  205. user_hash = { :screen_name => user.screen_name,
  206. :password => user.password }
  207. user_hash.merge!(options)
  208. post :login, :user => user_hash
  209. end
  210. # Authorize a user.
  211. def authorize(user)
  212. @request.session[:user_id] = user.id
  213. end
  214. def friendly_url_forwarding_aux(test_page, protected_page, user)
  215. # Get a protected page.
  216. get protected_page
  217. assert_response :redirect
  218. assert_redirected_to :action => "login"
  219. post test_page, :user => user
  220. assert_response :redirect
  221. # This is a hack. See http://www.ruby-forum.com/topic/69760
  222. assert_redirected_to :action => protected_page
  223. # Make sure the forwarding url has been cleared.
  224. assert_nil session[:protected_page]
  225. end
  226. # Some utility assertions for testing HTML.
  227. # Assert that the email field has the correct HTML.
  228. def assert_email_field(email = nil, options = {})
  229. assert_input_field("user[email]", email, "text",
  230. User::EMAIL_SIZE, User::EMAIL_MAX_LENGTH,
  231. options)
  232. end
  233. # Assert that the password field has the correct HTML.
  234. def assert_password_field(password_field_name = "password", options = {})
  235. # We never want a password to appear pre-filled into a form.
  236. blank = nil
  237. assert_input_field("user[#{password_field_name}]", blank, "password",
  238. User::PASSWORD_SIZE, User::PASSWORD_MAX_LENGTH,
  239. options)
  240. end
  241. # Assert that the screen name field has the correct HTML.
  242. def assert_screen_name_field(screen_name = nil, options = {})
  243. assert_input_field("user[screen_name]", screen_name, "text",
  244. User::SCREEN_NAME_SIZE, User::SCREEN_NAME_MAX_LENGTH,
  245. options)
  246. end
  247. # Return the cookie value given a symbol.
  248. def cookie_value(symbol)
  249. cookies[symbol.to_s].value.first
  250. end
  251. # Return the cookie expiration given a symbol.
  252. def cookie_expires(symbol)
  253. cookies[symbol.to_s].expires
  254. end
  255. end