1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Module docstring goes here."""
__metaclass__ = type
import datetime
from StringIO import StringIO
import time
from zope.component import getUtility
from zope.interface import Interface
from zope.schema.interfaces import TooShort
from lp.services.database.lpstorm import IStore
from lp.testing.layers import DatabaseFunctionalLayer
from lp.app.validators import LaunchpadValidationError
from lp.registry.interfaces.nameblacklist import INameBlacklistSet
from lp.registry.interfaces.person import (
CLOSED_TEAM_POLICY,
OPEN_TEAM_POLICY,
)
from lp.services.fields import (
BaseImageUpload,
BlacklistableContentNameField,
FormattableDate,
is_public_person_or_closed_team,
StrippableText,
)
from lp.testing import (
login_person,
TestCase,
TestCaseWithFactory,
)
def make_target():
"""Make a trivial object to be a target of the field setting."""
class Simple:
"""A simple class to test setting fields on."""
return Simple()
class TestFormattableDate(TestCase):
def test_validation_fails_on_bad_data(self):
field = FormattableDate()
date_value = datetime.date(
*(time.strptime('1000-01-01', '%Y-%m-%d'))[:3])
self.assertRaises(
LaunchpadValidationError, field.validate, date_value)
def test_validation_passes_good_data(self):
field = FormattableDate()
date_value = datetime.date(
*(time.strptime('2010-01-01', '%Y-%m-%d'))[:3])
self.assertIs(None, field.validate(date_value))
class TestStrippableText(TestCase):
def test_strips_text(self):
# The set method should strip the string before setting the field.
target = make_target()
field = StrippableText(__name__='test', strip_text=True)
self.assertTrue(field.strip_text)
field.set(target, ' testing ')
self.assertEqual('testing', target.test)
def test_strips_text_trailing_only(self):
# The set method strips the trailing whitespace.
target = make_target()
field = StrippableText(
__name__='test', strip_text=True, trailing_only=True)
self.assertTrue(field.trailing_only)
field.set(target, ' testing ')
self.assertEqual(' testing', target.test)
def test_default_constructor(self):
# If strip_text is not set, or set to false, then the text is not
# stripped when set.
target = make_target()
field = StrippableText(__name__='test')
self.assertFalse(field.strip_text)
field.set(target, ' testing ')
self.assertEqual(' testing ', target.test)
def test_setting_with_none(self):
# The set method is given None, the attribute is set to None
target = make_target()
field = StrippableText(__name__='test', strip_text=True)
field.set(target, None)
self.assertIs(None, target.test)
def test_validate_min_contraints(self):
# The minimum length constraint tests the stripped string.
field = StrippableText(
__name__='test', strip_text=True, min_length=1)
self.assertRaises(TooShort, field.validate, u' ')
def test_validate_max_contraints(self):
# The minimum length constraint tests the stripped string.
field = StrippableText(
__name__='test', strip_text=True, max_length=2)
self.assertEqual(None, field.validate(u' a '))
class TestBlacklistableContentNameField(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestBlacklistableContentNameField, self).setUp()
name_blacklist_set = getUtility(INameBlacklistSet)
self.team = self.factory.makeTeam()
admin_exp = name_blacklist_set.create(u'fnord', admin=self.team)
IStore(admin_exp).flush()
def makeTestField(self):
"""Return testible subclass."""
class ITestInterface(Interface):
pass
class TestField(BlacklistableContentNameField):
_content_iface = ITestInterface
def _getByName(self, name):
return None
return TestField(__name__='test')
def test_validate_fails_with_blacklisted_name_anonymous(self):
# Anonymous users, processes, cannot create a name that matches
# a blacklisted name.
field = self.makeTestField()
date_value = u'fnord'
self.assertRaises(
LaunchpadValidationError, field.validate, date_value)
def test_validate_fails_with_blacklisted_name_not_admin(self):
# Users who do not adminster a blacklisted name cannot create
# a matching name.
field = self.makeTestField()
date_value = u'fnord'
login_person(self.factory.makePerson())
self.assertRaises(
LaunchpadValidationError, field.validate, date_value)
def test_validate_passes_for_admin(self):
# Users in the team that adminsters a blacklisted name may create
# matching names.
field = self.makeTestField()
date_value = u'fnord'
login_person(self.team.teamowner)
self.assertEqual(None, field.validate(date_value))
class TestBaseImageUpload(TestCase):
"""Test for the BaseImageUpload field."""
class ExampleImageUpload(BaseImageUpload):
dimensions = (192, 192)
max_size = 100 * 1024
def test_validation_corrupt_image(self):
# ValueErrors raised by PIL become LaunchpadValidationErrors.
field = self.ExampleImageUpload(default_image_resource='dummy')
image = StringIO(
'/* XPM */\n'
'static char *pixmap[] = {\n'
'"32 32 253 2",\n'
' "00 c #01CAA3",\n'
' ".. s None c None",\n'
'};')
image.filename = 'foo.xpm'
self.assertRaises(
LaunchpadValidationError, field.validate, image)
def test_validation_non_image(self):
# IOError raised by PIL become LaunchpadValidationErrors.
field = self.ExampleImageUpload(default_image_resource='dummy')
image = StringIO('foo bar bz')
image.filename = 'foo.jpg'
self.assertRaises(
LaunchpadValidationError, field.validate, image)
class Test_is_person_or_closed_team(TestCaseWithFactory):
""" Tests for is_person_or_closed_team()."""
layer = DatabaseFunctionalLayer
def test_non_person(self):
self.assertFalse(is_public_person_or_closed_team(0))
def test_person(self):
person = self.factory.makePerson()
self.assertTrue(is_public_person_or_closed_team(person))
def test_open_team(self):
for policy in OPEN_TEAM_POLICY:
open_team = self.factory.makeTeam(subscription_policy=policy)
self.assertFalse(
is_public_person_or_closed_team(open_team),
"%s is not open" % policy)
def test_closed_team(self):
for policy in CLOSED_TEAM_POLICY:
closed_team = self.factory.makeTeam(subscription_policy=policy)
self.assertTrue(
is_public_person_or_closed_team(closed_team),
"%s is not closed" % policy)
|