~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/registry/tests/test_nickname.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-05-24 14:01:38 UTC
  • mfrom: (13101.3.7 dont-expose-domains)
  • Revision ID: launchpad@pqm.canonical.com-20110524140138-bu4a8zelr0kof1zy
[r=benji][bug=228355] Removes the use of email domain information in
 the creation of nicks in generate_nick

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for nickname generation"""
 
5
 
 
6
__metaclass__ = type
 
7
 
 
8
from zope.component import getUtility
 
9
 
 
10
from canonical.testing.layers import DatabaseFunctionalLayer
 
11
from lp.registry.interfaces.person import IPersonSet
 
12
from lp.registry.model.person import (
 
13
    generate_nick,
 
14
    NicknameGenerationError,
 
15
    )
 
16
from lp.testing import TestCaseWithFactory
 
17
 
 
18
 
 
19
class TestNicknameGeneration(TestCaseWithFactory):
 
20
 
 
21
    layer = DatabaseFunctionalLayer
 
22
 
 
23
    def test_rejects_invalid_emails(self):
 
24
        # generate_nick rejects invalid email addresses
 
25
        self.assertRaises(
 
26
            NicknameGenerationError,
 
27
            generate_nick,
 
28
            'foo@example@com')
 
29
 
 
30
    def test_uses_email_address(self):
 
31
        # generate_nick uses the first part of the email address to create
 
32
        # the nick.
 
33
        nick = generate_nick('bar@example.com')
 
34
        self.assertEqual('bar', nick)
 
35
 
 
36
    def test_handles_symbols(self):
 
37
        # If an email starts with symbols, generate_nick still creates a
 
38
        # valid nick that doesn't start with symbols.
 
39
        nicks = [generate_nick(email) for email in [
 
40
                                            '---bar@example.com',
 
41
                                            'foo.bar@example.com',
 
42
                                            'foo-bar@example.com',
 
43
                                            'foo+bar@example.com',
 
44
                                            ]]
 
45
        self.assertEqual(
 
46
            ['bar', 'foo-bar', 'foo-bar', 'foo+bar'],
 
47
            nicks)
 
48
 
 
49
    def test_enforces_minimum_length(self):
 
50
        # Nicks must be a minimum length. generate_nick enforces this by 
 
51
        # adding random suffixes to the required length.
 
52
        # The nick 'i' isn't used, so we know any additional prefi
 
53
        person = getUtility(IPersonSet).getByName('i')
 
54
        self.assertIs(None, person)
 
55
        nick = generate_nick('i@example.com')
 
56
        self.assertEqual('i-5', nick)
 
57
 
 
58
    def test_can_create_noncolliding_nicknames(self):
 
59
        # Given the same email address, generate_nick doesn't recreate the
 
60
        # same nick once that nick is used.
 
61
        self._useNicknames(['bar'])
 
62
        nick = generate_nick('bar@example.com')
 
63
        self.assertEqual('bar-c', nick)
 
64
 
 
65
        # If we used the previously created nick and get another bar@ email
 
66
        # address, another new nick is generated.
 
67
        self._useNicknames(['bar-c'])
 
68
        nick = generate_nick('bar@example.com')
 
69
        self.assertEqual('a-bar', nick)
 
70
 
 
71
    def _useNicknames(self, nicknames):
 
72
        # Helper method to consume a nickname
 
73
        for nick in nicknames:
 
74
            self.factory.makePerson(name=nick)