Adding test for announce

master
Bob Mottram 2019-07-11 18:55:10 +01:00
parent 4c298025f4
commit ca0db9e45f
3 changed files with 76 additions and 22 deletions

View File

@ -11,11 +11,18 @@ import commentjson
from utils import getStatusNumber from utils import getStatusNumber
from utils import createOutboxDir from utils import createOutboxDir
from utils import urlPermitted from utils import urlPermitted
from utils import getNicknameFromActor
from utils import getDomainFromActor
from posts import sendSignedJson
def createAnnounce(baseDir: str,federationList: [], \ def createAnnounce(session,baseDir: str,federationList: [], \
nickname: str, domain: str, port: int, \ nickname: str, domain: str, port: int, \
toUrl: str, ccUrl: str, httpPrefix: str, \ toUrl: str, ccUrl: str, httpPrefix: str, \
objectUrl: str, saveToFile: bool) -> {}: objectUrl: str, saveToFile: bool, \
clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
debug: bool) -> {}:
"""Creates an announce message """Creates an announce message
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
and ccUrl might be a specific person favorited or repeated and the and ccUrl might be a specific person favorited or repeated and the
@ -25,15 +32,18 @@ def createAnnounce(baseDir: str,federationList: [], \
if not urlPermitted(objectUrl,federationList,"inbox:write"): if not urlPermitted(objectUrl,federationList,"inbox:write"):
return None return None
if ':' in domain:
domain=domain.split(':')[0]
fullDomain=domain
if port!=80 and port!=443: if port!=80 and port!=443:
domain=domain+':'+str(port) fullDomain=domain+':'+str(port)
statusNumber,published = getStatusNumber() statusNumber,published = getStatusNumber()
newAnnounceId= \ newAnnounceId= \
httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber
newAnnounce = { newAnnounce = {
'actor': httpPrefix+'://'+domain+'/users/'+nickname, 'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
'atomUri': httpPrefix+'://'+domain+'/users/'+nickname+'/statuses/'+statusNumber, 'atomUri': httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber,
'cc': [], 'cc': [],
'id': newAnnounceId+'/activity', 'id': newAnnounceId+'/activity',
'object': objectUrl, 'object': objectUrl,
@ -43,44 +53,75 @@ def createAnnounce(baseDir: str,federationList: [], \
} }
if ccUrl: if ccUrl:
if len(ccUrl)>0: if len(ccUrl)>0:
newAnnounce['cc']=ccUrl newAnnounce['cc']=[ccUrl]
if saveToFile: if saveToFile:
if ':' in domain:
domain=domain.split(':')[0]
outboxDir = createOutboxDir(nickname,domain,baseDir) outboxDir = createOutboxDir(nickname,domain,baseDir)
filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json' filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json'
with open(filename, 'w') as fp: with open(filename, 'w') as fp:
commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False) commentjson.dump(newAnnounce, fp, indent=4, sort_keys=False)
announceNickname=None
announceDomain=None
announcePort=None
if '/users/' in objectUrl:
announceNickname=getNicknameFromActor(objectUrl)
announceDomain,announcePort=getDomainFromActor(objectUrl)
if announceNickname and announceDomain:
sendSignedJson(newAnnounce,session,baseDir, \
nickname,domain,port, \
announceNickname,announceDomain,announcePort, \
'https://www.w3.org/ns/activitystreams#Public', \
httpPrefix,True,clientToServer,federationList, \
sendThreads,postLog,cachedWebfingers,personCache,debug)
return newAnnounce return newAnnounce
def announcePublic(baseDir: str,federationList: [], \ def announcePublic(session,baseDir: str,federationList: [], \
nickname: str, domain: str, port: int, httpPrefix: str, \ nickname: str, domain: str, port: int, httpPrefix: str, \
objectUrl: str, saveToFile: bool) -> {}: objectUrl: str,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
debug: bool) -> {}:
"""Makes a public announcement """Makes a public announcement
""" """
fromDomain=domain fromDomain=domain
if port!=80 and port!=443: if port!=80 and port!=443:
fromDomain=fromDomain+':'+str(port) if ':' not in domain:
fromDomain=domain+':'+str(port)
toUrl = 'https://www.w3.org/ns/activitystreams#Public' toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl = httpPrefix + '://'+fromDomain+'/users/'+nickname+'/followers' ccUrl = httpPrefix + '://'+fromDomain+'/users/'+nickname+'/followers'
return createAnnounce(baseDir,nickname, domain, port, \ return createAnnounce(session,baseDir,federationList, \
toUrl, ccUrl, httpPrefix, objectUrl, saveToFile) nickname,domain,port, \
toUrl,ccUrl,httpPrefix, \
objectUrl,True,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
debug)
def repeatPost(baseDir: str,federationList: [], \ def repeatPost(session,baseDir: str,federationList: [], \
nickname: str, domain: str, port: int, httpPrefix: str, \ nickname: str, domain: str, port: int, httpPrefix: str, \
announceNickname: str, announceDomain: str, \ announceNickname: str, announceDomain: str, \
announcePort: int, announceHttpsPrefix: str, \ announcePort: int, announceHttpsPrefix: str, \
announceStatusNumber: int, saveToFile: bool) -> {}: announceStatusNumber: int,clientToServer: bool, \
sendThreads: [],postLog: [], \
personCache: {},cachedWebfingers: {}, \
debug: bool) -> {}:
"""Repeats a given status post """Repeats a given status post
""" """
announcedDomain=announceDomain announcedDomain=announceDomain
if announcePort!=80 and announcePort!=443: if announcePort!=80 and announcePort!=443:
announcedDomain=announcedDomain+':'+str(announcePort) if ':' not in announcedDomain:
announcedDomain=announcedDomain+':'+str(announcePort)
objectUrl = announceHttpsPrefix + '://'+announcedDomain+'/users/'+ \ objectUrl = announceHttpsPrefix + '://'+announcedDomain+'/users/'+ \
announceNickname+'/statuses/'+str(announceStatusNumber) announceNickname+'/statuses/'+str(announceStatusNumber)
return announcePublic(baseDir,nickname, domain, port, \ return announcePublic(session,baseDir,federationList, \
httpPrefix, objectUrl, saveToFile) nickname,domain,port,httpPrefix, \
objectUrl,clientToServer, \
sendThreads,postLog, \
personCache,cachedWebfingers, \
debug)

View File

@ -88,7 +88,9 @@ def inboxPermittedMessage(domain: str,messageJson: {},federationList: []) -> boo
if not urlPermitted(actor,federationList,"inbox:write"): if not urlPermitted(actor,federationList,"inbox:write"):
return False return False
if messageJson['type']!='Follow' and messageJson['type']!='Like': if messageJson['type']!='Follow' and \
messageJson['type']!='Like' and \
messageJson['type']!='Announce':
if messageJson.get('object'): if messageJson.get('object'):
if messageJson['object'].get('inReplyTo'): if messageJson['object'].get('inReplyTo'):
inReplyTo=messageJson['object']['inReplyTo'] inReplyTo=messageJson['object']['inReplyTo']

View File

@ -43,6 +43,7 @@ from auth import createBasicAuthHeader
from auth import authorizeBasic from auth import authorizeBasic
from auth import storeBasicCredentials from auth import storeBasicCredentials
from like import likePost from like import likePost
from announce import announcePublic
testServerAliceRunning = False testServerAliceRunning = False
testServerBobRunning = False testServerBobRunning = False
@ -296,7 +297,7 @@ def testPostMessageBetweenServers():
if '#statuses#' in name: if '#statuses#' in name:
statusNumber=int(name.split('#statuses#')[1].replace('.json','')) statusNumber=int(name.split('#statuses#')[1].replace('.json',''))
outboxPostFilename=outboxPath+'/'+name outboxPostFilename=outboxPath+'/'+name
assert statusNumber assert statusNumber>0
assert outboxPostFilename assert outboxPostFilename
assert likePost(sessionBob,bobDir,federationList, \ assert likePost(sessionBob,bobDir,federationList, \
'bob',bobDomain,bobPort,httpPrefix, \ 'bob',bobDomain,bobPort,httpPrefix, \
@ -313,7 +314,17 @@ def testPostMessageBetweenServers():
alicePostJson=commentjson.load(fp) alicePostJson=commentjson.load(fp)
pprint(alicePostJson) pprint(alicePostJson)
assert 'likes' in open(outboxPostFilename).read() assert 'likes' in open(outboxPostFilename).read()
print('\n\n*******************************************************')
print("Bob repeats Alice's post")
objectUrl=httpPrefix+'://'+aliceDomain+':'+str(alicePort)+'/users/alice/statuses/'+str(statusNumber)
announcePublic(sessionBob,bobDir,federationList, \
'bob',bobDomain,bobPort,httpPrefix, \
objectUrl, \
False,bobSendThreads,bobPostLog, \
bobPersonCache,bobCachedWebfingers, \
True)
# stop the servers # stop the servers
thrAlice.kill() thrAlice.kill()
thrAlice.join() thrAlice.join()