Unit test for word switching

merge-requests/30/head
Bob Mottram 2021-07-06 17:29:03 +01:00
parent f51b7194f4
commit 0eb405ef5b
2 changed files with 50 additions and 25 deletions

View File

@ -202,35 +202,35 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool:
return False
def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str:
def switchWords(baseDir: str, nickname: str, domain: str, content: str,
rules: [] = []) -> str:
"""Performs word replacements. eg. Trump -> The Orange Menace
"""
if isPGPEncrypted(content) or containsPGPPublicKey(content):
return content
switchWordsFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/replacewords.txt'
if not os.path.isfile(switchWordsFilename):
return content
with open(switchWordsFilename, 'r') as fp:
for line in fp:
replaceStr = line.replace('\n', '').replace('\r', '')
wordTransform = None
if '->' in replaceStr:
wordTransform = replaceStr.split('->')
elif ':' in replaceStr:
wordTransform = replaceStr.split(':')
elif ',' in replaceStr:
wordTransform = replaceStr.split(',')
elif ';' in replaceStr:
wordTransform = replaceStr.split(';')
elif '-' in replaceStr:
wordTransform = replaceStr.split('-')
if not wordTransform:
continue
if len(wordTransform) == 2:
replaceStr1 = wordTransform[0].strip().replace('"', '')
replaceStr2 = wordTransform[1].strip().replace('"', '')
content = content.replace(replaceStr1, replaceStr2)
if not rules:
switchWordsFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/replacewords.txt'
if not os.path.isfile(switchWordsFilename):
return content
with open(switchWordsFilename, 'r') as fp:
rules = fp.readlines()
for line in rules:
replaceStr = line.replace('\n', '').replace('\r', '')
splitters = ('->', ':', ',', ';', '-')
wordTransform = None
for splitStr in splitters:
if splitStr in replaceStr:
wordTransform = replaceStr.split(splitStr)
break
if not wordTransform:
continue
if len(wordTransform) == 2:
replaceStr1 = wordTransform[0].strip().replace('"', '')
replaceStr2 = wordTransform[1].strip().replace('"', '')
content = content.replace(replaceStr1, replaceStr2)
return content

View File

@ -94,6 +94,7 @@ from inbox import jsonPostAllowsComments
from inbox import validInbox
from inbox import validInboxFilenames
from categories import guessHashtagCategory
from content import switchWords
from content import extractTextFieldsInPOST
from content import validHashTag
from content import htmlReplaceEmailQuote
@ -4117,9 +4118,33 @@ def _testUserAgentDomain() -> None:
assert userAgentDomain(userAgent, False) is None
def _testSwitchWords() -> None:
print('testSwitchWords')
rules = [
"rock -> hamster",
"orange -> lemon"
]
baseDir = os.getcwd()
nickname = 'testuser'
domain = 'testdomain.com'
content = 'This is a test'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == content
content = 'This is orange test'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == 'This is lemon test'
content = 'This is a test rock'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == 'This is a test hamster'
def runAllTests():
print('Running tests...')
updateDefaultThemesList(os.getcwd())
_testSwitchWords()
_testFunctions()
_testUserAgentDomain()
_testRoles()