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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test the person vocabularies."""
__metaclass__ = type
from storm.store import Store
from testtools.matchers import Equals
from zope.component import getUtility
from zope.schema.vocabulary import getVocabularyRegistry
from zope.security.proxy import removeSecurityProxy
from lp.registry.interfaces.irc import IIrcIDSet
from lp.registry.interfaces.karma import IKarmaCacheManager
from lp.registry.interfaces.person import (
CLOSED_TEAM_POLICY,
OPEN_TEAM_POLICY,
PersonVisibility,
TeamSubscriptionPolicy,
)
from lp.registry.vocabularies import ValidPersonOrTeamVocabulary
from lp.services.webapp.vocabulary import FilteredVocabularyBase
from lp.testing import (
login_person,
StormStatementRecorder,
TestCaseWithFactory,
)
from lp.testing.dbuser import dbuser
from lp.testing.layers import (
DatabaseFunctionalLayer,
LaunchpadZopelessLayer,
)
from lp.testing.matchers import HasQueryCount
class VocabularyTestBase:
vocabulary_name = None
def setUp(self):
super(VocabularyTestBase, self).setUp()
self.vocabulary_registry = getVocabularyRegistry()
def getVocabulary(self, context):
return self.vocabulary_registry.get(context, self.vocabulary_name)
def searchVocabulary(self, context, text, vocab_filter=None):
if Store.of(context) is not None:
Store.of(context).flush()
vocabulary = self.getVocabulary(context)
removeSecurityProxy(vocabulary).allow_null_search = True
return removeSecurityProxy(vocabulary).search(text, vocab_filter)
class ValidPersonOrTeamVocabularyMixin(VocabularyTestBase):
"""Common tests for the ValidPersonOrTeam vocabulary derivatives."""
def test_supported_filters(self):
# The vocab supports the correct filters.
self.assertEqual([
FilteredVocabularyBase.ALL_FILTER,
ValidPersonOrTeamVocabulary.PERSON_FILTER,
ValidPersonOrTeamVocabulary.TEAM_FILTER,
],
self.getVocabulary(None).supportedFilters()
)
def addKarma(self, person, value, product=None, distribution=None):
if product:
kwargs = dict(product_id=product.id)
elif distribution:
kwargs = dict(distribution_id=distribution.id)
with dbuser('karma'):
getUtility(IKarmaCacheManager).new(
value, person.id, None, **kwargs)
def test_people_with_karma_sort_higher(self):
exact_person = self.factory.makePerson(
name='fooix', displayname='Fooix Bar')
prefix_person = self.factory.makePerson(
name='fooix-bar', displayname='Fooix Bar')
contributor_person = self.factory.makePerson(
name='bar', displayname='Fooix Bar')
product = self.factory.makeProduct()
# Exact is better than prefix is better than FTI.
self.assertEqual(
[exact_person, prefix_person, contributor_person],
list(self.searchVocabulary(product, u'fooix')))
# But karma can bump people up, behind the exact match.
self.addKarma(contributor_person, 500, product=product)
self.assertEqual(
[exact_person, contributor_person, prefix_person],
list(self.searchVocabulary(product, u'fooix')))
self.addKarma(prefix_person, 500, product=product)
self.assertEqual(
[exact_person, prefix_person, contributor_person],
list(self.searchVocabulary(product, u'fooix')))
def assertKarmaContextConstraint(self, expected, context):
"""Check that the karma context constraint works.
Confirms that the karma context constraint matches the expected
value, and that a search with it works.
"""
if expected is not None:
expected = expected % context.id
self.assertEquals(
expected,
removeSecurityProxy(
self.getVocabulary(context))._karma_context_constraint)
self.searchVocabulary(context, 'foo')
def test_product_karma_context(self):
self.assertKarmaContextConstraint(
'product = %d', self.factory.makeProduct())
def test_project_karma_context(self):
self.assertKarmaContextConstraint(
'project = %d', self.factory.makeProject())
def test_distribution_karma_context(self):
self.assertKarmaContextConstraint(
'distribution = %d', self.factory.makeDistribution())
def test_root_karma_context(self):
self.assertKarmaContextConstraint(None, None)
def test_irc_nick_match_is_not_case_sensitive(self):
person = self.factory.makePerson()
irc = getUtility(IIrcIDSet).new(
person, 'somenet', 'MiXeD' + self.factory.getUniqueString())
self.assertContentEqual(
[person], self.searchVocabulary(person, irc.nickname.lower()))
def _person_filter_tests(self, person):
results = self.searchVocabulary(None, '', 'PERSON')
for personorteam in results:
self.assertFalse(personorteam.is_team)
results = self.searchVocabulary(None, u'fred', 'PERSON')
self.assertEqual([person], list(results))
def test_person_filter(self):
# Test that the person filter only returns people.
person = self.factory.makePerson(
name="fredperson", email="fredperson@foo.com")
self.factory.makeTeam(
name="fredteam", email="fredteam@foo.com")
self._person_filter_tests(person)
def _team_filter_tests(self, teams):
results = self.searchVocabulary(None, '', 'TEAM')
for personorteam in results:
self.assertTrue(personorteam.is_team)
results = self.searchVocabulary(None, u'fred', 'TEAM')
self.assertContentEqual(teams, list(results))
class TestValidPersonOrTeamVocabulary(ValidPersonOrTeamVocabularyMixin,
TestCaseWithFactory):
"""Test that the ValidPersonOrTeamVocabulary behaves as expected.
Most tests are in lib/lp/registry/doc/vocabularies.txt.
"""
layer = LaunchpadZopelessLayer
vocabulary_name = 'ValidPersonOrTeam'
def test_team_filter(self):
# Test that the team filter only returns teams.
self.factory.makePerson(
name="fredperson", email="fredperson@foo.com")
team = self.factory.makeTeam(
name="fredteam", email="fredteam@foo.com")
self._team_filter_tests([team])
class TestValidPersonOrTeamPreloading(VocabularyTestBase,
TestCaseWithFactory):
"""Tests for ValidPersonOrTeamVocabulary's preloading behaviour."""
layer = DatabaseFunctionalLayer
vocabulary_name = 'ValidPersonOrTeam'
def test_preloads_irc_nicks_and_preferredemail(self):
"""Test that IRC nicks and preferred email addresses are preloaded."""
# Create three people with IRC nicks, and one without.
people = []
for num in range(3):
person = self.factory.makePerson(displayname='foobar %d' % num)
getUtility(IIrcIDSet).new(person, 'launchpad', person.name)
people.append(person)
people.append(self.factory.makePerson(displayname='foobar 4'))
# Remember the current values for checking later, and throw out
# the cache.
expected_nicks = dict(
(person.id, list(person.ircnicknames)) for person in people)
expected_emails = dict(
(person.id, person.preferredemail) for person in people)
Store.of(people[0]).invalidate()
results = list(self.searchVocabulary(None, u'foobar'))
with StormStatementRecorder() as recorder:
self.assertEquals(4, len(results))
for person in results:
self.assertEqual(
expected_nicks[person.id], person.ircnicknames)
self.assertEqual(
expected_emails[person.id], person.preferredemail)
self.assertThat(recorder, HasQueryCount(Equals(0)))
class TestValidPersonOrClosedTeamVocabulary(ValidPersonOrTeamVocabularyMixin,
TestCaseWithFactory):
"""Test that the ValidPersonOrClosedTeamVocabulary behaves as expected."""
layer = LaunchpadZopelessLayer
vocabulary_name = 'ValidPillarOwner'
def test_team_filter(self):
# Test that the team filter only returns closed teams.
self.factory.makePerson(
name="fredperson", email="fredperson@foo.com")
for policy in OPEN_TEAM_POLICY:
self.factory.makeTeam(
name="fred%s" % policy.name.lower(),
email="team_%s@foo.com" % policy.name,
subscription_policy=policy)
closed_teams = []
for policy in CLOSED_TEAM_POLICY:
closed_teams.append(self.factory.makeTeam(
name="fred%s" % policy.name.lower(),
email="team_%s@foo.com" % policy.name,
subscription_policy=policy))
self._team_filter_tests(closed_teams)
class TeamMemberVocabularyTestBase(VocabularyTestBase):
def test_open_team_cannot_be_a_member_of_a_closed_team(self):
context_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.MODERATED)
open_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.OPEN)
moderated_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.MODERATED)
restricted_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.RESTRICTED)
user = self.factory.makePerson()
all_possible_members = self.searchVocabulary(context_team, '')
self.assertNotIn(open_team, all_possible_members)
self.assertIn(moderated_team, all_possible_members)
self.assertIn(restricted_team, all_possible_members)
self.assertIn(user, all_possible_members)
def test_open_team_can_be_a_member_of_an_open_team(self):
context_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.OPEN)
open_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.OPEN)
moderated_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.MODERATED)
restricted_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.RESTRICTED)
user = self.factory.makePerson()
all_possible_members = self.searchVocabulary(context_team, '')
self.assertIn(open_team, all_possible_members)
self.assertIn(moderated_team, all_possible_members)
self.assertIn(restricted_team, all_possible_members)
self.assertIn(user, all_possible_members)
def test_vocabulary_displayname(self):
context_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.OPEN)
vocabulary = self.getVocabulary(context_team)
self.assertEqual(
'Select a Team or Person', vocabulary.displayname)
def test_open_team_vocabulary_step_title(self):
context_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.OPEN)
vocabulary = self.getVocabulary(context_team)
self.assertEqual('Search', vocabulary.step_title)
def test_closed_team_vocabulary_step_title(self):
context_team = self.factory.makeTeam(
subscription_policy=TeamSubscriptionPolicy.MODERATED)
vocabulary = self.getVocabulary(context_team)
self.assertEqual(
'Search for a restricted team, a moderated team, or a person',
vocabulary.step_title)
class TestValidTeamMemberVocabulary(TeamMemberVocabularyTestBase,
TestCaseWithFactory):
"""Test that the ValidTeamMemberVocabulary behaves as expected."""
layer = DatabaseFunctionalLayer
vocabulary_name = 'ValidTeamMember'
def test_public_team_cannot_be_a_member_of_itself(self):
# A public team should be filtered by the vocab.extra_clause
# when provided a search term.
team = self.factory.makeTeam()
self.assertNotIn(team, self.searchVocabulary(team, team.name))
def test_private_team_cannot_be_a_member_of_itself(self):
# A private team should be filtered by the vocab.extra_clause
# when provided a search term.
owner = self.factory.makePerson()
team = self.factory.makeTeam(
owner=owner, visibility=PersonVisibility.PRIVATE)
login_person(owner)
self.assertNotIn(team, self.searchVocabulary(team, team.name))
class TestValidTeamOwnerVocabulary(TeamMemberVocabularyTestBase,
TestCaseWithFactory):
"""Test that the ValidTeamOwnerVocabulary behaves as expected."""
layer = DatabaseFunctionalLayer
vocabulary_name = 'ValidTeamOwner'
def test_team_cannot_own_itself(self):
context_team = self.factory.makeTeam()
results = self.searchVocabulary(context_team, context_team.name)
self.assertNotIn(context_team, results)
def test_team_cannot_own_its_owner(self):
context_team = self.factory.makeTeam()
owned_team = self.factory.makeTeam(owner=context_team)
results = self.searchVocabulary(context_team, owned_team.name)
self.assertNotIn(owned_team, results)
class TestValidPersonVocabulary(VocabularyTestBase,
TestCaseWithFactory):
"""Test that the ValidPersonVocabulary behaves as expected."""
layer = LaunchpadZopelessLayer
vocabulary_name = 'ValidPerson'
def test_supported_filters(self):
# The vocab shouldn't support person or team filters.
self.assertEqual([], self.getVocabulary(None).supportedFilters())
class TestValidTeamVocabulary(VocabularyTestBase,
TestCaseWithFactory):
"""Test that the ValidTeamVocabulary behaves as expected."""
layer = LaunchpadZopelessLayer
vocabulary_name = 'ValidTeam'
def test_supported_filters(self):
# The vocab shouldn't support person or team filters.
self.assertEqual([], self.getVocabulary(None).supportedFilters())
|