~canonical-isd-hackers/canonical-identity-provider/sst-changes

« back to all changes in this revision

Viewing changes to identityprovider/tests/test_utils.py

  • Committer: Danny Tamez
  • Date: 2010-04-21 15:29:24 UTC
  • Revision ID: danny.tamez@canonical.com-20100421152924-lq1m92tstk2iz75a
Canonical SSO Provider (Open Source) - Initial Commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
from django.utils.translation import ugettext as _
 
5
 
 
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)
 
14
 
 
15
 
 
16
class CanonicalUrlTestCase(SQLCachedTestCase):
 
17
 
 
18
    def test_when_object_is_not_person(self):
 
19
        self.assertTrue(canonical_url(None) is None)
 
20
 
 
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")
 
24
 
 
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")
 
28
 
 
29
 
 
30
class GetPersonAndAccountByEmailTestCase(BasicAccountTestCase):
 
31
 
 
32
    fixtures = ["test"]
 
33
 
 
34
    def test_account_and_person_is_none(self):
 
35
        email = EmailAddress.objects.get(email__iexact="mark@example.com")
 
36
        email.account = None
 
37
        email.person = None
 
38
        email.save()
 
39
 
 
40
        self.assertRaises(PersonAndAccountNotFoundException,
 
41
                          get_person_and_account_by_email,
 
42
                          "mark@example.com")
 
43
 
 
44
 
 
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
 
50
        email.person_id = 0
 
51
        email.save()
 
52
 
 
53
        # test method
 
54
        person, account = get_person_and_account_by_email('mark@example.com')
 
55
        self.assertEquals(None, person)
 
56
 
 
57
        # restore model integrity
 
58
        email.person_id = person_id
 
59
        email.save()
 
60
 
 
61
 
 
62
class PasswordPolicyCompliantTestCase(SQLCachedTestCase):
 
63
 
 
64
    def test_when_password_is_to_short(self):
 
65
        self.assertFalse(password_policy_compliant('a'))
 
66
 
 
67
 
 
68
class PoliteFormErrorsTestCase(SQLCachedTestCase):
 
69
 
 
70
    def test_polite_errors(self):
 
71
        text = _("Enter a valid e-mail address.")
 
72
        errors = {'email': [text]}
 
73
 
 
74
        polite_form_errors(errors)
 
75
 
 
76
        self.assertTrue(errors['email'][0] != text)
 
77
 
 
78
 
 
79
class ValidateLaunchpadPasswordTestCase(SQLCachedTestCase):
 
80
 
 
81
    def test_when_there_is_binascii_error_is_raised(self):
 
82
        self.assertFalse(validate_launchpad_password('a', 'b'))