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
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
from datetime import datetime
import unittest
from BeautifulSoup import BeautifulSoup
import pytz
from testtools.matchers import Equals
from zope.component import getUtility
from zope.publisher.interfaces import NotFound
from zope.security.proxy import removeSecurityProxy
from lp.services.webapp.interfaces import BrowserNotificationLevel
from lp.services.webapp.publisher import canonical_url
from lp.testing.layers import DatabaseFunctionalLayer
from lp.app.browser.tales import format_link
from lp.blueprints.browser import specification
from lp.blueprints.enums import SpecificationImplementationStatus
from lp.blueprints.interfaces.specification import (
ISpecification,
ISpecificationSet,
)
from lp.registry.interfaces.person import PersonVisibility
from lp.testing import (
BrowserTestCase,
FakeLaunchpadRequest,
login_person,
logout,
person_logged_in,
TestCaseWithFactory,
)
from lp.testing.matchers import DocTestMatches
from lp.testing.pages import (
extract_text,
find_tag_by_id,
setupBrowser,
setupBrowserForUser,
)
from lp.testing.views import create_initialized_view
class TestSpecificationSearch(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_search_with_percent(self):
# Using '%' in a search should not error.
specs = getUtility(ISpecificationSet)
form = {'field.search_text': r'%'}
view = create_initialized_view(specs, '+index', form=form)
self.assertEqual([], view.errors)
class TestBranchTraversal(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.specification = self.factory.makeSpecification()
def assertRedirects(self, segments, url):
redirection = self.traverse(segments)
self.assertEqual(url, redirection.target)
def linkBranch(self, branch):
self.specification.linkBranch(branch, self.factory.makePerson())
def traverse(self, segments):
stack = list(reversed(['+branch'] + segments))
name = stack.pop()
request = FakeLaunchpadRequest([], stack)
traverser = specification.SpecificationNavigation(
self.specification, request)
return traverser.publishTraverse(request, name)
def test_junk_branch(self):
branch = self.factory.makePersonalBranch()
self.linkBranch(branch)
segments = [branch.owner.name, '+junk', branch.name]
self.assertEqual(
self.specification.getBranchLink(branch), self.traverse(segments))
def test_junk_branch_no_such_person(self):
person_name = self.factory.getUniqueString()
branch_name = self.factory.getUniqueString()
self.assertRaises(
NotFound, self.traverse, [person_name, '+junk', branch_name])
def test_junk_branch_no_such_branch(self):
person = self.factory.makePerson()
branch_name = self.factory.getUniqueString()
self.assertRaises(
NotFound, self.traverse, [person.name, '+junk', branch_name])
def test_product_branch(self):
branch = self.factory.makeProductBranch()
self.linkBranch(branch)
segments = [branch.owner.name, branch.product.name, branch.name]
self.assertEqual(
self.specification.getBranchLink(branch), self.traverse(segments))
def test_product_branch_no_such_person(self):
person_name = self.factory.getUniqueString()
product_name = self.factory.getUniqueString()
branch_name = self.factory.getUniqueString()
self.assertRaises(
NotFound, self.traverse, [person_name, product_name, branch_name])
def test_product_branch_no_such_product(self):
person = self.factory.makePerson()
product_name = self.factory.getUniqueString()
branch_name = self.factory.getUniqueString()
self.assertRaises(
NotFound, self.traverse, [person.name, product_name, branch_name])
def test_product_branch_no_such_branch(self):
person = self.factory.makePerson()
product = self.factory.makeProduct()
branch_name = self.factory.getUniqueString()
self.assertRaises(
NotFound, self.traverse, [person.name, product.name, branch_name])
def test_package_branch(self):
branch = self.factory.makePackageBranch()
self.linkBranch(branch)
segments = [
branch.owner.name,
branch.distribution.name,
branch.distroseries.name,
branch.sourcepackagename.name,
branch.name]
self.assertEqual(
self.specification.getBranchLink(branch), self.traverse(segments))
class TestSpecificationView(TestCaseWithFactory):
"""Test the SpecificationView."""
layer = DatabaseFunctionalLayer
def test_offsite_url(self):
"""The specification URL is rendered when present."""
spec = self.factory.makeSpecification()
login_person(spec.owner)
spec.specurl = 'http://eg.dom/parrot'
view = create_initialized_view(
spec, name='+index', principal=spec.owner,
rootsite='blueprints')
li = find_tag_by_id(view.render(), 'spec-url')
self.assertEqual('nofollow', li.a['rel'])
self.assertEqual(spec.specurl, li.a['href'])
def test_registration_date_displayed(self):
"""The time frame does not prepend on incorrectly."""
spec = self.factory.makeSpecification(
owner=self.factory.makePerson(displayname="Some Person"))
html = create_initialized_view(
spec, '+index')()
self.assertThat(
extract_text(html), DocTestMatches(
"... Registered by Some Person ... ago ..."))
class TestSpecificationViewPrivateArtifacts(BrowserTestCase):
""" Tests that specifications with private team artifacts can be viewed.
A Specification may be associated with a private team as follows:
- a subscriber is a private team
A logged in user who is not authorised to see the private team(s) still
needs to be able to view the specification. The private team will be
rendered in the normal way, displaying the team name and Launchpad URL.
"""
layer = DatabaseFunctionalLayer
def _getBrowser(self, user=None):
if user is None:
browser = setupBrowser()
logout()
return browser
else:
login_person(user)
return setupBrowserForUser(user=user)
def test_view_specification_with_private_subscriber(self):
# A specification with a private subscriber is rendered.
private_subscriber = self.factory.makeTeam(
name="privateteam",
visibility=PersonVisibility.PRIVATE)
spec = self.factory.makeSpecification()
with person_logged_in(spec.owner):
spec.subscribe(private_subscriber, spec.owner)
# Ensure the specification subscriber is rendered.
url = canonical_url(spec, rootsite='blueprints')
user = self.factory.makePerson()
browser = self._getBrowser(user)
browser.open(url)
soup = BeautifulSoup(browser.contents)
subscriber_portlet = soup.find(
'div', attrs={'id': 'subscribers'})
self.assertIsNotNone(
subscriber_portlet.find('a', text='Privateteam'))
def test_anonymous_view_specification_with_private_subscriber(self):
# A specification with a private subscriber is not rendered for anon.
private_subscriber = self.factory.makeTeam(
name="privateteam",
visibility=PersonVisibility.PRIVATE)
spec = self.factory.makeSpecification()
with person_logged_in(spec.owner):
spec.subscribe(private_subscriber, spec.owner)
# Viewing the specification doesn't display private subscriber.
url = canonical_url(spec, rootsite='blueprints')
browser = self._getBrowser()
browser.open(url)
soup = BeautifulSoup(browser.contents)
self.assertIsNone(
soup.find('div', attrs={'id': 'subscriber-privateteam'}))
class TestSpecificationEditStatusView(TestCaseWithFactory):
"""Test the SpecificationEditStatusView."""
layer = DatabaseFunctionalLayer
def test_records_started(self):
not_started = SpecificationImplementationStatus.NOTSTARTED
spec = self.factory.makeSpecification(
implementation_status=not_started)
login_person(spec.owner)
form = {
'field.implementation_status': 'STARTED',
'field.actions.change': 'Change',
}
view = create_initialized_view(spec, name='+status', form=form)
self.assertEqual(
SpecificationImplementationStatus.STARTED,
spec.implementation_status)
self.assertEqual(spec.owner, spec.starter)
[notification] = view.request.notifications
self.assertEqual(BrowserNotificationLevel.INFO, notification.level)
self.assertEqual(
'Blueprint is now considered "Started".', notification.message)
def test_unchanged_lifecycle_has_no_notification(self):
spec = self.factory.makeSpecification(
implementation_status=SpecificationImplementationStatus.STARTED)
login_person(spec.owner)
form = {
'field.implementation_status': 'SLOW',
'field.actions.change': 'Change',
}
view = create_initialized_view(spec, name='+status', form=form)
self.assertEqual(
SpecificationImplementationStatus.SLOW,
spec.implementation_status)
self.assertEqual(0, len(view.request.notifications))
def test_records_unstarting(self):
# If a spec was started, and is changed to not started,
# a notice is shown. Also the spec.starter is cleared out.
spec = self.factory.makeSpecification(
implementation_status=SpecificationImplementationStatus.STARTED)
login_person(spec.owner)
form = {
'field.implementation_status': 'NOTSTARTED',
'field.actions.change': 'Change',
}
view = create_initialized_view(spec, name='+status', form=form)
self.assertEqual(
SpecificationImplementationStatus.NOTSTARTED,
spec.implementation_status)
self.assertIs(None, spec.starter)
[notification] = view.request.notifications
self.assertEqual(BrowserNotificationLevel.INFO, notification.level)
self.assertEqual(
'Blueprint is now considered "Not started".',
notification.message)
def test_records_completion(self):
# If a spec is marked as implemented the user is notifiec it is now
# complete.
spec = self.factory.makeSpecification(
implementation_status=SpecificationImplementationStatus.STARTED)
login_person(spec.owner)
form = {
'field.implementation_status': 'IMPLEMENTED',
'field.actions.change': 'Change',
}
view = create_initialized_view(spec, name='+status', form=form)
self.assertEqual(
SpecificationImplementationStatus.IMPLEMENTED,
spec.implementation_status)
self.assertEqual(spec.owner, spec.completer)
[notification] = view.request.notifications
self.assertEqual(BrowserNotificationLevel.INFO, notification.level)
self.assertEqual(
'Blueprint is now considered "Complete".', notification.message)
class TestSecificationHelpers(unittest.TestCase):
"""Test specification helper functions."""
def test_dict_to_DOT_attrs(self):
"""Verify that dicts are converted to a sorted DOT attr string."""
expected_attrs = (
u' [\n'
u' "bar"="bar \\" \\n bar",\n'
u' "baz"="zab",\n'
u' "foo"="foo"\n'
u' ]')
dict_attrs = dict(
foo="foo",
bar="bar \" \n bar",
baz="zab")
dot_attrs = specification.dict_to_DOT_attrs(dict_attrs, indent=' ')
self.assertEqual(dot_attrs, expected_attrs)
class TestSpecificationFieldXHTMLRepresentations(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_starter_empty(self):
blueprint = self.factory.makeBlueprint()
repr_method = specification.starter_xhtml_representation(
blueprint, ISpecification['starter'], None)
self.assertThat(repr_method(), Equals(''))
def test_starter_set(self):
user = self.factory.makePerson()
blueprint = self.factory.makeBlueprint(owner=user)
when = datetime(2011, 1, 1, tzinfo=pytz.UTC)
with person_logged_in(user):
blueprint.setImplementationStatus(
SpecificationImplementationStatus.STARTED, user)
removeSecurityProxy(blueprint).date_started = when
repr_method = specification.starter_xhtml_representation(
blueprint, ISpecification['starter'], None)
expected = format_link(user) + ' on 2011-01-01'
self.assertThat(repr_method(), Equals(expected))
def test_completer_empty(self):
blueprint = self.factory.makeBlueprint()
repr_method = specification.completer_xhtml_representation(
blueprint, ISpecification['completer'], None)
self.assertThat(repr_method(), Equals(''))
def test_completer_set(self):
user = self.factory.makePerson()
blueprint = self.factory.makeBlueprint(owner=user)
when = datetime(2011, 1, 1, tzinfo=pytz.UTC)
with person_logged_in(user):
blueprint.setImplementationStatus(
SpecificationImplementationStatus.IMPLEMENTED, user)
removeSecurityProxy(blueprint).date_completed = when
repr_method = specification.completer_xhtml_representation(
blueprint, ISpecification['completer'], None)
expected = format_link(user) + ' on 2011-01-01'
self.assertThat(repr_method(), Equals(expected))
|