From patchwork Fri May 6 17:55:01 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guilherme Salgado X-Patchwork-Id: 94419 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from ozlabs.org (localhost [IPv6:::1]) by ozlabs.org (Postfix) with ESMTP id 4BCC4100811 for ; Sat, 7 May 2011 03:55:10 +1000 (EST) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by ozlabs.org (Postfix) with ESMTP id 5F3B71007DB for ; Sat, 7 May 2011 03:55:08 +1000 (EST) Received: from youngberry.canonical.com ([91.189.89.112]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QIPF4-0001nH-BV; Fri, 06 May 2011 17:55:06 +0000 Received: from [187.126.147.188] (helo=feioso) by youngberry.canonical.com with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1QIPF3-0003bm-VX; Fri, 06 May 2011 17:55:06 +0000 Received: from localhost6.localdomain6 (localhost.localdomain [127.0.0.1]) by feioso (Postfix) with ESMTP id 8D5D34035D; Fri, 6 May 2011 14:55:01 -0300 (BRT) Subject: [PATCH] Make sure Person.email is really unique by lower casing it To: patchwork@lists.ozlabs.org From: Guilherme Salgado Date: Fri, 06 May 2011 14:55:01 -0300 Message-ID: <20110506175444.29392.98017.stgit@localhost6.localdomain6> User-Agent: StGit/0.15 MIME-Version: 1.0 Cc: patches@linaro.org X-BeenThere: patchwork@lists.ozlabs.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Patchwork development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: patchwork-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Currently it's possible to have two semantically identical email addresses in the DB by varying the case of one or more letters between them. This patch fixes it by changing Person.save() to always lower case the email address before it is inserted into the DB. --- apps/patchwork/models.py | 6 ++++++ apps/patchwork/tests/models.py | 13 +++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/patchwork/models.py b/apps/patchwork/models.py index cfead27..8c5188f 100644 --- a/apps/patchwork/models.py +++ b/apps/patchwork/models.py @@ -45,6 +45,12 @@ class Person(models.Model): name = models.CharField(max_length=255, null = True, blank = True) user = models.ForeignKey(User, null = True, blank = True) + def save(self): + # Convert to lower case to avoid identical emails with case variations + # from being inserted in the DB. + self.email = self.email.lower() + super(Person, self).save() + def __unicode__(self): if self.name: return u'%s <%s>' % (self.name, self.email) diff --git a/apps/patchwork/tests/models.py b/apps/patchwork/tests/models.py index 91bfb9d..bb26270 100644 --- a/apps/patchwork/tests/models.py +++ b/apps/patchwork/tests/models.py @@ -1,7 +1,8 @@ -from django.test import TestCase +from django.db.utils import IntegrityError +from django.test import TestCase, TransactionTestCase -from patchwork.models import State +from patchwork.models import Person, State from patchwork.tests.factory import factory @@ -40,3 +41,11 @@ class UserProfileTestCase(TestCase): # profile. patches = profile.submitted_patches_waiting_feedback(patch1.project) self.assertEquals([patch1, patch2], list(patches)) + + +class PersonTestCase(TestCase): + + def test_email_uniqueness_is_case_insensitive(self): + Person(email='foo.bar@example.com').save() + p2 = Person(email='Foo.Bar@example.com') + self.assertRaises(IntegrityError, p2.save)