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

"""Tests for the Packaging content class."""

__metaclass__ = type

from unittest import TestLoader

from lazr.lifecycle.event import (
    ObjectCreatedEvent,
    ObjectDeletedEvent,
    )
from zope.component import getUtility
from zope.security.interfaces import Unauthorized
from zope.security.proxy import removeSecurityProxy

from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    LaunchpadFunctionalLayer,
    )
from lp.registry.interfaces.distribution import IDistributionSet
from lp.registry.interfaces.packaging import (
    IPackagingUtil,
    PackagingType,
    )
from lp.registry.interfaces.product import IProductSet
from lp.registry.interfaces.sourcepackagename import ISourcePackageNameSet
from lp.registry.model.packaging import (
    Packaging,
    )
from lp.soyuz.tests.test_publishing import SoyuzTestPublisher
from lp.testing import (
    EventRecorder,
    login,
    person_logged_in,
    TestCaseWithFactory,
    )


class TestPackaging(TestCaseWithFactory):
    """Test Packaging object."""

    layer = LaunchpadFunctionalLayer

    def test_init_notifies(self):
        """Creating a Packaging should generate an event."""
        with EventRecorder() as recorder:
            packaging = Packaging()
        (event,) = recorder.events
        self.assertIsInstance(event, ObjectCreatedEvent)
        self.assertIs(packaging, event.object)

    def test_destroySelf_notifies(self):
        """destroySelf creates a notification."""
        packaging_util = getUtility(IPackagingUtil)
        packaging = self.factory.makePackagingLink()
        with person_logged_in(packaging.owner):
            with EventRecorder() as recorder:
                removeSecurityProxy(packaging).destroySelf()
        (event,) = recorder.events
        self.assertIsInstance(event, ObjectDeletedEvent)
        self.assertIs(removeSecurityProxy(packaging), event.object)

    def test_destroySelf__not_allowed_for_anonymous(self):
        """Anonymous cannot delete a packaging."""
        packaging = self.factory.makePackagingLink()
        packaging_util = getUtility(IPackagingUtil)
        self.assertRaises(
            Unauthorized, packaging_util.deletePackaging,
            packaging.productseries, packaging.sourcepackagename,
            packaging.distroseries)

    def test_destroySelf__not_allowed_for_arbitrary_user(self):
        """Arbitrary users cannot delete a packaging."""
        packaging = self.factory.makePackagingLink()
        packaging_util = getUtility(IPackagingUtil)
        with person_logged_in(self.factory.makePerson()):
            self.assertRaises(
                Unauthorized, packaging_util.deletePackaging,
                packaging.productseries, packaging.sourcepackagename,
                packaging.distroseries)

    def test_destroySelf__allowed_for_packaging_owner(self):
        """A packaging owner can delete a packaging."""
        packaging = self.factory.makePackagingLink()
        sourcepackagename = packaging.sourcepackagename
        distroseries = packaging.distroseries
        productseries = packaging.productseries
        packaging_util = getUtility(IPackagingUtil)
        with person_logged_in(packaging.owner):
            packaging_util.deletePackaging(
                packaging.productseries, packaging.sourcepackagename,
                packaging.distroseries)
        self.assertFalse(
            packaging_util.packagingEntryExists(
                sourcepackagename, distroseries, productseries))

    def test_destroySelf__allowed_for_package_maintainer(self):
        """A package maintainer can delete a packaging."""
        # The package maintainer is the, strictly speaking, the
        # maintainer of the current release. SoyuzTestPublisher
        # knows how to create a current release.
        publisher = SoyuzTestPublisher()
        distroseries = publisher.setUpDefaultDistroSeries()
        publisher.getPubSource(version='0.9')
        sourcepackage = distroseries.getSourcePackage(
            publisher.default_package_name)
        sourcepackagename = sourcepackage.sourcepackagename
        packaging = publisher.factory.makePackagingLink(
            sourcepackagename=sourcepackagename, distroseries=distroseries)
        productseries = packaging.productseries
        packaging_util = getUtility(IPackagingUtil)

        with person_logged_in(sourcepackage.currentrelease.maintainer):
            packaging_util.deletePackaging(
                packaging.productseries, packaging.sourcepackagename,
                packaging.distroseries)
        self.assertFalse(
            packaging_util.packagingEntryExists(
                sourcepackagename, distroseries, productseries))

    def test_destroySelf__allowed_for_admin(self):
        """A Launchpad admin can delete a packaging."""
        packaging = self.factory.makePackagingLink()
        sourcepackagename = packaging.sourcepackagename
        distroseries = packaging.distroseries
        productseries = packaging.productseries
        packaging_util = getUtility(IPackagingUtil)
        login('foo.bar@canonical.com')
        packaging_util.deletePackaging(
            packaging.productseries, packaging.sourcepackagename,
            packaging.distroseries)
        self.assertFalse(
            packaging_util.packagingEntryExists(
                sourcepackagename, distroseries, productseries))


class PackagingUtilMixin:
    """Common items for testing IPackagingUtil."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        TestCaseWithFactory.setUp(self)
        self.packaging_util = getUtility(IPackagingUtil)
        self.sourcepackagename = self.factory.makeSourcePackageName('sparkle')
        self.distroseries = self.factory.makeDistroRelease(name='dazzle')
        self.productseries = self.factory.makeProductSeries(name='glitter')
        self.owner = self.productseries.product.owner


class TestCreatePackaging(PackagingUtilMixin, TestCaseWithFactory):
    """Test PackagingUtil.packagingEntryExists."""

    def test_CreatePackaging_unique(self):
        """Packaging is unique distroseries+sourcepackagename."""
        self.packaging_util.createPackaging(
            self.productseries, self.sourcepackagename, self.distroseries,
            PackagingType.PRIME, owner=self.owner)
        sourcepackage = self.distroseries.getSourcePackage('sparkle')
        packaging = sourcepackage.direct_packaging
        self.assertEqual(packaging.distroseries, self.distroseries)
        self.assertEqual(packaging.sourcepackagename, self.sourcepackagename)
        self.assertEqual(packaging.productseries, self.productseries)

    def test_CreatePackaging_assert_unique(self):
        """Assert unique distroseries+sourcepackagename."""
        self.packaging_util.createPackaging(
            self.productseries, self.sourcepackagename, self.distroseries,
            PackagingType.PRIME, owner=self.owner)
        self.assertRaises(
            AssertionError, self.packaging_util.createPackaging,
            self.productseries, self.sourcepackagename, self.distroseries,
            PackagingType.PRIME, self.owner)


class TestPackagingEntryExists(PackagingUtilMixin, TestCaseWithFactory):
    """Test PackagingUtil.packagingEntryExists."""

    def setUpPackaging(self):
        self.packaging_util.createPackaging(
            self.productseries, self.sourcepackagename, self.distroseries,
            PackagingType.PRIME, owner=self.owner)

    def test_packagingEntryExists_false(self):
        """Verify that non-existent entries are false."""
        self.assertFalse(
            self.packaging_util.packagingEntryExists(
                sourcepackagename=self.sourcepackagename,
                distroseries=self.distroseries))

    def test_packagingEntryExists_unique(self):
        """Packaging entries are unique to distroseries+sourcepackagename."""
        self.setUpPackaging()
        self.assertTrue(
            self.packaging_util.packagingEntryExists(
                sourcepackagename=self.sourcepackagename,
                distroseries=self.distroseries))
        other_distroseries = self.factory.makeDistroRelease(name='shimmer')
        self.assertFalse(
            self.packaging_util.packagingEntryExists(
                sourcepackagename=self.sourcepackagename,
                distroseries=other_distroseries))

    def test_packagingEntryExists_specific(self):
        """Packaging entries are also specifc to both kinds of series."""
        self.setUpPackaging()
        self.assertTrue(
            self.packaging_util.packagingEntryExists(
                sourcepackagename=self.sourcepackagename,
                distroseries=self.distroseries,
                productseries=self.productseries))
        other_productseries = self.factory.makeProductSeries(name='flash')
        self.assertFalse(
            self.packaging_util.packagingEntryExists(
                sourcepackagename=self.sourcepackagename,
                distroseries=self.distroseries,
                productseries=other_productseries))


class TestDeletePackaging(TestCaseWithFactory):
    """Test PackagingUtil.deletePackaging.

    The essential functionality: deleting a Packaging record, is already
    covered in doctests.
    """

    layer = DatabaseFunctionalLayer

    def test_deleteNonExistentPackaging(self):
        """Deleting a non-existent Packaging fails.

        PackagingUtil.deletePackaging raises an Assertion error with a
        useful message if the specified Packaging record does not exist.
        """
        # Any authenticated user can delete a packaging entry.
        login('no-priv@canonical.com')

        # Get a SourcePackageName from the sample data.
        source_package_name_set = getUtility(ISourcePackageNameSet)
        firefox_name = source_package_name_set.queryByName('mozilla-firefox')

        # Get a DistroSeries from the sample data.
        distribution_set = getUtility(IDistributionSet)
        ubuntu_hoary = distribution_set.getByName('ubuntu').getSeries('hoary')

        # Get a ProductSeries from the sample data.
        product_set = getUtility(IProductSet)
        firefox_trunk = product_set.getByName('firefox').getSeries('trunk')

        # There must not be a packaging entry associating mozilla-firefox
        # ubunt/hoary to firefox/trunk.
        packaging_util = getUtility(IPackagingUtil)
        self.assertFalse(
            packaging_util.packagingEntryExists(
                productseries=firefox_trunk,
                sourcepackagename=firefox_name,
                distroseries=ubuntu_hoary),
            "This packaging entry should not exist in sample data.")

        # If we try to delete this non-existent entry, we get an
        # AssertionError with a helpful message.
        try:
            packaging_util.deletePackaging(
                productseries=firefox_trunk,
                sourcepackagename=firefox_name,
                distroseries=ubuntu_hoary)
        except AssertionError, exception:
            self.assertEqual(
                str(exception),
                "Tried to delete non-existent Packaging: "
                "productseries=trunk/firefox, "
                "sourcepackagename=mozilla-firefox, "
                "distroseries=ubuntu/hoary")
        else:
            self.fail("AssertionError was not raised.")

    def test_deletePackaging_notifies(self):
        """Deleting a Packaging creates a notification."""
        packaging_util = getUtility(IPackagingUtil)
        packaging = self.factory.makePackagingLink()
        with person_logged_in(packaging.owner):
            with EventRecorder() as recorder:
                packaging_util.deletePackaging(
                    packaging.productseries, packaging.sourcepackagename,
                    packaging.distroseries)
        (event,) = recorder.events
        self.assertIsInstance(event, ObjectDeletedEvent)
        self.assertIs(removeSecurityProxy(packaging), event.object)


def test_suite():
    return TestLoader().loadTestsFromName(__name__)