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

__metaclass__ = type

import unittest

from lazr.restfulclient.errors import ClientError
from zope.component import getUtility
from zope.security.interfaces import Unauthorized

from canonical.launchpad.webapp.errorlog import globalErrorUtility
from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    LaunchpadFunctionalLayer,
    )
from lp.registry.interfaces.projectgroup import IProjectGroupSet
from lp.testing import (
    launchpadlib_for,
    login_celebrity,
    login_person,
    TestCaseWithFactory,
    )


class ProjectGroupSearchTestCase(TestCaseWithFactory):

    layer = LaunchpadFunctionalLayer

    def setUp(self):
        super(ProjectGroupSearchTestCase, self).setUp()
        self.person = self.factory.makePerson()
        self.project1 = self.factory.makeProject(
            name="zazzle", owner=self.person)
        self.project2 = self.factory.makeProject(
            name="zazzle-dazzle", owner=self.person)
        self.project3 = self.factory.makeProject(
            name="razzle-dazzle", owner=self.person,
            description="Giving 110% at all times.")
        self.projectset = getUtility(IProjectGroupSet)
        login_person(self.person)

    def testSearchNoMatch(self):
        # Search for a string that does not exist.
        results = self.projectset.search(
            text="Fuzzle", search_products=False)
        self.assertEqual(0, results.count())

    def testSearchMatch(self):
        # Search for a matching string.
        results = self.projectset.search(
            text="zazzle", search_products=False)
        self.assertEqual(2, results.count())
        expected = sorted([self.project1, self.project2])
        self.assertEqual(expected, sorted(results))

    def testSearchDifferingCaseMatch(self):
        # Search for a matching string with a different case.
        results = self.projectset.search(
            text="Zazzle", search_products=False)
        self.assertEqual(2, results.count())
        expected = sorted([self.project1, self.project2])
        self.assertEqual(expected, sorted(results))

    def testProductSearchNoMatch(self):
        # Search for only project even if a product matches.
        product = self.factory.makeProduct(
            name="zazzle-product",
            title="Hoozah",
            owner=self.person)
        product.project = self.project1
        results = self.projectset.search(
            text="Hoozah", search_products=False)
        self.assertEqual(0, results.count())

    def testProductSearchMatch(self):
        # Search for products belonging to a project.  Note the project is
        # returned.
        product = self.factory.makeProduct(
            name="zazzle-product",
            title="Hoozah",
            owner=self.person)
        product.project = self.project1
        results = self.projectset.search(
            text="Hoozah", search_products=True)
        self.assertEqual(1, results.count())
        self.assertEqual(self.project1, results[0])

    def testProductSearchMatchOnProject(self):
        # Use the 'search_products' option but only look for a matching
        # project group to demonstrate projects are NOT searched too.

        # XXX: BradCrittenden 2009-11-10 bug=479984:
        # The behavior is currently unintuitive when search_products is used.
        # An exact match on a project is not returned since only products are
        # searched and the corresponding project for those matched is
        # returned.  This test demonstrates the current wrong behavior and
        # needs to be fixed when the search is fixed.
        results = self.projectset.search(
            text="zazzle-dazzle", search_products=True)
        self.assertEqual(0, results.count())

    def testProductSearchPercentMatch(self):
        # Search including a percent sign.  The match succeeds and does not
        # raise an exception.
        results = self.projectset.search(
            text="110%", search_products=False)
        self.assertEqual(1, results.count())
        self.assertEqual(self.project3, results[0])


class TestProjectGroupPermissions(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestProjectGroupPermissions, self).setUp()
        self.pg = self.factory.makeProject(name='my-project-group')

    def test_attribute_changes_by_admin(self):
        login_celebrity('admin')
        self.pg.name = 'new-name'
        self.pg.owner = self.factory.makePerson(name='project-group-owner')

    def test_attribute_changes_by_registry_admin(self):
        login_celebrity('registry_experts')
        new_owner = self.factory.makePerson(name='project-group-owner')
        self.pg.name = 'new-name'
        self.assertRaises(
            Unauthorized, setattr, self.pg, 'owner', new_owner)

    def test_attribute_changes_by_owner(self):
        login_person(self.pg.owner)
        self.assertRaises(
            Unauthorized, setattr, self.pg, 'name', 'new-name')
        self.pg.owner = self.factory.makePerson(name='project-group-owner')


class TestLaunchpadlibAPI(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def test_inappropriate_deactivation_does_not_cause_an_OOPS(self):
        # Make sure a 400 error and not an OOPS is returned when a ValueError
        # is raised when trying to deactivate a project that has source
        # releases.
        last_oops = globalErrorUtility.getLastOopsReport()

        launchpad = launchpadlib_for("test", "salgado", "WRITE_PUBLIC")
        project = launchpad.projects['evolution']
        project.active = False
        e = self.assertRaises(ClientError, project.lp_save)

        # no OOPS was generated as a result of the exception
        self.assertNoNewOops(last_oops)
        self.assertEqual(400, e.response.status)
        self.assertIn(
            'This project cannot be deactivated since it is linked to source '
            'packages.', e.content)


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