Make the app config more powerful in terms of injecting fake dependencies. Refactor the tests to use metaclasses and to actually all run.
This commit is contained in:
parent
2a849f631b
commit
2cd98fc58e
15 changed files with 669 additions and 511 deletions
|
@ -6,10 +6,12 @@ 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
|
||||
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):
|
||||
|
@ -19,31 +21,64 @@ class ApiTestCase(unittest.TestCase):
|
|||
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):
|
||||
def __runspec(self, client, spec):
|
||||
for url, (expected_status, open_kwargs) in spec.items():
|
||||
rv = client.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)
|
||||
__metaclass__ = SpecTestBuilder
|
||||
spec_func = build_anon_spec
|
||||
auth_username = None
|
||||
|
||||
def test_anonymous_public_access(self):
|
||||
with app.test_request_context() as ctx:
|
||||
spec = build_anon_spec()
|
||||
|
||||
with app.test_client() as c:
|
||||
self.__runspec(c, spec)
|
||||
class TestNoAccess(ApiTestCase):
|
||||
__metaclass__ = SpecTestBuilder
|
||||
spec_func = build_no_access_spec
|
||||
auth_username = NO_ACCESS_USER
|
||||
|
||||
def test_authenticated_but_not_authorized(self):
|
||||
with app.test_request_context() as ctx:
|
||||
spec = build_no_access_spec()
|
||||
|
||||
with app.test_client() as c:
|
||||
with c.session_transaction() as sess:
|
||||
sess['user_id'] = NO_ACCESS_USER
|
||||
|
||||
self.__runspec(c, spec)
|
||||
class TestNoAccess(ApiTestCase):
|
||||
__metaclass__ = SpecTestBuilder
|
||||
spec_func = build_read_access_spec
|
||||
auth_username = READ_ACCESS_USER
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
|
Reference in a new issue