From 37c55eb192ca156ecccbcc1ec095d89f820383d2 Mon Sep 17 00:00:00 2001 From: Chris Oliver Date: Fri, 15 Jun 2012 13:06:29 -0500 Subject: [PATCH] Added tests for flexible routing constraints --- test/integration/authenticatable_test.rb | 44 +++++++++++++++++++ test/rails_app/config/routes.rb | 8 ++++ .../migrate/20100401102949_create_tables.rb | 3 ++ test/support/integration.rb | 3 +- 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/test/integration/authenticatable_test.rb b/test/integration/authenticatable_test.rb index fdfe0b1e..650afbd0 100644 --- a/test/integration/authenticatable_test.rb +++ b/test/integration/authenticatable_test.rb @@ -161,6 +161,28 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest assert_contain 'Private!' end + test 'signed in as inactive admin should not be able to access private/active route restricted to active admins (authenticate denied)' do + sign_in_as_admin(:active => false) + assert warden.authenticated?(:admin) + assert_not warden.authenticated?(:user) + + assert_raises ActionController::RoutingError do + get "/private/active" + end + end + + test 'signed in as active admin should be able to access private/active route restricted to active admins (authenticate accepted)' do + sign_in_as_admin(:active => true) + assert warden.authenticated?(:admin) + assert_not warden.authenticated?(:user) + + get private_active_path + + assert_response :success + assert_template 'home/private' + assert_contain 'Private!' + end + test 'signed in as admin should get admin dashboard (authenticated accepted)' do sign_in_as_admin assert warden.authenticated?(:admin) @@ -191,6 +213,28 @@ class AuthenticationRoutesRestrictions < ActionController::IntegrationTest end end + test 'signed in as inactive admin should not be able to access dashboard/active route restricted to active admins (authenticated denied)' do + sign_in_as_admin(:active => false) + assert warden.authenticated?(:admin) + assert_not warden.authenticated?(:user) + + assert_raises ActionController::RoutingError do + get "/dashboard/active" + end + end + + test 'signed in as active admin should be able to access dashboard/active route restricted to active admins (authenticated accepted)' do + sign_in_as_admin(:active => true) + assert warden.authenticated?(:admin) + assert_not warden.authenticated?(:user) + + get dashboard_active_path + + assert_response :success + assert_template 'home/admin_dashboard' + assert_contain 'Admin dashboard' + end + test 'signed in user should not see unauthenticated page (unauthenticated denied)' do sign_in_as_user assert warden.authenticated?(:user) diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index ec0b9895..48cc899f 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -30,10 +30,18 @@ Rails.application.routes.draw do match "/private", :to => "home#private", :as => :private end + authenticate(:admin, lambda { |admin| admin.active? }) do + match "/private/active", :to => "home#private", :as => :private_active + end + authenticated :admin do match "/dashboard", :to => "home#admin_dashboard" end + authenticated :admin, lambda { |admin| admin.active? } do + match "/dashboard/active", :to => "home#admin_dashboard" + end + authenticated do match "/dashboard", :to => "home#user_dashboard" end diff --git a/test/rails_app/db/migrate/20100401102949_create_tables.rb b/test/rails_app/db/migrate/20100401102949_create_tables.rb index c7d7210b..85e3000b 100644 --- a/test/rails_app/db/migrate/20100401102949_create_tables.rb +++ b/test/rails_app/db/migrate/20100401102949_create_tables.rb @@ -60,6 +60,9 @@ class CreateTables < ActiveRecord::Migration ## Lockable t.datetime :locked_at + ## Attribute for testing route blocks + t.boolean :active, :default => false + t.timestamps end end diff --git a/test/support/integration.rb b/test/support/integration.rb index 44db4b7c..dadd5835 100644 --- a/test/support/integration.rb +++ b/test/support/integration.rb @@ -24,7 +24,8 @@ class ActionDispatch::IntegrationTest @admin ||= begin admin = Admin.create!( :email => options[:email] || 'admin@test.com', - :password => '123456', :password_confirmation => '123456' + :password => '123456', :password_confirmation => '123456', + :active => options[:active] ) admin.confirm! unless options[:confirm] == false admin