12
15
from canonical.testing.layers import ZopelessDatabaseLayer
13
16
from lp.app.interfaces.security import IAuthorization
14
from lp.app.security import AuthorizationBase
17
from lp.app.security import (
19
ForwardedAuthorization,
15
21
from lp.testing import TestCaseWithFactory
16
22
from lp.testing.fakemethod import FakeMethod
25
def registerFakeSecurityAdapter(interface, permission, adapter=None):
26
"""Register an instance of FakeSecurityAdapter.
28
Create a factory for an instance of FakeSecurityAdapter and register
29
it as an adapter for the given interface and permission name.
32
adapter = FakeSecurityAdapter()
34
def adapter_factory(adaptee):
37
getSiteManager().registerAdapter(
38
adapter_factory, (interface,), IAuthorization, permission)
19
42
class FakeSecurityAdapter(AuthorizationBase):
21
44
def __init__(self, adaptee=None):
24
47
self.checkUnauthenticated = FakeMethod()
27
class DummyInterface(Interface):
50
class IDummy(Interface):
28
51
"""Marker interface to test forwarding."""
32
"""An implementation of DummyInterface."""
33
implements(DummyInterface)
55
"""An implementation of IDummy."""
36
59
class TestAuthorizationBase(TestCaseWithFactory):
59
82
(0, adapter.checkAuthenticated.call_count),
60
83
(1, adapter.checkUnauthenticated.call_count))
62
def _registerFakeSecurityAdpater(self, interface, permission):
63
"""Register an instance of FakeSecurityAdapter.
65
Create a factory for an instance of FakeSecurityAdapter and register
66
it as an adapter for the given interface and permission name.
68
adapter = FakeSecurityAdapter()
70
def adapter_factory(adaptee):
73
getSiteManager().registerAdapter(
74
adapter_factory, (interface,), IAuthorization, permission)
77
85
def test_forwardCheckAuthenticated_object_changes(self):
78
86
# Requesting a check for the same permission on a different object.
79
87
permission = self.factory.getUniqueString()
80
next_adapter = self._registerFakeSecurityAdpater(
81
DummyInterface, permission)
88
next_adapter = registerFakeSecurityAdapter(
83
91
adapter = FakeSecurityAdapter()
84
92
adapter.permission = permission
85
93
adapter.usedfor = None
86
94
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
88
adapter.forwardCheckAuthenticated(None, DummyClass())
96
adapter.forwardCheckAuthenticated(None, Dummy())
90
98
self.assertVectorEqual(
91
99
(1, adapter.checkPermissionIsRegistered.call_count),
94
102
def test_forwardCheckAuthenticated_permission_changes(self):
95
103
# Requesting a check for a different permission on the same object.
96
104
next_permission = self.factory.getUniqueString()
97
next_adapter = self._registerFakeSecurityAdpater(
98
DummyInterface, next_permission)
105
next_adapter = registerFakeSecurityAdapter(
106
IDummy, next_permission)
100
adapter = FakeSecurityAdapter(DummyClass())
108
adapter = FakeSecurityAdapter(Dummy())
101
109
adapter.permission = self.factory.getUniqueString()
102
adapter.usedfor = DummyInterface
110
adapter.usedfor = IDummy
103
111
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
105
113
adapter.forwardCheckAuthenticated(None, permission=next_permission)
112
120
# Requesting a check for a different permission and a different
114
122
next_permission = self.factory.getUniqueString()
115
next_adapter = self._registerFakeSecurityAdpater(
116
DummyInterface, next_permission)
123
next_adapter = registerFakeSecurityAdapter(
124
IDummy, next_permission)
118
126
adapter = FakeSecurityAdapter()
119
127
adapter.permission = self.factory.getUniqueString()
120
128
adapter.usedfor = None
121
129
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
123
adapter.forwardCheckAuthenticated(None, DummyClass(), next_permission)
131
adapter.forwardCheckAuthenticated(None, Dummy(), next_permission)
125
133
self.assertVectorEqual(
126
134
(1, adapter.checkPermissionIsRegistered.call_count),
130
138
# If the requested forwarding adapter does not exist, return False.
131
139
adapter = FakeSecurityAdapter()
132
140
adapter.permission = self.factory.getUniqueString()
133
adapter.usedfor = DummyInterface
141
adapter.usedfor = IDummy
134
142
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
136
144
self.assertFalse(
137
adapter.forwardCheckAuthenticated(None, DummyClass()))
145
adapter.forwardCheckAuthenticated(None, Dummy()))
148
class FakeForwardedAuthorization(ForwardedAuthorization):
149
def __init__(self, obj, permission=None):
150
super(FakeForwardedAuthorization, self).__init__(
151
obj.child_obj, permission)
154
class FakeForwardedObject:
157
self.child_obj = Dummy()
160
class TestForwardedAuthorizationBase(TestCaseWithFactory):
162
layer = ZopelessDatabaseLayer
164
def test_ForwardedAuthorization_same_permissions(self):
166
permission = self.factory.getUniqueString()
167
fake_obj = FakeForwardedObject()
168
outer_adapter = FakeForwardedAuthorization(fake_obj)
169
outer_adapter.permission = permission
171
inner_adapter = FakeSecurityAdapter()
172
inner_adapter.permission = permission
173
registerFakeSecurityAdapter(IDummy, permission, inner_adapter)
175
outer_adapter.checkAuthenticated(user)
176
outer_adapter.checkUnauthenticated()
177
self.assertVectorEqual(
178
(1, inner_adapter.checkAuthenticated.call_count),
179
(1, inner_adapter.checkUnauthenticated.call_count))
181
def test_ForwardedAuthorization_different_permissions(self):
184
fake_obj = FakeForwardedObject()
185
outer_adapter = FakeForwardedAuthorization(fake_obj, perm_inner)
186
registerFakeSecurityAdapter(IDummy, perm_outer, outer_adapter)
188
inner_adapter = FakeSecurityAdapter()
189
inner_adapter.permission = perm_inner
190
registerFakeSecurityAdapter(IDummy, perm_inner, inner_adapter)
193
adapter = queryAdapter(
194
FakeForwardedObject(), IAuthorization, perm_outer)
195
adapter.checkAuthenticated(user)
196
adapter.checkUnauthenticated()
197
self.assertVectorEqual(
198
(1, inner_adapter.checkAuthenticated.call_count),
199
(1, inner_adapter.checkUnauthenticated.call_count))