[IMP] survey_extra_fields: new params max_file_size and allowed_extensions for file question type
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m45s
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m45s
This commit is contained in:
@@ -109,6 +109,72 @@ class TestSurveyFileSaveLines(TestSurveyFileCommon):
|
||||
self.assertTrue(line.skipped)
|
||||
|
||||
|
||||
class TestSurveyFileConstraints(TestSurveyFileCommon):
|
||||
"""Test _check_file_constraints validation logic."""
|
||||
|
||||
def test_no_constraints(self):
|
||||
"""No max_file_size and no allowed_extensions: any file passes."""
|
||||
self.question_file.write({"max_file_size": 0, "allowed_extensions": False})
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": self.file_b64, "name": "anything.exe"})
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
line = answer.user_input_line_ids.filtered(
|
||||
lambda l: l.question_id == self.question_file
|
||||
)
|
||||
self.assertFalse(line.skipped)
|
||||
|
||||
def test_file_within_size_limit(self):
|
||||
"""File smaller than max_file_size passes."""
|
||||
self.question_file.write({"max_file_size": 10})
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": self.file_b64, "name": self.file_name})
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
line = answer.user_input_line_ids.filtered(
|
||||
lambda l: l.question_id == self.question_file
|
||||
)
|
||||
self.assertFalse(line.skipped)
|
||||
|
||||
def test_file_exceeds_size_limit(self):
|
||||
"""File larger than max_file_size raises ValidationError."""
|
||||
self.question_file.write({"max_file_size": 1})
|
||||
large_content = b"x" * (1 * 1024 * 1024 + 1)
|
||||
large_b64 = base64.b64encode(large_content).decode()
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": large_b64, "name": self.file_name})
|
||||
with self.assertRaises(Exception):
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
|
||||
def test_allowed_extension_passes(self):
|
||||
"""File with an allowed extension passes."""
|
||||
self.question_file.write({"allowed_extensions": ".pdf,.docx"})
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": self.file_b64, "name": "report.docx"})
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
line = answer.user_input_line_ids.filtered(
|
||||
lambda l: l.question_id == self.question_file
|
||||
)
|
||||
self.assertFalse(line.skipped)
|
||||
|
||||
def test_disallowed_extension_raises(self):
|
||||
"""File with a disallowed extension raises ValidationError."""
|
||||
self.question_file.write({"allowed_extensions": ".pdf,.docx"})
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": self.file_b64, "name": "script.exe"})
|
||||
with self.assertRaises(Exception):
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
|
||||
def test_both_constraints_valid(self):
|
||||
"""File respecting both size and extension constraints passes."""
|
||||
self.question_file.write({"max_file_size": 10, "allowed_extensions": ".pdf"})
|
||||
answer = self._add_answer(self.survey, self.survey_manager.partner_id)
|
||||
file_json = json.dumps({"data": self.file_b64, "name": self.file_name})
|
||||
answer.save_lines(self.question_file, file_json)
|
||||
line = answer.user_input_line_ids.filtered(
|
||||
lambda l: l.question_id == self.question_file
|
||||
)
|
||||
self.assertFalse(line.skipped)
|
||||
|
||||
|
||||
class TestSurveyFileDisplayName(TestSurveyFileCommon):
|
||||
"""Test the display_name computation for file answer lines."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user