Froze rails gems
[depot.git] / vendor / rails / actionpack / test / activerecord / active_record_store_test.rb
1 # These tests exercise CGI::Session::ActiveRecordStore, so you're going to
2 # need AR in a sibling directory to AP and have SQLite installed.
3 require 'active_record_unit'
4 require 'action_controller/session/active_record_store'
5
6 module CommonActiveRecordStoreTests
7 def test_basics
8 s = session_class.new(:session_id => '1234', :data => { 'foo' => 'bar' })
9 assert_equal 'bar', s.data['foo']
10 assert s.save
11 assert_equal 'bar', s.data['foo']
12
13 assert_not_nil t = session_class.find_by_session_id('1234')
14 assert_not_nil t.data
15 assert_equal 'bar', t.data['foo']
16 end
17
18 def test_reload_same_session
19 @new_session.update
20 reloaded = CGI::Session.new(CGI.new, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
21 assert_equal 'bar', reloaded['foo']
22 end
23
24 def test_tolerates_close_close
25 assert_nothing_raised do
26 @new_session.close
27 @new_session.close
28 end
29 end
30 end
31
32 class ActiveRecordStoreTest < ActiveRecordTestCase
33 include CommonActiveRecordStoreTests
34
35 def session_class
36 CGI::Session::ActiveRecordStore::Session
37 end
38
39 def session_id_column
40 "session_id"
41 end
42
43 def setup
44 session_class.create_table!
45
46 ENV['REQUEST_METHOD'] = 'GET'
47 ENV['REQUEST_URI'] = '/'
48 CGI::Session::ActiveRecordStore.session_class = session_class
49
50 @cgi = CGI.new
51 @new_session = CGI::Session.new(@cgi, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
52 @new_session['foo'] = 'bar'
53 end
54
55 # this test only applies for eager session saving
56 # def test_another_instance
57 # @another = CGI::Session.new(@cgi, 'session_id' => @new_session.session_id, 'database_manager' => CGI::Session::ActiveRecordStore)
58 # assert_equal @new_session.session_id, @another.session_id
59 # end
60
61 def test_model_attribute
62 assert_kind_of CGI::Session::ActiveRecordStore::Session, @new_session.model
63 assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
64 end
65
66 def test_save_unloaded_session
67 c = session_class.connection
68 bogus_class = c.quote(ActiveSupport::Base64.encode64("\004\010o:\vBlammo\000"))
69 c.insert("INSERT INTO #{session_class.table_name} ('#{session_id_column}', 'data') VALUES ('abcdefghijklmnop', #{bogus_class})")
70
71 sess = session_class.find_by_session_id('abcdefghijklmnop')
72 assert_not_nil sess
73 assert !sess.loaded?
74
75 # because the session is not loaded, the save should be a no-op. If it
76 # isn't, this'll try and unmarshall the bogus class, and should get an error.
77 assert_nothing_raised { sess.save }
78 end
79
80 def teardown
81 session_class.drop_table!
82 end
83 end
84
85 class ColumnLimitTest < ActiveRecordTestCase
86 def setup
87 @session_class = CGI::Session::ActiveRecordStore::Session
88 @session_class.create_table!
89 end
90
91 def teardown
92 @session_class.drop_table!
93 end
94
95 def test_protection_from_data_larger_than_column
96 # Can't test this unless there is a limit
97 return unless limit = @session_class.data_column_size_limit
98 too_big = ':(' * limit
99 s = @session_class.new(:session_id => '666', :data => {'foo' => too_big})
100 s.data
101 assert_raise(ActionController::SessionOverflowError) { s.save }
102 end
103 end
104
105 class DeprecatedActiveRecordStoreTest < ActiveRecordStoreTest
106 def session_id_column
107 "sessid"
108 end
109
110 def setup
111 session_class.connection.execute 'create table old_sessions (id integer primary key, sessid text unique, data text)'
112 session_class.table_name = 'old_sessions'
113 session_class.send :setup_sessid_compatibility!
114
115 ENV['REQUEST_METHOD'] = 'GET'
116 CGI::Session::ActiveRecordStore.session_class = session_class
117
118 @new_session = CGI::Session.new(CGI.new, 'database_manager' => CGI::Session::ActiveRecordStore, 'new_session' => true)
119 @new_session['foo'] = 'bar'
120 end
121
122 def teardown
123 session_class.connection.execute 'drop table old_sessions'
124 session_class.table_name = 'sessions'
125 end
126 end
127
128 class SqlBypassActiveRecordStoreTest < ActiveRecordStoreTest
129 def session_class
130 unless defined? @session_class
131 @session_class = CGI::Session::ActiveRecordStore::SqlBypass
132 @session_class.connection = CGI::Session::ActiveRecordStore::Session.connection
133 end
134 @session_class
135 end
136
137 def test_model_attribute
138 assert_kind_of CGI::Session::ActiveRecordStore::SqlBypass, @new_session.model
139 assert_equal({ 'foo' => 'bar' }, @new_session.model.data)
140 end
141 end