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

__metaclass__ = type

import re

from BeautifulSoup import BeautifulSoup

from zope.interface import (
    implements,
    Interface,
    )
from zope.app.form.browser.interfaces import IBrowserWidget
from zope.app.form.interfaces import IInputWidget

from lazr.restful.fields import Reference

from canonical.launchpad.webapp.servers import LaunchpadTestRequest
from canonical.launchpad.webapp.testing import verifyObject
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.app.widgets.launchpadtarget import LaunchpadTargetWidget
from lp.app.validators import LaunchpadValidationError
from lp.registry.vocabularies import (
    DistributionVocabulary,
    DistributionSourcePackageVocabulary,
    ProductVocabulary,
    )
from lp.services.features.testing import FeatureFixture
from lp.soyuz.model.binaryandsourcepackagename import (
    BinaryAndSourcePackageNameVocabulary,
    )
from lp.testing import TestCaseWithFactory


class IThing(Interface):
    target = Reference(schema=Interface)


class Thing:
    implements(IThing)
    target = None


class LaunchpadTargetWidgetTestCase(TestCaseWithFactory):
    """Test the LaunchpadTargetWidget class."""

    layer = DatabaseFunctionalLayer

    @property
    def form(self):
        return {
        'field.target': 'package',
        'field.target.distribution': 'fnord',
        'field.target.package': 'snarf',
        'field.target.product': 'pting',
        }

    def setUp(self):
        super(LaunchpadTargetWidgetTestCase, self).setUp()
        self.distribution, self.package = self.factory.makeDSPCache(
            distro_name='fnord', package_name='snarf')
        self.project = self.factory.makeProduct('pting')
        field = Reference(
            __name__='target', schema=Interface, title=u'target')
        field = field.bind(Thing())
        request = LaunchpadTestRequest()
        self.widget = LaunchpadTargetWidget(field, request)

    def test_implements(self):
        self.assertTrue(verifyObject(IBrowserWidget, self.widget))
        self.assertTrue(verifyObject(IInputWidget, self.widget))

    def test_template(self):
        # The render template is setup.
        self.assertTrue(
            self.widget.template.filename.endswith('launchpad-target.pt'),
            'Template was not setup.')

    def test_default_option(self):
        # This package field is the default option.
        self.assertEqual('package', self.widget.default_option)

    def test_getDistributionVocabulary(self):
        # The vocabulary is always "Distribution".
        self.assertEqual(
            'Distribution', self.widget.getDistributionVocabulary())

    def test_hasInput_false(self):
        # hasInput is false when the widget's name is not in the form data.
        self.widget.request = LaunchpadTestRequest(form={})
        self.assertEqual('field.target', self.widget.name)
        self.assertFalse(self.widget.hasInput())

    def test_hasInput_true(self):
        # hasInput is true is the widget's name in the form data.
        self.widget.request = LaunchpadTestRequest(form=self.form)
        self.assertEqual('field.target', self.widget.name)
        self.assertTrue(self.widget.hasInput())

    def test_setUpSubWidgets_first_call(self):
        # The subwidgets are setup and a flag is set.
        self.widget.setUpSubWidgets()
        self.assertTrue(self.widget._widgets_set_up)
        self.assertIsInstance(
            self.widget.distribution_widget.context.vocabulary,
            DistributionVocabulary)
        self.assertIsInstance(
            self.widget.package_widget.context.vocabulary,
            BinaryAndSourcePackageNameVocabulary)
        self.assertIsInstance(
            self.widget.product_widget.context.vocabulary,
            ProductVocabulary)

    def test_setUpSubWidgets_second_call(self):
        # The setUpSubWidgets method exits early if a flag is set to
        # indicate that the widgets were setup.
        self.widget._widgets_set_up = True
        self.widget.setUpSubWidgets()
        self.assertIs(None, getattr(self.widget, 'distribution_widget', None))
        self.assertIs(None, getattr(self.widget, 'package_widget', None))
        self.assertIs(None, getattr(self.widget, 'product_widget', None))

    def test_setUpSubWidgets_dsp_picker_feature_flag(self):
        # The DistributionSourcePackageVocabulary is used when the
        # disclosure.dsp_picker.enabled is true.
        with FeatureFixture({u"disclosure.dsp_picker.enabled": u"on"}):
            self.widget.setUpSubWidgets()
        self.assertIsInstance(
            self.widget.package_widget.context.vocabulary,
            DistributionSourcePackageVocabulary)

    def test_setUpOptions_default_package_checked(self):
        # The radio button options are composed of the setup widgets with
        # the package widget set as the default.
        self.widget.setUpSubWidgets()
        self.widget.setUpOptions()
        self.assertEqual(
            "selectWidget('field.target.option.package', event)",
            self.widget.package_widget.onKeyPress)
        self.assertEqual(
            "selectWidget('field.target.option.product', event)",
            self.widget.product_widget.onKeyPress)
        self.assertEqual(
            '<input class="radioType" checked="checked" '
            'id="field.target.option.package" name="field.target" '
            'type="radio" value="package" />',
            self.widget.options['package'])
        self.assertEqual(
            '<input class="radioType" '
            'id="field.target.option.product" name="field.target" '
            'type="radio" value="product" />',
            self.widget.options['product'])

    def test_setUpOptions_product_checked(self):
        # The product radio button is selected when the form is submitted
        # when the target field's value is 'product'.
        form = {
            'field.target': 'product',
            }
        self.widget.request = LaunchpadTestRequest(form=form)
        self.widget.setUpSubWidgets()
        self.widget.setUpOptions()
        self.assertEqual(
            '<input class="radioType" '
            'id="field.target.option.package" name="field.target" '
            'type="radio" value="package" />',
            self.widget.options['package'])
        self.assertEqual(
            '<input class="radioType" checked="checked" '
            'id="field.target.option.product" name="field.target" '
            'type="radio" value="product" />',
            self.widget.options['product'])

    def test_hasValidInput_true(self):
        # The field input is valid when all submitted parts are valid.
        self.widget.request = LaunchpadTestRequest(form=self.form)
        self.assertTrue(self.widget.hasValidInput())

    def test_hasValidInput_false(self):
        # The field input is invalid if any of the submitted parts are
        # invalid.
        form = self.form
        form['field.target.package'] = 'non-existent'
        self.widget.request = LaunchpadTestRequest(form=form)
        self.assertFalse(self.widget.hasValidInput())

    def test_getInputValue_package_spn(self):
        # The field value is the package when the package radio button
        # is selected and the package sub field has official spn.
        self.widget.request = LaunchpadTestRequest(form=self.form)
        self.assertEqual(self.package, self.widget.getInputValue())

    def test_getInputValue_package_spn_dsp_picker_feature_flag(self):
        # The field value is the package when the package radio button
        # is selected and the package sub field has a official dsp.
        self.widget.request = LaunchpadTestRequest(form=self.form)
        with FeatureFixture({u"disclosure.dsp_picker.enabled": u"on"}):
            self.widget.setUpSubWidgets()
            self.assertEqual(self.package, self.widget.getInputValue())

    def test_getInputValue_package_dsp_dsp_picker_feature_flag(self):
        # The field value is the package when the package radio button
        # is selected and the package sub field has valid input.
        form = self.form
        form['field.target.package'] = 'fnord/snarf'
        self.widget.request = LaunchpadTestRequest(form=form)
        with FeatureFixture({u"disclosure.dsp_picker.enabled": u"on"}):
            self.widget.setUpSubWidgets()
            self.assertEqual(self.package, self.widget.getInputValue())

    def test_getInputValue_package_invalid(self):
        # An error is raised when the package is not published in the distro.
        form = self.form
        form['field.target.package'] = 'non-existent'
        self.widget.request = LaunchpadTestRequest(form=form)
        message = (
            "There is no package name 'non-existent' published in Fnord")
        self.assertRaisesWithContent(
            LaunchpadValidationError, message, self.widget.getInputValue)
        self.assertEqual(message, self.widget.error())

    def test_getInputValue_distribution(self):
        # The field value is the distribution when the package radio button
        # is selected and the package sub field empty.
        form = self.form
        form['field.target.package'] = ''
        self.widget.request = LaunchpadTestRequest(form=form)
        self.assertEqual(self.distribution, self.widget.getInputValue())

    def test_getInputValue_distribution_invalid(self):
        # An error is raised when the distribution is invalid.
        form = self.form
        form['field.target.package'] = ''
        form['field.target.distribution'] = 'non-existent'
        self.widget.request = LaunchpadTestRequest(form=form)
        message = (
            "There is no distribution named 'non-existent' registered in "
            "Launchpad")
        self.assertRaisesWithContent(
            LaunchpadValidationError, message, self.widget.getInputValue)
        self.assertEqual(message, self.widget.error())

    def test_getInputValue_product(self):
        # The field value is the product when the project radio button
        # is selected and the project sub field is valid.
        form = self.form
        form['field.target'] = 'product'
        self.widget.request = LaunchpadTestRequest(form=form)
        self.assertEqual(self.project, self.widget.getInputValue())

    def test_getInputValue_product_missing(self):
        # An error is raised when the product field is missing.
        form = self.form
        form['field.target'] = 'product'
        del form['field.target.product']
        self.widget.request = LaunchpadTestRequest(form=form)
        message = 'Please enter a project name'
        self.assertRaisesWithContent(
            LaunchpadValidationError, message, self.widget.getInputValue)
        self.assertEqual(message, self.widget.error())

    def test_getInputValue_product_invalid(self):
        # An error is raised when the product is not valid.
        form = self.form
        form['field.target'] = 'product'
        form['field.target.product'] = 'non-existent'
        self.widget.request = LaunchpadTestRequest(form=form)
        message = (
            "There is no project named 'non-existent' registered in "
            "Launchpad")
        self.assertRaisesWithContent(
            LaunchpadValidationError, message, self.widget.getInputValue)
        self.assertEqual(message, self.widget.error())

    def test_setRenderedValue_product(self):
        # Passing a product will set the widget's render state to 'product'.
        self.widget.setUpSubWidgets()
        self.widget.setRenderedValue(self.project)
        self.assertEqual('product', self.widget.default_option)
        self.assertEqual(
            self.project, self.widget.product_widget._getCurrentValue())

    def test_setRenderedValue_distribution(self):
        # Passing a distribution will set the widget's render state to
        # 'package', but only the distribution widget is set.
        self.widget.setUpSubWidgets()
        self.widget.setRenderedValue(self.distribution)
        self.assertEqual('package', self.widget.default_option)
        self.assertEqual(
            self.distribution,
            self.widget.distribution_widget._getCurrentValue())
        self.assertEqual(
            None, self.widget.package_widget._getCurrentValue())

    def test_setRenderedValue_package(self):
        # Passing a package will set the widget's render state to
        # 'package'.
        self.widget.setUpSubWidgets()
        self.widget.setRenderedValue(self.package)
        self.assertEqual('package', self.widget.default_option)
        self.assertEqual(
            self.distribution,
            self.widget.distribution_widget._getCurrentValue())
        self.assertEqual(
            self.package.sourcepackagename,
            self.widget.package_widget._getCurrentValue())

    def test_call(self):
        # The __call__ method setups the widgets and the options.
        markup = self.widget()
        self.assertIsNot(None, self.widget.package_widget)
        self.assertTrue('package' in self.widget.options)
        expected_ids = [
            'field.target.distribution',
            'field.target.option.package',
            'field.target.option.product',
            'field.target.package',
            'field.target.product',
            ]
        soup = BeautifulSoup(markup)
        fields = soup.findAll(['input', 'select'], {'id': re.compile('.*')})
        ids = [field['id'] for field in fields]
        self.assertContentEqual(expected_ids, ids)