~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
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
# Copyright 2009 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,
    queryAdapter,
    )
from zope.interface import (
    implements,
    Interface,
    )

from canonical.testing.layers import ZopelessDatabaseLayer
from lp.app.interfaces.security import IAuthorization
from lp.app.security import (
    AuthorizationBase,
    DelegatedAuthorization,
    )
from lp.testing import TestCaseWithFactory
from lp.testing.fakemethod import FakeMethod


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_checkAccountAuthenticated_for_full_fledged_account(self):
        # AuthorizationBase.checkAccountAuthenticated should delegate to
        # checkAuthenticated() when the given account can be adapted into an
        # IPerson.
        full_fledged_account = self.factory.makePerson().account
        adapter = FakeSecurityAdapter()
        adapter.checkAccountAuthenticated(full_fledged_account)
        self.assertVectorEqual(
            (1, adapter.checkAuthenticated.call_count),
            (0, adapter.checkUnauthenticated.call_count))

    def test_checkAccountAuthenticated_for_personless_account(self):
        # AuthorizationBase.checkAccountAuthenticated should delegate to
        # checkUnauthenticated() when the given account can't be adapted into
        # an IPerson.
        personless_account = self.factory.makeAccount('Test account')
        adapter = FakeSecurityAdapter()
        adapter.checkAccountAuthenticated(personless_account)
        self.assertVectorEqual(
            (0, adapter.checkAuthenticated.call_count),
            (1, 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 FakeDelegatedAuthorization(DelegatedAuthorization):
    def __init__(self, obj, permission=None):
        super(FakeDelegatedAuthorization, self).__init__(
            obj, obj.child_obj, permission)


class FakeForwardedObject:
    implements(IDummy)

    def __init__(self):
        self.child_obj = Dummy()


class TestDelegatedAuthorizationBase(TestCaseWithFactory):

    layer = ZopelessDatabaseLayer

    def test_DelegatedAuthorization_same_permissions(self):

        permission = self.factory.getUniqueString()
        fake_obj = FakeForwardedObject()
        outer_adapter = FakeDelegatedAuthorization(fake_obj)
        outer_adapter.permission = permission

        inner_adapter = FakeSecurityAdapter()
        inner_adapter.permission = permission
        registerFakeSecurityAdapter(IDummy, permission, inner_adapter)
        user = object()
        outer_adapter.checkAuthenticated(user)
        outer_adapter.checkUnauthenticated()
        self.assertVectorEqual(
            (1, inner_adapter.checkAuthenticated.call_count),
            (1, inner_adapter.checkUnauthenticated.call_count))

    def test_DelegatedAuthorization_different_permissions(self):
        perm_inner = 'inner'
        perm_outer = 'outer'
        fake_obj = FakeForwardedObject()
        outer_adapter = FakeDelegatedAuthorization(fake_obj, perm_inner)
        registerFakeSecurityAdapter(IDummy, perm_outer, outer_adapter)

        inner_adapter = FakeSecurityAdapter()
        inner_adapter.permission = perm_inner
        registerFakeSecurityAdapter(IDummy, perm_inner, inner_adapter)

        user = object()
        adapter = queryAdapter(
            FakeForwardedObject(), IAuthorization, perm_outer)
        adapter.checkAuthenticated(user)
        adapter.checkUnauthenticated()
        self.assertVectorEqual(
            (1, inner_adapter.checkAuthenticated.call_count),
            (1, inner_adapter.checkUnauthenticated.call_count))