commit d7f9d5990be9975df46e92164eb1b044c97c89ab Author: Arturo Filastò arturo@filasto.net Date: Thu Mar 10 19:11:22 2016 +0100
Implement test that attempts to write to the backend using JSON --- oonib/test/test_report.py | 119 ++++++++++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 52 deletions(-)
diff --git a/oonib/test/test_report.py b/oonib/test/test_report.py index 5e1712b..63865ae 100644 --- a/oonib/test/test_report.py +++ b/oonib/test/test_report.py @@ -12,54 +12,45 @@ from oonib.report.handlers import report_file_name from oonib.report.api import reportAPI from oonib.test.handler_helpers import HandlerTestCase, mock_initialize
-sample_report_entry = """--- -agent: agent -input: null -requests: -- request: - body: null - headers: - - - ACCePT-LAnGuagE - - ['en-US,en;q=0.8'] - - - aCCEPT-ENcODInG - - ['gzip,deflate,sdch'] - - - aCcEPT - - ['text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'] - - - User-AGeNt - - ['Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) Gecko/20091221 - Firefox/3.5.7'] - - - aCCEpt-cHArSEt - - ['ISO-8859-1,utf-8;q=0.7,*;q=0.3'] - - - HOsT - - [KIXnnZDJfGKRNab.com] - method: GET - url: http://12.34.56.78 -response: - body: '{"headers_dict": {"ACCePT-LAnGuagE": ["en-US,en;q=0.8"], "aCCEPT-ENcODInG": - ["gzip,deflate,sdch"], "HOsT": ["KIXnnZDJfGKRNab.com"], "aCcEPT": ["text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], - "User-AGeNt": ["Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) - Gecko/20091221 Firefox/3.5.7"], "aCCEpt-cHArSEt": ["ISO-8859-1,utf-8;q=0.7,*;q=0.3"], - "Connection": ["close"]}, "request_line": "GET / HTTP/1.1", "request_headers": - [["Connection", "close"], ["ACCePT-LAnGuagE", "en-US,en;q=0.8"], ["aCCEPT-ENcODInG", - "gzip,deflate,sdch"], ["aCcEPT", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"], - ["User-AGeNt", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) - Gecko/20091221 Firefox/3.5.7"], ["aCCEpt-cHArSEt", "ISO-8859-1,utf-8;q=0.7,*;q=0.3"], - ["HOsT", "KIXnnZDJfGKRNab.com"]]}' - code: 200 - headers: [] -socksproxy: null -tampering: -header_field_name: false -header_field_number: false -header_field_value: false -header_name_capitalization: false -header_name_diff: [] -request_line_capitalization: false -total: false -... -""" +sample_report_entry = { + 'agent': 'agent', + 'input': 'http://example.com', + 'requests': [ + { + 'request': { + 'body': None, + 'headers': { + 'Accept-Language': 'en-US,en;q=0.8', + 'User-AGeNt': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) Gecko/20091221' + }, + 'method': 'GET', + 'url': 'http://example.com' + }, + 'response': { + 'body': 'widgets', + 'headers': [] + } + } + ], + 'test_specific_key': 'some value', + 'input_hashes': [], + 'options': [], + 'probe_asn': 'AS0', + 'probe_cc': 'ZZ', + 'probe_city': None, + 'probe_ip': '127.0.0.1', + 'software_name': 'ooniprobe', + 'software_version': '1.1.0', + 'start_time': 0, + 'test_name': 'fake_test', + 'test_version': '0.1.0' +}
-sample_report_header = """--- +sample_report_entry_yaml = '---\n' +sample_report_entry_yaml += yaml.dump(sample_report_entry) +sample_report_entry_yaml += '...\n' + +sample_report_header_yaml = """--- input_hashes: [] options: [] probe_asn: AS0 @@ -92,7 +83,7 @@ class TestReport(HandlerTestCase): app = web.Application(reportAPI, name='reportAPI')
@defer.inlineCallbacks - def update_report(self, report_id, content=sample_report_entry): + def update_report(self, report_id, content=sample_report_entry_yaml): data = { 'content': content } @@ -113,7 +104,7 @@ class TestReport(HandlerTestCase): @defer.inlineCallbacks def test_create_valid_report_with_content(self): data = deepcopy(dummy_data) - data['content'] = sample_report_header + data['content'] = sample_report_header_yaml response = yield self.request('/report', "POST", data) response_body = json.loads(response.body) self.assertIn('backend_version', response_body) @@ -148,7 +139,7 @@ class TestReport(HandlerTestCase): written_report_header = written_report.next() for key in dummy_data.keys(): self.assertEqual(written_report_header[key], dummy_data[key]) - self.assertEqual(yaml.safe_load(sample_report_entry), + self.assertEqual(yaml.safe_load(sample_report_entry_yaml), written_report.next())
@defer.inlineCallbacks @@ -172,7 +163,7 @@ class TestReport(HandlerTestCase): self.assertEqual(written_report_header[key], dummy_data[key])
- self.assertEqual(yaml.safe_load(sample_report_entry), + self.assertEqual(yaml.safe_load(sample_report_entry_yaml), written_report.next())
response = yield self.request('/report/%s/close' % report_id, "POST") @@ -185,5 +176,29 @@ class TestReport(HandlerTestCase): written_report.next()
for i in range(report_entry_count): - self.assertEqual(yaml.safe_load(sample_report_entry), + self.assertEqual(yaml.safe_load(sample_report_entry_yaml), written_report.next()) + + @defer.inlineCallbacks + def test_create_update_and_close_report_json(self): + report_header = dummy_data.copy() + report_header['format'] = 'json' + response = yield self.request('/report', "POST", report_header) + response_body = json.loads(response.body) + self.assertIn('backend_version', response_body) + self.assertIn('report_id', response_body) + + report_entry_count = 100 + + report_id = response_body['report_id'] + for i in range(report_entry_count): + yield self.update_report(report_id, content=sample_report_entry) + + response = yield self.request('/report/%s/close' % report_id, "POST") + + written_report_path = report_file_name(".", report_header, report_id) + with open(written_report_path) as f: + self.filenames.add(written_report_path) + for line in f: + written_report = json.loads(line) + self.assertEqual(sample_report_entry, written_report)