Creating Devise.map to generate and recognize urls based on scope.

This commit is contained in:
Carlos A. da Silva
2009-10-09 21:11:58 -03:00
parent 984cf8ee20
commit f4de0f6c5a
27 changed files with 357 additions and 71 deletions

View File

@@ -1,4 +1,4 @@
require 'test_helper'
require 'test/test_helper'
class Authenticable < ActiveRecord::Base
devise
@@ -20,7 +20,7 @@ class Devisable < ActiveRecord::Base
devise :all
end
class DeviseActiveRecordTest < ActiveSupport::TestCase
class ActiveRecordTest < ActiveSupport::TestCase
def include_authenticable_module?(mod)
mod.included_modules.include?(Devise::Models::Authenticable)

View File

@@ -0,0 +1,20 @@
require 'test/test_helper'
class ResourcesTest < ActionController::TestCase
tests ApplicationController
test 'should get resource name from request path' do
@request.path = '/users/session'
assert_equal 'users', @controller.resource_name
end
test 'should get translated resource name from request path' do
@request.path = '/conta/session'
assert_equal 'account', @controller.resource_name
end
test 'should get resource class from request path' do
@request.path = '/users/session'
assert_equal User, @controller.resource_class
end
end

View File

@@ -0,0 +1,41 @@
require 'test/test_helper'
class RoutesTest < ActionController::TestCase
tests ApplicationController
def test_path_and_url(name, prepend_path=nil)
@request.path = '/users/session'
prepend_path = "#{prepend_path}_" if prepend_path
assert_equal @controller.send(:"#{prepend_path}#{name}_path"),
send(:"#{prepend_path}users_#{name}_path")
assert_equal @controller.send(:"#{prepend_path}#{name}_url"),
send(:"#{prepend_path}users_#{name}_url")
assert_equal @controller.send(:"#{prepend_path}#{name}_path", :param => 123),
send(:"#{prepend_path}users_#{name}_path", :param => 123)
assert_equal @controller.send(:"#{prepend_path}#{name}_url", :param => 123),
send(:"#{prepend_path}users_#{name}_url", :param => 123)
# @request.path = nil
# assert_equal @controller.send(:"#{prepend_path}#{name}_path", User.new),
# send(:"#{prepend_path}users_#{name}_path")
# assert_equal @controller.send(:"#{prepend_path}#{name}_url", User.new),
# send(:"#{prepend_path}users_#{name}_url")
end
test 'should alias session to mapped user session' do
test_path_and_url :session
test_path_and_url :session, :new
end
test 'should alias password to mapped user password' do
test_path_and_url :password
test_path_and_url :password, :new
end
test 'should alias confirmation to mapped user confirmation' do
test_path_and_url :confirmation
test_path_and_url :confirmation, :new
end
end

View File

@@ -1,4 +1,4 @@
require 'test_helper'
require 'test/test_helper'
class DeviseHelperTest < ActionView::TestCase
@@ -13,29 +13,35 @@ class DeviseHelperTest < ActionView::TestCase
end
test 'should generate a link to sign in' do
assert_equal %[<a href="#{new_session_path}">Sign in</a>], link_to_sign_in
self.stubs(:new_session_path).returns(new_users_session_path)
assert_equal %[<a href="#{new_users_session_path}">Sign in</a>], link_to_sign_in
end
test 'should use i18n to translante sign in link' do
self.stubs(:new_session_path).returns(new_users_session_path)
store_translations(:sign_in => 'Login')
assert_equal %[<a href="#{new_session_path}">Login</a>], link_to_sign_in
assert_equal %[<a href="#{new_users_session_path}">Login</a>], link_to_sign_in
end
test 'should generate a link to forgot password' do
assert_equal %[<a href="#{new_password_path}">Forgot password?</a>], link_to_new_password
self.stubs(:new_password_path).returns(new_users_password_path)
assert_equal %[<a href="#{new_users_password_path}">Forgot password?</a>], link_to_new_password
end
test 'should use i18n to translante forgot password link' do
self.stubs(:new_password_path).returns(new_users_password_path)
store_translations(:new_password => 'New password?')
assert_equal %[<a href="#{new_password_path}">New password?</a>], link_to_new_password
assert_equal %[<a href="#{new_users_password_path}">New password?</a>], link_to_new_password
end
test 'should generate a link to confirmation instructions' do
assert_equal %[<a href="#{new_confirmation_path}">Didn't receive confirmation instructions?</a>], link_to_new_confirmation
self.stubs(:new_confirmation_path).returns(new_users_confirmation_path)
assert_equal %[<a href="#{new_users_confirmation_path}">Didn't receive confirmation instructions?</a>], link_to_new_confirmation
end
test 'should use i18n to translante confirmation link' do
self.stubs(:new_confirmation_path).returns(new_users_confirmation_path)
store_translations(:new_confirmation => 'New confirmation?')
assert_equal %[<a href="#{new_confirmation_path}">New confirmation?</a>], link_to_new_confirmation
assert_equal %[<a href="#{new_users_confirmation_path}">New confirmation?</a>], link_to_new_confirmation
end
end

View File

@@ -1,4 +1,4 @@
require 'test_helper'
require 'test/test_helper'
class AuthenticationTest < ActionController::IntegrationTest
@@ -47,7 +47,7 @@ class AuthenticationTest < ActionController::IntegrationTest
end
test 'not authenticated user should not be able to sign out' do
delete '/session'
delete 'users/session'
assert_response :success
assert_template 'sessions/new'
@@ -58,9 +58,9 @@ class AuthenticationTest < ActionController::IntegrationTest
sign_in
assert warden.authenticated?
delete '/session'
delete 'users/session'
assert_response :redirect
assert_redirected_to new_session_path
assert_redirected_to new_users_session_path
assert !warden.authenticated?
end
end

View File

@@ -1,11 +1,11 @@
require 'test_helper'
require 'test/test_helper'
class ConfirmationsTest < ActionController::IntegrationTest
test 'authenticated user should not be able to visit confirmation page' do
sign_in
get new_confirmation_path
get new_users_confirmation_path
assert_response :redirect
assert_redirected_to root_path
@@ -15,7 +15,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
test 'not authenticated user should be able to request a new confirmation' do
user = create_user
visit '/session/new'
visit 'users/session/new'
click_link 'Didn\'t receive confirmation instructions?'
fill_in 'email', :with => user.email
@@ -28,7 +28,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
end
test 'not authenticated user with invalid perishable token should not be able to confirm an account' do
visit confirmation_path(:perishable_token => 'invalid_perishable')
visit users_confirmation_path(:perishable_token => 'invalid_perishable')
assert_response :success
assert_template 'confirmations/new'
@@ -40,7 +40,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
user = create_user(:confirm => false)
assert_not user.confirmed?
visit confirmation_path(:perishable_token => user.perishable_token)
visit users_confirmation_path(:perishable_token => user.perishable_token)
# assert_response :redirect
assert_template 'sessions/new'
@@ -51,7 +51,7 @@ class ConfirmationsTest < ActionController::IntegrationTest
test 'already confirmed user should not be able to confirm the account again' do
user = create_user
visit confirmation_path(:perishable_token => user.perishable_token)
visit users_confirmation_path(:perishable_token => user.perishable_token)
assert_template 'confirmations/new'
assert_have_selector '#errorExplanation'

View File

@@ -1,9 +1,9 @@
require 'test_helper'
require 'test/test_helper'
class PasswordRecoveryTest < ActionController::IntegrationTest
def visit_new_password_path
visit '/session/new'
visit 'users/session/new'
click_link 'Forgot password?'
end
@@ -18,7 +18,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'authenticated user should not be able to visit forgot password page' do
sign_in
get new_password_path
get new_users_password_path
assert_response :redirect
assert_redirected_to root_path
@@ -55,16 +55,10 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
assert_contain 'Email not found'
end
# test 'request forgot password should send an email to the user' do
# ActionMailer::Base.deliveries = []
# request_forgot_password
# assert_equal 1, ActionMailer::Base.deliveries.size
# end
test 'authenticated user should not be able to visit edit password page' do
sign_in
get edit_password_path
get edit_users_password_path
assert_response :redirect
assert_redirected_to root_path
@@ -73,7 +67,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with invalid perishable token should not be able to change his password' do
create_user
visit edit_password_path(:perishable_token => 'invalid_perishable')
visit edit_users_password_path(:perishable_token => 'invalid_perishable')
assert_response :success
assert_template 'passwords/edit'
@@ -90,7 +84,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with valid perisable token but invalid password should not be able to change his password' do
create_user
visit edit_password_path(:perishable_token => @user.perishable_token)
visit edit_users_password_path(:perishable_token => @user.perishable_token)
fill_in 'Password', :with => '987654321'
fill_in 'Password confirmation', :with => 'other_password'
@@ -105,7 +99,7 @@ class PasswordRecoveryTest < ActionController::IntegrationTest
test 'not authenticated with valid data should be able to change his password' do
create_user
visit edit_password_path(:perishable_token => @user.perishable_token)
visit edit_users_password_path(:perishable_token => @user.perishable_token)
fill_in 'Password', :with => '987654321'
fill_in 'Password confirmation', :with => '987654321'

View File

@@ -36,7 +36,7 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
test 'body should have link to confirm the account' do
host = ActionMailer::Base.default_url_options[:host]
confirmation_url_regexp = %r{<a href=\"http://#{host}/confirmation\?perishable_token=#{@user.perishable_token}">}
confirmation_url_regexp = %r{<a href=\"http://#{host}/users/confirmation\?perishable_token=#{@user.perishable_token}">}
assert_match confirmation_url_regexp, @mail.body
end
end

View File

@@ -36,7 +36,7 @@ class ResetPasswordInstructionsTest < ActionMailer::TestCase
test 'body should have link to confirm the account' do
host = ActionMailer::Base.default_url_options[:host]
confirmation_url_regexp = %r{<a href=\"http://#{host}/password/edit\?perishable_token=#{@user.perishable_token}">}
confirmation_url_regexp = %r{<a href=\"http://#{host}/users/password/edit\?perishable_token=#{@user.perishable_token}">}
assert_match confirmation_url_regexp, @mail.body
end
end

67
test/map_test.rb Normal file
View File

@@ -0,0 +1,67 @@
require 'test/test_helper'
class Participant < User; end
class MapTest < ActiveSupport::TestCase
def setup
@mappings = Devise.mappings
Devise.mappings = {}
end
def teardown
Devise.mappings = @mappings
end
test 'store options' do
Devise.map :participants, :to => Participant, :for => [:authenticable]
mappings = Devise.mappings
assert_not mappings.empty?
assert_equal({:to => Participant, :for => [:authenticable], :as => :participants}, mappings[:participants])
end
test 'require :for option' do
assert_raise ArgumentError do
Devise.map :participants, :to => Participant
end
end
test 'assert valid keys in options' do
assert_raise ArgumentError do
Devise.map :participants, :to => Participant, :for => [:authenticable], :other => 123
end
end
test 'set the first mapping as default' do
Devise.mappings.default = nil
assert_nil Devise.mappings.default
Devise.map :participants, :for => [:authenticable]
assert_equal :participants, Devise.mappings.default
end
test 'map should lookup for the mapping class if no one is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal Participant, Devise.mappings[:participants][:to]
end
test 'use mapping to :as option if none is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal :participants, Devise.mappings[:participants][:as]
end
test 'find right mapping to use for routing' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participants', Devise.find_mapping('participants')
end
test 'find right mapping to Participant for routing with :as option' do
Devise.map :participants, :for => [:authenticable], :as => 'usuarios'
assert_equal 'participants', Devise.find_mapping('usuarios')
end
test 'find mapping should return default map in no one is found or empty is given' do
Devise.map :participants, :for => [:authenticable]
assert_equal 'participants', Devise.find_mapping('test_drive')
assert_equal 'participants', Devise.find_mapping(nil)
end
end

View File

@@ -0,0 +1,3 @@
class Account < ActiveRecord::Base
devise :all
end

View File

@@ -0,0 +1,2 @@
Devise.map :users, :to => User, :for => [:authenticable, :recoverable, :confirmable]
Devise.map :account, :to => Account, :for => [:authenticable, :confirmable], :as => 'conta'

View File

@@ -3,20 +3,20 @@ require 'test_helper'
class ConfirmationRoutingTest < ActionController::TestCase
test 'new session route' do
assert_routing('/confirmation/new', :controller => 'confirmations', :action => 'new')
assert_routing('users/confirmation/new', :controller => 'confirmations', :action => 'new')
end
test 'create confirmation route' do
assert_routing({:path => '/confirmation', :method => :post}, {:controller => 'confirmations', :action => 'create'})
assert_routing({:path => 'users/confirmation', :method => :post}, {:controller => 'confirmations', :action => 'create'})
end
test 'show confirmation route' do
assert_routing('/confirmation', :controller => 'confirmations', :action => 'show')
assert_routing('users/confirmation', :controller => 'confirmations', :action => 'show')
end
test 'translated confirmation route' do
translated_route(:confirmation => 'confirmacao') do
assert_routing('/confirmacao/new', :controller => 'confirmations', :action => 'new')
assert_routing('users/confirmacao/new', :controller => 'confirmations', :action => 'new')
end
end
end

View File

@@ -0,0 +1,28 @@
require 'test/test_helper'
class MapRoutingTest < ActionController::TestCase
test 'map devise user session' do
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'users/session/new')
end
test 'map devise user confirmation' do
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'users/confirmation/new')
end
test 'map devise user password' do
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'users/password/new')
end
test 'map devise account session with :as option' do
assert_recognizes({:controller => 'sessions', :action => 'new'}, 'conta/session/new')
end
test 'map devise account confirmation with :as option' do
assert_recognizes({:controller => 'confirmations', :action => 'new'}, 'conta/confirmation/new')
end
test 'map devise account password with :as option' do
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'conta/password/new')
end
end

View File

@@ -3,24 +3,24 @@ require 'test_helper'
class PasswordRoutingTest < ActionController::TestCase
test 'new password route' do
assert_routing('/password/new', :controller => 'passwords', :action => 'new')
assert_routing('users/password/new', :controller => 'passwords', :action => 'new')
end
test 'create password route' do
assert_routing({:path => '/password', :method => :post}, {:controller => 'passwords', :action => 'create'})
assert_routing({:path => 'users/password', :method => :post}, {:controller => 'passwords', :action => 'create'})
end
test 'edit password route' do
assert_routing('/password/edit', :controller => 'passwords', :action => 'edit')
assert_routing('users/password/edit', :controller => 'passwords', :action => 'edit')
end
test 'update password route' do
assert_routing({:path => '/password', :method => :put}, {:controller => 'passwords', :action => 'update'})
assert_routing({:path => 'users/password', :method => :put}, {:controller => 'passwords', :action => 'update'})
end
test 'translated password route' do
translated_route(:password => 'senha') do
assert_routing('/senha/new', :controller => 'passwords', :action => 'new')
assert_routing('users/senha/new', :controller => 'passwords', :action => 'new')
end
end
end

View File

@@ -3,20 +3,20 @@ require 'test_helper'
class SessionRoutingTest < ActionController::TestCase
test 'new session route' do
assert_routing('/session/new', :controller => 'sessions', :action => 'new')
assert_routing('users/session/new', :controller => 'sessions', :action => 'new')
end
test 'create session route' do
assert_routing({:path => '/session', :method => :post}, {:controller => 'sessions', :action => 'create'})
assert_routing({:path => 'users/session', :method => :post}, {:controller => 'sessions', :action => 'create'})
end
test 'destroy session route' do
assert_routing({:path => '/session', :method => :delete}, {:controller => 'sessions', :action => 'destroy'})
assert_routing({:path => 'users/session', :method => :delete}, {:controller => 'sessions', :action => 'destroy'})
end
test 'translate session route' do
translated_route(:session => 'sessao') do
assert_routing('/sessao/new', :controller => 'sessions', :action => 'new')
assert_routing('users/sessao/new', :controller => 'sessions', :action => 'new')
end
end
end

View File

@@ -16,7 +16,7 @@ class ActionController::IntegrationTest
def sign_in(options={}, &block)
create_user(options)
visit '/session/new'
visit 'users/session/new'
fill_in 'email', :with => 'test@test.com'
fill_in 'password', :with => '123456'
yield if block_given?

View File

@@ -1,4 +1,4 @@
RAILS_ENV = ENV["RAILS_ENV"] = "test"
ENV["RAILS_ENV"] = "test"
require File.join(File.dirname(__FILE__), 'rails_app', 'config', 'environment')
require 'test_help'