diff --git a/.gitignore b/.gitignore index a0d8bfa..c10b738 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,6 @@ dmypy.json # idea .idea/ + +# for AutoTest +TestData/ \ No newline at end of file diff --git a/testscripts/testmain.py b/testscripts/testmain.py index d043cf8..b0f5e5b 100644 --- a/testscripts/testmain.py +++ b/testscripts/testmain.py @@ -19,4 +19,13 @@ fileList = tests.parseTest(shareUrl, sharePass, expectedFolder) testPrint('TestMain', 'info', '') testPrint('TestMain', 'info', 'ParseTest Complete!') + +testPrint('TestMain', 'info', 'Starting DownloadTest...') +tests.downloadTest(fileList) +testPrint('TestMain', 'info', 'DownloadTest Complete!') + +testPrint('TestMain', 'info', 'Starting ContentTest...') +tests.contentTest(expectedFileContents) +testPrint('TestMain', 'info', 'ContentTest Complete!') + testPrint('TestMain', 'info', 'Test Complete!') \ No newline at end of file diff --git a/testscripts/tests.py b/testscripts/tests.py index 41ce4a7..5c9ba13 100644 --- a/testscripts/tests.py +++ b/testscripts/tests.py @@ -1,7 +1,9 @@ from ctparse.CTFile import CTFileShare from utils.logging import testPrint from utils.models import CTFile +import os, time, urllib.request +# ParseTest Start def parseTest(link, passwd, expectedFolder): ct = CTFileShare(link, passwd) @@ -40,4 +42,55 @@ def proccessRetFolder(retFolder): items.append(proccessRetFolder(retItem)) return ['Folder', retFolder[1], items] +# ParseTest End +# DownloadTest Start + +def downloadTest(fileList): + try: + os.mkdir('TestData') + except FileExistsError: + pass + links = getDownloadLinkForFolder(fileList) + testPrint('DownloadTest', 'debug', f'Links: {links}') + for fileName in list(links.keys()): + testPrint('DownloadTest', 'info', f'Downloading {fileName} to TestData/{fileName}') + urllib.request.urlretrieve(links[fileName], f'TestData/{fileName}') + + +def getDownloadLinkForFolder(fileList): + testPrint('DownloadTest', 'debug', f'Get folder: {fileList}') + links = {} + for file in fileList[2]: + if type(file) == CTFile: + fileName = file.name + testPrint('DownloadTest', 'info', f'Get download link: {fileName}') + link = file.genDownloadLink() + if link == None: # 出问题了就消停会儿再试一次 + time.sleep(1) + link = file.genDownloadLink() + links[fileName] = link.fileDownloadLink + testPrint('DownloadTest', 'info', f'Download Link: {links[fileName]}') + elif type(file) == list: + nxtLinks = getDownloadLinkForFolder(file) + for key in list(nxtLinks.keys()): + links[key] = nxtLinks[key] + time.sleep(0.25) + return links + +# DownloadTest End + +# ContentTest Start +def contentTest(expectedContents): + for fileName in list(expectedContents.keys()): + testPrint('ContentTest', 'info', f'Check file: {fileName}') + f = open(f'TestData/{fileName}') + content = f.read() + if content[-1:] == '\n': + content = content[:-1] # 检测到结尾的换行符就去掉,避免干扰 + if content == expectedContents[fileName]: + testPrint('ContentTest', 'info', f'Check PASS: {expectedContents[fileName]} -> {content}') + else: + testPrint('ContentTest', 'err', f'Check FAIL: {expectedContents[fileName]} -> {content}') + raise ValueError +