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

More specs.

This commit is contained in:
José Valim 2009-10-12 09:56:12 -03:00
parent 78525e2536
commit e50dfcc73f
14 changed files with 129 additions and 83 deletions

15
TODO
View file

@ -1,15 +1,14 @@
* Add mappings
Devise.map :users, :to => User, :for => [:authenticable, :recoverable, :confirmable], :as => :usuario
* Add remember me (with customizable time frame) * Add remember me (with customizable time frame)
* Store session[:return_to] in session * Store session[:return_to] in session
* Add confirmable filters
* Use path_names in routes
* Create generators * Create generators
* Allow stretches and pepper per model * Allow stretches and pepper per model
* Allow multiple models per controller * Mailer subjects namespaced by model
* devise :authenticable, :confirmable, :recoverable
* Devise::BruteForceProtection * Devise::BruteForceProtection
* Devise::MagicColumns * Devise::MagicColumns
@ -28,3 +27,7 @@
* ConfirmationsController * ConfirmationsController
* Create an example app * Create an example app
* devise :authenticable, :confirmable, :recoverable
* Allow multiple models per controller
* Add mappings

View file

@ -9,5 +9,8 @@
<p><%= f.submit "Resend confirmation instructions" %></p> <p><%= f.submit "Resend confirmation instructions" %></p>
<% end %> <% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> | <%= link_to "Sign in", new_session_path(resource_name) %><br />
<%= link_to "Forgot password?", new_password_path(resource_name) %>
<%- if devise_mapping.allows?(:passwords) %>
<%= link_to "Forgot password?", new_password_path(resource_name) %><br />
<% end -%>

View file

@ -13,5 +13,8 @@
<p><%= f.submit "Change my password" %></p> <p><%= f.submit "Change my password" %></p>
<% end %> <% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> | <%= link_to "Sign in", new_session_path(resource_name) %><br />
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<%- if devise_mapping.allows?(:confirmations) %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View file

@ -9,5 +9,8 @@
<p><%= f.submit "Send me reset password instructions" %></p> <p><%= f.submit "Send me reset password instructions" %></p>
<% end %> <% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> | <%= link_to "Sign in", new_session_path(resource_name) %><br />
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<%- if devise_mapping.allows?(:confirmations) %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View file

@ -10,5 +10,10 @@
<p><%= f.submit "Sign in" %></p> <p><%= f.submit "Sign in" %></p>
<% end -%> <% end -%>
<%= link_to "Forgot password?", new_password_path(resource_name) %> | <%- if devise_mapping.allows?(:passwords) %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %> <%= link_to "Forgot password?", new_password_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.allows?(:confirmations) %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>

View file

@ -1,9 +1,20 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
Devise.mappings.each do |resource, mapping| Devise.mappings.each_value do |mapping|
map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |devise_map| map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m|
devise_map.resource :session, :only => [:new, :create, :destroy] if mapping.allows?(:sessions)
devise_map.resource :password, :only => [:new, :create, :edit, :update] m.resource :session,
devise_map.resource :confirmation, :only => [:new, :create, :show] :only => [:new, :create, :destroy]
end
if mapping.allows?(:passwords)
m.resource :password,
:only => [:new, :create, :edit, :update]
end
if mapping.allows?(:confirmations)
m.resource :confirmation,
:only => [:new, :create, :show]
end
end end
end end
end end

View file

@ -2,6 +2,13 @@ module Devise
module Controllers module Controllers
module Filters module Filters
def self.included(base)
base.class_eval do
helper_method :warden, :signed_in?, :authenticated?,
*Devise.mappings.keys.map { |m| :"current_#{m}" }
end
end
protected protected
# The main accessor for the warden proxy instance # The main accessor for the warden proxy instance

View file

@ -4,7 +4,7 @@ module Devise
def self.included(base) def self.included(base)
base.class_eval do base.class_eval do
helper_method :resource, :resource_name, :resource_class helper_method :resource, :resource_name, :resource_class, :devise_mapping
end end
end end

View file

@ -98,15 +98,13 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not warden.authenticated?(:admin) assert_not warden.authenticated?(:admin)
end end
test 'already confirmed admin should be able to sign in successfully' do test 'authenticated admin should not be able to sign as admin again' do
sign_in_as_admin sign_in_as_admin
get new_admin_session_path
assert_response :success assert_response :redirect
assert_template 'home/index' assert_redirected_to root_path
assert_contain 'Signed in successfully'
assert_not_contain 'Sign In'
assert warden.authenticated?(:admin) assert warden.authenticated?(:admin)
assert_not warden.authenticated?(:user)
end end
test 'authenticated admin should be able to sign out' do test 'authenticated admin should be able to sign out' do
@ -131,7 +129,7 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_not_contain 'Signed out successfully' assert_not_contain 'Signed out successfully'
end end
test 'redirect from warden show error message' do test 'redirect with warden show error message' do
get admins_path get admins_path
warden_path = new_admin_session_path(:unauthenticated => true) warden_path = new_admin_session_path(:unauthenticated => true)
@ -142,7 +140,7 @@ class AuthenticationTest < ActionController::IntegrationTest
end end
test 'render 404 on roles without permission' do test 'render 404 on roles without permission' do
get "users/password/new" get "admin_area/password/new"
assert_response :not_found assert_response :not_found
assert_not_contain 'Send me reset password instructions' assert_not_contain 'Send me reset password instructions'
end end

View file

@ -1,15 +1,15 @@
require 'test/test_helper' require 'test/test_helper'
class AdminsConfirmationTest < ActionController::IntegrationTest class UsersConfirmationTest < ActionController::IntegrationTest
test 'admin should be able to request a new confirmation' do test 'user should be able to request a new confirmation' do
admin = create_admin user = create_user
ActionMailer::Base.deliveries.clear ActionMailer::Base.deliveries.clear
visit new_admin_session_path visit new_user_session_path
click_link 'Didn\'t receive confirmation instructions?' click_link 'Didn\'t receive confirmation instructions?'
fill_in 'email', :with => admin.email fill_in 'email', :with => user.email
click_button 'Resend confirmation instructions' click_button 'Resend confirmation instructions'
assert_template 'sessions/new' assert_template 'sessions/new'
@ -17,7 +17,7 @@ class AdminsConfirmationTest < ActionController::IntegrationTest
assert_equal 1, ActionMailer::Base.deliveries.size assert_equal 1, ActionMailer::Base.deliveries.size
end end
test 'admin with invalid perishable token should not be able to confirm an account' do test 'user with invalid perishable token should not be able to confirm an account' do
visit user_confirmation_path(:perishable_token => 'invalid_perishable') visit user_confirmation_path(:perishable_token => 'invalid_perishable')
assert_response :success assert_response :success
@ -26,21 +26,21 @@ class AdminsConfirmationTest < ActionController::IntegrationTest
assert_contain 'invalid confirmation' assert_contain 'invalid confirmation'
end end
test 'admin with valid perishable token should be able to confirm an account' do test 'user with valid perishable token should be able to confirm an account' do
admin = create_admin(:confirm => false) user = create_user(:confirm => false)
assert_not admin.confirmed? assert_not user.confirmed?
visit admin_confirmation_path(:perishable_token => admin.perishable_token) visit user_confirmation_path(:perishable_token => user.perishable_token)
assert_template 'sessions/new' assert_template 'sessions/new'
assert_contain 'Your account was successfully confirmed!' assert_contain 'Your account was successfully confirmed!'
assert admin.reload.confirmed? assert user.reload.confirmed?
end end
test 'admin already confirmed user should not be able to confirm the account again' do test 'user already confirmed user should not be able to confirm the account again' do
admin = create_admin user = create_user
visit admin_confirmation_path(:perishable_token => admin.perishable_token) visit user_confirmation_path(:perishable_token => user.perishable_token)
assert_template 'confirmations/new' assert_template 'confirmations/new'
assert_have_selector '#errorExplanation' assert_have_selector '#errorExplanation'

View file

@ -1,9 +1,9 @@
require 'test/test_helper' require 'test/test_helper'
class AdminsPasswordRecoveryTest < ActionController::IntegrationTest class UsersPasswordRecoveryTest < ActionController::IntegrationTest
def visit_new_password_path def visit_new_password_path
visit new_admin_session_path visit new_user_session_path
click_link 'Forgot password?' click_link 'Forgot password?'
end end
@ -12,15 +12,15 @@ class AdminsPasswordRecoveryTest < ActionController::IntegrationTest
assert_response :success assert_response :success
assert_template 'passwords/new' assert_template 'passwords/new'
assert_not warden.authenticated?(:admin) assert_not warden.authenticated?(:user)
fill_in 'email', :with => 'admin@test.com' fill_in 'email', :with => 'user@test.com'
yield if block_given? yield if block_given?
click_button 'Send me reset password instructions' click_button 'Send me reset password instructions'
end end
def reset_password(options={}, &block) def reset_password(options={}, &block)
visit edit_admin_password_path(:perishable_token => options[:perishable_token]) visit edit_user_password_path(:perishable_token => options[:perishable_token])
assert_response :success assert_response :success
assert_template 'passwords/edit' assert_template 'passwords/edit'
@ -30,25 +30,25 @@ class AdminsPasswordRecoveryTest < ActionController::IntegrationTest
click_button 'Change my password' click_button 'Change my password'
end end
test 'authenticated admin should not be able to visit forgot password page' do test 'authenticated user should not be able to visit forgot password page' do
sign_in_as_admin sign_in_as_user
assert warden.authenticated?(:admin) assert warden.authenticated?(:user)
get new_admin_password_path get new_user_password_path
assert_response :redirect assert_response :redirect
assert_redirected_to root_path assert_redirected_to root_path
end end
test 'not authenticated admin should be able to request a forgot password' do test 'not authenticated user should be able to request a forgot password' do
create_admin create_user
request_forgot_password request_forgot_password
assert_template 'sessions/new' assert_template 'sessions/new'
assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.' assert_contain 'You will receive an email with instructions about how to reset your password in a few minutes.'
end end
test 'not authenticated admin with invalid email should receive an error message' do test 'not authenticated user with invalid email should receive an error message' do
request_forgot_password do request_forgot_password do
fill_in 'email', :with => 'invalid.test@test.com' fill_in 'email', :with => 'invalid.test@test.com'
end end
@ -59,30 +59,30 @@ class AdminsPasswordRecoveryTest < ActionController::IntegrationTest
assert_contain 'Email not found' assert_contain 'Email not found'
end end
test 'authenticated admin should not be able to visit edit password page' do test 'authenticated user should not be able to visit edit password page' do
sign_in_as_admin sign_in_as_user
get edit_admin_password_path get edit_user_password_path
assert_response :redirect assert_response :redirect
assert_redirected_to root_path assert_redirected_to root_path
assert warden.authenticated?(:admin) assert warden.authenticated?(:user)
end end
test 'not authenticated admin with invalid perishable token should not be able to change his password' do test 'not authenticated user with invalid perishable token should not be able to change his password' do
admin = create_admin user = create_user
reset_password :perishable_token => 'invalid_perishable' reset_password :perishable_token => 'invalid_perishable'
assert_response :success assert_response :success
assert_template 'passwords/edit' assert_template 'passwords/edit'
assert_have_selector '#errorExplanation' assert_have_selector '#errorExplanation'
assert_contain 'invalid confirmation' assert_contain 'invalid confirmation'
assert_not admin.reload.valid_password?('987654321') assert_not user.reload.valid_password?('987654321')
end end
test 'not authenticated admin with valid perisable token but invalid password should not be able to change his password' do test 'not authenticated user with valid perisable token but invalid password should not be able to change his password' do
admin = create_admin user = create_user
reset_password :perishable_token => admin.perishable_token do reset_password :perishable_token => user.perishable_token do
fill_in 'Password confirmation', :with => 'other_password' fill_in 'Password confirmation', :with => 'other_password'
end end
@ -90,15 +90,15 @@ class AdminsPasswordRecoveryTest < ActionController::IntegrationTest
assert_template 'passwords/edit' assert_template 'passwords/edit'
assert_have_selector '#errorExplanation' assert_have_selector '#errorExplanation'
assert_contain 'Password doesn\'t match confirmation' assert_contain 'Password doesn\'t match confirmation'
assert_not admin.reload.valid_password?('987654321') assert_not user.reload.valid_password?('987654321')
end end
test 'not authenticated admin with valid data should be able to change his password' do test 'not authenticated user with valid data should be able to change his password' do
admin = create_admin user = create_user
reset_password :perishable_token => admin.perishable_token reset_password :perishable_token => user.perishable_token
assert_template 'sessions/new' assert_template 'sessions/new'
assert_contain 'Your password was changed successfully.' assert_contain 'Your password was changed successfully.'
assert admin.reload.valid_password?('987654321') assert user.reload.valid_password?('987654321')
end end
end end

View file

@ -49,4 +49,15 @@ class MapTest < ActiveSupport::TestCase
assert Devise.mappings[:participant].allows?(:confirmations) assert Devise.mappings[:participant].allows?(:confirmations)
assert_not Devise.mappings[:participant].allows?(:passwords) assert_not Devise.mappings[:participant].allows?(:passwords)
end end
test 'return mapping by path' do
Devise.map :participant, :for => [:authenticable, :confirmable]
assert_equal Devise.mappings[:participant], Devise.find_mapping_by_path("/participants/session")
assert_nil Devise.find_mapping_by_path("/foo/bar")
end
test 'return mapping by customized path' do
Devise.map :participant, :for => [:authenticable, :confirmable], :as => "participantes"
assert_equal Devise.mappings[:participant], Devise.find_mapping_by_path("/participantes/session")
end
end end

View file

@ -1,2 +1,2 @@
Devise.map :user, :for => [:authenticable, :confirmable, :validatable] Devise.map :user, :for => [:authenticable, :recoverable, :confirmable, :validatable]
Devise.map :admin, :for => [:authenticable, :recoverable, :confirmable, :validatable], :as => 'admin_area' Devise.map :admin, :for => [:authenticable, :confirmable, :validatable], :as => 'admin_area'

View file

@ -23,6 +23,8 @@ class MapRoutingTest < ActionController::TestCase
end end
test 'map devise admin password with :as option' do test 'map devise admin password with :as option' do
assert_raise ActionController::RoutingError do
assert_recognizes({:controller => 'passwords', :action => 'new'}, 'admin_area/password/new') assert_recognizes({:controller => 'passwords', :action => 'new'}, 'admin_area/password/new')
end end
end end
end