import os import json import tempfile import cherrypy from cherrypy.test import helper from photoapp.daemon import setup_webapp class MockAuth: def check(self, *args, **kwargs): return True class PhotolibTest(helper.CPWebCase): @classmethod def setup_server(cls): cls.tmpd = tempfile.TemporaryDirectory() libd = os.path.join(cls.tmpd.name, "library") cached = os.path.join(cls.tmpd.name, "cache") os.mkdir(libd) os.mkdir(cached) setup_webapp( "sqlite:///{}".format(os.path.join(cls.tmpd.name, "testing.db")), "file://{}".format(libd), "file://{}".format(cached), None, debug=True, ) cherrypy.config.update({ 'tools.db.on': True, }) cls.disable_auth() @staticmethod def disable_auth(): cherrypy.tree.apps['/api'].config['/'].update({ 'tools.auth_basic.on': False, }) cherrypy.tree.apps[''].config['/'].update({ 'tools.auth_basic.on': False, }) def setUp(self): self.do_gc_test = False def getHeader(self, name): for key, value in self.headers: if name == key: return value def assertHeaderEndsWith(self, name, value): header = self.getHeader(name) assert header is not None, "Header '{}' not present".format(name) assert header.endswith(value), "Needed to end with '{}' but got '{}'".format(value, header) def postJSON(self, path, body): body = json.dumps(body) return self.getPage(path, method="POST", body=body, headers=[('Content-Type', 'application/json'), ('Content-Length', str(len(body)))])