MetaData Sharing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

83 lines
2.0 KiB

  1. #!/usr/bin/env python
  2. # Notes:
  3. # Python requests: https://2.python-requests.org/en/master/
  4. # IRequest interface: https://twistedmatrix.com/documents/current/api/twisted.web.iweb.IRequest.html
  5. # IResource interface: https://twistedmatrix.com/documents/current/api/twisted.web.resource.IResource.html
  6. # Twisted TDD: https://twistedmatrix.com/documents/current/core/howto/trial.html
  7. # Hypothesis: https://hypothesis.readthedocs.io/en/latest/
  8. # Going Async from Flask to Twisted Klein: https://crossbario.com/blog/Going-Asynchronous-from-Flask-to-Twisted-Klein/
  9. # Klein POST docs: https://klein.readthedocs.io/en/latest/examples/handlingpost.html
  10. from klein import Klein
  11. from twisted.trial import unittest
  12. from twisted.web.iweb import IRequest
  13. from kleintest import *
  14. import hashlib
  15. import mock
  16. import os.path
  17. import shutil
  18. import tempfile
  19. class MEDAServer:
  20. app = Klein()
  21. def store(self):
  22. pass
  23. @app.route('/')
  24. def home(request):
  25. return 'hello'
  26. # twistd support
  27. #medaserver = MEDAServer()
  28. #resource = medaserver.app.resource
  29. def main():
  30. from optparse import OptionParser
  31. parser = OptionParser()
  32. options, args = parser.parse_args()
  33. medaserver = MEDAServer()
  34. try:
  35. medaserver.app.run()
  36. finally:
  37. medaserver.store()
  38. if __name__ == '__main__': # pragma: no cover
  39. main()
  40. class _TestCases(unittest.TestCase):
  41. def setUp(self):
  42. d = os.path.realpath(tempfile.mkdtemp())
  43. self.basetempdir = d
  44. self.medaserver = MEDAServer()
  45. self.requests = FakeRequests(self.medaserver.app)
  46. def tearDown(self):
  47. shutil.rmtree(self.basetempdir)
  48. self.basetempdir = None
  49. def test_404(self):
  50. h = hashlib.sha256(open('fixtures/testfiles/test.txt').read()).hexdigest()
  51. r = self.requests.get('/chash/%s' % h)
  52. self.assertEqual(r.status_code, 404)
  53. def test_addpubkey(self):
  54. import cli
  55. persona = cli.Persona()
  56. persona.generate_key()
  57. @mock.patch('server.MEDAServer.store')
  58. @mock.patch('klein.Klein.run')
  59. def test_appruns(self, kleinrun, storefun):
  60. main()
  61. kleinrun.assert_called()
  62. storefun.assert_called()