commit 4c4ea8b014bed5fae89094a82e81d643a836b76d
Author: Arturo Filastò <a...@fuffa.org>
Date:   Fri Jan 31 15:03:03 2014 +0100

    Add unittests for OONIB reporter
---
 ooni/reporter.py            |    2 +-
 ooni/tests/test_reporter.py |   62 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/ooni/reporter.py b/ooni/reporter.py
index 545997d..7db24aa 100644
--- a/ooni/reporter.py
+++ b/ooni/reporter.py
@@ -367,7 +367,7 @@ class OONIBReporter(OReporter):
             log.err("Failed to parse collector response %s" % backend_response)
             log.exception(e)
             raise errors.OONIBReportCreationError
-        
+       
         if response.code == 406:
             # XXX make this more strict
             log.err("The specified input or nettests cannot be submitted to 
this collector.")
diff --git a/ooni/tests/test_reporter.py b/ooni/tests/test_reporter.py
index 0a4d240..23c1190 100644
--- a/ooni/tests/test_reporter.py
+++ b/ooni/tests/test_reporter.py
@@ -1,24 +1,43 @@
 import yaml
+import json
 import time
+from mock import MagicMock
 
+from twisted.internet import defer
 from twisted.trial import unittest
 
-from ooni.reporter import YAMLReporter
+from ooni.utils.net import StringProducer
+from ooni import errors as e
+from ooni.reporter import YAMLReporter, OONIBReporter
 
 class MockTest(object):
     _start_time = time.time()
     report = {'report_content': 'ham'}
     input = 'spam'
 
+test_details = {
+    'test_name': 'spam',
+    'test_version': '1.0',
+    'software_name': 'spam',
+    'software_version': '1.0',
+    'input_hashes': [],
+    'probe_asn': 'AS0'
+}
+
+oonib_new_report_message = {
+    'report_id': "2014-01-29T202038Z_AS0_"+"A"*50,
+    'backend_version': "1.0"
+}
+
+oonib_generic_error_message = {
+    'error': 'generic-error'
+}
+
 class TestYAMLReporter(unittest.TestCase):
     def setUp(self):
         pass
 
     def test_write_report(self):
-        test_details = {
-                'test_name': 'spam',
-                'test_version': '1.0'
-        }
         test = MockTest()
 
         y_reporter = YAMLReporter(test_details)
@@ -37,3 +56,36 @@ class TestYAMLReporter(unittest.TestCase):
                                  'test_name', 'test_started', \
                                  'test_runtime'])
 
+class TestOONIBReporter(unittest.TestCase):
+    
+    def setUp(self):
+        self.mock_response = {}
+        self.collector_address = 'http://example.com'
+
+        self.oonib_reporter = OONIBReporter(test_details, 
self.collector_address)
+        self.oonib_reporter.agent = MagicMock()
+        self.mock_agent_response = MagicMock()
+        def deliverBody(body_receiver):
+            body_receiver.dataReceived(json.dumps(self.mock_response))
+            body_receiver.connectionLost(None)
+        self.mock_agent_response.deliverBody = deliverBody
+        self.oonib_reporter.agent.request.return_value = 
defer.succeed(self.mock_agent_response)
+    
+    @defer.inlineCallbacks
+    def test_create_report(self):
+        self.mock_response = oonib_new_report_message
+        yield self.oonib_reporter.createReport()
+        assert self.oonib_reporter.reportID == 
oonib_new_report_message['report_id']
+
+    @defer.inlineCallbacks
+    def test_create_report_failure(self):
+        self.mock_response = oonib_generic_error_message
+        self.mock_agent_response.code = 406
+        yield self.assertFailure(self.oonib_reporter.createReport(), 
e.OONIBReportCreationError)
+
+    @defer.inlineCallbacks
+    def test_write_report_entry(self):
+        req = {'content': 'something'}
+        yield self.oonib_reporter.writeReportEntry(req)
+        assert self.oonib_reporter.agent.request.called
+



_______________________________________________
tor-commits mailing list
tor-commits@lists.torproject.org
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits

Reply via email to