|
- #!/usr/bin/env python
-
- # Notes:
- # Python requests: https://2.python-requests.org/en/master/
- # IRequest interface: https://twistedmatrix.com/documents/current/api/twisted.web.iweb.IRequest.html
- # IResource interface: https://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html
- # Twisted TDD: https://twistedmatrix.com/documents/current/core/howto/trial.html
- # Hypothesis: https://hypothesis.readthedocs.io/en/latest/
- # Going Async from Flask to Twisted Klein: https://crossbario.com/blog/Going-Asynchronous-from-Flask-to-Twisted-Klein/
- # Klein POST docs: https://klein.readthedocs.io/en/latest/examples/handlingpost.html
-
- from klein import Klein
- from twisted.trial import unittest
- from twisted.web.iweb import IRequest
- from kleintest import *
-
- import hashlib
- import mock
- import os.path
- import shutil
- import tempfile
-
- class MEDAServer:
- app = Klein()
-
- def store(self):
- pass
-
- @app.route('/')
- def home(request):
- return 'hello'
-
- # twistd support
- #medaserver = MEDAServer()
- #resource = medaserver.app.resource
-
- def main():
- from optparse import OptionParser
-
- parser = OptionParser()
-
- options, args = parser.parse_args()
-
- medaserver = MEDAServer()
-
- try:
- medaserver.app.run()
- finally:
- medaserver.store()
-
- if __name__ == '__main__': # pragma: no cover
- main()
-
- class _TestCases(unittest.TestCase):
- def setUp(self):
- d = os.path.realpath(tempfile.mkdtemp())
- self.basetempdir = d
- self.medaserver = MEDAServer()
- self.requests = FakeRequests(self.medaserver.app)
-
- def tearDown(self):
- shutil.rmtree(self.basetempdir)
- self.basetempdir = None
-
- def test_404(self):
- h = hashlib.sha256(open('fixtures/testfiles/test.txt').read()).hexdigest()
- r = self.requests.get('/chash/%s' % h)
- self.assertEqual(r.status_code, 404)
-
- def test_addpubkey(self):
- import cli
-
- persona = cli.Persona()
- persona.generate_key()
-
- @mock.patch('server.MEDAServer.store')
- @mock.patch('klein.Klein.run')
- def test_appruns(self, kleinrun, storefun):
- main()
-
- kleinrun.assert_called()
- storefun.assert_called()
|