w31rd0 avatar w31rd0 committed 8a01857

Added tests for cookies

Comments (0)

Files changed (2)

beaker/session.py

             data_dir=self.data_dir, digest_filenames=False,
             **self.namespace_args)
         now = time.time()
-        self.request['set_cookie'] = True
+        if self.use_cookies:
+            self.request['set_cookie'] = True
 
         self.namespace.acquire_read_lock()
         timed_out = False
                 self.namespace['session'] = data
         finally:
             self.namespace.release_write_lock()
-        if self.is_new:
+        if self.use_cookies and self.is_new:
             self.request['set_cookie'] = True
 
     def revert(self):

tests/test_session.py

     assert u'Suomi' not in session
     assert u'Great Britain' not in session
     assert u'Deutchland' not in session
+
+
+def test_cookies_enabled():
+    """
+    Test if cookies are sent out properly when ``use_cookies``
+    is set to ``True``
+    """
+    session = get_session(use_cookies=True)
+    assert 'cookie_out' in session.request
+    assert session.request['set_cookie'] == False
+
+    session.domain = 'example.com'
+    session.path = '/example'
+    assert session.request['set_cookie'] == True
+    assert 'beaker.session.id=%s' % session.id in session.request['cookie_out']
+    assert 'Domain=example.com' in session.request['cookie_out']
+    assert 'Path=/' in session.request['cookie_out']
+
+    session = get_session(use_cookies=True)
+    session.save()
+    assert session.request['set_cookie'] == True
+    assert 'beaker.session.id=%s' % session.id in session.request['cookie_out']
+
+    session = get_session(use_cookies=True, id=session.id)
+    session.delete()
+    assert session.request['set_cookie'] == True
+    assert 'beaker.session.id=%s' % session.id in session.request['cookie_out']
+    assert 'expires=' in session.request['cookie_out']
+
+
+def test_cookies_disabled():
+    """
+    Test that no cookies are sent when ``use_cookies`` is set to ``False``
+    """
+    session = get_session(use_cookies=False)
+    assert 'set_cookie' not in session.request
+    assert 'cookie_out' not in session.request
+
+    session.save()
+    assert 'set_cookie' not in session.request
+    assert 'cookie_out' not in session.request
+
+    session = get_session(use_cookies=False, id=session.id)
+    assert 'set_cookie' not in session.request
+    assert 'cookie_out' not in session.request
+
+    session.delete()
+    assert 'set_cookie' not in session.request
+    assert 'cookie_out' not in session.request
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.