-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in changes from trunk. These are necessary, since they remove all
services other than the utility, adapter and service service, which will be removed by the work of this branch.
- Loading branch information
0 parents
commit a3959c7
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2001, 2002 Zope Corporation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
# | ||
############################################################################## | ||
"""Test Unauthorized Exception Views | ||
$Id$ | ||
""" | ||
from unittest import TestCase, main, makeSuite | ||
from zope.interface import implements | ||
from zope.publisher.browser import TestRequest | ||
from zope.app import zapi | ||
from zope.app.tests import ztapi | ||
from zope.app.security.interfaces import IAuthenticationUtility, IPrincipal | ||
from zope.app.container.contained import contained | ||
from zope.app.exception.browser.unauthorized import Unauthorized | ||
from zope.app.tests.placelesssetup import PlacelessSetup | ||
|
||
class Unauthorized(Unauthorized): | ||
"""Unusually done by ZCML.""" | ||
|
||
def __init__(self, context, request): | ||
self.context = context | ||
self.request = request | ||
|
||
|
||
class DummyPrincipal(object): | ||
implements(IPrincipal) # this is a lie | ||
|
||
def __init__(self, id): | ||
self.id = id | ||
|
||
def getId(self): | ||
return self.id | ||
|
||
class DummyAuthUtility(object): | ||
implements(IAuthenticationUtility) # this is a lie | ||
|
||
def unauthorized(self, principal_id, request): | ||
self.principal_id = principal_id | ||
self.request = request | ||
|
||
|
||
class DummyPrincipalSource(object): | ||
pass | ||
|
||
class Test(PlacelessSetup, TestCase): | ||
|
||
def setUp(self): | ||
super(Test, self).setUp() | ||
self.auth = DummyAuthUtility() | ||
ztapi.provideUtility(IAuthenticationUtility, self.auth) | ||
|
||
def tearDown(self): | ||
super(Test, self).tearDown() | ||
|
||
def testUnauthorized(self): | ||
exception = Exception() | ||
try: | ||
raise exception | ||
except: | ||
pass | ||
request = TestRequest('/') | ||
request.setPrincipal(DummyPrincipal(23)) | ||
u = Unauthorized(exception, request) | ||
u.issueChallenge() | ||
|
||
# Make sure the response status was set | ||
self.assertEqual(request.response.getStatus(), 403) | ||
|
||
# Make sure the auth service was called | ||
self.failUnless(self.auth.request is request) | ||
self.assertEqual(self.auth.principal_id, 23) | ||
|
||
def testPluggableAuthUtility(self): | ||
exception = Exception() | ||
try: | ||
raise exception | ||
except: | ||
pass | ||
request = TestRequest('/') | ||
psrc = DummyPrincipalSource() | ||
request.setPrincipal(DummyPrincipal(23)) | ||
u = Unauthorized(exception, request) | ||
u.issueChallenge() | ||
|
||
# Make sure the response status was set | ||
self.assertEqual(request.response.getStatus(), 403) | ||
|
||
# Make sure the auth service was called | ||
self.failUnless(self.auth.request is request) | ||
self.assertEqual(self.auth.principal_id, 23) | ||
|
||
def test_suite(): | ||
return makeSuite(Test) | ||
|
||
if __name__=='__main__': | ||
main(defaultTest='test_suite') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
############################################################################## | ||
# | ||
# Copyright (c) 2003 Zope Corporation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
# | ||
############################################################################## | ||
"""Unauthorized Exception View Class | ||
$Id$ | ||
""" | ||
__docformat__ = 'restructuredtext' | ||
|
||
from zope.app import zapi | ||
|
||
|
||
class Unauthorized(object): | ||
|
||
def issueChallenge(self): | ||
# Set the error status to 403 (Forbidden) in the case when we don't | ||
# challenge the user | ||
self.request.response.setStatus(403) | ||
principal = self.request.principal | ||
auth = zapi.principals() | ||
auth.unauthorized(principal.id, self.request) |