~launchpad-pqm/launchpad/devel

11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
1
#!/usr/bin/python
2
#
3
# Copyright 2010 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
11426.1.6 by Guilherme Salgado
Add a unit test for AbstractUploadPolicy.setDistroSeriesAndPocket
6
from lp.app.errors import NotFoundError
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
7
from lp.archiveuploader.uploadpolicy import (
11426.1.5 by Guilherme Salgado
A couple changes suggested by Jeroen
8
    AbstractUploadPolicy,
9
    ArchiveUploadType,
10
    )
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
11
from lp.testing import (
12
    TestCase,
13
    TestCaseWithFactory,
14
    )
14612.2.1 by William Grant
format-imports on lib/. So many imports.
15
from lp.testing.layers import DatabaseFunctionalLayer
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
16
17
18
class TestUploadPolicy_validateUploadType(TestCase):
19
    """Test what kind (sourceful/binaryful/mixed) of uploads are accepted."""
20
21
    def test_sourceful_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
22
        policy = make_policy(accepted_type=ArchiveUploadType.SOURCE_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
23
        upload = make_fake_upload(sourceful=True)
24
25
        policy.validateUploadType(upload)
26
27
        self.assertEquals([], upload.rejections)
28
29
    def test_binaryful_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
30
        policy = make_policy(accepted_type=ArchiveUploadType.BINARY_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
31
        upload = make_fake_upload(binaryful=True)
32
33
        policy.validateUploadType(upload)
34
35
        self.assertEquals([], upload.rejections)
36
37
    def test_mixed_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
38
        policy = make_policy(accepted_type=ArchiveUploadType.MIXED_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
39
        upload = make_fake_upload(sourceful=True, binaryful=True)
40
41
        policy.validateUploadType(upload)
42
43
        self.assertEquals([], upload.rejections)
44
45
    def test_sourceful_not_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
46
        policy = make_policy(accepted_type=ArchiveUploadType.BINARY_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
47
        upload = make_fake_upload(sourceful=True)
48
49
        policy.validateUploadType(upload)
50
51
        self.assertIn(
52
            'Sourceful uploads are not accepted by this policy.',
53
            upload.rejections)
54
55
    def test_binaryful_not_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
56
        policy = make_policy(accepted_type=ArchiveUploadType.SOURCE_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
57
        upload = make_fake_upload(binaryful=True)
58
59
        policy.validateUploadType(upload)
60
61
        self.assertTrue(len(upload.rejections) > 0)
62
        self.assertIn(
63
            'Upload rejected because it contains binary packages.',
64
            upload.rejections[0])
65
66
    def test_mixed_not_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
67
        policy = make_policy(accepted_type=ArchiveUploadType.SOURCE_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
68
        upload = make_fake_upload(sourceful=True, binaryful=True)
69
70
        policy.validateUploadType(upload)
71
72
        self.assertIn(
73
            'Source/binary (i.e. mixed) uploads are not allowed.',
74
            upload.rejections)
75
76
    def test_sourceful_when_only_mixed_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
77
        policy = make_policy(accepted_type=ArchiveUploadType.MIXED_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
78
        upload = make_fake_upload(sourceful=True, binaryful=False)
79
80
        policy.validateUploadType(upload)
81
82
        self.assertIn(
83
            'Sourceful uploads are not accepted by this policy.',
84
            upload.rejections)
85
86
    def test_binaryful_when_only_mixed_accepted(self):
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
87
        policy = make_policy(accepted_type=ArchiveUploadType.MIXED_ONLY)
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
88
        upload = make_fake_upload(sourceful=False, binaryful=True)
89
90
        policy.validateUploadType(upload)
91
92
        self.assertTrue(len(upload.rejections) > 0)
93
        self.assertIn(
94
            'Upload rejected because it contains binary packages.',
95
            upload.rejections[0])
96
97
11426.1.6 by Guilherme Salgado
Add a unit test for AbstractUploadPolicy.setDistroSeriesAndPocket
98
class TestUploadPolicy(TestCaseWithFactory):
99
100
    layer = DatabaseFunctionalLayer
101
102
    def test_setDistroSeriesAndPocket_distro_not_found(self):
103
        policy = AbstractUploadPolicy()
104
        policy.distro = self.factory.makeDistribution()
105
        self.assertRaises(
106
            NotFoundError, policy.setDistroSeriesAndPocket,
107
            'nonexistent_security')
108
109
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
110
class FakeNascentUpload:
111
112
    def __init__(self, sourceful, binaryful):
113
        self.sourceful = sourceful
114
        self.binaryful = binaryful
115
        self.is_ppa = False
116
        self.rejections = []
117
118
    def reject(self, msg):
119
        self.rejections.append(msg)
120
121
122
def make_fake_upload(sourceful=False, binaryful=False):
123
    return FakeNascentUpload(sourceful, binaryful)
124
125
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
126
def make_policy(accepted_type):
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
127
    policy = AbstractUploadPolicy()
11426.1.4 by Guilherme Salgado
Replace the can_upload_* attributes of AbstractUploadPolicy with a single enum.
128
    policy.accepted_type = accepted_type
11426.1.3 by Guilherme Salgado
Refactor AbstractUploadPolicy.validateUploadType so that a policy doesn't have to specify sourceful, binaryful and mixed uploads in order for mixed uploads to be accepted. Now a policy only has to set can_upload_mixed=True to do that. Also adds a bunch of unit tests for that method.
129
    return policy