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

"""Tests for branch traversal."""

from zope.component import getUtility
from zope.publisher.interfaces import NotFound
from zope.security.proxy import removeSecurityProxy

from lp.registry.browser.person import PersonNavigation
from lp.registry.browser.personproduct import PersonProductNavigation
from lp.registry.interfaces.personproduct import (
    IPersonProduct,
    IPersonProductFactory,
    )
from lp.services.webapp.publisher import canonical_url
from lp.testing import (
    FakeLaunchpadRequest,
    TestCaseWithFactory,
    )
from lp.testing.layers import DatabaseFunctionalLayer


class TestPersonBranchTraversal(TestCaseWithFactory):
    """Branches are traversed to from IPersons. Test we can reach them.

    This class tests the `PersonNavigation` class to see that we can traverse
    to branches from such objects.
    """

    layer = DatabaseFunctionalLayer

    def setUp(self):
        TestCaseWithFactory.setUp(self)
        self.person = self.factory.makePerson()

    def assertRedirects(self, segments, url):
        redirection = self.traverse(segments)
        self.assertEqual(url, redirection.target)

    def traverse(self, segments):
        """Traverse to 'segments' using a 'PersonNavigation' object.

        Using the Zope traversal machinery, traverse to the path given by
        'segments', starting at a `PersonNavigation` object wrapped around the
        'person' attribute.

        :param segments: A list of path segments.
        :return: The object found.
        """
        stack = list(reversed(segments))
        name = stack.pop()
        request = FakeLaunchpadRequest(['~' + self.person.name], stack)
        traverser = PersonNavigation(self.person, request)
        return traverser.publishTraverse(request, name)

    def test_redirect_product_branch(self):
        branch = self.factory.makeProductBranch(owner=self.person)
        segments = ['+branch', branch.product.name, branch.name]
        self.assertRedirects(segments, canonical_url(branch))

    def test_redirect_junk_branch(self):
        branch = self.factory.makePersonalBranch(owner=self.person)
        segments = ['+branch', '+junk', branch.name]
        self.assertRedirects(segments, canonical_url(branch))

    def test_redirect_branch_not_found(self):
        self.assertRaises(
            NotFound, self.traverse, ['+branch', 'no-product', 'no-branch'])

    def test_redirect_on_package_branch_aliases(self):
        branch = self.factory.makePackageBranch(owner=self.person)
        distro = removeSecurityProxy(branch.distribution)
        distro.setAliases(['foo'])
        self.assertRedirects(
            ['foo', branch.distroseries.name, branch.sourcepackagename.name,
             branch.name],
            canonical_url(branch))

    def test_junk_branch(self):
        branch = self.factory.makePersonalBranch(owner=self.person)
        segments = ['+junk', branch.name]
        self.assertEqual(branch, self.traverse(segments))

    def test_junk_branch_no_such_branch(self):
        branch_name = self.factory.getUniqueString()
        self.assertRaises(NotFound, self.traverse, ['+junk', branch_name])

    def test_product_only(self):
        # Traversal to the product returns an IPersonProduct.
        product = self.factory.makeProduct()
        target = self.traverse([product.name])
        self.assertTrue(IPersonProduct.providedBy(target))

    def test_product_branch_no_such_product(self):
        product_name = self.factory.getUniqueString()
        branch_name = self.factory.getUniqueString()
        self.assertRaises(
            NotFound, self.traverse, [product_name, branch_name])

    def test_package_branch(self):
        branch = self.factory.makePackageBranch(owner=self.person)
        segments = [
            branch.distribution.name,
            branch.distroseries.name,
            branch.sourcepackagename.name,
            branch.name]
        self.assertEqual(branch, self.traverse(segments))


class TestPersonProductBranchTraversal(TestCaseWithFactory):
    """Test the traversal from a `PersonProuct`."""

    layer = DatabaseFunctionalLayer

    def setUp(self):
        TestCaseWithFactory.setUp(self)
        self.person = self.factory.makePerson()
        self.product = self.factory.makeProduct()
        self.person_product = getUtility(IPersonProductFactory).create(
                self.person, self.product)

    def traverse(self, segments):
        """Traverse to 'segments' using a 'PersonNavigation' object.

        Using the Zope traversal machinery, traverse to the path given by
        'segments', starting at a `PersonNavigation` object wrapped around the
        'person' attribute.

        :param segments: A list of path segments.
        :return: The object found.
        """
        stack = list(reversed(segments))
        name = stack.pop()
        request = FakeLaunchpadRequest(
            ['~' + self.person.name, self.product.name], stack)
        traverser = PersonProductNavigation(self.person_product, request)
        return traverser.publishTraverse(request, name)

    def test_product_branch(self):
        # The branch is returned if the branch does exist.
        branch = self.factory.makeProductBranch(
            owner=self.person, product=self.product)
        segments = [branch.name]
        self.assertEqual(branch, self.traverse(segments))

    def test_product_branch_no_such_branch(self):
        # NotFound is raised if the branch name doesn't exist.
        branch_name = self.factory.getUniqueString()
        self.assertRaises(NotFound, self.traverse, [branch_name])