/usr/share/pyshared/repoze/who/plugins/tests/test_authtkt.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 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | import unittest
class TestAuthTktCookiePlugin(unittest.TestCase):
tempdir = None
_now_testing = None
def setUp(self):
pass
def tearDown(self):
if self.tempdir is not None:
import shutil
shutil.rmtree(self.tempdir)
if self._now_testing is not None:
self._setNowTesting(self._now_testing)
def _getTargetClass(self):
from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin
return AuthTktCookiePlugin
def _makeEnviron(self, kw=None):
environ = {'wsgi.version': (1,0)}
if kw is not None:
environ.update(kw)
environ['REMOTE_ADDR'] = '1.1.1.1'
environ['SERVER_NAME'] = 'localhost'
return environ
def _makeOne(self, *arg, **kw):
plugin = self._getTargetClass()(*arg, **kw)
return plugin
def _makeTicket(self, userid='userid', remote_addr='0.0.0.0',
tokens = [], userdata='userdata',
cookie_name='auth_tkt', secure=False,
time=None):
from paste.auth import auth_tkt
ticket = auth_tkt.AuthTicket(
'secret',
userid,
remote_addr,
tokens=tokens,
user_data=userdata,
time=time,
cookie_name=cookie_name,
secure=secure)
return ticket.cookie_value()
def _setNowTesting(self, value):
from repoze.who.plugins import auth_tkt
auth_tkt._NOW_TESTING, self._now_testing = value, auth_tkt._NOW_TESTING
def test_implements(self):
from zope.interface.verify import verifyClass
from repoze.who.interfaces import IIdentifier
klass = self._getTargetClass()
verifyClass(IIdentifier, klass)
def test_identify_nocookie(self):
plugin = self._makeOne('secret')
environ = self._makeEnviron()
result = plugin.identify(environ)
self.assertEqual(result, None)
def test_identify_good_cookie_include_ip(self):
plugin = self._makeOne('secret', include_ip=True)
val = self._makeTicket(remote_addr='1.1.1.1')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(len(result), 4)
self.assertEqual(result['tokens'], [''])
self.assertEqual(result['repoze.who.userid'], 'userid')
self.assertEqual(result['userdata'], 'userdata')
self.failUnless('timestamp' in result)
self.assertEqual(environ['REMOTE_USER_TOKENS'], [''])
self.assertEqual(environ['REMOTE_USER_DATA'],'userdata')
self.assertEqual(environ['AUTH_TYPE'],'cookie')
def test_identify_good_cookie_dont_include_ip(self):
plugin = self._makeOne('secret', include_ip=False)
val = self._makeTicket()
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(len(result), 4)
self.assertEqual(result['tokens'], [''])
self.assertEqual(result['repoze.who.userid'], 'userid')
self.assertEqual(result['userdata'], 'userdata')
self.failUnless('timestamp' in result)
self.assertEqual(environ['REMOTE_USER_TOKENS'], [''])
self.assertEqual(environ['REMOTE_USER_DATA'],'userdata')
self.assertEqual(environ['AUTH_TYPE'],'cookie')
def test_identify_good_cookie_int_useridtype(self):
plugin = self._makeOne('secret', include_ip=False)
val = self._makeTicket(userid='1', userdata='userid_type:int')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(len(result), 4)
self.assertEqual(result['tokens'], [''])
self.assertEqual(result['repoze.who.userid'], 1)
self.assertEqual(result['userdata'], 'userid_type:int')
self.failUnless('timestamp' in result)
self.assertEqual(environ['REMOTE_USER_TOKENS'], [''])
self.assertEqual(environ['REMOTE_USER_DATA'],'userid_type:int')
self.assertEqual(environ['AUTH_TYPE'],'cookie')
def test_identify_good_cookie_unknown_useridtype(self):
plugin = self._makeOne('secret', include_ip=False)
val = self._makeTicket(userid='userid', userdata='userid_type:unknown')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(len(result), 4)
self.assertEqual(result['tokens'], [''])
self.assertEqual(result['repoze.who.userid'], 'userid')
self.assertEqual(result['userdata'], 'userid_type:unknown')
self.failUnless('timestamp' in result)
self.assertEqual(environ['REMOTE_USER_TOKENS'], [''])
self.assertEqual(environ['REMOTE_USER_DATA'],'userid_type:unknown')
self.assertEqual(environ['AUTH_TYPE'],'cookie')
def test_identify_bad_cookie(self):
plugin = self._makeOne('secret', include_ip=True)
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=bogus'})
result = plugin.identify(environ)
self.assertEqual(result, None)
def test_identify_bad_cookie_expired(self):
import time
plugin = self._makeOne('secret', timeout=2, reissue_time=1)
val = self._makeTicket(userid='userid', time=time.time()-3)
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(result, None)
def test_remember_creds_same(self):
plugin = self._makeOne('secret')
val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.remember(environ, {'repoze.who.userid':'userid',
'userdata':'userdata'})
self.assertEqual(result, None)
def test_remember_creds_different(self):
plugin = self._makeOne('secret')
old_val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='other', userdata='userdata')
result = plugin.remember(environ, {'repoze.who.userid':'other',
'userdata':'userdata'})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
self.assertEqual(result[1],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=localhost'
% new_val))
self.assertEqual(result[2],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=.localhost'
% new_val))
def test_remember_creds_different_include_ip(self):
plugin = self._makeOne('secret', include_ip=True)
old_val = self._makeTicket(userid='userid', remote_addr='1.1.1.1')
environ = self._makeEnviron({'HTTP_COOKIE': 'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='other',
userdata='userdata',
remote_addr='1.1.1.1')
result = plugin.remember(environ, {'repoze.who.userid':'other',
'userdata':'userdata'})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
self.assertEqual(result[1],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=localhost'
% new_val))
self.assertEqual(result[2],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=.localhost'
% new_val))
def test_remember_creds_different_bad_old_cookie(self):
plugin = self._makeOne('secret')
old_val = 'BOGUS'
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='other', userdata='userdata')
result = plugin.remember(environ, {'repoze.who.userid':'other',
'userdata':'userdata'})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
self.assertEqual(result[1],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=localhost'
% new_val))
self.assertEqual(result[2],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=.localhost'
% new_val))
def test_remember_creds_different_with_nonstring_tokens(self):
plugin = self._makeOne('secret')
old_val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='other',
userdata='userdata',
tokens='foo,bar',
)
result = plugin.remember(environ, {'repoze.who.userid': 'other',
'userdata': 'userdata',
'tokens': ['foo', 'bar'],
})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
self.assertEqual(result[1],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=localhost'
% new_val))
self.assertEqual(result[2],
('Set-Cookie',
'auth_tkt="%s"; Path=/; Domain=.localhost'
% new_val))
def test_remember_creds_different_int_userid(self):
plugin = self._makeOne('secret')
old_val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='1', userdata='userid_type:int')
result = plugin.remember(environ, {'repoze.who.userid':1,
'userdata':''})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
def test_remember_creds_different_long_userid(self):
plugin = self._makeOne('secret')
old_val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='1', userdata='userid_type:int')
result = plugin.remember(environ, {'repoze.who.userid':long(1),
'userdata':''})
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
def test_remember_creds_different_unicode_userid(self):
plugin = self._makeOne('secret')
old_val = self._makeTicket(userid='userid')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
userid = unicode('\xc2\xa9', 'utf-8')
new_val = self._makeTicket(userid=userid.encode('utf-8'),
userdata='userid_type:unicode')
result = plugin.remember(environ, {'repoze.who.userid':userid,
'userdata':''})
self.assertEqual(type(result[0][1]), str)
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
def test_remember_creds_reissue(self):
import time
plugin = self._makeOne('secret', reissue_time=1)
old_val = self._makeTicket(userid='userid', userdata='',
time=time.time()-2)
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % old_val})
new_val = self._makeTicket(userid='userid', userdata='')
result = plugin.remember(environ, {'repoze.who.userid':'userid',
'userdata':''})
self.assertEqual(type(result[0][1]), str)
self.assertEqual(len(result), 3)
self.assertEqual(result[0],
('Set-Cookie',
'auth_tkt="%s"; Path=/' % new_val))
def test_forget(self):
from datetime import datetime
now = datetime(2009, 11, 5, 16, 15, 22)
self._setNowTesting(now)
plugin = self._makeOne('secret')
environ = self._makeEnviron()
headers = plugin.forget(environ, None)
self.assertEqual(len(headers), 3)
header = headers[0]
name, value = header
self.assertEqual(name, 'Set-Cookie')
self.assertEqual(value,
'auth_tkt="INVALID"; Path=/; '
'Max-Age=0; Expires=Thu, 05 Nov 2009 16:15:22'
)
header = headers[1]
name, value = header
self.assertEqual(name, 'Set-Cookie')
self.assertEqual(value,
'auth_tkt="INVALID"; Path=/; Domain=localhost; '
'Max-Age=0; Expires=Thu, 05 Nov 2009 16:15:22'
)
header = headers[2]
name, value = header
self.assertEqual(name, 'Set-Cookie')
self.assertEqual(value,
'auth_tkt="INVALID"; Path=/; Domain=.localhost; '
'Max-Age=0; Expires=Thu, 05 Nov 2009 16:15:22'
)
def test_factory_wo_secret_wo_secretfile_raises_ValueError(self):
from repoze.who.plugins.auth_tkt import make_plugin
self.assertRaises(ValueError, make_plugin)
def test_factory_w_secret_w_secretfile_raises_ValueError(self):
from repoze.who.plugins.auth_tkt import make_plugin
self.assertRaises(ValueError, make_plugin, 'secret', 'secretfile')
def test_factory_w_bad_secretfile_raises_ValueError(self):
from repoze.who.plugins.auth_tkt import make_plugin
self.assertRaises(ValueError, make_plugin, secretfile='nonesuch.txt')
def test_factory_w_secret(self):
from repoze.who.plugins.auth_tkt import make_plugin
plugin = make_plugin('secret')
self.assertEqual(plugin.cookie_name, 'auth_tkt')
self.assertEqual(plugin.secret, 'secret')
self.assertEqual(plugin.include_ip, False)
self.assertEqual(plugin.secure, False)
def test_factory_w_secretfile(self):
import os
from tempfile import mkdtemp
from repoze.who.plugins.auth_tkt import make_plugin
tempdir = self.tempdir = mkdtemp()
path = os.path.join(tempdir, 'who.secret')
secret = open(path, 'w')
secret.write('s33kr1t\n')
secret.flush()
secret.close()
plugin = make_plugin(secretfile=path)
self.assertEqual(plugin.secret, 's33kr1t')
def test_factory_with_timeout_and_reissue_time(self):
from repoze.who.plugins.auth_tkt import make_plugin
plugin = make_plugin('secret', timeout=5, reissue_time=1)
self.assertEqual(plugin.timeout, 5)
self.assertEqual(plugin.reissue_time, 1)
def test_factory_with_userid_checker(self):
from repoze.who.plugins.auth_tkt import make_plugin
plugin = make_plugin(
'secret',
userid_checker='repoze.who.plugins.auth_tkt:make_plugin')
self.assertEqual(plugin.userid_checker, make_plugin)
def test_timeout_no_reissue(self):
self.assertRaises(ValueError, self._makeOne, 'userid', timeout=1)
def test_timeout_lower_than_reissue(self):
self.assertRaises(ValueError, self._makeOne, 'userid', timeout=1,
reissue_time=2)
def test_identify_with_checker_and_existing_account(self):
plugin = self._makeOne('secret', userid_checker=dummy_userid_checker)
val = self._makeTicket(userid='existing')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
result = plugin.identify(environ)
self.assertEqual(len(result), 4)
self.assertEqual(result['tokens'], [''])
self.assertEqual(result['repoze.who.userid'], 'existing')
self.assertEqual(result['userdata'], 'userdata')
self.failUnless('timestamp' in result)
self.assertEqual(environ['REMOTE_USER_TOKENS'], [''])
self.assertEqual(environ['REMOTE_USER_DATA'],'userdata')
self.assertEqual(environ['AUTH_TYPE'],'cookie')
def test_identify_with_checker_and_non_existing_account(self):
plugin = self._makeOne('secret', userid_checker=dummy_userid_checker)
val = self._makeTicket(userid='nonexisting')
environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=%s' % val})
original_environ = environ.copy()
result = plugin.identify(environ)
self.assertEqual(result, None)
# The environ must not have been modified, excuding the paste.cookies
# variable:
del environ['paste.cookies']
self.assertEqual(environ, original_environ)
def test_remember_max_age(self):
plugin = self._makeOne('secret')
environ = {'HTTP_HOST':'example.com'}
tkt = self._makeTicket(userid='chris', userdata='')
result = plugin.remember(environ, {'repoze.who.userid':'chris',
'max_age':'500'})
name,value = result.pop(0)
self.assertEqual('Set-Cookie', name)
self.failUnless(
value.startswith('auth_tkt="%s"; Path=/; Max-Age=500' % tkt),
value)
self.failUnless('; Expires=' in value)
name,value = result.pop(0)
self.assertEqual('Set-Cookie', name)
self.failUnless(
value.startswith(
'auth_tkt="%s"; Path=/; Domain=example.com; Max-Age=500'
% tkt), value)
self.failUnless('; Expires=' in value)
name,value = result.pop(0)
self.assertEqual('Set-Cookie', name)
self.failUnless(
value.startswith(
'auth_tkt="%s"; Path=/; Domain=.example.com; Max-Age=500' % tkt),
value)
self.failUnless('; Expires=' in value)
def dummy_userid_checker(userid):
return userid == 'existing'
|