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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Tests for traversal from the root branch object."""
__metaclass__ = type
import unittest
from zope.component import getUtility
from zope.publisher.interfaces import NotFound
from zope.security.proxy import removeSecurityProxy
from canonical.launchpad.interfaces.account import AccountStatus
from canonical.launchpad.webapp import canonical_url
from canonical.launchpad.webapp.interfaces import BrowserNotificationLevel
from canonical.launchpad.webapp.servers import LaunchpadTestRequest
from canonical.launchpad.webapp.url import urlappend
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.app.browser.launchpad import LaunchpadRootNavigation
from lp.app.errors import GoneError
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.code.interfaces.linkedbranch import ICanHasLinkedBranch
from lp.registry.interfaces.person import (
IPersonSet,
PersonVisibility,
)
from lp.testing import (
ANONYMOUS,
login,
login_person,
person_logged_in,
TestCaseWithFactory,
)
from lp.testing.views import create_view
# We set the request header HTTP_REFERER when we want to simulate navigation
# from a valid page. This is used in the assertDisplaysNotification check.
DEFAULT_REFERER = 'http://launchpad.dev'
class TraversalMixin:
def _validateNotificationContext(
self, request, notification=None,
level=BrowserNotificationLevel.INFO):
"""Check the browser notifications associated with the request.
Ensure that the notification instances attached to the request match
the expected values for text and type.
:param notification: The exact notification text to validate. If None
then we don't care what the notification text is, so long as there
is some.
: param level: the required notification level
"""
notifications = request.notifications
if notification is None:
self.assertEquals(len(notifications), 0)
return
self.assertEqual(len(notifications), 1)
self.assertEquals(notifications[0].level, level)
self.assertEqual(notification, notifications[0].message)
def assertDisplaysNotification(
self, path, notification=None,
level=BrowserNotificationLevel.INFO):
"""Assert that an invalid path redirects back to referrer.
The request object is expected to have a notification message to
display to the user to explain the reason for the error.
:param path: The path to check
:param notification: The exact notification text to validate. If None
then we don't care what the notification text is, so long as there
is some.
: param level: the required notification level
"""
redirection = self.traverse(path)
self.assertIs(redirection.target, DEFAULT_REFERER)
self._validateNotificationContext(
redirection.request, notification, level)
def assertNotFound(self, path, use_default_referer=True):
self.assertRaises(
NotFound, self.traverse, path,
use_default_referer=use_default_referer)
def assertRedirects(self, segments, url):
redirection = self.traverse(segments)
self.assertEqual(url, redirection.target)
def traverse(self, path, first_segment, use_default_referer=True):
"""Traverse to 'segments' using a 'LaunchpadRootNavigation' object.
Using the Zope traversal machinery, traverse to the path given by
'segments', starting at a `LaunchpadRootNavigation` object.
:param segments: A list of path segments.
:param use_default_referer: If True, set the referer attribute in the
request header to DEFAULT_REFERER = "http://launchpad.dev"
(otherwise it remains as None)
:return: The object found.
"""
extra = {'PATH_INFO': urlappend('/%s' % first_segment, path)}
if use_default_referer:
extra['HTTP_REFERER'] = DEFAULT_REFERER
request = LaunchpadTestRequest(**extra)
segments = reversed(path.split('/'))
request.setTraversalStack(segments)
traverser = LaunchpadRootNavigation(None, request=request)
return traverser.publishTraverse(request, first_segment)
class TestBranchTraversal(TestCaseWithFactory, TraversalMixin):
"""Branches are traversed to from IPersons. Test we can reach them.
This class tests the `LaunchpadRootNavigation` class to see that we can
traverse to branches from URLs of the form +branch/xxxx.
"""
layer = DatabaseFunctionalLayer
def assertDisplaysNotice(self, path, notification):
"""Assert that traversal redirects back with the specified notice."""
self.assertDisplaysNotification(
path, notification, BrowserNotificationLevel.INFO)
def assertDisplaysError(self, path, notification):
"""Assert that traversal redirects back with the specified notice."""
self.assertDisplaysNotification(
path, notification, BrowserNotificationLevel.ERROR)
def traverse(self, path, **kwargs):
return super(TestBranchTraversal, self).traverse(
path, '+branch', **kwargs)
def test_unique_name_traversal(self):
# Traversing to /+branch/<unique_name> redirects to the page for that
# branch.
branch = self.factory.makeAnyBranch()
self.assertRedirects(branch.unique_name, canonical_url(branch))
def test_no_such_unique_name(self):
# Traversing to /+branch/<unique_name> where 'unique_name' is for a
# branch that doesn't exist will display an error message.
branch = self.factory.makeAnyBranch()
bad_name = branch.unique_name + 'wibble'
requiredMessage = "No such branch: '%s'." % (
branch.name + "wibble")
self.assertDisplaysError(bad_name, requiredMessage)
def test_private_branch(self):
# If an attempt is made to access a private branch, display an error.
branch = self.factory.makeProductBranch(private=True)
branch_unique_name = removeSecurityProxy(branch).unique_name
login(ANONYMOUS)
requiredMessage = "No such branch: '%s'." % branch_unique_name
self.assertDisplaysError(branch_unique_name, requiredMessage)
def test_product_alias(self):
# Traversing to /+branch/<product> redirects to the page for the
# branch that is the development focus branch for that product.
branch = self.factory.makeProductBranch()
naked_product = removeSecurityProxy(branch.product)
ICanHasLinkedBranch(naked_product).setBranch(branch)
self.assertRedirects(naked_product.name, canonical_url(branch))
def test_private_branch_for_product(self):
# If the development focus of a product is private, display a
# message telling the user there is no linked branch.
branch = self.factory.makeProductBranch()
naked_product = removeSecurityProxy(branch.product)
ICanHasLinkedBranch(naked_product).setBranch(branch)
removeSecurityProxy(branch).private = True
login(ANONYMOUS)
requiredMessage = (
u"The target %s does not have a linked branch." %
naked_product.name)
self.assertDisplaysNotice(naked_product.name, requiredMessage)
def test_nonexistent_product(self):
# Traversing to /+branch/<no-such-product> displays an error message.
non_existent = 'non-existent'
requiredMessage = u"No such product: '%s'." % non_existent
self.assertDisplaysError(non_existent, requiredMessage)
def test_nonexistent_product_without_referer(self):
# Traversing to /+branch/<no-such-product> without a referer results
# in a 404 error. This happens if the user hacks the URL rather than
# navigating via a link
self.assertNotFound('non-existent', use_default_referer=False)
def test_private_without_referer(self):
# If the development focus of a product is private and there is no
# referer, we will get a 404 error. This happens if the user hacks
# the URL rather than navigating via a link
branch = self.factory.makeProductBranch()
naked_product = removeSecurityProxy(branch.product)
ICanHasLinkedBranch(naked_product).setBranch(branch)
removeSecurityProxy(branch).private = True
login(ANONYMOUS)
self.assertNotFound(naked_product.name, use_default_referer=False)
def test_product_without_dev_focus(self):
# Traversing to a product without a development focus displays a
# user message on the same page.
product = self.factory.makeProduct()
requiredMessage = (
u"The target %s does not have a linked branch." % product.name)
self.assertDisplaysNotice(product.name, requiredMessage)
def test_distro_package_alias(self):
# Traversing to /+branch/<distro>/<sourcepackage package> redirects
# to the page for the branch that is the development focus branch
# for that package.
sourcepackage = self.factory.makeSourcePackage()
branch = self.factory.makePackageBranch(sourcepackage=sourcepackage)
distro_package = sourcepackage.distribution_sourcepackage
registrant = distro_package.distribution.owner
target = ICanHasLinkedBranch(distro_package)
with person_logged_in(registrant):
target.setBranch(branch, registrant)
self.assertRedirects("%s" % target.bzr_path, canonical_url(branch))
def test_private_branch_for_distro_package(self):
# If the development focus of a distro package is private, display a
# message telling the user there is no linked branch.
sourcepackage = self.factory.makeSourcePackage()
branch = self.factory.makePackageBranch(
sourcepackage=sourcepackage, private=True)
distro_package = sourcepackage.distribution_sourcepackage
registrant = distro_package.distribution.owner
with person_logged_in(registrant):
ICanHasLinkedBranch(distro_package).setBranch(branch, registrant)
login(ANONYMOUS)
path = ICanHasLinkedBranch(distro_package).bzr_path
requiredMessage = (
u"The target %s does not have a linked branch." % path)
self.assertDisplaysNotice(path, requiredMessage)
def test_trailing_path_redirect(self):
# If there are any trailing path segments after the branch identifier,
# these stick around at the redirected URL.
branch = self.factory.makeAnyBranch()
path = urlappend(branch.unique_name, '+edit')
self.assertRedirects(path, canonical_url(branch, view_name='+edit'))
def test_product_series_redirect(self):
# Traversing to /+branch/<product>/<series> redirects to the branch
# for that series, if there is one.
branch = self.factory.makeBranch()
series = self.factory.makeProductSeries(branch=branch)
self.assertRedirects(
ICanHasLinkedBranch(series).bzr_path, canonical_url(branch))
def test_nonexistent_product_series(self):
# /+branch/<product>/<series> displays an error message if there is
# no such series.
product = self.factory.makeProduct()
non_existent = 'nonexistent'
requiredMessage = u"No such product series: '%s'." % non_existent
path = '%s/%s' % (product.name, non_existent)
self.assertDisplaysError(path, requiredMessage)
def test_no_branch_for_series(self):
# If there's no branch for a product series, display a
# message telling the user there is no linked branch.
series = self.factory.makeProductSeries()
path = ICanHasLinkedBranch(series).bzr_path
requiredMessage = (
"The target %s does not have a linked branch." % path)
self.assertDisplaysNotice(path, requiredMessage)
def test_private_branch_for_series(self):
# If the development focus of a product series is private, display a
# message telling the user there is no linked branch.
branch = self.factory.makeBranch(private=True)
series = self.factory.makeProductSeries(branch=branch)
login(ANONYMOUS)
path = ICanHasLinkedBranch(series).bzr_path
requiredMessage = (
u"The target %s does not have a linked branch." % path)
self.assertDisplaysNotice(path, requiredMessage)
def test_too_short_branch_name(self):
# error notification if the thing following +branch is a unique name
# that's too short to be a real unique name.
owner = self.factory.makePerson()
requiredMessage = (
u"Cannot understand namespace name: '%s'" % owner.name)
self.assertDisplaysError('~%s' % owner.name, requiredMessage)
def test_invalid_product_name(self):
# error notification if the thing following +branch has an invalid
# product name.
self.assertDisplaysError('_foo', u"Invalid name for product: _foo.")
def test_invalid_product_name_without_referer(self):
# error notification if the thing following +branch has an invalid
# product name.
self.assertNotFound("_foo", use_default_referer=False)
class TestPersonTraversal(TestCaseWithFactory, TraversalMixin):
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestPersonTraversal, self).setUp()
self.any_user = self.factory.makePerson()
self.admin = getUtility(IPersonSet).getByName('name16')
self.registry_expert = self.factory.makePerson()
registry = getUtility(ILaunchpadCelebrities).registry_experts
with person_logged_in(registry.teamowner):
registry.addMember(self.registry_expert, registry.teamowner)
def test_person(self):
# Verify a user is returned.
name = 'active-person'
person = self.factory.makePerson(name=name)
segment = '~%s' % name
traversed = self.traverse(segment, segment)
self.assertEqual(person, traversed)
def test_suspended_person_visibility(self):
# Verify a suspended user is only traversable by an admin.
name = 'suspended-person'
person = self.factory.makePerson(name=name)
login_person(self.admin)
removeSecurityProxy(person).account_status = AccountStatus.SUSPENDED
segment = '~%s' % name
# Admins can see the suspended user.
traversed = self.traverse(segment, segment)
self.assertEqual(person, traversed)
# Registry experts can see the suspended user.
login_person(self.registry_expert)
traversed = self.traverse(segment, segment)
# Regular users cannot see the suspended user.
login_person(self.any_user)
self.assertRaises(GoneError, self.traverse, segment, segment)
def test_public_team(self):
# Verify a public team is returned.
name = 'public-team'
team = self.factory.makeTeam(name=name)
segment = '~%s' % name
traversed = self.traverse(segment, segment)
self.assertEqual(team, traversed)
def test_private_team_visible_to_admin_and_members_only(self):
# Verify a private team is team is returned.
name = 'private-team'
team = self.factory.makeTeam(name=name)
login_person(self.admin)
team.visibility = PersonVisibility.PRIVATE
segment = '~%s' % name
# Admins can traverse to the team.
traversed = self.traverse(segment, segment)
self.assertEqual(team, traversed)
# Members can traverse to the team.
login_person(team.teamowner)
traversed = self.traverse(segment, segment)
self.assertEqual(team, traversed)
# All other user cannot traverse to the team.
login_person(self.any_user)
self.assertRaises(NotFound, self.traverse, segment, segment)
class TestErrorViews(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_GoneError(self):
error = GoneError('User is suspended')
view = create_view(error, 'index.html')
self.assertEqual('Error: Page gone', view.page_title)
self.assertEqual(410, view.request.response.getStatus())
def test_suite():
return unittest.TestLoader().loadTestsFromName(__name__)
|