1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Use sign_up instead of registration in routes. Fix issue with users being signed in while attempting to sign up with info from already existing user. Also fix signed up flash.

This commit is contained in:
Carlos Antonio da Silva 2010-02-08 11:03:15 -02:00
parent 9856646fac
commit 445070f6ec
11 changed files with 71 additions and 32 deletions

View file

@ -7,14 +7,9 @@ class RegistrationsController < ApplicationController
self.resource = resource_class.new(params[resource_name])
if resource.save
# Attempt to sign the resource in. When there is no other thing blocking
# the resource (ie confirmations), then the resource will be signed in,
# otherwise the specific message is shown and the resource will be
# redirected to the sign in page.
sign_in(resource_name, resource)
# At this time the resource has signed in and no hook has signed it out.
flash[:"#{resource_name}.signed_up"] = true
set_flash_message :notice, :signed_up
sign_in_and_redirect(resource_name, resource, true)
sign_in_and_redirect(resource_name, resource)
else
render_with_scope :new
end

View file

@ -8,7 +8,7 @@ class SessionsController < ApplicationController
def new
Devise::FLASH_MESSAGES.each do |message|
set_now_flash_message :alert, message if params.try(:[], message) == "true"
end
end unless flash[:"#{resource_name}.signed_up"]
super
end

View file

@ -11,7 +11,7 @@
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Register" %></p>
<p><%= f.submit "Sign up" %></p>
<% end -%>
<%= render :partial => "shared/devise_links" %>

View file

@ -53,7 +53,7 @@ module Devise
ROUTES = [:session, :password, :confirmation, :registration, :unlock]
# Path names used in routes.
PATH_NAMES = [:sign_in, :sign_out, :password, :confirmation, :registration, :unlock]
PATH_NAMES = [:sign_in, :sign_out, :sign_up, :password, :confirmation, :unlock]
STRATEGIES = [:rememberable, :http_authenticatable, :token_authenticatable, :authenticatable]

View file

@ -118,7 +118,10 @@ module ActionController::Routing
end
def registerable(routes, mapping)
routes.resource :registration, :only => [:new, :create], :as => mapping.path_names[:registration]
routes.with_options(:controller => 'registrations', :name_prefix => nil) do |session|
session.send(:"new_#{mapping.name}_registration", mapping.path_names[:sign_up], :action => 'new', :conditions => { :method => :get })
session.send(:"#{mapping.name}_registration", mapping.path_names[:sign_up], :action => 'create', :conditions => { :method => :post })
end
end
end
end

View file

@ -6,7 +6,7 @@ module Devise
# Redirects to sign_in page if it's not authenticated
class Authenticatable < Base
def valid?
params[scope] && params[scope][:password].present? && mapping.to.respond_to?(:authenticate)
valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate)
end
# Authenticate a user based on email and password params, returning to warden
@ -19,6 +19,16 @@ module Devise
fail!(:invalid)
end
end
protected
def valid_controller?
params[:controller] == 'sessions'
end
def valid_params?
params[scope] && params[scope][:password].present?
end
end
end
end

View file

@ -11,7 +11,7 @@ class RegistrationTest < ActionController::IntegrationTest
fill_in 'email', :with => 'new_user@test.com'
fill_in 'password', :with => 'new_user123'
fill_in 'password confirmation', :with => 'new_user123'
click_button 'Register'
click_button 'Sign up'
assert_contain 'You have signed up successfully.'
assert warden.authenticated?(:admin)
@ -29,15 +29,23 @@ class RegistrationTest < ActionController::IntegrationTest
fill_in 'email', :with => 'new_user@test.com'
fill_in 'password', :with => 'new_user123'
fill_in 'password confirmation', :with => 'new_user123'
click_button 'Register'
click_button 'Sign up'
follow_redirect!
assert_equal true, @controller.send(:flash)[:"user.signed_up"]
assert_equal "You have signed up successfully.", @controller.send(:flash)[:notice]
# For some reason flash is not being set correctly, so instead of getting the
# "signed_up" message we get the unconfirmed one. Seems to be an issue with
# the internal redirect by the hook and the tests.
# follow_redirect!
# assert_contain 'You have signed up successfully.'
# assert_not_contain 'confirm your account'
assert_contain 'You have to confirm your account before continuing.'
assert_not warden.authenticated?(:user)
user = User.last
assert_equal user.email, 'new_user@test.com'
assert_not user.confirmed?
end
test 'a guest user cannot sign up with invalid information' do
@ -47,12 +55,31 @@ class RegistrationTest < ActionController::IntegrationTest
fill_in 'email', :with => 'invalid_email'
fill_in 'password', :with => 'new_user123'
fill_in 'password confirmation', :with => 'new_user321'
click_button 'Register'
click_button 'Sign up'
assert_template 'registrations/new'
assert_have_selector '#errorExplanation'
assert_contain "Email is invalid"
assert_contain "Password doesn't match confirmation"
assert_nil User.first
assert_not warden.authenticated?(:user)
end
test 'a guest should not sign up with email/password that already exists' do
user = create_user
visit new_user_session_path
click_link 'Sign up'
fill_in 'email', :with => 'user@test.com'
fill_in 'password', :with => '123456'
fill_in 'password confirmation', :with => '123456'
click_button 'Sign up'
assert_template 'registrations/new'
assert_contain 'Email has already been taken'
assert_not warden.authenticated?(:user)
end
end

View file

@ -63,22 +63,22 @@ class MappingTest < ActiveSupport::TestCase
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 '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]
assert_equal 'registration', mapping.path_names[:registration]
assert_equal 'unlock', mapping.path_names[:unlock]
assert_equal 'sign_up', mapping.path_names[:sign_up]
assert_equal 'unlock', mapping.path_names[:unlock]
end
test 'allow custom path names to be given' do
mapping = Devise.mappings[:manager]
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 '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]
assert_equal 'sign_up', mapping.path_names[:registration]
assert_equal 'unblock', mapping.path_names[:unlock]
assert_equal 'register', mapping.path_names[:sign_up]
assert_equal 'unblock', mapping.path_names[:unlock]
end
test 'has an empty path as default path prefix' do

View file

@ -7,9 +7,13 @@
<body>
<div id="container">
<%- flash.each do |name, msg| -%>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
<%- end -%>
<% if user_signed_in? -%>
<p>Hello User! You are signed in!</p>
<% end -%>
<%= yield %>
</div>
</body>

View file

@ -5,7 +5,7 @@ ActionController::Routing::Routes.draw do |map|
:class_name => "User", :requirements => { :extra => 'value' }, :path_names => {
:sign_in => 'login', :sign_out => 'logout',
:password => 'secret', :confirmation => 'verification',
:unlock => 'unblock', :registration => 'sign_up'
:unlock => 'unblock', :sign_up => 'register'
}
map.resources :users, :only => [:index], :member => { :expire => :get }

View file

@ -55,11 +55,11 @@ class MapRoutingTest < ActionController::TestCase
end
test 'map new user registration' do
assert_recognizes({:controller => 'registrations', :action => 'new'}, 'users/registration/new')
assert_recognizes({:controller => 'registrations', :action => 'new'}, 'users/sign_up')
end
test 'map create user registration' do
assert_recognizes({:controller => 'registrations', :action => 'create'}, {:path => 'users/registration', :method => :post})
assert_recognizes({:controller => 'registrations', :action => 'create'}, {:path => 'users/sign_up', :method => :post})
end
test 'map admin session with :as option' do
@ -93,6 +93,6 @@ class MapRoutingTest < ActionController::TestCase
end
test 'map account with custom path name for registration' do
assert_recognizes({:controller => 'registrations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/sign_up/new')
assert_recognizes({:controller => 'registrations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/register')
end
end