~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/testing/__init__.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-12-19 20:39:48 UTC
  • mfrom: (14515.2.29 limited-view-pages-0)
  • Revision ID: launchpad@pqm.canonical.com-20111219203948-bgiz214cd8ox1dej
[r=wallyworld][bug=904283] Users can see pages when they have
        limitedview.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
    'celebrity_logged_in',
18
18
    'ExpectedException',
19
19
    'extract_lp_cache',
 
20
    'FakeAdapterMixin',
20
21
    'FakeLaunchpadRequest',
21
22
    'FakeTime',
22
23
    'get_lsb_information',
100
101
from testtools.matchers import MatchesRegex
101
102
from testtools.testcase import ExpectedException as TTExpectedException
102
103
import transaction
103
 
from zope.component import getUtility
 
104
from zope.component import (
 
105
    getSiteManager,
 
106
    getMultiAdapter,
 
107
    getUtility,
 
108
    )
104
109
import zope.event
 
110
from zope.interface import Interface
105
111
from zope.interface.verify import verifyClass
 
112
from zope.publisher.interfaces.browser import IBrowserRequest
106
113
from zope.security.proxy import (
107
114
    isinstance as zope_isinstance,
108
115
    removeSecurityProxy,
126
133
    StepsToGo,
127
134
    WebServiceTestRequest,
128
135
    )
 
136
from lp.app.interfaces.security import IAuthorization
129
137
from lp.codehosting.vfs import (
130
138
    branch_id_to_path,
131
139
    get_rw_server,
1429
1437
    def stepstogo(self):
1430
1438
        """See `IBasicLaunchpadRequest`."""
1431
1439
        return StepsToGo(self)
 
1440
 
 
1441
 
 
1442
class FakeAdapterMixin:
 
1443
    """A testcase mixin that helps register/unregister Zope adapters.
 
1444
 
 
1445
    These helper methods simplify the task to registering Zope adapters
 
1446
    during the setup of a test and they will be unregistered when the
 
1447
    test completes.
 
1448
    """
 
1449
    def registerAdapter(self, adapter_class, for_interfaces,
 
1450
                        provided_interface, name=None):
 
1451
        """Register an adapter from the required interfacs to the provided.
 
1452
 
 
1453
        eg. registerAdapter(
 
1454
                TestOtherThing, (IThing, ILayer), IOther, name='fnord')
 
1455
        """
 
1456
        getSiteManager().registerAdapter(
 
1457
            adapter_class, for_interfaces, provided_interface, name=name)
 
1458
        self.addCleanup(
 
1459
            getSiteManager().unregisterAdapter, adapter_class,
 
1460
            for_interfaces, provided_interface, name=name)
 
1461
 
 
1462
    def registerAuthorizationAdapter(self, authorization_class,
 
1463
                                     for_interface, permission_name):
 
1464
        """Register a security checker to test authorisation.
 
1465
 
 
1466
        eg. registerAuthorizationAdapter(
 
1467
                TestChecker, IPerson, 'launchpad.View')
 
1468
        """
 
1469
        self.registerAdapter(
 
1470
            authorization_class, (for_interface, ), IAuthorization,
 
1471
            name=permission_name)
 
1472
 
 
1473
    def registerBrowserViewAdapter(self, view_class, for_interface, name):
 
1474
        """Register a security checker to test authorization.
 
1475
 
 
1476
        eg registerBrowserViewAdapter(TestView, IPerson, '+test-view')
 
1477
        """
 
1478
        self.registerAdapter(
 
1479
            view_class, (for_interface, IBrowserRequest), Interface,
 
1480
            name=name)
 
1481
 
 
1482
    def getAdapter(self, for_interfaces, provided_interface, name=None):
 
1483
        return getMultiAdapter(for_interfaces, provided_interface, name=name)