Test for attaching an image

master
Bob Mottram 2019-07-12 20:26:54 +01:00
parent 83b172d046
commit 0b1e64906e
3 changed files with 25 additions and 13 deletions

View File

@ -17,7 +17,7 @@ from auth import createPassword
from shutil import copyfile from shutil import copyfile
def getImageHash(imageFilename: str): def getImageHash(imageFilename: str):
return blurencode(numpy.array(Image.open("img/logo.png").convert("RGB"))) return blurencode(numpy.array(Image.open(imageFilename).convert("RGB")))
def isImage(imageFilename: str) -> bool: def isImage(imageFilename: str) -> bool:
if imageFilename.endswith('.png') or \ if imageFilename.endswith('.png') or \
@ -26,12 +26,17 @@ def isImage(imageFilename: str) -> bool:
return True return True
return False return False
def createMediaPath(baseDir: str,weeksSinceEpoch: int) -> None: def createMediaDirs(baseDir: str,mediaPath: str) -> None:
if not os.path.isdir(baseDir+'/media'): if not os.path.isdir(baseDir+'/media'):
os.mkdir(baseDir+'/media') os.mkdir(baseDir+'/media')
if not os.path.isdir(baseDir+'/media/'+str(weeksSinceEpoch)): if not os.path.isdir(baseDir+'/'+mediaPath):
os.mkdir(baseDir+'/media/'+str(weeksSinceEpoch)) os.mkdir(baseDir+'/'+mediaPath)
def getMediaPath() -> str:
currTime=datetime.datetime.utcnow()
weeksSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days/7
return 'media/'+str(weeksSinceEpoch)
def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \ def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \
postJson: {},imageFilename: str,description: str, \ postJson: {},imageFilename: str,description: str, \
useBlurhash: bool) -> {}: useBlurhash: bool) -> {}:
@ -55,10 +60,9 @@ def attachImage(baseDir: str,httpPrefix: str,domain: str,port: int, \
if ':' not in domain: if ':' not in domain:
domain=domain+':'+str(port) domain=domain+':'+str(port)
currTime=datetime.datetime.utcnow() mPath=getMediaPath()
weeksSinceEpoch=(currTime - datetime.datetime(1970,1,1)).days/7 createMediaDirs(baseDir,mPath)
createMediaPath(baseDir,weeksSinceEpoch) mediaPath=mPath+'/'+createPassword(32)+'.'+fileExtension
mediaPath='media/'+str(weeksSinceEpoch)+'/'+createPassword(32)+'.'+fileExtension
mediaFilename=baseDir+'/'+mediaPath mediaFilename=baseDir+'/'+mediaPath
attachmentJson={ attachmentJson={

View File

@ -401,7 +401,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
if attachImageFilename: if attachImageFilename:
newPost['object']= \ newPost['object']= \
attachImage(baseDir,httpPrefix,domain,port, \ attachImage(baseDir,httpPrefix,domain,port, \
postJson['object'],attachImageFilename, \ newPost['object'],attachImageFilename, \
imageDescription,useBlurhash) imageDescription,useBlurhash)
else: else:
newPost = { newPost = {
@ -437,7 +437,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
if attachImageFilename: if attachImageFilename:
newPost= \ newPost= \
attachImage(baseDir,httpPrefix,domain,port, \ attachImage(baseDir,httpPrefix,domain,port, \
postJson,attachImageFilename, \ newPost,attachImageFilename, \
imageDescription,useBlurhash) imageDescription,useBlurhash)
if ccUrl: if ccUrl:
if len(ccUrl)>0: if len(ccUrl)>0:

View File

@ -44,6 +44,7 @@ from auth import authorizeBasic
from auth import storeBasicCredentials from auth import storeBasicCredentials
from like import likePost from like import likePost
from announce import announcePublic from announce import announcePublic
from media import getMediaPath
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -259,23 +260,30 @@ def testPostMessageBetweenServers():
ccUrl=None ccUrl=None
alicePersonCache={} alicePersonCache={}
aliceCachedWebfingers={} aliceCachedWebfingers={}
useBlurhash=False attachedImageFilename=baseDir+'/img/logo.png'
attachedImageDescription='Logo'
useBlurhash=True
# nothing in Alice's outbox # nothing in Alice's outbox
outboxPath=aliceDir+'/accounts/alice@'+aliceDomain+'/outbox' outboxPath=aliceDir+'/accounts/alice@'+aliceDomain+'/outbox'
assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==0 assert len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==0
sendResult = sendPost(sessionAlice,aliceDir,'alice', aliceDomain, alicePort, 'bob', bobDomain, bobPort, ccUrl, httpPrefix, 'Why is a mouse when it spins?', followersOnly, saveToFile, clientToServer,None,None,useBlurhash, federationList, aliceSendThreads, alicePostLog, aliceCachedWebfingers,alicePersonCache,inReplyTo, inReplyToAtomUri, subject) sendResult = sendPost(sessionAlice,aliceDir,'alice', aliceDomain, alicePort, 'bob', bobDomain, bobPort, ccUrl, httpPrefix, 'Why is a mouse when it spins?', followersOnly, saveToFile, clientToServer,attachedImageFilename,attachedImageDescription,useBlurhash, federationList, aliceSendThreads, alicePostLog, aliceCachedWebfingers,alicePersonCache,inReplyTo, inReplyToAtomUri, subject)
print('sendResult: '+str(sendResult)) print('sendResult: '+str(sendResult))
queuePath=bobDir+'/accounts/bob@'+bobDomain+'/queue' queuePath=bobDir+'/accounts/bob@'+bobDomain+'/queue'
inboxPath=bobDir+'/accounts/bob@'+bobDomain+'/inbox' inboxPath=bobDir+'/accounts/bob@'+bobDomain+'/inbox'
mPath=getMediaPath()
mediaPath=aliceDir+'/'+mPath
for i in range(30): for i in range(30):
if os.path.isdir(inboxPath): if os.path.isdir(inboxPath):
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0: if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0:
if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1: if len([name for name in os.listdir(outboxPath) if os.path.isfile(os.path.join(outboxPath, name))])==1:
break if len([name for name in os.listdir(mediaPath) if os.path.isfile(os.path.join(mediaPath, name))])>0:
break
time.sleep(1) time.sleep(1)
# Image attachment created
assert len([name for name in os.listdir(mediaPath) if os.path.isfile(os.path.join(mediaPath, name))])>0
# inbox item created # inbox item created
assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1 assert len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])==1
# queue item removed # queue item removed