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)
* Store session[:return_to] in session
* Add confirmable filters
* Use path_names in routes
* Create generators
* Allow stretches and pepper per model
* Allow multiple models per controller
* devise :authenticable, :confirmable, :recoverable
* Mailer subjects namespaced by model
* Devise::BruteForceProtection
* Devise::MagicColumns
@ -28,3 +27,7 @@
* ConfirmationsController
* 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>
<% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> |
<%= link_to "Forgot password?", new_password_path(resource_name) %>
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- 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>
<% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> |
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- 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>
<% end %>
<%= link_to "Sign in", new_session_path(resource_name) %> |
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<%= link_to "Sign in", new_session_path(resource_name) %><br />
<%- 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>
<% end -%>
<%= link_to "Forgot password?", new_password_path(resource_name) %> |
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %>
<%- if devise_mapping.allows?(:passwords) %>
<%= 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|
Devise.mappings.each do |resource, mapping|
map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |devise_map|
devise_map.resource :session, :only => [:new, :create, :destroy]
devise_map.resource :password, :only => [:new, :create, :edit, :update]
devise_map.resource :confirmation, :only => [:new, :create, :show]
Devise.mappings.each_value do |mapping|
map.namespace mapping.name, :namespace => nil, :path_prefix => mapping.as do |m|
if mapping.allows?(:sessions)
m.resource :session,
: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

View file

@ -2,6 +2,13 @@ module Devise
module Controllers
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
# The main accessor for the warden proxy instance

View file

@ -4,7 +4,7 @@ module Devise
def self.included(base)
base.class_eval do
helper_method :resource, :resource_name, :resource_class
helper_method :resource, :resource_name, :resource_class, :devise_mapping
end
end
@ -20,20 +20,20 @@ module Devise
devise_mapping.to
end
protected
protected
def devise_mapping
@devise_mapping ||= Devise.find_mapping_by_path(request.path)
end
def devise_mapping
@devise_mapping ||= Devise.find_mapping_by_path(request.path)
end
def resource=(new_resource)
instance_variable_set(:"@#{resource_name}", new_resource)
end
def resource=(new_resource)
instance_variable_set(:"@#{resource_name}", new_resource)
end
def set_flash_message(key, kind)
flash[key] = I18n.t(:"#{resource_name}.#{kind}",
:scope => [:devise, controller_name.to_sym], :default => kind)
end
def set_flash_message(key, kind)
flash[key] = I18n.t(:"#{resource_name}.#{kind}",
:scope => [:devise, controller_name.to_sym], :default => kind)
end
end
end

View file

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

View file

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

View file

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

View file

@ -49,4 +49,15 @@ class MapTest < ActiveSupport::TestCase
assert Devise.mappings[:participant].allows?(:confirmations)
assert_not Devise.mappings[:participant].allows?(:passwords)
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

View file

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

View file

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