1# -*- coding: utf-8 -*-
2import webapp2
3from webapp2_extras import sessions
4from webapp2_extras import sessions_memcache
5
6import test_base
7
8
9app = webapp2.WSGIApplication(config={
10    'webapp2_extras.sessions': {
11        'secret_key': 'my-super-secret',
12    },
13})
14
15
16class TestMemcacheSession(test_base.BaseTestCase):
17    #factory = sessions_memcache.MemcacheSessionFactory
18
19    def test_get_save_session(self):
20
21        # Round 1 -------------------------------------------------------------
22
23        req = webapp2.Request.blank('/')
24        req.app = app
25        store = sessions.SessionStore(req)
26
27        session = store.get_session(backend='memcache')
28
29        rsp = webapp2.Response()
30        # Nothing changed, we want to test anyway.
31        store.save_sessions(rsp)
32
33        session['a'] = 'b'
34        session['c'] = 'd'
35        session['e'] = 'f'
36
37        store.save_sessions(rsp)
38
39        # Round 2 -------------------------------------------------------------
40
41        cookies = rsp.headers.get('Set-Cookie')
42        req = webapp2.Request.blank('/', headers=[('Cookie', cookies)])
43        req.app = app
44        store = sessions.SessionStore(req)
45
46        session = store.get_session(backend='memcache')
47        self.assertEqual(session['a'], 'b')
48        self.assertEqual(session['c'], 'd')
49        self.assertEqual(session['e'], 'f')
50
51        session['g'] = 'h'
52
53        rsp = webapp2.Response()
54        store.save_sessions(rsp)
55
56        # Round 3 -------------------------------------------------------------
57
58        cookies = rsp.headers.get('Set-Cookie')
59        req = webapp2.Request.blank('/', headers=[('Cookie', cookies)])
60        req.app = app
61        store = sessions.SessionStore(req)
62
63        session = store.get_session(backend='memcache')
64        self.assertEqual(session['a'], 'b')
65        self.assertEqual(session['c'], 'd')
66        self.assertEqual(session['e'], 'f')
67        self.assertEqual(session['g'], 'h')
68
69    def test_flashes(self):
70
71        # Round 1 -------------------------------------------------------------
72
73        req = webapp2.Request.blank('/')
74        req.app = app
75        store = sessions.SessionStore(req)
76
77        session = store.get_session(backend='memcache')
78        flashes = session.get_flashes()
79        self.assertEqual(flashes, [])
80        session.add_flash('foo')
81
82        rsp = webapp2.Response()
83        store.save_sessions(rsp)
84
85        # Round 2 -------------------------------------------------------------
86
87        cookies = rsp.headers.get('Set-Cookie')
88        req = webapp2.Request.blank('/', headers=[('Cookie', cookies)])
89        req.app = app
90        store = sessions.SessionStore(req)
91
92        session = store.get_session(backend='memcache')
93
94        flashes = session.get_flashes()
95        self.assertEqual(flashes, [(u'foo', None)])
96
97        flashes = session.get_flashes()
98        self.assertEqual(flashes, [])
99
100        session.add_flash('bar')
101        session.add_flash('baz', 'important')
102
103        rsp = webapp2.Response()
104        store.save_sessions(rsp)
105
106        # Round 3 -------------------------------------------------------------
107
108        cookies = rsp.headers.get('Set-Cookie')
109        req = webapp2.Request.blank('/', headers=[('Cookie', cookies)])
110        req.app = app
111        store = sessions.SessionStore(req)
112
113        session = store.get_session(backend='memcache')
114
115        flashes = session.get_flashes()
116        self.assertEqual(flashes, [(u'bar', None), (u'baz', 'important')])
117
118        flashes = session.get_flashes()
119        self.assertEqual(flashes, [])
120
121        rsp = webapp2.Response()
122        store.save_sessions(rsp)
123
124        # Round 4 -------------------------------------------------------------
125
126        cookies = rsp.headers.get('Set-Cookie')
127        req = webapp2.Request.blank('/', headers=[('Cookie', cookies)])
128        req.app = app
129        store = sessions.SessionStore(req)
130
131        session = store.get_session(backend='memcache')
132        flashes = session.get_flashes()
133        self.assertEqual(flashes, [])
134
135if __name__ == '__main__':
136    test_base.main()
137