summaryrefslogtreecommitdiff
path: root/devel
diff options
context:
space:
mode:
Diffstat (limited to 'devel')
-rw-r--r--devel/tests.py45
-rw-r--r--devel/utils.py26
2 files changed, 62 insertions, 9 deletions
diff --git a/devel/tests.py b/devel/tests.py
index 604da74c..33b02582 100644
--- a/devel/tests.py
+++ b/devel/tests.py
@@ -1,8 +1,10 @@
from django.test import TestCase
+from django.contrib.auth.models import User
+from devel.utils import find_user
+from main.models import UserProfile
class DevelTest(TestCase):
-
def test_index(self):
response = self.client.get('/devel/')
self.assertEqual(response.status_code, 302)
@@ -27,3 +29,44 @@ class DevelTest(TestCase):
def test_mirrors(self):
response = self.client.get('/mirrors/')
self.assertEqual(response.status_code, 200)
+
+class FindUserTest(TestCase):
+
+ def setUp(self):
+ self.user1 = User.objects.create(username="joeuser", first_name="Joe",
+ last_name="User", email="user1@example.com")
+ self.user2 = User.objects.create(username="john", first_name="John",
+ last_name="", email="user2@example.com")
+ self.user3 = User.objects.create(username="bjones", first_name="Bob",
+ last_name="Jones", email="user3@example.com")
+
+ for user in (self.user1, self.user2, self.user3):
+ email_addr = "%s@awesome.com" % user.username
+ UserProfile.objects.create(user=user, public_email=email_addr)
+
+ def test_not_matching(self):
+ self.assertIsNone(find_user(None))
+ self.assertIsNone(find_user(""))
+ self.assertIsNone(find_user("Bogus"))
+ self.assertIsNone(find_user("Bogus <invalid"))
+ self.assertIsNone(find_user("Bogus User <bogus@example.com>"))
+ self.assertIsNone(find_user("<bogus@example.com>"))
+ self.assertIsNone(find_user("bogus@example.com"))
+ self.assertIsNone(find_user("Unknown Packager"))
+
+ def test_by_email(self):
+ self.assertEqual(self.user1, find_user("XXX YYY <user1@example.com>"))
+ self.assertEqual(self.user2, find_user("YYY ZZZ <user2@example.com>"))
+
+ def test_by_profile_email(self):
+ self.assertEqual(self.user1, find_user("XXX <joeuser@awesome.com>"))
+ self.assertEqual(self.user2, find_user("YYY <john@awesome.com>"))
+ self.assertEqual(self.user3, find_user("ZZZ <bjones@awesome.com>"))
+
+ def test_by_name(self):
+ self.assertEqual(self.user1, find_user("Joe User <joe@differentdomain.com>"))
+ self.assertEqual(self.user1, find_user("Joe User"))
+ self.assertEqual(self.user2, find_user("John <john@differentdomain.com>"))
+ self.assertEqual(self.user3, find_user("Bob Jones <bjones AT Arch Linux DOT org>"))
+
+# vim: set ts=4 sw=4 et:
diff --git a/devel/utils.py b/devel/utils.py
index 9d7dfb2d..acdda959 100644
--- a/devel/utils.py
+++ b/devel/utils.py
@@ -51,24 +51,32 @@ def find_user(userstring):
We start by searching for a matching email address; we then move onto
matching by first/last name. If we cannot find a user, then return None.
'''
+ if not userstring:
+ return None
if userstring in find_user.cache:
return find_user.cache[userstring]
matches = re.match(r'^([^<]+)? ?<([^>]*)>', userstring)
if not matches:
- return None
-
- user = None
- name = matches.group(1)
- email = matches.group(2)
+ name = userstring
+ email = None
+ else:
+ name = matches.group(1)
+ email = matches.group(2)
def user_email():
- return User.objects.get(email=email)
+ if email:
+ return User.objects.get(email=email)
+ return None
def profile_email():
- return User.objects.get(userprofile__public_email=email)
+ if email:
+ return User.objects.get(userprofile__public_email=email)
+ return None
def user_name():
# yes, a bit odd but this is the easiest way since we can't always be
# sure how to split the name. Ensure every 'token' appears in at least
# one of the two name fields.
+ if not name:
+ return None
name_q = Q()
for token in name.split():
# ignore quoted parts; e.g. nicknames in strings
@@ -78,10 +86,12 @@ def find_user(userstring):
Q(last_name__icontains=token))
return User.objects.get(name_q)
+ user = None
for matcher in (user_email, profile_email, user_name):
try:
user = matcher()
- break
+ if user != None:
+ break
except (User.DoesNotExist, User.MultipleObjectsReturned):
pass