Commit c4917a60 by Gabe Mulley

Merge pull request #139 from edx/gabe/fix-answer-dist

Fix answer distribution handling of malformed event data
parents 35e5861a 26f1a389
...@@ -983,7 +983,16 @@ def get_problem_check_event(line): ...@@ -983,7 +983,16 @@ def get_problem_check_event(line):
log.error("encountered explicit problem_check event with bogus problem_id: %s", event) log.error("encountered explicit problem_check event with bogus problem_id: %s", event)
return None return None
if len(event.get('event', {}).get('answers', [])) == 0: event = event.get('event', {})
answers = event.get('answers', {})
if len(answers) == 0:
return None
try:
_check_answer_ids(answers)
_check_answer_ids(event.get('submission', {}))
except (TypeError, ValueError):
log.error("encountered explicit problem_check event with invalid answers: %s", event)
return None return None
problem_data_json = json.dumps(problem_data) problem_data_json = json.dumps(problem_data)
...@@ -991,3 +1000,11 @@ def get_problem_check_event(line): ...@@ -991,3 +1000,11 @@ def get_problem_check_event(line):
value = (problem_data.get('timestamp'), problem_data_json) value = (problem_data.get('timestamp'), problem_data_json)
return key, value return key, value
def _check_answer_ids(answer_dict):
if not isinstance(answer_dict, dict):
raise TypeError('Expected dictionaries for answers')
for answer_id in answer_dict:
if '\n' in answer_id or '\t' in answer_id:
raise ValueError('Malformed answer_id')
...@@ -179,6 +179,16 @@ class ProblemCheckEventMapTest(InitializeOpaqueKeysMixin, ProblemCheckEventBaseT ...@@ -179,6 +179,16 @@ class ProblemCheckEventMapTest(InitializeOpaqueKeysMixin, ProblemCheckEventBaseT
line = self.create_event_log_line(context=None) line = self.create_event_log_line(context=None)
self.assert_no_map_output_for(line) self.assert_no_map_output_for(line)
def test_invalid_answer_id(self):
self.answer_id = 'foo\nbar'
line = self.create_event_log_line()
self.assert_no_map_output_for(line)
def test_invalid_answer_id_with_tab(self):
self.answer_id = 'foo\tbar'
line = self.create_event_log_line()
self.assert_no_map_output_for(line)
def test_good_problem_check_event(self): def test_good_problem_check_event(self):
# Here, we make the event as a dictionary since we're comparing based on dictionaries anyway. # Here, we make the event as a dictionary since we're comparing based on dictionaries anyway.
event = self.create_event_dict() event = self.create_event_dict()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment