This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/test/test_api_security.py

84 lines
2.4 KiB
Python

import unittest
import json
import endpoints.api
from app import app
from data import model
from initdb import wipe_database, initialize_database, populate_database
from specs import (build_anon_spec, build_no_access_spec,
build_read_access_spec)
NO_ACCESS_USER = 'freshuser'
READ_ACCESS_USER = 'reader'
class ApiTestCase(unittest.TestCase):
def setUp(self):
wipe_database()
initialize_database()
populate_database()
class SpecTestBuilder(type):
@staticmethod
def _test_generator(url, expected_status, open_kwargs, auth_username=None):
def test(self):
with app.test_client() as c:
if auth_username:
# Temporarily remove the teardown functions
teardown_funcs = app.teardown_request_funcs[None]
app.teardown_request_funcs[None] = []
with c.session_transaction() as sess:
sess['user_id'] = auth_username
sess['identity.id'] = auth_username
sess['identity.auth_type'] = 'username'
# Restore the teardown functions
app.teardown_request_funcs[None] = teardown_funcs
rv = c.open(url, **open_kwargs)
msg = '%s %s: %s expected: %s' % (open_kwargs['method'], url,
rv.status_code, expected_status)
self.assertEqual(rv.status_code, expected_status, msg)
return test
def __new__(cls, name, bases, attrs):
with app.test_request_context() as ctx:
spec = attrs['spec_func']()
for (url, open_kwargs), expected_status in spec.items():
test = SpecTestBuilder._test_generator(url, expected_status,
open_kwargs,
attrs['auth_username'])
test_name_url = url.replace('/', '_').replace('-', '_')
test_name = 'test_%s_%s' % (open_kwargs['method'].lower(),
test_name_url)
attrs[test_name] = test
return type(name, bases, attrs)
class TestAnonymousAccess(ApiTestCase):
__metaclass__ = SpecTestBuilder
spec_func = build_anon_spec
auth_username = None
class TestNoAccess(ApiTestCase):
__metaclass__ = SpecTestBuilder
spec_func = build_no_access_spec
auth_username = NO_ACCESS_USER
class TestNoAccess(ApiTestCase):
__metaclass__ = SpecTestBuilder
spec_func = build_read_access_spec
auth_username = READ_ACCESS_USER
if __name__ == '__main__':
unittest.main()