diff --git a/tests/lib.py b/tests/lib.py new file mode 100644 index 0000000..dfc8262 --- /dev/null +++ b/tests/lib.py @@ -0,0 +1,57 @@ +import os +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 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) diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 0000000..3503b9a --- /dev/null +++ b/tests/test_misc.py @@ -0,0 +1,33 @@ +import pytest +import cherrypy +from tests.lib import * # NOQA - fixtures + + +class PhotolibTestExample(PhotolibTest): + def test_index(self): + """ + Tests that the '/' url returns a redirect to '/feed' + """ + self.getPage("/") + self.assertStatus('302 Found') + self.assertHeaderEndsWith('Location', '/feed') + + +class PhotolibTestApi(PhotolibTest): + def test_photos(self): + self.getPage("/api/v1/photos") + self.assertStatus('200 OK') + + def test_byhash(self): + self.getPage("/api/v1/byhash?sha=lol") + self.assertStatus('404 Not Found') + + # self.assertBody('Hello world') + # self.assertHeader('Content-Type', 'text/html;charset=utf-8') + # self.getPage("/echo?message=A+bient%F4t", + # headers=[ + # ('Accept-Charset', 'ISO-8859-1,utf-8'), + # ('Content-Type', 'text/html;charset=ISO-8859-1') + # ] + # ) +