Browse Source

fix #737: handle unicodes within hash_email utility

Rafał Pitoń 8 years ago
parent
commit
f126f6f6bf
3 changed files with 22 additions and 4 deletions
  1. 12 0
      misago/users/tests/test_user_model.py
  2. 9 3
      misago/users/tests/test_utils.py
  3. 1 1
      misago/users/utils.py

+ 12 - 0
misago/users/tests/test_user_model.py

@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 from django.core.exceptions import ValidationError
 from django.test import TestCase
 
@@ -50,6 +51,17 @@ class UserManagerTests(TestCase):
         db_user = User.objects.get_by_username_or_email(user.email)
         self.assertEqual(user.pk, db_user.pk)
 
+    def test_getters_unicode_handling(self):
+        """get_by_ methods handle unicode"""
+        with self.assertRaises(User.DoesNotExist):
+            User.objects.get_by_username(u'łóć')
+
+        with self.assertRaises(User.DoesNotExist):
+            User.objects.get_by_email(u'łóć@polskimail.pl')
+
+        with self.assertRaises(User.DoesNotExist):
+            User.objects.get_by_username_or_email(u'łóć@polskimail.pl')
+
 
 class UserModelTests(TestCase):
     def test_set_username(self):

+ 9 - 3
misago/users/tests/test_utils.py

@@ -1,10 +1,16 @@
+# -*- coding: utf-8 -*-
 from django.test import TestCase
 
 from misago.users.utils import hash_email
 
 
-class UserModelTests(TestCase):
-    def test_hash_email_works(self):
-        """hash email produces repeatable outcomes"""
+class HashEmailTests(TestCase):
+    def test_is_case_insensitive(self):
+        """util is case insensitive"""
         self.assertEqual(hash_email('abc@test.com'),
                          hash_email('aBc@tEst.cOm'))
+
+    def test_handles_unicode(self):
+        """util works with unicode strings"""
+        self.assertEqual(hash_email(u'łóć@test.com'),
+                         hash_email(u'ŁÓĆ@tEst.cOm'))

+ 1 - 1
misago/users/utils.py

@@ -2,4 +2,4 @@ import hashlib
 
 
 def hash_email(email):
-    return hashlib.md5(email.lower().encode()).hexdigest()
+    return hashlib.md5(email.lower().encode('utf-8')).hexdigest()