routes_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. require 'test_helper'
  2. class DefaultRoutingTest < ActionController::TestCase
  3. test 'map new user session' do
  4. assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, {:path => 'users/sign_in', :method => :get})
  5. assert_named_route "/users/sign_in", :new_user_session_path
  6. end
  7. test 'map create user session' do
  8. assert_recognizes({:controller => 'devise/sessions', :action => 'create'}, {:path => 'users/sign_in', :method => :post})
  9. assert_named_route "/users/sign_in", :user_session_path
  10. end
  11. test 'map destroy user session' do
  12. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => 'users/sign_out', :method => :get})
  13. assert_named_route "/users/sign_out", :destroy_user_session_path
  14. end
  15. test 'map new user confirmation' do
  16. assert_recognizes({:controller => 'devise/confirmations', :action => 'new'}, 'users/confirmation/new')
  17. assert_named_route "/users/confirmation/new", :new_user_confirmation_path
  18. end
  19. test 'map create user confirmation' do
  20. assert_recognizes({:controller => 'devise/confirmations', :action => 'create'}, {:path => 'users/confirmation', :method => :post})
  21. assert_named_route "/users/confirmation", :user_confirmation_path
  22. end
  23. test 'map show user confirmation' do
  24. assert_recognizes({:controller => 'devise/confirmations', :action => 'show'}, {:path => 'users/confirmation', :method => :get})
  25. end
  26. test 'map new user password' do
  27. assert_recognizes({:controller => 'devise/passwords', :action => 'new'}, 'users/password/new')
  28. assert_named_route "/users/password/new", :new_user_password_path
  29. end
  30. test 'map create user password' do
  31. assert_recognizes({:controller => 'devise/passwords', :action => 'create'}, {:path => 'users/password', :method => :post})
  32. assert_named_route "/users/password", :user_password_path
  33. end
  34. test 'map edit user password' do
  35. assert_recognizes({:controller => 'devise/passwords', :action => 'edit'}, 'users/password/edit')
  36. assert_named_route "/users/password/edit", :edit_user_password_path
  37. end
  38. test 'map update user password' do
  39. assert_recognizes({:controller => 'devise/passwords', :action => 'update'}, {:path => 'users/password', :method => :put})
  40. end
  41. test 'map new user unlock' do
  42. assert_recognizes({:controller => 'devise/unlocks', :action => 'new'}, 'users/unlock/new')
  43. assert_named_route "/users/unlock/new", :new_user_unlock_path
  44. end
  45. test 'map create user unlock' do
  46. assert_recognizes({:controller => 'devise/unlocks', :action => 'create'}, {:path => 'users/unlock', :method => :post})
  47. assert_named_route "/users/unlock", :user_unlock_path
  48. end
  49. test 'map show user unlock' do
  50. assert_recognizes({:controller => 'devise/unlocks', :action => 'show'}, {:path => 'users/unlock', :method => :get})
  51. end
  52. test 'map new user registration' do
  53. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, 'users/sign_up')
  54. assert_named_route "/users/sign_up", :new_user_registration_path
  55. end
  56. test 'map create user registration' do
  57. assert_recognizes({:controller => 'devise/registrations', :action => 'create'}, {:path => 'users', :method => :post})
  58. assert_named_route "/users", :user_registration_path
  59. end
  60. test 'map edit user registration' do
  61. assert_recognizes({:controller => 'devise/registrations', :action => 'edit'}, {:path => 'users/edit', :method => :get})
  62. assert_named_route "/users/edit", :edit_user_registration_path
  63. end
  64. test 'map update user registration' do
  65. assert_recognizes({:controller => 'devise/registrations', :action => 'update'}, {:path => 'users', :method => :put})
  66. end
  67. test 'map destroy user registration' do
  68. assert_recognizes({:controller => 'devise/registrations', :action => 'destroy'}, {:path => 'users', :method => :delete})
  69. end
  70. test 'map cancel user registration' do
  71. assert_recognizes({:controller => 'devise/registrations', :action => 'cancel'}, {:path => 'users/cancel', :method => :get})
  72. assert_named_route "/users/cancel", :cancel_user_registration_path
  73. end
  74. test 'map omniauth callbacks' do
  75. assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'facebook'}, {:path => 'users/auth/facebook/callback', :method => :get})
  76. assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'facebook'}, {:path => 'users/auth/facebook/callback', :method => :post})
  77. assert_named_route "/users/auth/facebook/callback", :user_omniauth_callback_path, :facebook
  78. # named open_id
  79. assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'google'}, {:path => 'users/auth/google/callback', :method => :get})
  80. assert_recognizes({:controller => 'users/omniauth_callbacks', :action => 'google'}, {:path => 'users/auth/google/callback', :method => :post})
  81. assert_named_route "/users/auth/google/callback", :user_omniauth_callback_path, :google
  82. assert_raise ActionController::RoutingError do
  83. assert_recognizes({:controller => 'ysers/omniauth_callbacks', :action => 'twitter'}, {:path => 'users/auth/twitter/callback', :method => :get})
  84. end
  85. end
  86. protected
  87. def assert_named_route(result, *args)
  88. assert_equal result, @routes.url_helpers.send(*args)
  89. end
  90. end
  91. class CustomizedRoutingTest < ActionController::TestCase
  92. test 'map admin with :path option' do
  93. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'admin_area/sign_up', :method => :get})
  94. end
  95. test 'map admin with :controllers option' do
  96. assert_recognizes({:controller => 'admins/sessions', :action => 'new'}, {:path => 'admin_area/sign_in', :method => :get})
  97. end
  98. test 'does not map admin password' do
  99. assert_raise ActionController::RoutingError do
  100. assert_recognizes({:controller => 'devise/passwords', :action => 'new'}, 'admin_area/password/new')
  101. end
  102. end
  103. test 'subdomain admin' do
  104. assert_recognizes({"host"=>"sub.example.com", :controller => 'devise/sessions', :action => 'new'}, {:host => "sub.example.com", :path => '/sub_admin/sign_in', :method => :get})
  105. end
  106. test 'does only map reader password' do
  107. assert_raise ActionController::RoutingError do
  108. assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, 'reader/sessions/new')
  109. end
  110. assert_recognizes({:controller => 'devise/passwords', :action => 'new'}, 'reader/password/new')
  111. end
  112. test 'map account with custom path name for session sign in' do
  113. assert_recognizes({:controller => 'devise/sessions', :action => 'new', :locale => 'en'}, '/en/accounts/login')
  114. end
  115. test 'map account with custom path name for session sign out' do
  116. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy', :locale => 'en'}, '/en/accounts/logout')
  117. end
  118. test 'map account with custom path name for password' do
  119. assert_recognizes({:controller => 'devise/passwords', :action => 'new', :locale => 'en'}, '/en/accounts/secret/new')
  120. end
  121. test 'map account with custom path name for registration' do
  122. assert_recognizes({:controller => 'devise/registrations', :action => 'new', :locale => 'en'}, '/en/accounts/management/register')
  123. end
  124. test 'map account with custom path name for cancel registration' do
  125. assert_recognizes({:controller => 'devise/registrations', :action => 'cancel', :locale => 'en'}, '/en/accounts/management/giveup')
  126. end
  127. test 'map deletes with :sign_out_via option' do
  128. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/deletes/sign_out', :method => :delete})
  129. assert_raise ActionController::RoutingError do
  130. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/deletes/sign_out', :method => :get})
  131. end
  132. end
  133. test 'map posts with :sign_out_via option' do
  134. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/posts/sign_out', :method => :post})
  135. assert_raise ActionController::RoutingError do
  136. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/posts/sign_out', :method => :get})
  137. end
  138. end
  139. test 'map delete_or_posts with :sign_out_via option' do
  140. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/delete_or_posts/sign_out', :method => :post})
  141. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/delete_or_posts/sign_out', :method => :delete})
  142. assert_raise ActionController::RoutingError do
  143. assert_recognizes({:controller => 'devise/sessions', :action => 'destroy'}, {:path => '/sign_out_via/delete_or_posts/sign_out', :method => :get})
  144. end
  145. end
  146. test 'map with constraints defined in hash' do
  147. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'http://192.168.1.100/headquarters/sign_up', :method => :get})
  148. assert_raise ActionController::RoutingError do
  149. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'http://10.0.0.100/headquarters/sign_up', :method => :get})
  150. end
  151. end
  152. test 'map with constraints defined in block' do
  153. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'http://192.168.1.100/homebase/sign_up', :method => :get})
  154. assert_raise ActionController::RoutingError do
  155. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => 'http://10.0.0.100//homebase/sign_up', :method => :get})
  156. end
  157. end
  158. test 'map with format false for sessions' do
  159. assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, {:path => '/htmlonly_admin/sign_in', :method => :get})
  160. assert_raise ActionController::RoutingError do
  161. assert_recognizes({:controller => 'devise/sessions', :action => 'new'}, {:path => '/htmlonly_admin/sign_in.xml', :method => :get})
  162. end
  163. end
  164. test 'map with format false for passwords' do
  165. assert_recognizes({:controller => 'devise/passwords', :action => 'create'}, {:path => '/htmlonly_admin/password', :method => :post})
  166. assert_raise ActionController::RoutingError do
  167. assert_recognizes({:controller => 'devise/passwords', :action => 'create'}, {:path => '/htmlonly_admin/password.xml', :method => :post})
  168. end
  169. end
  170. test 'map with format false for registrations' do
  171. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => '/htmlonly_admin/sign_up', :method => :get})
  172. assert_raise ActionController::RoutingError do
  173. assert_recognizes({:controller => 'devise/registrations', :action => 'new'}, {:path => '/htmlonly_admin/sign_up.xml', :method => :get})
  174. end
  175. end
  176. test 'map with format false for confirmations' do
  177. assert_recognizes({:controller => 'devise/confirmations', :action => 'show'}, {:path => '/htmlonly_users/confirmation', :method => :get})
  178. assert_raise ActionController::RoutingError do
  179. assert_recognizes({:controller => 'devise/confirmations', :action => 'show'}, {:path => '/htmlonly_users/confirmation.xml', :method => :get})
  180. end
  181. end
  182. test 'map with format false for unlocks' do
  183. assert_recognizes({:controller => 'devise/unlocks', :action => 'show'}, {:path => '/htmlonly_users/unlock', :method => :get})
  184. assert_raise ActionController::RoutingError do
  185. assert_recognizes({:controller => 'devise/unlocks', :action => 'show'}, {:path => '/htmlonly_users/unlock.xml', :method => :get})
  186. end
  187. end
  188. test 'map with format false is not permanent' do
  189. assert_equal "/set.xml", @routes.url_helpers.set_path(:xml)
  190. end
  191. end
  192. class ScopedRoutingTest < ActionController::TestCase
  193. test 'map publisher account' do
  194. assert_recognizes({:controller => 'publisher/registrations', :action => 'new'}, {:path => '/publisher/accounts/sign_up', :method => :get})
  195. assert_equal '/publisher/accounts/sign_up', @routes.url_helpers.new_publisher_account_registration_path
  196. end
  197. test 'map publisher account merges path names' do
  198. assert_recognizes({:controller => 'publisher/sessions', :action => 'new'}, {:path => '/publisher/accounts/get_in', :method => :get})
  199. assert_equal '/publisher/accounts/get_in', @routes.url_helpers.new_publisher_account_session_path
  200. end
  201. end