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

"""Browser tests for Packaging actions."""

__metaclass__ = type

from unittest import TestLoader

from zope.component import getUtility

from canonical.launchpad.ftests import (
    login,
    logout,
    )
from canonical.launchpad.testing.pages import setupBrowser
from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    PageTestLayer,
    )
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
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.testing import TestCaseWithFactory
from lp.testing.memcache import MemcacheTestCase
from lp.testing.views import create_initialized_view


class TestProductSeriesUbuntuPackagingView(TestCaseWithFactory):
    """Browser tests for deletion of Packaging objects."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestProductSeriesUbuntuPackagingView, self).setUp()
        self.ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        self.hoary = self.ubuntu.getSeries('hoary')
        self.sourcepackagename = self.factory.makeSourcePackageName('hot')
        self.sourcepackage = self.factory.makeSourcePackage(
            sourcepackagename=self.sourcepackagename, distroseries=self.hoary)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.sourcepackagename, distroseries=self.hoary)
        self.product = self.factory.makeProduct(name="hot", displayname='Hot')
        self.productseries = self.factory.makeProductSeries(
            product=self.product, name='hotter')
        self.packaging_util = getUtility(IPackagingUtil)

    def test_no_error_when_trying_to_readd_same_package(self):
        # There is no reason to display an error when the user's action
        # wouldn't cause a state change.
        self.packaging_util.createPackaging(
            productseries=self.productseries,
            sourcepackagename=self.sourcepackagename,
            distroseries=self.hoary, packaging=PackagingType.PRIME,
            owner=self.product.owner)

        form = {
            'field.distroseries': self.hoary.name,
            'field.sourcepackagename': self.sourcepackagename.name,
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            self.productseries, '+ubuntupkg', form=form)
        self.assertEqual([], view.errors)

    def test_cannot_link_to_linked_package(self):
        # Once a distro series sourcepackage is linked to a product series,
        # no other product series can link to it.
        form = {
            'field.distroseries': 'hoary',
            'field.sourcepackagename': 'hot',
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            self.productseries, '+ubuntupkg', form=form)
        self.assertEqual([], view.errors)
        other_productseries = self.factory.makeProductSeries(
            product=self.product, name='hottest')
        form = {
            'field.distroseries': 'hoary',
            'field.sourcepackagename': 'hot',
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            other_productseries, '+ubuntupkg', form=form)
        view_errors = [
            'The <a href="http://launchpad.dev/ubuntu/hoary/+source/hot">'
             'hot</a> package in Hoary is already linked to another series.']
        self.assertEqual(view_errors, view.errors)

    def test_sourcepackgename_required(self):
        # A source package name must be provided.
        form = {
            'field.distroseries': 'hoary',
            'field.sourcepackagename': '',
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            self.productseries, '+ubuntupkg', form=form)
        self.assertEqual(1, len(view.errors))
        self.assertEqual('sourcepackagename', view.errors[0].field_name)
        self.assertEqual('Required input is missing.', view.errors[0].doc())

    def test_cannot_link_to_nonexistant_ubuntu_package(self):
        # In the case of full functionality distributions like Ubuntu, the
        # source package must be published in the distro series.
        self.factory.makeSourcePackageName('vapor')
        form = {
            'field.distroseries': 'hoary',
            'field.sourcepackagename': 'vapor',
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            self.productseries, '+ubuntupkg', form=form)
        view_errors = ['The source package is not published in Hoary.']
        self.assertEqual(view_errors, view.errors)

    def test_link_older_distroseries(self):
        # The view allows users to link to older Ubuntu series.
        warty = self.ubuntu.getSeries('warty')
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.sourcepackagename, distroseries=warty)
        form = {
            'field.distroseries': 'warty',
            'field.sourcepackagename': 'hot',
            'field.actions.continue': 'Continue',
            }
        view = create_initialized_view(
            self.productseries, '+ubuntupkg', form=form)
        self.assertEqual([], view.errors)
        has_packaging = self.packaging_util.packagingEntryExists(
            self.sourcepackagename, warty, self.productseries)
        self.assertTrue(has_packaging)


class TestBrowserDeletePackaging(TestCaseWithFactory):
    """Browser tests for deletion of Packaging objects."""

    layer = PageTestLayer

    def setUp(self):
        super(TestBrowserDeletePackaging, self).setUp()
        # Only the person which created the packaging, admins
        # and other people with certain privileges can delete a
        # packaging. Since the sample data record we'll use for
        # deletion testing does not have any owner set, we'll
        # log in as an admin.
        self.user_browser = setupBrowser(
            auth="Basic foo.bar@canonical.com:test")

    def test_deletionIsPersistent(self):
        # Test that deleting a Packaging entry is persistent.
        #
        # When developing the initial Packaging deletion feature, we hit a bug
        # where submitting the Packaging deletion form apparently worked, and
        # rendered a page where the deleted Packaging was not present, but a
        # silent error occurred while rendering the page, which caused the
        # transaction to abort. As a consequence, the Packaging deletion was
        # not recorded, and reloading the page would make the deleted
        # Packaging data reappear on the page.
        # Check sampledata expectations
        login('no-priv@canonical.com')
        source_package_name_set = getUtility(ISourcePackageNameSet)
        package_name = source_package_name_set.queryByName('alsa-utils')
        distribution_set = getUtility(IDistributionSet)
        distroseries = distribution_set.getByName('ubuntu').getSeries('warty')
        product_set = getUtility(IProductSet)
        product = product_set.getByName('alsa-utils')
        productseries = product.getSeries('trunk')
        packaging_util = getUtility(IPackagingUtil)
        self.assertTrue(packaging_util.packagingEntryExists(
            productseries=productseries,
            sourcepackagename=package_name,
            distroseries=distroseries))
        logout()
        # Delete the packaging
        user_browser = self.user_browser
        user_browser.open('http://launchpad.dev/ubuntu/+source/alsa-utils')
        link = user_browser.getLink(
            url='/ubuntu/warty/+source/alsa-utils/+remove-packaging')
        link.click()
        user_browser.getControl('Unlink').click()
        # Check that the change was committed.
        login('no-priv@canonical.com')
        self.assertFalse(packaging_util.packagingEntryExists(
            productseries=productseries,
            sourcepackagename=package_name,
            distroseries=distroseries))


class TestDistroseriesPackagingMemcache(MemcacheTestCase):
    """Tests distroseries packaging cache rules."""

    def setUp(self):
        super(TestDistroseriesPackagingMemcache, self).setUp()
        ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
        self.hoary = ubuntu.getSeries('hoary')
        self.observer = self.factory.makePerson()

    def test_needs_packaging_memcache(self):
        # Verify that the packages table is cached.
        # Miss the cache on first render.
        view = create_initialized_view(
            self.hoary, name='+needs-packaging', principal=self.observer)
        self.assertCacheMiss('<table id="packages"', view.render())
        # Hit the cache on the second render.
        view = create_initialized_view(
            self.hoary, name='+needs-packaging', principal=self.observer)
        self.assertCacheHit(
            '<table id="packages"', 'public, 30 minute', view.render())

    def test_packaging_memcache(self):
        # Verify that the packagings table is cached.
        # Miss the cache on first render.
        view = create_initialized_view(
            self.hoary, name='+packaging', principal=self.observer)
        self.assertCacheMiss('<table id="packagings"', view.render())
        # Hit the cache on the second render.
        view = create_initialized_view(
            self.hoary, name='+packaging', principal=self.observer)
        self.assertCacheHit(
            '<table id="packagings"', 'public, 30 minute', view.render())


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