~launchpad-pqm/launchpad/devel

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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Webservice unit tests related to Launchpad blueprints."""

__metaclass__ = type

import transaction

from zope.security.management import endInteraction

from canonical.launchpad.testing.pages import (
    LaunchpadWebServiceCaller,
    webservice_for_person,
    )
from canonical.launchpad.webapp.interaction import ANONYMOUS
from canonical.testing import (
    AppServerLayer,
    DatabaseFunctionalLayer,
    )
from lp.blueprints.enums import SpecificationDefinitionStatus
from lp.testing import (
    launchpadlib_for,
    person_logged_in,
    TestCaseWithFactory,
    ws_object,
    )


class SpecificationWebserviceTestCase(TestCaseWithFactory):

    def getLaunchpadlib(self):
        user = self.factory.makePerson()
        return launchpadlib_for("testing", user, version='devel')

    def getSpecOnWebservice(self, spec_object):
        launchpadlib = self.getLaunchpadlib()
        return launchpadlib.load(
            '/%s/+spec/%s' % (spec_object.target.name, spec_object.name))

    def getPillarOnWebservice(self, pillar_obj):
        # XXX: 2010-11-26, salgado, bug=681767: Can't use relative URLs here.
        launchpadlib = self.getLaunchpadlib()
        return launchpadlib.load(
            str(launchpadlib._root_uri) + '/' + pillar_obj.name)


class SpecificationAttributeWebserviceTests(SpecificationWebserviceTestCase):
    """Test accessing specification attributes over the webservice."""
    layer = AppServerLayer

    def test_representation_is_empty_on_1_dot_0(self):
        # ISpecification is exposed on the 1.0 version so that they can be
        # linked against branches, but none of its fields is exposed on that
        # version as we expect it to undergo significant refactorings before
        # it's ready for prime time.
        spec = self.factory.makeSpecification()
        user = self.factory.makePerson()
        webservice = webservice_for_person(user)
        response = webservice.get(
            '/%s/+spec/%s' % (spec.product.name, spec.name))
        expected_keys = [u'self_link', u'http_etag', u'resource_type_link',
                         u'web_link']
        self.assertEqual(response.status, 200)
        self.assertContentEqual(expected_keys, response.jsonBody().keys())

    def test_representation_contains_name(self):
        spec = self.factory.makeSpecification()
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.name, spec_webservice.name)

    def test_representation_contains_target(self):
        spec = self.factory.makeSpecification(
            product=self.factory.makeProduct())
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.target.name, spec_webservice.target.name)

    def test_representation_contains_title(self):
        spec = self.factory.makeSpecification(title='Foo')
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.title, spec_webservice.title)

    def test_representation_contains_specification_url(self):
        spec = self.factory.makeSpecification(specurl='http://example.com')
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.specurl, spec_webservice.specification_url)

    def test_representation_contains_summary(self):
        spec = self.factory.makeSpecification(summary='Foo')
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.summary, spec_webservice.summary)

    def test_representation_contains_implementation_status(self):
        spec = self.factory.makeSpecification()
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(
            spec.implementation_status.title,
            spec_webservice.implementation_status)

    def test_representation_contains_definition_status(self):
        spec = self.factory.makeSpecification()
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(
            spec.definition_status.title, spec_webservice.definition_status)

    def test_representation_contains_assignee(self):
        # Hard-code the person's name or else we'd need to set up a zope
        # interaction as IPerson.name is protected.
        spec = self.factory.makeSpecification(
            assignee=self.factory.makePerson(name='test-person'))
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual('test-person', spec_webservice.assignee.name)

    def test_representation_contains_drafter(self):
        spec = self.factory.makeSpecification(
            drafter=self.factory.makePerson(name='test-person'))
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual('test-person', spec_webservice.drafter.name)

    def test_representation_contains_approver(self):
        spec = self.factory.makeSpecification(
            approver=self.factory.makePerson(name='test-person'))
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual('test-person', spec_webservice.approver.name)

    def test_representation_contains_owner(self):
        spec = self.factory.makeSpecification(
            owner=self.factory.makePerson(name='test-person'))
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual('test-person', spec_webservice.owner.name)

    def test_representation_contains_priority(self):
        spec = self.factory.makeSpecification()
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.priority.title, spec_webservice.priority)

    def test_representation_contains_date_created(self):
        spec = self.factory.makeSpecification()
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.datecreated, spec_webservice.date_created)

    def test_representation_contains_whiteboard(self):
        spec = self.factory.makeSpecification(whiteboard='Test')
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(spec.whiteboard, spec_webservice.whiteboard)

    def test_representation_contains_milestone(self):
        product = self.factory.makeProduct()
        productseries = self.factory.makeProductSeries(product=product)
        milestone = self.factory.makeMilestone(
            name="1.0", product=product, productseries=productseries)
        spec_object = self.factory.makeSpecification(
            product=product, goal=productseries, milestone=milestone)
        spec = self.getSpecOnWebservice(spec_object)
        self.assertEqual("1.0", spec.milestone.name)

    def test_representation_contains_dependencies(self):
        spec = self.factory.makeSpecification()
        spec2 = self.factory.makeSpecification()
        spec.createDependency(spec2)
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(1, spec_webservice.dependencies.total_size)
        self.assertEqual(spec2.name, spec_webservice.dependencies[0].name)

    def test_representation_contains_linked_branches(self):
        spec = self.factory.makeSpecification()
        branch = self.factory.makeBranch()
        person = self.factory.makePerson()
        spec.linkBranch(branch, person)
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(1, spec_webservice.linked_branches.total_size)

    def test_representation_contains_bug_links(self):
        spec = self.factory.makeSpecification()
        bug = self.factory.makeBug()
        person = self.factory.makePerson()
        with person_logged_in(person):
            spec.linkBug(bug)
        spec_webservice = self.getSpecOnWebservice(spec)
        self.assertEqual(1, spec_webservice.bugs.total_size)
        self.assertEqual(bug.id, spec_webservice.bugs[0].id)


class SpecificationTargetTests(SpecificationWebserviceTestCase):
    """Tests for accessing specifications via their targets."""
    layer = AppServerLayer

    def test_get_specification_on_product(self):
        product = self.factory.makeProduct(name="fooix")
        self.factory.makeSpecification(
            product=product, name="some-spec")
        product_on_webservice = self.getPillarOnWebservice(product)
        spec = product_on_webservice.getSpecification(name="some-spec")
        self.assertEqual("some-spec", spec.name)
        self.assertEqual("fooix", spec.target.name)

    def test_get_specification_on_distribution(self):
        distribution = self.factory.makeDistribution(name="foobuntu")
        self.factory.makeSpecification(
            distribution=distribution, name="some-spec")
        distro_on_webservice = self.getPillarOnWebservice(distribution)
        spec = distro_on_webservice.getSpecification(name="some-spec")
        self.assertEqual("some-spec", spec.name)
        self.assertEqual("foobuntu", spec.target.name)

    def test_get_specification_on_productseries(self):
        product = self.factory.makeProduct(name="fooix")
        productseries = self.factory.makeProductSeries(
            product=product, name="fooix-dev")
        self.factory.makeSpecification(
            product=product, name="some-spec", goal=productseries)
        product_on_webservice = self.getPillarOnWebservice(product)
        productseries_on_webservice = product_on_webservice.getSeries(
            name="fooix-dev")
        spec = productseries_on_webservice.getSpecification(name="some-spec")
        self.assertEqual("some-spec", spec.name)
        self.assertEqual("fooix", spec.target.name)

    def test_get_specification_on_distroseries(self):
        distribution = self.factory.makeDistribution(name="foobuntu")
        distroseries = self.factory.makeDistroSeries(
            distribution=distribution, name="maudlin")
        self.factory.makeSpecification(
            distribution=distribution, name="some-spec",
            goal=distroseries)
        distro_on_webservice = self.getPillarOnWebservice(distribution)
        distroseries_on_webservice = distro_on_webservice.getSeries(
            name_or_version="maudlin")
        spec = distroseries_on_webservice.getSpecification(name="some-spec")
        self.assertEqual("some-spec", spec.name)
        self.assertEqual("foobuntu", spec.target.name)

    def test_get_specification_not_found(self):
        product = self.factory.makeProduct()
        product_on_webservice = self.getPillarOnWebservice(product)
        spec = product_on_webservice.getSpecification(name="nonexistant")
        self.assertEqual(None, spec)


class IHasSpecificationsTests(SpecificationWebserviceTestCase):
    """Tests for accessing IHasSpecifications methods over the webservice."""
    layer = DatabaseFunctionalLayer

    def assertNamesOfSpecificationsAre(self, expected_names, specifications):
        names = [s.name for s in specifications]
        self.assertContentEqual(expected_names, names)

    def test_anonymous_access_to_collection(self):
        product = self.factory.makeProduct()
        self.factory.makeSpecification(product=product, name="spec1")
        self.factory.makeSpecification(product=product, name="spec2")
        # Need to endInteraction() because launchpadlib_for_anonymous() will
        # setup a new one.
        endInteraction()
        lplib = launchpadlib_for('lplib-test', person=None, version='devel')
        ws_product = ws_object(lplib, product)
        self.assertNamesOfSpecificationsAre(
            ["spec1", "spec2"], ws_product.all_specifications)

    def test_product_all_specifications(self):
        product = self.factory.makeProduct()
        self.factory.makeSpecification(product=product, name="spec1")
        self.factory.makeSpecification(product=product, name="spec2")
        product_on_webservice = self.getPillarOnWebservice(product)
        self.assertNamesOfSpecificationsAre(
            ["spec1", "spec2"], product_on_webservice.all_specifications)

    def test_distribution_valid_specifications(self):
        distribution = self.factory.makeDistribution()
        self.factory.makeSpecification(
            distribution=distribution, name="spec1")
        self.factory.makeSpecification(
            distribution=distribution, name="spec2",
            status=SpecificationDefinitionStatus.OBSOLETE)
        distro_on_webservice = self.getPillarOnWebservice(distribution)
        self.assertNamesOfSpecificationsAre(
            ["spec1"], distro_on_webservice.valid_specifications)


class TestSpecificationSubscription(SpecificationWebserviceTestCase):

    layer = AppServerLayer

    def test_subscribe(self):
        # Test subscribe() API.
        with person_logged_in(ANONYMOUS):
            db_spec = self.factory.makeSpecification()
            db_person = self.factory.makePerson()
            launchpad = self.factory.makeLaunchpadService()

        spec = ws_object(launchpad, db_spec)
        person = ws_object(launchpad, db_person)
        spec.subscribe(person=person, essential=True)
        transaction.commit()

        # Check the results.
        sub = db_spec.subscription(db_person)
        self.assertIsNot(None, sub)
        self.assertTrue(sub.essential)

    def test_unsubscribe(self):
        # Test unsubscribe() API.
        with person_logged_in(ANONYMOUS):
            db_spec = self.factory.makeBlueprint()
            db_person = self.factory.makePerson()
            db_spec.subscribe(person=db_person)
            launchpad = self.factory.makeLaunchpadService(person=db_person)

        spec = ws_object(launchpad, db_spec)
        person = ws_object(launchpad, db_person)
        spec.unsubscribe(person=person)
        transaction.commit()

        # Check the results.
        self.assertFalse(db_spec.isSubscribed(db_person))

    def test_canBeUnsubscribedByUser(self):
        # Test canBeUnsubscribedByUser() API.
        webservice = LaunchpadWebServiceCaller(
            'launchpad-library', 'salgado-change-anything',
            domain='api.launchpad.dev:8085')

        with person_logged_in(ANONYMOUS):
            db_spec = self.factory.makeSpecification()
            db_person = self.factory.makePerson()
            launchpad = self.factory.makeLaunchpadService()

            spec = ws_object(launchpad, db_spec)
            person = ws_object(launchpad, db_person)
            subscription = spec.subscribe(person=person, essential=True)
            transaction.commit()

        result = webservice.named_get(
            subscription['self_link'], 'canBeUnsubscribedByUser').jsonBody()
        self.assertFalse(result)


class TestSpecificationBugLinks(SpecificationWebserviceTestCase):

    layer = AppServerLayer

    def test_bug_linking(self):
        # Set up a spec, person, and bug.
        with person_logged_in(ANONYMOUS):
            db_spec = self.factory.makeSpecification()
            db_person = self.factory.makePerson()
            db_bug = self.factory.makeBug()
            launchpad = self.factory.makeLaunchpadService()

        # Link the bug to the spec via the web service.
        with person_logged_in(db_person):
            spec = ws_object(launchpad, db_spec)
            bug = ws_object(launchpad, db_bug)
            # There are no bugs associated with the spec/blueprint yet.
            self.assertEqual(0, spec.bugs.total_size)
            spec.linkBug(bug=bug)
            transaction.commit()

        # The spec now has one bug associated with it and that bug is the one
        # we linked.
        self.assertEqual(1, spec.bugs.total_size)
        self.assertEqual(bug.id, spec.bugs[0].id)

    def test_bug_unlinking(self):
        # Set up a spec, person, and bug, then link the bug to the spec.
        with person_logged_in(ANONYMOUS):
            db_spec = self.factory.makeBlueprint()
            db_person = self.factory.makePerson()
            db_bug = self.factory.makeBug()
            launchpad = self.factory.makeLaunchpadService(person=db_person)

        spec = ws_object(launchpad, db_spec)
        bug = ws_object(launchpad, db_bug)
        spec.linkBug(bug=bug)

        # There is only one bug linked at the moment.
        self.assertEqual(1, spec.bugs.total_size)

        spec.unlinkBug(bug=bug)
        transaction.commit()

        # Now that we've unlinked the bug, there are no linked bugs at all.
        self.assertEqual(0, spec.bugs.total_size)