Skip to content

Commit

Permalink
Implement test that attempts to write to the backend using JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Mar 10, 2016
1 parent bc2f24f commit d7f9d59
Showing 1 changed file with 67 additions and 52 deletions.
119 changes: 67 additions & 52 deletions oonib/test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,45 @@
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
Expand Down Expand Up @@ -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
}
Expand All @@ -113,7 +104,7 @@ def test_create_valid_report(self):
@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)
Expand Down Expand Up @@ -148,7 +139,7 @@ def test_create_and_update_report(self):
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
Expand All @@ -172,7 +163,7 @@ def test_create_update_and_close_report(self):
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")
Expand All @@ -185,5 +176,29 @@ def test_create_update_and_close_report(self):
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)

0 comments on commit d7f9d59

Please sign in to comment.