1
# Copyright 2010 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
from django.utils.translation import ugettext as _
6
from identityprovider.tests.utils import (BasicAccountTestCase,
7
LPAccountTestCase, SQLCachedTestCase)
8
from identityprovider.models.person import Person
9
from identityprovider.models.emailaddress import EmailAddress
10
from identityprovider.utils import (canonical_url,
11
get_person_and_account_by_email, password_policy_compliant,
12
polite_form_errors, validate_launchpad_password,
13
PersonAndAccountNotFoundException)
16
class CanonicalUrlTestCase(SQLCachedTestCase):
18
def test_when_object_is_not_person(self):
19
self.assertTrue(canonical_url(None) is None)
21
def test_when_view_name_is_none(self):
22
url = canonical_url(Person(name="test"), view_name=None)
23
self.assertEquals(url, "https://launchpad.net/~test")
25
def test_when_view_name_is_not_none(self):
26
url = canonical_url(Person(name="test"), view_name="TEST")
27
self.assertEquals(url, "https://launchpad.net/~test/TEST")
30
class GetPersonAndAccountByEmailTestCase(BasicAccountTestCase):
34
def test_account_and_person_is_none(self):
35
email = EmailAddress.objects.get(email__iexact="mark@example.com")
40
self.assertRaises(PersonAndAccountNotFoundException,
41
get_person_and_account_by_email,
45
class LPGetPersonAndAccountByEmailTestCase(LPAccountTestCase):
46
def test_person_does_not_exist(self):
47
# create email address with broken foreign key
48
email = EmailAddress.objects.get(email__iexact='mark@example.com')
49
person_id = email.person_id
54
person, account = get_person_and_account_by_email('mark@example.com')
55
self.assertEquals(None, person)
57
# restore model integrity
58
email.person_id = person_id
62
class PasswordPolicyCompliantTestCase(SQLCachedTestCase):
64
def test_when_password_is_to_short(self):
65
self.assertFalse(password_policy_compliant('a'))
68
class PoliteFormErrorsTestCase(SQLCachedTestCase):
70
def test_polite_errors(self):
71
text = _("Enter a valid e-mail address.")
72
errors = {'email': [text]}
74
polite_form_errors(errors)
76
self.assertTrue(errors['email'][0] != text)
79
class ValidateLaunchpadPasswordTestCase(SQLCachedTestCase):
81
def test_when_there_is_binascii_error_is_raised(self):
82
self.assertFalse(validate_launchpad_password('a', 'b'))