2009-10-12 07:37:42 -04:00
|
|
|
require 'test/test_helper'
|
|
|
|
|
2009-11-03 06:35:11 -05:00
|
|
|
class MappingTest < ActiveSupport::TestCase
|
2009-10-12 07:37:42 -04:00
|
|
|
|
|
|
|
test 'store options' do
|
2009-10-12 20:49:51 -04:00
|
|
|
mapping = Devise.mappings[:user]
|
2009-10-12 07:37:42 -04:00
|
|
|
|
2009-10-12 20:49:51 -04:00
|
|
|
assert_equal User, mapping.to
|
|
|
|
assert_equal User.devise_modules, mapping.for
|
|
|
|
assert_equal :users, mapping.as
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
|
|
|
|
2009-10-12 20:49:51 -04:00
|
|
|
test 'allows as to be given' do
|
|
|
|
assert_equal :admin_area, Devise.mappings[:admin].as
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|
2009-10-12 07:49:28 -04:00
|
|
|
|
2009-10-18 10:08:07 -04:00
|
|
|
test 'allow custom singular to be given' do
|
|
|
|
assert_equal :organizers, Devise.mappings[:manager].as
|
|
|
|
end
|
|
|
|
|
2009-10-12 07:49:28 -04:00
|
|
|
test 'allows a controller depending on the mapping' do
|
2009-10-12 20:49:51 -04:00
|
|
|
mapping = Devise.mappings[:user]
|
|
|
|
assert mapping.allows?(:sessions)
|
|
|
|
assert mapping.allows?(:confirmations)
|
|
|
|
assert mapping.allows?(:passwords)
|
2009-10-12 07:49:28 -04:00
|
|
|
|
2009-10-12 20:49:51 -04:00
|
|
|
mapping = Devise.mappings[:admin]
|
|
|
|
assert mapping.allows?(:sessions)
|
|
|
|
assert_not mapping.allows?(:confirmations)
|
|
|
|
assert_not mapping.allows?(:passwords)
|
2009-10-12 07:49:28 -04:00
|
|
|
end
|
2009-10-12 08:56:12 -04:00
|
|
|
|
2009-11-15 12:14:08 -05:00
|
|
|
test 'find mapping by path' do
|
2009-11-06 11:27:27 -05:00
|
|
|
assert_nil Devise::Mapping.find_by_path("/foo/bar")
|
|
|
|
assert_equal Devise.mappings[:user], Devise::Mapping.find_by_path("/users/session")
|
2009-10-12 08:56:12 -04:00
|
|
|
end
|
|
|
|
|
2009-11-15 12:14:08 -05:00
|
|
|
test 'find mapping by customized path' do
|
2009-11-06 11:27:27 -05:00
|
|
|
assert_equal Devise.mappings[:admin], Devise::Mapping.find_by_path("/admin_area/session")
|
2009-10-12 08:56:12 -04:00
|
|
|
end
|
2009-10-12 20:06:39 -04:00
|
|
|
|
2009-11-15 12:14:08 -05:00
|
|
|
test 'find mapping by class' do
|
|
|
|
assert_nil Devise::Mapping.find_by_class(String)
|
|
|
|
assert_equal Devise.mappings[:user], Devise::Mapping.find_by_class(User)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'find mapping by class works with single table inheritance' do
|
|
|
|
klass = Class.new(User)
|
|
|
|
assert_equal Devise.mappings[:user], Devise::Mapping.find_by_class(klass)
|
|
|
|
end
|
|
|
|
|
2009-11-16 12:07:01 -05:00
|
|
|
test 'find scope for a given object' do
|
|
|
|
assert_equal :user, Devise::Mapping.find_scope!(User)
|
|
|
|
assert_equal :user, Devise::Mapping.find_scope!(:user)
|
|
|
|
assert_equal :user, Devise::Mapping.find_scope!(User.new)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'find scope raises an error if cannot be found' do
|
2009-11-15 12:14:08 -05:00
|
|
|
assert_raise RuntimeError do
|
2009-11-16 12:07:01 -05:00
|
|
|
Devise::Mapping.find_scope!(String)
|
2009-11-15 12:14:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-16 19:46:06 -04:00
|
|
|
test 'return default path names' do
|
|
|
|
mapping = Devise.mappings[:user]
|
|
|
|
assert_equal 'sign_in', mapping.path_names[:sign_in]
|
|
|
|
assert_equal 'sign_out', mapping.path_names[:sign_out]
|
|
|
|
assert_equal 'password', mapping.path_names[:password]
|
|
|
|
assert_equal 'confirmation', mapping.path_names[:confirmation]
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'allow custom path names to be given' do
|
|
|
|
mapping = Devise.mappings[:account]
|
|
|
|
assert_equal 'login', mapping.path_names[:sign_in]
|
|
|
|
assert_equal 'logout', mapping.path_names[:sign_out]
|
|
|
|
assert_equal 'secret', mapping.path_names[:password]
|
|
|
|
assert_equal 'verification', mapping.path_names[:confirmation]
|
|
|
|
end
|
|
|
|
|
2009-11-06 11:27:27 -05:00
|
|
|
test 'has an empty path as default path prefix' do
|
|
|
|
mapping = Devise.mappings[:account]
|
|
|
|
assert_equal '/', mapping.path_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'allow path prefix to be configured' do
|
|
|
|
mapping = Devise.mappings[:manager]
|
|
|
|
assert_equal '/:locale/', mapping.path_prefix
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'retrieve as from the proper position' do
|
|
|
|
assert_equal 1, Devise.mappings[:account].as_position
|
|
|
|
assert_equal 2, Devise.mappings[:manager].as_position
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'raw path is returned' do
|
|
|
|
assert_equal '/account', Devise.mappings[:account].raw_path
|
|
|
|
assert_equal '/:locale/organizers', Devise.mappings[:manager].raw_path
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'parsed path is returned' do
|
2009-11-06 17:59:36 -05:00
|
|
|
begin
|
|
|
|
Devise.default_url_options {{ :locale => I18n.locale }}
|
|
|
|
assert_equal '/account', Devise.mappings[:account].parsed_path
|
|
|
|
assert_equal '/en/organizers', Devise.mappings[:manager].parsed_path
|
|
|
|
ensure
|
|
|
|
Devise.default_url_options {{ }}
|
|
|
|
end
|
2009-11-06 11:27:27 -05:00
|
|
|
end
|
|
|
|
|
2009-11-13 05:49:22 -05:00
|
|
|
test 'should have default route options' do
|
|
|
|
assert_equal({}, Devise.mappings[:user].route_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should allow passing route options to devise routes' do
|
|
|
|
assert_equal({ :requirements => { :extra => 'value' } }, Devise.mappings[:manager].route_options)
|
|
|
|
end
|
|
|
|
|
2009-10-12 20:06:39 -04:00
|
|
|
test 'magic predicates' do
|
2009-10-12 20:49:51 -04:00
|
|
|
mapping = Devise.mappings[:user]
|
2009-10-30 06:29:10 -04:00
|
|
|
assert mapping.authenticatable?
|
2009-10-12 20:06:39 -04:00
|
|
|
assert mapping.confirmable?
|
2009-10-12 20:49:51 -04:00
|
|
|
assert mapping.recoverable?
|
2009-10-19 22:31:33 -04:00
|
|
|
assert mapping.rememberable?
|
2009-10-12 20:49:51 -04:00
|
|
|
|
|
|
|
mapping = Devise.mappings[:admin]
|
2009-10-30 06:29:10 -04:00
|
|
|
assert mapping.authenticatable?
|
2009-10-12 20:49:51 -04:00
|
|
|
assert_not mapping.confirmable?
|
|
|
|
assert_not mapping.recoverable?
|
2009-10-19 22:31:33 -04:00
|
|
|
assert_not mapping.rememberable?
|
2009-10-12 20:06:39 -04:00
|
|
|
end
|
2009-10-12 07:37:42 -04:00
|
|
|
end
|