/usr/lib/python2.7/dist-packages/pecan/tests/test_secure.py is in python-pecan 0.6.1-2.
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 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 | import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest # noqa
from six import b as b_
from webtest import TestApp
from pecan import expose, make_app
from pecan.secure import secure, unlocked, SecureController
from pecan.tests import PecanTestCase
try:
set()
except:
from sets import Set as set
class TestSecure(PecanTestCase):
def test_simple_secure(self):
authorized = False
class SecretController(SecureController):
@expose()
def index(self):
return 'Index'
@expose()
@unlocked
def allowed(self):
return 'Allowed!'
@classmethod
def check_permissions(cls):
return authorized
class RootController(object):
@expose()
def index(self):
return 'Hello, World!'
@expose()
@secure(lambda: False)
def locked(self):
return 'No dice!'
@expose()
@secure(lambda: True)
def unlocked(self):
return 'Sure thing'
secret = SecretController()
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello, World!')
response = app.get('/unlocked')
assert response.status_int == 200
assert response.body == b_('Sure thing')
response = app.get('/locked', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/allowed')
assert response.status_int == 200
assert response.body == b_('Allowed!')
def test_unlocked_attribute(self):
class AuthorizedSubController(object):
@expose()
def index(self):
return 'Index'
@expose()
def allowed(self):
return 'Allowed!'
class SecretController(SecureController):
@expose()
def index(self):
return 'Index'
@expose()
@unlocked
def allowed(self):
return 'Allowed!'
authorized = unlocked(AuthorizedSubController())
class RootController(object):
@expose()
def index(self):
return 'Hello, World!'
@expose()
@secure(lambda: False)
def locked(self):
return 'No dice!'
@expose()
@secure(lambda: True)
def unlocked(self):
return 'Sure thing'
secret = SecretController()
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello, World!')
response = app.get('/unlocked')
assert response.status_int == 200
assert response.body == b_('Sure thing')
response = app.get('/locked', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/allowed')
assert response.status_int == 200
assert response.body == b_('Allowed!')
response = app.get('/secret/authorized/')
assert response.status_int == 200
assert response.body == b_('Index')
response = app.get('/secret/authorized/allowed')
assert response.status_int == 200
assert response.body == b_('Allowed!')
def test_secure_attribute(self):
authorized = False
class SubController(object):
@expose()
def index(self):
return 'Hello from sub!'
class RootController(object):
@expose()
def index(self):
return 'Hello from root!'
sub = secure(SubController(), lambda: authorized)
app = TestApp(make_app(RootController()))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello from root!')
response = app.get('/sub/', expect_errors=True)
assert response.status_int == 401
authorized = True
response = app.get('/sub/')
assert response.status_int == 200
assert response.body == b_('Hello from sub!')
def test_secured_generic_controller(self):
authorized = False
class RootController(object):
@classmethod
def check_permissions(cls):
return authorized
@expose(generic=True)
def index(self):
return 'Index'
@secure('check_permissions')
@index.when(method='POST')
def index_post(self):
return 'I should not be allowed'
@secure('check_permissions')
@expose(generic=True)
def secret(self):
return 'I should not be allowed'
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/')
assert response.status_int == 200
response = app.post('/', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/', expect_errors=True)
assert response.status_int == 401
def test_secured_generic_controller_lambda(self):
authorized = False
class RootController(object):
@expose(generic=True)
def index(self):
return 'Index'
@secure(lambda: authorized)
@index.when(method='POST')
def index_post(self):
return 'I should not be allowed'
@secure(lambda: authorized)
@expose(generic=True)
def secret(self):
return 'I should not be allowed'
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/')
assert response.status_int == 200
response = app.post('/', expect_errors=True)
assert response.status_int == 401
response = app.get('/secret/', expect_errors=True)
assert response.status_int == 401
def test_secured_generic_controller_secure_attribute(self):
authorized = False
class SecureController(object):
@expose(generic=True)
def index(self):
return 'I should not be allowed'
@index.when(method='POST')
def index_post(self):
return 'I should not be allowed'
@expose(generic=True)
def secret(self):
return 'I should not be allowed'
class RootController(object):
sub = secure(SecureController(), lambda: authorized)
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/sub/', expect_errors=True)
assert response.status_int == 401
response = app.post('/sub/', expect_errors=True)
assert response.status_int == 401
response = app.get('/sub/secret/', expect_errors=True)
assert response.status_int == 401
def test_secured_generic_controller_secure_attribute_with_unlocked(self):
class RootController(SecureController):
@unlocked
@expose(generic=True)
def index(self):
return 'Unlocked!'
@unlocked
@index.when(method='POST')
def index_post(self):
return 'Unlocked!'
@expose(generic=True)
def secret(self):
return 'I should not be allowed'
app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
response = app.get('/')
assert response.status_int == 200
response = app.post('/')
assert response.status_int == 200
response = app.get('/secret/', expect_errors=True)
assert response.status_int == 401
def test_state_attribute(self):
from pecan.secure import Any, Protected
assert repr(Any) == '<SecureState Any>'
assert bool(Any) is False
assert repr(Protected) == '<SecureState Protected>'
assert bool(Protected) is True
def test_secure_obj_only_failure(self):
class Foo(object):
pass
try:
secure(Foo())
except Exception as e:
assert isinstance(e, TypeError)
class TestObjectPathSecurity(PecanTestCase):
def setUp(self):
super(TestObjectPathSecurity, self).setUp()
permissions_checked = set()
class DeepSecretController(SecureController):
authorized = False
@expose()
@unlocked
def _lookup(self, someID, *remainder):
if someID == 'notfound':
return None
return SubController(someID), remainder
@expose()
def index(self):
return 'Deep Secret'
@classmethod
def check_permissions(cls):
permissions_checked.add('deepsecret')
return cls.authorized
class SubController(object):
def __init__(self, myID):
self.myID = myID
@expose()
def index(self):
return 'Index %s' % self.myID
deepsecret = DeepSecretController()
class SecretController(SecureController):
authorized = False
independent_authorization = False
@expose()
def _lookup(self, someID, *remainder):
if someID == 'notfound':
return None
elif someID == 'lookup_wrapped':
return self.wrapped, remainder
return SubController(someID), remainder
@secure('independent_check_permissions')
@expose()
def independent(self):
return 'Independent Security'
wrapped = secure(
SubController('wrapped'), 'independent_check_permissions'
)
@classmethod
def check_permissions(cls):
permissions_checked.add('secretcontroller')
return cls.authorized
@classmethod
def independent_check_permissions(cls):
permissions_checked.add('independent')
return cls.independent_authorization
class NotSecretController(object):
@expose()
def _lookup(self, someID, *remainder):
if someID == 'notfound':
return None
return SubController(someID), remainder
unlocked = unlocked(SubController('unlocked'))
class RootController(object):
secret = SecretController()
notsecret = NotSecretController()
self.deepsecret_cls = DeepSecretController
self.secret_cls = SecretController
self.permissions_checked = permissions_checked
self.app = TestApp(make_app(
RootController(),
debug=True,
static_root='tests/static'
))
def tearDown(self):
self.permissions_checked.clear()
self.secret_cls.authorized = False
self.deepsecret_cls.authorized = False
def test_sub_of_both_not_secret(self):
response = self.app.get('/notsecret/hi/')
assert response.status_int == 200
assert response.body == b_('Index hi')
def test_protected_lookup(self):
response = self.app.get('/secret/hi/', expect_errors=True)
assert response.status_int == 401
self.secret_cls.authorized = True
response = self.app.get('/secret/hi/')
assert response.status_int == 200
assert response.body == b_('Index hi')
assert 'secretcontroller' in self.permissions_checked
def test_secured_notfound_lookup(self):
response = self.app.get('/secret/notfound/', expect_errors=True)
assert response.status_int == 404
def test_secret_through_lookup(self):
response = self.app.get(
'/notsecret/hi/deepsecret/', expect_errors=True
)
assert response.status_int == 401
def test_layered_protection(self):
response = self.app.get('/secret/hi/deepsecret/', expect_errors=True)
assert response.status_int == 401
assert 'secretcontroller' in self.permissions_checked
self.secret_cls.authorized = True
response = self.app.get('/secret/hi/deepsecret/', expect_errors=True)
assert response.status_int == 401
assert 'secretcontroller' in self.permissions_checked
assert 'deepsecret' in self.permissions_checked
self.deepsecret_cls.authorized = True
response = self.app.get('/secret/hi/deepsecret/')
assert response.status_int == 200
assert response.body == b_('Deep Secret')
assert 'secretcontroller' in self.permissions_checked
assert 'deepsecret' in self.permissions_checked
def test_cyclical_protection(self):
self.secret_cls.authorized = True
self.deepsecret_cls.authorized = True
response = self.app.get('/secret/1/deepsecret/2/deepsecret/')
assert response.status_int == 200
assert response.body == b_('Deep Secret')
assert 'secretcontroller' in self.permissions_checked
assert 'deepsecret' in self.permissions_checked
def test_unlocked_lookup(self):
response = self.app.get('/notsecret/1/deepsecret/2/')
assert response.status_int == 200
assert response.body == b_('Index 2')
assert 'deepsecret' not in self.permissions_checked
response = self.app.get(
'/notsecret/1/deepsecret/notfound/', expect_errors=True
)
assert response.status_int == 404
assert 'deepsecret' not in self.permissions_checked
def test_mixed_protection(self):
self.secret_cls.authorized = True
response = self.app.get(
'/secret/1/deepsecret/notfound/', expect_errors=True
)
assert response.status_int == 404
assert 'secretcontroller' in self.permissions_checked
assert 'deepsecret' not in self.permissions_checked
def test_independent_check_failure(self):
response = self.app.get('/secret/independent/', expect_errors=True)
assert response.status_int == 401
assert len(self.permissions_checked) == 1
assert 'independent' in self.permissions_checked
def test_independent_check_success(self):
self.secret_cls.independent_authorization = True
response = self.app.get('/secret/independent')
assert response.status_int == 200
assert response.body == b_('Independent Security')
assert len(self.permissions_checked) == 1
assert 'independent' in self.permissions_checked
def test_wrapped_attribute_failure(self):
self.secret_cls.independent_authorization = False
response = self.app.get('/secret/wrapped/', expect_errors=True)
assert response.status_int == 401
assert len(self.permissions_checked) == 1
assert 'independent' in self.permissions_checked
def test_wrapped_attribute_success(self):
self.secret_cls.independent_authorization = True
response = self.app.get('/secret/wrapped/')
assert response.status_int == 200
assert response.body == b_('Index wrapped')
assert len(self.permissions_checked) == 1
assert 'independent' in self.permissions_checked
def test_lookup_to_wrapped_attribute_on_self(self):
self.secret_cls.authorized = True
self.secret_cls.independent_authorization = True
response = self.app.get('/secret/lookup_wrapped/')
assert response.status_int == 200
assert response.body == b_('Index wrapped')
assert len(self.permissions_checked) == 2
assert 'independent' in self.permissions_checked
assert 'secretcontroller' in self.permissions_checked
def test_unlocked_attribute_in_insecure(self):
response = self.app.get('/notsecret/unlocked/')
assert response.status_int == 200
assert response.body == b_('Index unlocked')
class SecureControllerSharedPermissionsRegression(PecanTestCase):
"""Regression tests for https://github.com/dreamhost/pecan/issues/131"""
def setUp(self):
super(SecureControllerSharedPermissionsRegression, self).setUp()
class Parent(object):
@expose()
def index(self):
return 'hello'
class UnsecuredChild(Parent):
pass
class SecureChild(Parent, SecureController):
@classmethod
def check_permissions(cls):
return False
class RootController(object):
secured = SecureChild()
unsecured = UnsecuredChild()
self.app = TestApp(make_app(RootController()))
def test_inherited_security(self):
assert self.app.get('/secured/', status=401).status_int == 401
assert self.app.get('/unsecured/').status_int == 200
|