commit df768a6c02bef81a7d4f58e14057e74cfcc2eca5 Author: kudrom kudrom@riseup.net Date: Sat Jul 5 15:52:58 2014 +0200
Improved http error handling, the test coverage and some cleanups --- .gitignore | 2 ++ oonib/api.py | 2 ++ oonib/handlers.py | 4 +++- oonib/main/api.py | 8 ++++++++ oonib/main/handlers.py | 19 +++++++++++++++++++ oonib/test/handler_helpers.py | 8 ++++++++ oonib/test/test_global_handler.py | 19 +++++++++++++++++++ oonib/test/test_report.py | 5 +++++ 8 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore index 628ccf5..7fc5cbd 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,5 @@ installed-files.txt
# client config: oonib.conf + +oonib.pid diff --git a/oonib/api.py b/oonib/api.py index 2427837..baa151b 100644 --- a/oonib/api.py +++ b/oonib/api.py @@ -1,5 +1,6 @@ from cyclone import web
+from oonib.main.api import mainAPI from oonib.deck.api import deckAPI from oonib.report.api import reportAPI from oonib.input.api import inputAPI @@ -24,4 +25,5 @@ if config.main.policy_file: if config.main.bouncer_file: ooniBouncer = web.Application(bouncerAPI, debug=True, name='bouncer')
+ooniBackendAPI += mainAPI ooniBackend = web.Application(ooniBackendAPI, debug=True, name='collector') diff --git a/oonib/handlers.py b/oonib/handlers.py index f92d684..bbf8ade 100644 --- a/oonib/handlers.py +++ b/oonib/handlers.py @@ -9,8 +9,10 @@ from oonib import log class OONIBHandler(web.RequestHandler): def write_error(self, status_code, exception=None, **kw): self.set_status(status_code) - if hasattr(exception, 'log_message'): + if hasattr(exception, 'log_message') and exception.log_message is not None: self.write({'error': exception.log_message}) + elif 400 <= status_code < 600: + self.write({'error': status_code}) else: log.error(exception) self.write({'error': 'error'}) diff --git a/oonib/main/__init__.py b/oonib/main/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oonib/main/api.py b/oonib/main/api.py new file mode 100644 index 0000000..5bc9761 --- /dev/null +++ b/oonib/main/api.py @@ -0,0 +1,8 @@ +from cyclone import web + +from oonib.main import handlers +from oonib.config import config + +mainAPI = [ + (r"/.*", handlers.OONIBGlobalHandler) +] diff --git a/oonib/main/handlers.py b/oonib/main/handlers.py new file mode 100644 index 0000000..37810e7 --- /dev/null +++ b/oonib/main/handlers.py @@ -0,0 +1,19 @@ +from oonib.handlers import OONIBHandler + + +class OONIBGlobalHandler(OONIBHandler): + """ + Global handler to catch all orphaned requests. + """ + def default(self): + # XXX: Waiting to a stable release of cyclone which implements the default handler + return self.write_error(404) + + def get(self): + return self.default() + + def post(self): + return self.default() + + def options(self): + return self.default() diff --git a/oonib/test/handler_helpers.py b/oonib/test/handler_helpers.py index 2d704a2..843d5c5 100644 --- a/oonib/test/handler_helpers.py +++ b/oonib/test/handler_helpers.py @@ -1,5 +1,7 @@ +import os import socket import json +import shutil
from twisted.internet import reactor, defer from twisted.trial import unittest @@ -38,11 +40,17 @@ class HandlerTestCase(unittest.TestCase): return self._port
def setUp(self, *args, **kw): + self.filenames = set() + self.directories = set() if self.app: self._listener = reactor.listenTCP(self.port, self.app) return unittest.TestCase.setUp(self, *args, **kw)
def tearDown(self): + for filename in self.filenames: + os.remove(filename) + for dir in self.directories: + shutil.rmtree(dir) if self._listener: for report in reports.values(): try: diff --git a/oonib/test/test_global_handler.py b/oonib/test/test_global_handler.py new file mode 100644 index 0000000..675cf6c --- /dev/null +++ b/oonib/test/test_global_handler.py @@ -0,0 +1,19 @@ +import json + +from twisted.internet import defer + +from cyclone import web + +from oonib.test.handler_helpers import HandlerTestCase +from oonib.main.api import mainAPI + + +class GlobalHandler(HandlerTestCase): + app = web.Application(mainAPI, name='mainAPI') + + @defer.inlineCallbacks + def test_global_handler(self): + response = yield self.request("/foo") + res = json.loads(response.body) + self.assertIn("error", res.keys()) + self.assertIn(404, res.values()) diff --git a/oonib/test/test_report.py b/oonib/test/test_report.py index 9ed02de..698b667 100644 --- a/oonib/test/test_report.py +++ b/oonib/test/test_report.py @@ -87,6 +87,7 @@ class TestReport(HandlerTestCase): response_body = json.loads(response.body) self.assertIn('backend_version', response_body) self.assertIn('report_id', response_body) + self.filenames.add(response_body['report_id'])
@defer.inlineCallbacks def test_create_invalid_report(self): @@ -121,6 +122,7 @@ class TestReport(HandlerTestCase): response_body = json.loads(response.body)
with open(report_id) as f: + self.filenames.add(report_id) written_report = yaml.safe_load_all(f)
written_report_header = written_report.next() @@ -165,9 +167,12 @@ class TestReport(HandlerTestCase): written_report_path = os.path.join(written_report_header['probe_cc'], report_file_name(written_report_header)) with open(written_report_path) as f: + self.filenames.add(written_report_path) written_report = yaml.safe_load_all(f) written_report.next()
for i in range(report_entry_count): self.assertEqual(yaml.safe_load(sample_report_entry), written_report.next()) + + self.directories.add('ZZ')
tor-commits@lists.torproject.org