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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
# Copyright 2009-2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
from zope.component import (
getSiteManager,
getUtility,
)
from zope.interface import (
implements,
Interface,
)
from zope.security.proxy import removeSecurityProxy
from lp.app.interfaces.security import IAuthorization
from lp.app.security import (
AuthorizationBase,
DelegatedAuthorization,
)
from lp.registry.enums import PersonVisibility
from lp.registry.interfaces.person import IPerson
from lp.registry.interfaces.role import IPersonRoles
from lp.registry.interfaces.teammembership import (
ITeamMembershipSet,
TeamMembershipStatus,
)
from lp.security import PublicOrPrivateTeamsExistence
from lp.testing import (
admin_logged_in,
person_logged_in,
TestCase,
TestCaseWithFactory,
)
from lp.testing.fakemethod import FakeMethod
from lp.testing.layers import (
DatabaseFunctionalLayer,
ZopelessDatabaseLayer,
)
def registerFakeSecurityAdapter(interface, permission, adapter=None):
"""Register an instance of FakeSecurityAdapter.
Create a factory for an instance of FakeSecurityAdapter and register
it as an adapter for the given interface and permission name.
"""
if adapter is None:
adapter = FakeSecurityAdapter()
def adapter_factory(adaptee):
return adapter
getSiteManager().registerAdapter(
adapter_factory, (interface,), IAuthorization, permission)
return adapter
class FakeSecurityAdapter(AuthorizationBase):
def __init__(self, adaptee=None):
super(FakeSecurityAdapter, self).__init__(adaptee)
self.checkAuthenticated = FakeMethod()
self.checkUnauthenticated = FakeMethod()
class IDummy(Interface):
"""Marker interface to test forwarding."""
class Dummy:
"""An implementation of IDummy."""
implements(IDummy)
class TestAuthorizationBase(TestCaseWithFactory):
layer = ZopelessDatabaseLayer
def test_checkAuthenticated_for_full_fledged_account(self):
# AuthorizationBase.checkAuthenticated should delegate to
# checkAuthenticated() when the given account can be adapted
# into an IPerson.
full_fledged_account = self.factory.makePerson().account
adapter = FakeSecurityAdapter()
adapter.checkAuthenticated(IPerson(full_fledged_account))
self.assertVectorEqual(
(1, adapter.checkAuthenticated.call_count),
(0, adapter.checkUnauthenticated.call_count))
def test_forwardCheckAuthenticated_object_changes(self):
# Requesting a check for the same permission on a different object.
permission = self.factory.getUniqueString()
next_adapter = registerFakeSecurityAdapter(
IDummy, permission)
adapter = FakeSecurityAdapter()
adapter.permission = permission
adapter.usedfor = None
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
adapter.forwardCheckAuthenticated(None, Dummy())
self.assertVectorEqual(
(1, adapter.checkPermissionIsRegistered.call_count),
(1, next_adapter.checkAuthenticated.call_count))
def test_forwardCheckAuthenticated_permission_changes(self):
# Requesting a check for a different permission on the same object.
next_permission = self.factory.getUniqueString()
next_adapter = registerFakeSecurityAdapter(
IDummy, next_permission)
adapter = FakeSecurityAdapter(Dummy())
adapter.permission = self.factory.getUniqueString()
adapter.usedfor = IDummy
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
adapter.forwardCheckAuthenticated(None, permission=next_permission)
self.assertVectorEqual(
(1, adapter.checkPermissionIsRegistered.call_count),
(1, next_adapter.checkAuthenticated.call_count))
def test_forwardCheckAuthenticated_both_change(self):
# Requesting a check for a different permission and a different
# object.
next_permission = self.factory.getUniqueString()
next_adapter = registerFakeSecurityAdapter(
IDummy, next_permission)
adapter = FakeSecurityAdapter()
adapter.permission = self.factory.getUniqueString()
adapter.usedfor = None
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
adapter.forwardCheckAuthenticated(None, Dummy(), next_permission)
self.assertVectorEqual(
(1, adapter.checkPermissionIsRegistered.call_count),
(1, next_adapter.checkAuthenticated.call_count))
def test_forwardCheckAuthenticated_no_forwarder(self):
# If the requested forwarding adapter does not exist, return False.
adapter = FakeSecurityAdapter()
adapter.permission = self.factory.getUniqueString()
adapter.usedfor = IDummy
adapter.checkPermissionIsRegistered = FakeMethod(result=True)
self.assertFalse(
adapter.forwardCheckAuthenticated(None, Dummy()))
class TestDelegatedAuthorization(TestCase):
"""Tests for `DelegatedAuthorization`."""
def test_checkAuthenticated(self):
# DelegatedAuthorization.checkAuthenticated() punts the checks back up
# to the security policy by generating (object, permission) tuples.
# The security policy is in a much better position to, well, apply
# policy.
obj, delegated_obj = object(), object()
authorization = DelegatedAuthorization(
obj, delegated_obj, "dedicatemyselfto.Evil")
# By default DelegatedAuthorization.checkAuthenticated() ignores its
# user argument, so we pass None in below, but it is required for
# IAuthorization, and may be useful for subclasses.
self.assertEqual(
[(delegated_obj, "dedicatemyselfto.Evil")],
list(authorization.checkAuthenticated(None)))
def test_checkUnauthenticated(self):
# DelegatedAuthorization.checkUnauthenticated() punts the checks back
# up to the security policy by generating (object, permission) tuples.
# The security policy is in a much better position to, well, apply
# policy.
obj, delegated_obj = object(), object()
authorization = DelegatedAuthorization(
obj, delegated_obj, "dedicatemyselfto.Evil")
self.assertEqual(
[(delegated_obj, "dedicatemyselfto.Evil")],
list(authorization.checkUnauthenticated()))
class TestPublicOrPrivateTeamsExistence(TestCaseWithFactory):
"""Tests for the PublicOrPrivateTeamsExistence security adapter."""
layer = DatabaseFunctionalLayer
def test_members_of_parent_teams_get_limited_view(self):
team_owner = self.factory.makePerson()
private_team = self.factory.makeTeam(
owner=team_owner, visibility=PersonVisibility.PRIVATE)
public_team = self.factory.makeTeam(owner=team_owner)
team_user = self.factory.makePerson()
other_user = self.factory.makePerson()
with person_logged_in(team_owner):
public_team.addMember(team_user, team_owner)
public_team.addMember(private_team, team_owner)
checker = PublicOrPrivateTeamsExistence(
removeSecurityProxy(private_team))
self.assertTrue(checker.checkAuthenticated(IPersonRoles(team_user)))
self.assertFalse(checker.checkAuthenticated(IPersonRoles(other_user)))
def test_members_of_pending_parent_teams_get_limited_view(self):
team_owner = self.factory.makePerson()
private_team = self.factory.makeTeam(
owner=team_owner, visibility=PersonVisibility.PRIVATE)
public_team = self.factory.makeTeam(owner=team_owner)
team_user = self.factory.makePerson()
other_user = self.factory.makePerson()
with person_logged_in(team_owner):
public_team.addMember(team_user, team_owner)
getUtility(ITeamMembershipSet).new(
private_team,
public_team,
TeamMembershipStatus.INVITED,
team_owner)
checker = PublicOrPrivateTeamsExistence(
removeSecurityProxy(private_team))
self.assertTrue(checker.checkAuthenticated(IPersonRoles(team_user)))
self.assertFalse(checker.checkAuthenticated(IPersonRoles(other_user)))
def assertTeamOwnerCanListPrivateTeamWithTeamStatus(self, team_status):
main_team_owner = self.factory.makePerson()
main_team = self.factory.makeTeam(
owner=main_team_owner,
visibility=PersonVisibility.PRIVATE)
private_team_owner = self.factory.makePerson()
private_team = self.factory.makeTeam(
owner=private_team_owner,
visibility=PersonVisibility.PRIVATE)
with admin_logged_in():
# Cannot add a team with a non-APPROVED / PENDING status, so add
# it as approved and then edit the membership.
main_team.addMember(
private_team,
main_team_owner,
status=TeamMembershipStatus.APPROVED,
force_team_add=True)
main_team.setMembershipData(
private_team,
team_status,
main_team_owner)
checker = PublicOrPrivateTeamsExistence(removeSecurityProxy(private_team))
self.assertTrue(checker.checkAuthenticated(IPersonRoles(main_team_owner)))
def test_can_list_team_with_deactivated_private_team(self):
self.assertTeamOwnerCanListPrivateTeamWithTeamStatus(
TeamMembershipStatus.DEACTIVATED)
def test_can_list_team_with_expired_private_team(self):
self.assertTeamOwnerCanListPrivateTeamWithTeamStatus(
TeamMembershipStatus.EXPIRED)
|