Nuno Teixeira <tei...@gmail.com> added the comment:

Added tests for full coverage

__________________________________
Repoze Bugs <b...@bugs.repoze.org>
<http://bugs.repoze.org/issue155>
__________________________________
Index: repoze/who/plugins/cookie.py
===================================================================
--- repoze/who/plugins/cookie.py	(revision 9728)
+++ repoze/who/plugins/cookie.py	(working copy)
@@ -10,9 +10,10 @@
 
     implements(IIdentifier)
     
-    def __init__(self, cookie_name, cookie_path='/'):
+    def __init__(self, cookie_name, cookie_path='/', charset=None):
         self.cookie_name = cookie_name
         self.cookie_path = cookie_path
+        self.charset = charset
 
     # IIdentifier
     def identify(self, environ):
@@ -29,7 +30,11 @@
 
         try:
             login, password = auth.split(':', 1)
-            return {'login':login, 'password':password}
+            if self.charset is None:
+                return {'login':login, 'password':password}
+            else:
+                return {'login': login.decode(self.charset),
+                        'password': password.decode(self.charset)}
         except ValueError: # not enough values to unpack
             return None
 
@@ -44,6 +49,8 @@
     def remember(self, environ, identity):
         cookie_value = '%(login)s:%(password)s' % identity
         cookie_value = cookie_value.encode('base64').rstrip()
+        if self.charset:
+            cookie_value = cookie_value.encode(self.charset)
         cookies = get_cookies(environ)
         existing = cookies.get(self.cookie_name)
         value = getattr(existing, 'value', None)
@@ -57,7 +64,8 @@
         return '<%s %s>' % (self.__class__.__name__,
                             id(self)) #pragma NO COVERAGE
 
-def make_plugin(cookie_name='repoze.who.plugins.cookie', cookie_path='/'):
-    plugin = InsecureCookiePlugin(cookie_name, cookie_path)
+def make_plugin(cookie_name='repoze.who.plugins.cookie', cookie_path='/',
+                charset=None):
+    plugin = InsecureCookiePlugin(cookie_name, cookie_path, charset)
     return plugin
 
Index: repoze/who/plugins/tests/test_cookie.py
===================================================================
--- repoze/who/plugins/tests/test_cookie.py	(revision 9728)
+++ repoze/who/plugins/tests/test_cookie.py	(working copy)
@@ -49,6 +49,13 @@
         result = plugin.identify(environ)
         self.assertEqual(result, {'login':'foo', 'password':'password'})
 
+    def test_identify_encoded(self):
+        plugin = self._makeOne('oatmeal', charset='utf-8')
+        auth = 'foo:password'.encode('base64').rstrip()
+        environ = self._makeEnviron({'HTTP_COOKIE':'oatmeal=%s;' % auth})
+        result = plugin.identify(environ)
+        self.assertEqual(result, {'login':u'foo', 'password':u'password'})
+
     def test_remember_creds_same(self):
         plugin = self._makeOne('oatmeal')
         creds = {'login':'foo', 'password':'password'}
@@ -68,6 +75,15 @@
         expected = 'oatmeal=%s; Path=/;' % creds_auth
         self.assertEqual(result, [('Set-Cookie', expected)])
 
+    def test_remember_encoded(self):
+        plugin = self._makeOne('oatmeal', charset='utf-8')
+        creds = {'login':u'foo', 'password':u'password'}
+        auth = 'foo:password'.encode('base64').rstrip()
+        auth = 'oatmeal=%s;' % auth
+        environ = self._makeEnviron({'HTTP_COOKIE':auth})
+        result = plugin.remember(environ, creds)
+        self.assertEqual(result, None)
+
     def test_factory(self):
         from repoze.who.plugins.cookie import make_plugin
         plugin = make_plugin('foo')
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to