mapping_test.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. require 'test_helper'
  2. class FakeRequest < Struct.new(:path_info, :params)
  3. end
  4. class MappingTest < ActiveSupport::TestCase
  5. def fake_request(path, params={})
  6. FakeRequest.new(path, params)
  7. end
  8. test 'store options' do
  9. mapping = Devise.mappings[:user]
  10. assert_equal User, mapping.to
  11. assert_equal User.devise_modules, mapping.modules
  12. assert_equal "users", mapping.scoped_path
  13. assert_equal :user, mapping.singular
  14. assert_equal "users", mapping.path
  15. assert_equal "/users", mapping.fullpath
  16. end
  17. test 'store options with namespace' do
  18. mapping = Devise.mappings[:publisher_account]
  19. assert_equal Admin, mapping.to
  20. assert_equal "publisher/accounts", mapping.scoped_path
  21. assert_equal :publisher_account, mapping.singular
  22. assert_equal "accounts", mapping.path
  23. assert_equal "/publisher/accounts", mapping.fullpath
  24. end
  25. test 'allows path to be given' do
  26. assert_equal "admin_area", Devise.mappings[:admin].path
  27. end
  28. test 'allows to skip all routes' do
  29. assert_equal [], Devise.mappings[:skip_admin].used_routes
  30. end
  31. test 'sign_out_via defaults to :get' do
  32. assert_equal :get, Devise.mappings[:user].sign_out_via
  33. end
  34. test 'allows custom sign_out_via to be given' do
  35. assert_equal :delete, Devise.mappings[:sign_out_via_delete].sign_out_via
  36. assert_equal :post, Devise.mappings[:sign_out_via_post].sign_out_via
  37. assert_equal [:delete, :post], Devise.mappings[:sign_out_via_delete_or_post].sign_out_via
  38. end
  39. test 'allows custom singular to be given' do
  40. assert_equal "accounts", Devise.mappings[:manager].path
  41. end
  42. test 'has strategies depending on the model declaration' do
  43. assert_equal [:rememberable, :token_authenticatable, :database_authenticatable], Devise.mappings[:user].strategies
  44. assert_equal [:database_authenticatable], Devise.mappings[:admin].strategies
  45. end
  46. test 'has no input strategies depending on the model declaration' do
  47. assert_equal [:rememberable, :token_authenticatable], Devise.mappings[:user].no_input_strategies
  48. assert_equal [], Devise.mappings[:admin].no_input_strategies
  49. end
  50. test 'find scope for a given object' do
  51. assert_equal :user, Devise::Mapping.find_scope!(User)
  52. assert_equal :user, Devise::Mapping.find_scope!(:user)
  53. assert_equal :user, Devise::Mapping.find_scope!(User.new)
  54. end
  55. test 'find scope works with single table inheritance' do
  56. assert_equal :user, Devise::Mapping.find_scope!(Class.new(User))
  57. assert_equal :user, Devise::Mapping.find_scope!(Class.new(User).new)
  58. end
  59. test 'find scope raises an error if cannot be found' do
  60. assert_raise RuntimeError do
  61. Devise::Mapping.find_scope!(String)
  62. end
  63. end
  64. test 'return default path names' do
  65. mapping = Devise.mappings[:user]
  66. assert_equal 'sign_in', mapping.path_names[:sign_in]
  67. assert_equal 'sign_out', mapping.path_names[:sign_out]
  68. assert_equal 'password', mapping.path_names[:password]
  69. assert_equal 'confirmation', mapping.path_names[:confirmation]
  70. assert_equal 'sign_up', mapping.path_names[:sign_up]
  71. assert_equal 'unlock', mapping.path_names[:unlock]
  72. end
  73. test 'allow custom path names to be given' do
  74. mapping = Devise.mappings[:manager]
  75. assert_equal 'login', mapping.path_names[:sign_in]
  76. assert_equal 'logout', mapping.path_names[:sign_out]
  77. assert_equal 'secret', mapping.path_names[:password]
  78. assert_equal 'verification', mapping.path_names[:confirmation]
  79. assert_equal 'register', mapping.path_names[:sign_up]
  80. assert_equal 'unblock', mapping.path_names[:unlock]
  81. end
  82. test 'magic predicates' do
  83. mapping = Devise.mappings[:user]
  84. assert mapping.authenticatable?
  85. assert mapping.confirmable?
  86. assert mapping.recoverable?
  87. assert mapping.rememberable?
  88. assert mapping.registerable?
  89. mapping = Devise.mappings[:admin]
  90. assert mapping.authenticatable?
  91. assert mapping.recoverable?
  92. assert mapping.lockable?
  93. assert_not mapping.omniauthable?
  94. end
  95. test 'find mapping by path' do
  96. assert_raise RuntimeError do
  97. Devise::Mapping.find_by_path!('/accounts/facebook/callback')
  98. end
  99. assert_nothing_raised do
  100. Devise::Mapping.find_by_path!('/:locale/accounts/login')
  101. end
  102. assert_nothing_raised do
  103. Devise::Mapping.find_by_path!('/accounts/facebook/callback', :path)
  104. end
  105. end
  106. end