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 NO_ACCESS_USER = 'freshuser' class ApiTestCase(unittest.TestCase): def setUp(self): wipe_database() initialize_database() populate_database() 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) 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) 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) if __name__ == '__main__': unittest.main()