/usr/share/pyshared/repoze/who/tests/test_restrict.py is in python-repoze.who 1.0.18-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import unittest
class AuthenticatedPredicateTests(unittest.TestCase):
def _getFUT(self):
from repoze.who.restrict import authenticated_predicate
return authenticated_predicate()
def test___call___no_identity_returns_False(self):
predicate = self._getFUT()
environ = {}
self.failIf(predicate(environ))
def test___call___w_REMOTE_AUTH_returns_True(self):
predicate = self._getFUT()
environ = {'REMOTE_USER': 'fred'}
self.failUnless(predicate(environ))
def test___call___w_repoze_who_identity_returns_True(self):
predicate = self._getFUT()
environ = {'repoze.who.identity': {'login': 'fred'}}
self.failUnless(predicate(environ))
class MakeAuthenticatedRestrictionTests(unittest.TestCase):
def _getFUT(self):
from repoze.who.restrict import make_authenticated_restriction
return make_authenticated_restriction
def test_enabled(self):
from repoze.who.restrict import authenticated_predicate
fut = self._getFUT()
app = DummyApp()
filter = fut(app, {}, enabled=True)
self.failUnless(filter.app is app)
self.failUnless(filter.enabled)
predicate = filter.predicate
self.failUnless(predicate({'REMOTE_USER': 'fred'}))
self.failUnless(predicate({'repoze.who.identity': {'login': 'fred'}}))
class PredicateRestrictionTests(unittest.TestCase):
def _getTargetClass(self):
from repoze.who.restrict import PredicateRestriction
return PredicateRestriction
def _makeOne(self, app=None, **kw):
if app is None:
app = DummyApp()
return self._getTargetClass()(app, **kw)
def test___call___disabled_predicate_false_calls_app_not_predicate(self):
_tested = []
def _factory():
def _predicate(env):
_tested.append(env)
return False
return _predicate
_started = []
def _start_response(status, headers):
_started.append((status, headers))
environ = {'testing': True}
restrict = self._makeOne(predicate=_factory, enabled=False)
restrict(environ, _start_response)
self.assertEqual(len(_tested), 0)
self.assertEqual(len(_started), 0)
self.assertEqual(restrict.app.environ, environ)
def test___call___enabled_predicate_false_returns_401(self):
_tested = []
def _factory():
def _predicate(env):
_tested.append(env)
return False
return _predicate
_started = []
def _start_response(status, headers):
_started.append((status, headers))
environ = {'testing': True}
restrict = self._makeOne(predicate=_factory)
restrict(environ, _start_response)
self.assertEqual(len(_tested), 1)
self.assertEqual(len(_started), 1, _started)
self.assertEqual(_started[0][0], '401 Unauthorized')
self.assertEqual(restrict.app.environ, None)
def test___call___enabled_predicate_true_calls_app(self):
_tested = []
def _factory():
def _predicate(env):
_tested.append(env)
return True
return _predicate
_started = []
def _start_response(status, headers):
_started.append((status, headers))
environ = {'testing': True, 'REMOTE_USER': 'fred'}
restrict = self._makeOne(predicate=_factory)
restrict(environ, _start_response)
self.assertEqual(len(_tested), 1)
self.assertEqual(len(_started), 0)
self.assertEqual(restrict.app.environ, environ)
class MakePredicateRestrictionTests(unittest.TestCase):
def _getFUT(self):
from repoze.who.restrict import make_predicate_restriction
return make_predicate_restriction
def test_non_string_predicate_no_args(self):
fut = self._getFUT()
app = DummyApp()
def _predicate(env):
return True
def _factory():
return _predicate
filter = fut(app, {}, predicate=_factory)
self.failUnless(filter.app is app)
self.failUnless(filter.predicate is _predicate)
self.failUnless(filter.enabled)
def test_disabled_non_string_predicate_w_args(self):
fut = self._getFUT()
app = DummyApp()
filter = fut(app, {}, predicate=DummyPredicate, enabled=False,
foo='Foo')
self.failUnless(filter.app is app)
self.failUnless(isinstance(filter.predicate, DummyPredicate))
self.assertEqual(filter.predicate.foo, 'Foo')
self.failIf(filter.enabled)
def test_enabled_string_predicate_w_args(self):
fut = self._getFUT()
app = DummyApp()
filter = fut(app, {},
predicate='repoze.who.tests.test_restrict:DummyPredicate',
enabled=True, foo='Foo')
self.failUnless(filter.app is app)
self.failUnless(isinstance(filter.predicate, DummyPredicate))
self.assertEqual(filter.predicate.foo, 'Foo')
self.failUnless(filter.enabled)
class DummyApp:
environ = None
def __call__(self, environ, start_response):
self.environ = environ
return []
class DummyPredicate:
def __init__(self, **kw):
self.__dict__.update(kw)
def __call__(self, env):
return True
|