aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/session_management_test.rb
blob: 63917565effb5f9c9c92afb635a697880edcee83 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require File.dirname(__FILE__) + '/../abstract_unit'

class SessionManagementTest < Test::Unit::TestCase
  class SessionOffController < ActionController::Base
    session :off

    def show
      render_text "done"
    end

    def tell
      render_text "done"
    end
  end

  class TestController < ActionController::Base
    session :off, :only => :show
    session :session_secure => true, :except => :show
    session :off, :only => :conditional,
            :if => Proc.new { |r| r.parameters[:ws] }

    def show
      render_text "done"
    end

    def tell
      render_text "done"
    end

    def conditional
      render_text ">>>#{params[:ws]}<<<"
    end
  end

  class SpecializedController < SessionOffController
    session :disabled => false, :only => :something

    def something
      render_text "done"
    end

    def another
      render_text "done"
    end
  end

  class AssociationCachingTestController < ActionController::Base
    class ObjectWithAssociationCache
      def initialize
        @cached_associations = false
      end

      def fetch_associations
        @cached_associations = true
      end

      def clear_association_cache
        @cached_associations = false
      end

      def has_cached_associations?
        @cached_associations
      end
    end

    def show
      session[:object] = ObjectWithAssociationCache.new
      session[:object].fetch_associations
      if session[:object].has_cached_associations?
        render :text => "has cached associations"
      else
        render :text => "does not have cached associations"
      end
    end

    def tell
      if session[:object]
        if session[:object].has_cached_associations?
          render :text => "has cached associations"
        else
          render :text => "does not have cached associations"
        end
      else
        render :text => "there is no object"
      end
    end
  end


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

  def test_session_off_globally
    @controller = SessionOffController.new
    get :show
    assert_equal false, @request.session_options
    get :tell
    assert_equal false, @request.session_options
  end

  def test_session_off_conditionally
    @controller = TestController.new
    get :show
    assert_equal false, @request.session_options
    get :tell
    assert_instance_of Hash, @request.session_options
    assert @request.session_options[:session_secure]
  end

  def test_controller_specialization_overrides_settings
    @controller = SpecializedController.new
    get :something
    assert_instance_of Hash, @request.session_options
    get :another
    assert_equal false, @request.session_options
  end

  def test_session_off_with_if
    @controller = TestController.new
    get :conditional
    assert_instance_of Hash, @request.session_options
    get :conditional, :ws => "ws"
    assert_equal false, @request.session_options
  end
  
  def test_session_store_setting
    ActionController::Base.session_store = :drb_store
    assert_equal CGI::Session::DRbStore, ActionController::Base.session_store

    if Object.const_defined?(:ActiveRecord)
      ActionController::Base.session_store = :active_record_store
      assert_equal CGI::Session::ActiveRecordStore, ActionController::Base.session_store
    end
  end
  
  def test_process_cleanup_with_session_management_support
    @controller = AssociationCachingTestController.new
    get :show
    assert_equal "has cached associations", @response.body
    get :tell
    assert_equal "does not have cached associations", @response.body
  end
  
  def test_session_is_enabled
    @controller = TestController.new
    get :show
    assert_nothing_raised do
      assert_equal false, @controller.session_enabled?
    end
    
    get :tell
    assert @controller.session_enabled?
  end
end