~launchpad-pqm/launchpad/devel

7675.1062.6 by Julian Edwards
tests
1
# Copyright 2011 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4
"""Tests for publisherConfig model class."""
5
6
__metaclass__ = type
7
8
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
9
from storm.exceptions import IntegrityError
7675.1062.8 by Julian Edwards
Extra test for IPublisherConfigSet.getByDistribution
10
from zope.component import getUtility
7675.1062.6 by Julian Edwards
tests
11
from zope.interface.verify import verifyObject
7675.1062.13 by Julian Edwards
security declarations, oops
12
from zope.security.interfaces import Unauthorized
7675.1062.6 by Julian Edwards
tests
13
7675.1062.13 by Julian Edwards
security declarations, oops
14
from canonical.launchpad.ftests import login
15
from canonical.testing.layers import (
16
    DatabaseFunctionalLayer,
17
    ZopelessDatabaseLayer,
18
    )
7675.1062.6 by Julian Edwards
tests
19
from lp.archivepublisher.interfaces.publisherconfig import (
20
    IPublisherConfig,
7675.1062.8 by Julian Edwards
Extra test for IPublisherConfigSet.getByDistribution
21
    IPublisherConfigSet,
7675.1062.6 by Julian Edwards
tests
22
    )
14564.4.1 by Jeroen Vermeulen
Lint.
23
from lp.archivepublisher.model.publisherconfig import PublisherConfig
14577.1.1 by William Grant
Fix test_publisherconfig lpstorm import. Probably a silent conflict between megalint and apocalypse.
24
from lp.services.database.lpstorm import IStore
7675.1062.13 by Julian Edwards
security declarations, oops
25
from lp.testing import (
26
    ANONYMOUS,
27
    TestCaseWithFactory,
28
    )
29
from lp.testing.sampledata import LAUNCHPAD_ADMIN
7675.1062.6 by Julian Edwards
tests
30
31
32
class TestPublisherConfig(TestCaseWithFactory):
33
    """Test the `PublisherConfig` model."""
34
    layer = ZopelessDatabaseLayer
35
36
    def setUp(self):
37
        TestCaseWithFactory.setUp(self)
38
        self.distribution = self.factory.makeDistribution(name='conftest')
39
40
    def test_verify_interface(self):
41
        # Test the interface for the model.
42
        pubconf = self.factory.makePublisherConfig()
43
        verified = verifyObject(IPublisherConfig, pubconf)
44
        self.assertTrue(verified)
45
46
    def test_properties(self):
47
        # Test the model properties.
7675.1062.12 by Julian Edwards
Make everything unicode
48
        ROOT_DIR = u"rootdir/test"
49
        BASE_URL = u"http://base.url"
50
        COPY_BASE_URL = u"http://base.url"
7675.1062.6 by Julian Edwards
tests
51
        pubconf = self.factory.makePublisherConfig(
52
            distribution=self.distribution,
53
            root_dir=ROOT_DIR,
54
            base_url=BASE_URL,
55
            copy_base_url=COPY_BASE_URL,
56
            )
57
58
        self.assertEqual(self.distribution.name, pubconf.distribution.name)
59
        self.assertEqual(ROOT_DIR, pubconf.root_dir)
60
        self.assertEqual(BASE_URL, pubconf.base_url)
61
        self.assertEqual(COPY_BASE_URL, pubconf.copy_base_url)
7675.1062.7 by Julian Edwards
add test to ensure only one config per distro
62
63
    def test_one_config_per_distro(self):
64
        # Only one config for each distro is allowed.
14564.4.1 by Jeroen Vermeulen
Lint.
65
66
        def make_conflicting_configs():
67
            for counter in range(2):
68
                self.factory.makePublisherConfig(self.distribution)
69
            IStore(PublisherConfig).flush()
70
71
        self.assertRaises(IntegrityError, make_conflicting_configs)
7675.1062.8 by Julian Edwards
Extra test for IPublisherConfigSet.getByDistribution
72
73
    def test_getByDistribution(self):
74
        # Test that IPublisherConfigSet.getByDistribution works.
75
        pubconf = getUtility(IPublisherConfigSet).getByDistribution(
76
            self.distribution)
77
        self.assertEqual(self.distribution.name, pubconf.distribution.name)
7675.1062.13 by Julian Edwards
security declarations, oops
78
79
80
class TestPublisherConfigSecurity(TestCaseWithFactory):
81
82
    layer = DatabaseFunctionalLayer
83
84
    def test_only_admin(self):
85
        # Only admins can see and change the config.
7675.1069.14 by Julian Edwards
fix TestPublisherConfigSecurity
86
        distro = self.factory.makeDistribution(publish_root_dir=u"foo")
87
        config = getUtility(IPublisherConfigSet).getByDistribution(distro)
7675.1062.13 by Julian Edwards
security declarations, oops
88
89
        login(ANONYMOUS)
90
        self.assertRaises(Unauthorized, getattr, config, "root_dir")
91
        self.assertRaises(Unauthorized, setattr, config, "root_dir", "test")
92
93
        login(LAUNCHPAD_ADMIN)
94
        self.assertEqual(u"foo", config.root_dir)
95
        config.root_dir = u"bar"
96
        self.assertEqual(u"bar", config.root_dir)