class TestDeleteCookie(unittest.TestCase): """ Test the logic of deleting a cookie. """ def setUp(self): from weblayer.cookie import SignedSecureCookieWrapper self.request = Mock() self.response = Mock() self.settings = {'cookie_secret': ''} self.cookie_wrapper = SignedSecureCookieWrapper( self.request, self.response, self.settings) def test_delete(self): """ Calls `self.context.response.set_cookie` with `expires=datetime.timedelta(days=-5)`. """ self.cookie_wrapper.delete('name') args = self.response.set_cookie.call_args[0] kwargs = self.response.set_cookie.call_args[1] self.assertTrue(args[0] == 'name') self.assertTrue(kwargs['expires'] == timedelta(days=-5)) def test_delete_defaults(self): """ `path` defaults to '/' and domain defaults to `None`. """ self.cookie_wrapper.delete('name') kwargs = self.response.set_cookie.call_args[1] self.assertTrue(kwargs['path'] == '/') self.assertTrue(kwargs['domain'] is None) self.cookie_wrapper.delete('name', path='/foo', domain='bar') kwargs = self.response.set_cookie.call_args[1] self.assertTrue(kwargs['path'] == '/foo') self.assertTrue(kwargs['domain'] == 'bar')
class TestGetCookie(unittest.TestCase): """ Test the logic of getting a secure cookie. """ def setUp(self): from weblayer.cookie import SignedSecureCookieWrapper self.request = Mock() self.response = Mock() self.settings = {'cookie_secret': ''} self.cookie_wrapper = SignedSecureCookieWrapper( self.request, self.response, self.settings) def test_get_name(self): """ Calling `get('name')` tries to get the value from `self.context.request.cookies`. """ self.request.cookies.get.return_value = None self.cookie_wrapper.get('name') self.request.cookies.get.assert_called_with('name', None) def test_not_present(self): """ If the cookie doesn't exist, returns `None`. """ self.request.cookies.get.return_value = None self.assertTrue(self.cookie_wrapper.get('name') is None) def test_split_value(self): """ If the cookie value doesn't split into three parts, delimited by '|' returns `None`. """ value = Mock() value.__len__ = Mock() value.split.return_value = ['a', 'b'] result = self.cookie_wrapper.get('name', value=value) value.split.assert_called_with("|") self.assertTrue(result is None) def test_timestamp_expired(self): """ If the timestamp is more than 31 days old, returns `None`. """ from weblayer.cookie import _generate_cookie_signature t = time.time() too_old = str(int(t - 32 * 24 * 60 * 60)) cs = self.settings['cookie_secret'] sig = _generate_cookie_signature(cs, 'name', 'dmFsdWU=', too_old) value = 'dmFsdWU=|%s|%s' % (too_old, sig) result = self.cookie_wrapper.get('name', value=value) self.assertTrue(result is None) def test_signature_doesnt_match(self): """ If the signature doesn't match, returns `None`. """ t = time.time() ts = str(int(t)) value = 'dmFsdWU=|%s|%s' % (ts, 'not the right sig') result = self.cookie_wrapper.get('name', value=value) self.assertTrue(result is None) def test_value_is_base64_decodable(self): """ If the signature matches, the value comes back run through `base64.b64decode`. If it can't be decoded, it raises a TypeError. """ from weblayer.cookie import _generate_cookie_signature t = time.time() ts = str(int(t)) cs = self.settings['cookie_secret'] sig = _generate_cookie_signature(cs, 'name', 'a', ts) value = 'a|%s|%s' % (ts, sig) result = self.cookie_wrapper.get('name', value=value) self.assertTrue(result is None) def test_get_value(self): """ If the signature matches, the value comes back run through `base64.b64decode`. """ from weblayer.cookie import _generate_cookie_signature t = time.time() ts = str(int(t)) cs = self.settings['cookie_secret'] sig = _generate_cookie_signature(cs, 'name', 'dmFsdWU=', ts) value = 'dmFsdWU=|%s|%s' % (ts, sig) result = self.cookie_wrapper.get('name', value=value) self.assertTrue(result == 'value') def test_delete(self): """ Calls `self.context.response.set_cookie` with `expires=datetime.timedelta(days=-5)`. """ self.cookie_wrapper.delete('name') args = self.response.set_cookie.call_args[0] kwargs = self.response.set_cookie.call_args[1] self.assertTrue(args[0] == 'name') self.assertTrue(kwargs['expires'] == timedelta(days=-5)) def test_delete_path_domain_defaults(self): """ `path` defaults to '/' and domain defaults to `None`. """ self.cookie_wrapper.delete('name') kwargs = self.response.set_cookie.call_args[1] self.assertTrue(kwargs['path'] == '/') self.assertTrue(kwargs['domain'] is None) self.cookie_wrapper.delete('name', path='/foo', domain='bar') kwargs = self.response.set_cookie.call_args[1] self.assertTrue(kwargs['path'] == '/foo') self.assertTrue(kwargs['domain'] == 'bar')