aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/authentication_test.rb
blob: 098d0596ff9fb98cc164cd673168450191a042f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require File.dirname(__FILE__) + '/../abstract_unit'

class AuthenticationTest < Test::Unit::TestCase
  class ApplicationController < ActionController::Base
    authentication :by => '@session[:authenticated]', :before => '@session[:return_to] = "/weblog/"', :failure => { :controller => "login" }
  end

  class WeblogController < ApplicationController
    def show()   render_text "I showed something"  end
    def index()  render_text "I indexed something" end
    def edit()   render_text "I edited something"  end
    def update() render_text "I updated something" end
    def login
      @session[:authenticated] = true
      @session[:return_to] ? redirect_to_path(@session[:return_to]) : render_nothing
    end
  end

  class AuthenticatesWeblogController < WeblogController
    authenticates :edit, :update
  end

  class AuthenticatesAllWeblogController < WeblogController
    authenticates_all
  end

  class AuthenticatesAllExceptWeblogController < WeblogController
    authenticates_all_except :show, :index, :login
  end

  class AuthenticatesSomeController < AuthenticatesAllWeblogController
    authenticates_all_except :show
  end

  def setup
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
  end

  def test_access_on_authenticates
    @controller = AuthenticatesWeblogController.new

    get :show
    assert_success

    get :edit
    assert_redirected_to :controller => "login"
  end

  def test_access_on_authenticates_all
    @controller = AuthenticatesAllWeblogController.new

    get :show
    assert_redirected_to :controller => "login"

    get :edit
    assert_redirected_to :controller => "login"
  end

  def test_access_on_authenticates_all_except
    @controller = AuthenticatesAllExceptWeblogController.new

    get :show
    assert_success

    get :edit
    assert_redirected_to :controller => "login"
  end
  
  def test_access_on_authenticates_some
    @controller = AuthenticatesSomeController.new

    get :show
    assert_success

    get :edit
    assert_redirected_to :controller => "login"
  end
  
  def test_authenticated_access_on_authenticates
    @controller = AuthenticatesWeblogController.new

    get :login
    assert_success

    get :show
    assert_success

    get :edit
    assert_success
  end
  
  def test_before_condition
    @controller = AuthenticatesWeblogController.new

    get :edit
    assert_redirected_to :controller => "login"
    
    get :login
    assert_redirect_url "http://test.host/weblog/"
  end
end