Skip to content

Commit

Permalink
Add support for non-navigational formats in PasswordsController
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <[email protected]>
  • Loading branch information
sikachu authored and josevalim committed Mar 30, 2011
1 parent 210bc6a commit 97f0bac
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
17 changes: 11 additions & 6 deletions app/controllers/devise/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ def create
self.resource = resource_class.send_reset_password_instructions(params[resource_name])

if resource.errors.empty?
set_flash_message :notice, :send_instructions
redirect_to new_session_path(resource_name)
set_flash_message(:notice, :send_instructions) if is_navigational_format?
respond_with resource, :location => new_session_path(resource_name)
else
render_with_scope :new
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :new }
end
end
end

Expand All @@ -32,10 +34,13 @@ def update
self.resource = resource_class.reset_password_by_token(params[resource_name])

if resource.errors.empty?
set_flash_message :notice, :updated
sign_in_and_redirect(resource_name, resource)
set_flash_message(:notice, :updated) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
render_with_scope :edit
respond_with(resource) do |format|
format.any(*navigational_formats) { render_with_scope :edit }
end
end
end
end
37 changes: 37 additions & 0 deletions test/integration/recoverable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,41 @@ def reset_password(options={}, &block)
assert !warden.authenticated?(:user)
end

test 'reset password request with valid E-Mail in XML format should return valid response' do
create_user
post user_password_path(:format => 'xml'), :user => {:email => "[email protected]"}
assert_response :success
assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<user>)
end

test 'reset password request with invalid E-Mail in XML format should return valid response' do
create_user
post user_password_path(:format => 'xml'), :user => {:email => "[email protected]"}
assert_response :unprocessable_entity
assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
end

test 'change password with valid parameters in XML format should return valid response' do
user = create_user
request_forgot_password
put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '987654321', :password_confirmation => '987654321'}
assert_response :success
assert warden.authenticated?(:user)
end

test 'change password with invalid token in XML format should return invalid response' do
user = create_user
request_forgot_password
put user_password_path(:format => 'xml'), :user => {:reset_password_token => 'invalid.token', :password => '987654321', :password_confirmation => '987654321'}
assert_response :unprocessable_entity
assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
end

test 'change password with invalid new password in XML format should return invalid response' do
user = create_user
request_forgot_password
put user_password_path(:format => 'xml'), :user => {:reset_password_token => user.reload.reset_password_token, :password => '', :password_confirmation => '987654321'}
assert_response :unprocessable_entity
assert response.body.include? %(<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>)
end
end

0 comments on commit 97f0bac

Please sign in to comment.