diff --git a/content.py b/content.py index bc3db29e6..88ea8b9fa 100644 --- a/content.py +++ b/content.py @@ -979,16 +979,21 @@ def saveMediaInFormPOST(mediaBytes, debug: bool, return filename, attachmentMediaType -def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}: +def extractTextFieldsInPOST(postBytes, boundary, debug: bool, + unitTestData=None) -> {}: """Returns a dictionary containing the text fields of a http form POST The boundary argument comes from the http header """ - msg = email.parser.BytesParser().parsebytes(postBytes) + if not unitTestData: + msgBytes = email.parser.BytesParser().parsebytes(postBytes) + messageFields = msgBytes.decode('utf-8') + else: + messageFields = unitTestData + if debug: - print('DEBUG: POST arriving ' + - msg.get_payload(decode=True).decode('utf-8')) - messageFields = msg.get_payload(decode=True) - messageFields = messageFields.decode('utf-8').split(boundary) + print('DEBUG: POST arriving ' + messageFields) + + messageFields = messageFields.split(boundary) fields = {} # examine each section of the POST, separated by the boundary for f in messageFields: @@ -1002,7 +1007,8 @@ def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}: postKey = postStr.split('"', 1)[0] postValueStr = postStr.split('"', 1)[1] if ';' in postValueStr: - continue + if postKey != 'message': + continue if '\r\n' not in postValueStr: continue postLines = postValueStr.split('\r\n') diff --git a/tests.py b/tests.py index e4dace257..92fb6c7e6 100644 --- a/tests.py +++ b/tests.py @@ -77,6 +77,7 @@ from inbox import jsonPostAllowsComments from inbox import validInbox from inbox import validInboxFilenames from categories import guessHashtagCategory +from content import extractTextFieldsInPOST from content import validHashTag from content import htmlReplaceEmailQuote from content import htmlReplaceQuoteMarks @@ -3330,9 +3331,47 @@ def testMarkdownToHtml(): 'Or pounce.' +def testExtractTextFieldsInPOST(): + print('testExtractTextFieldsInPOST') + boundary = '-----------------------------116202748023898664511855843036' + formData = '-----------------------------116202748023898664511855' + \ + '843036\r\nContent-Disposition: form-data; name="submitPost"' + \ + '\r\n\r\nSubmit\r\n-----------------------------116202748023' + \ + '898664511855843036\r\nContent-Disposition: form-data; name=' + \ + '"subject"\r\n\r\n\r\n-----------------------------116202748' + \ + '023898664511855843036\r\nContent-Disposition: form-data; na' + \ + 'me="message"\r\n\r\nThis is a ; test\r\n-------------------' + \ + '----------116202748023898664511855843036\r\nContent-Disposi' + \ + 'tion: form-data; name="commentsEnabled"\r\n\r\non\r\n------' + \ + '-----------------------116202748023898664511855843036\r\nCo' + \ + 'ntent-Disposition: form-data; name="eventDate"\r\n\r\n\r\n' + \ + '-----------------------------116202748023898664511855843036' + \ + '\r\nContent-Disposition: form-data; name="eventTime"\r\n\r' + \ + '\n\r\n-----------------------------116202748023898664511855' + \ + '843036\r\nContent-Disposition: form-data; name="location"' + \ + '\r\n\r\n\r\n-----------------------------116202748023898664' + \ + '511855843036\r\nContent-Disposition: form-data; name=' + \ + '"imageDescription"\r\n\r\n\r\n-----------------------------' + \ + '116202748023898664511855843036\r\nContent-Disposition: ' + \ + 'form-data; name="attachpic"; filename=""\r\nContent-Type: ' + \ + 'application/octet-stream\r\n\r\n\r\n----------------------' + \ + '-------116202748023898664511855843036--\r\n' + debug = False + fields = extractTextFieldsInPOST(None, boundary, debug, formData) + assert fields['submitPost'] == 'Submit' + assert fields['subject'] == '' + assert fields['commentsEnabled'] == 'on' + assert fields['eventDate'] == '' + assert fields['eventTime'] == '' + assert fields['location'] == '' + assert fields['imageDescription'] == '' + assert fields['message'] == 'This is a ; test' + + def runAllTests(): print('Running tests...') testFunctions() + testExtractTextFieldsInPOST() testMarkdownToHtml() testValidHashTag() testPrepareHtmlPostNickname()