1
# Copyright 2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Tests for nickname generation"""
8
from zope.component import getUtility
10
from canonical.testing.layers import DatabaseFunctionalLayer
11
from lp.registry.interfaces.person import IPersonSet
12
from lp.registry.model.person import (
14
NicknameGenerationError,
16
from lp.testing import TestCaseWithFactory
19
class TestNicknameGeneration(TestCaseWithFactory):
21
layer = DatabaseFunctionalLayer
23
def test_rejects_invalid_emails(self):
24
# generate_nick rejects invalid email addresses
26
NicknameGenerationError,
30
def test_uses_email_address(self):
31
# generate_nick uses the first part of the email address to create
33
nick = generate_nick('bar@example.com')
34
self.assertEqual('bar', nick)
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 [
41
'foo.bar@example.com',
42
'foo-bar@example.com',
43
'foo+bar@example.com',
46
['bar', 'foo-bar', 'foo-bar', 'foo+bar'],
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)
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)
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)
71
def _useNicknames(self, nicknames):
72
# Helper method to consume a nickname
73
for nick in nicknames:
74
self.factory.makePerson(name=nick)