Test follow via c2s

master
Bob Mottram 2019-07-16 22:38:06 +01:00
parent 930cd963f8
commit f02839e32f
4 changed files with 103 additions and 9 deletions

View File

@ -173,7 +173,7 @@ class PubServer(BaseHTTPRequestHandler):
' is not a permitted activity type') ' is not a permitted activity type')
return False return False
if messageJson.get('id'): if messageJson.get('id'):
postId=messageJson['id'] postId=messageJson['id'].replace('/activity','')
else: else:
postId=None postId=None
if self.server.debug: if self.server.debug:

View File

@ -216,8 +216,8 @@ if args.tests:
if args.testsnetwork: if args.testsnetwork:
print('Network Tests') print('Network Tests')
testPostMessageBetweenServers() #testPostMessageBetweenServers()
testFollowBetweenServers() #testFollowBetweenServers()
testClientToServer() testClientToServer()
sys.exit() sys.exit()

View File

@ -18,7 +18,11 @@ from utils import getNicknameFromActor
from utils import getStatusNumber from utils import getStatusNumber
from utils import followPerson from utils import followPerson
from posts import sendSignedJson from posts import sendSignedJson
from posts import getPersonBox
from acceptreject import createAccept from acceptreject import createAccept
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
from session import postJson
def getFollowersOfPerson(baseDir: str, \ def getFollowersOfPerson(baseDir: str, \
nickname: str,domain: str, \ nickname: str,domain: str, \
@ -329,7 +333,6 @@ def sendFollowRequest(session,baseDir: str, \
followedId=followHttpPrefix+'://'+requestDomain+'/users/'+followNickname followedId=followHttpPrefix+'://'+requestDomain+'/users/'+followNickname
newFollowJson = { newFollowJson = {
'id': httpPrefix+'://'+fullDomain+'/users/'+nickname+'/statuses/'+statusNumber,
'type': 'Follow', 'type': 'Follow',
'actor': followActor, 'actor': followActor,
'object': followedId, 'object': followedId,
@ -347,6 +350,79 @@ def sendFollowRequest(session,baseDir: str, \
return newFollowJson return newFollowJson
def sendFollowRequestViaServer(session,fromNickname: str,password: str,
fromDomain: str,fromPort: int, \
followNickname: str,followDomain: str,followPort: int, \
httpPrefix: str, \
cachedWebfingers: {},personCache: {}, \
debug: bool) -> {}:
"""Creates a follow request via c2s
"""
if not session:
print('WARN: No session for sendFollowRequestViaServer')
return 6
fromDomainFull=fromDomain
if fromPort!=80 and fromPort!=443:
fromDomainFull=fromDomain+':'+str(fromPort)
followDomainFull=followDomain
if followPort!=80 and followPort!=443:
followDomainFull=followDomain+':'+str(followPort)
followActor=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname
followedId=httpPrefix+'://'+followDomainFull+'/users/'+followNickname
statusNumber,published = getStatusNumber()
newFollowJson = {
'type': 'Follow',
'actor': followActor,
'object': followedId,
'to': [followedId],
'cc': ['https://www.w3.org/ns/activitystreams#Public'],
'published': published
}
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname
# lookup the inbox for the To handle
wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers)
if not wfRequest:
if debug:
print('DEBUG: announce webfinger failed for '+handle)
return 1
postToBox='outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \
getPersonBox(session,wfRequest,personCache,postToBox)
if not inboxUrl:
if debug:
print('DEBUG: No '+postToBox+' was found for '+handle)
return 3
if not fromPersonId:
if debug:
print('DEBUG: No actor was found for '+handle)
return 4
authHeader=createBasicAuthHeader(fromNickname,password)
headers = {'host': fromDomain, \
'Content-type': 'application/json', \
'Authorization': authHeader}
postResult = \
postJson(session,newFollowJson,[],inboxUrl,headers,"inbox:write")
#if not postResult:
# if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
# return 5
if debug:
print('DEBUG: c2s POST follow success')
return newFollowJson
def getFollowersOfActor(baseDir :str,actor :str,debug: bool) -> {}: def getFollowersOfActor(baseDir :str,actor :str,debug: bool) -> {}:
"""In a shared inbox if we receive a post we know who it's from """In a shared inbox if we receive a post we know who it's from
and if it's addressed to followers then we need to get a list of those. and if it's addressed to followers then we need to get a list of those.

View File

@ -32,6 +32,7 @@ from posts import archivePostsForPerson
from posts import sendPostViaServer from posts import sendPostViaServer
from follow import clearFollows from follow import clearFollows
from follow import clearFollowers from follow import clearFollowers
from follow import sendFollowRequestViaServer
from utils import followPerson from utils import followPerson
from follow import followerOfPerson from follow import followerOfPerson
from follow import unfollowPerson from follow import unfollowPerson
@ -1068,17 +1069,34 @@ def testClientToServer():
outboxPostId=postJsonObject['id'].replace('/activity','') outboxPostId=postJsonObject['id'].replace('/activity','')
assert outboxPostId assert outboxPostId
print('message id obtained: '+outboxPostId) print('message id obtained: '+outboxPostId)
print('\n\nAlice follows Bob')
sendFollowRequestViaServer(sessionAlice,'alice',password, \
aliceDomain,alicePort, \
'bob',bobDomain,bobPort, \
httpPrefix, \
cachedWebfingers,personCache, \
True)
for t in range(10):
if os.path.isfile(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt'):
if os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt'):
break
time.sleep(1)
assert os.path.isfile(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt')
assert os.path.isfile(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt')
print('\n\nBob repeats the post') print('\n\nBob repeats the post')
sessionBob = createSession(bobDomain,bobPort,useTor) sessionBob = createSession(bobDomain,bobPort,useTor)
password='bobpass' password='bobpass'
outboxPath=bobDir+'/accounts/bob@'+bobDomain+'/outbox' outboxPath=bobDir+'/accounts/bob@'+bobDomain+'/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
sendAnnounceViaServer(sessionBob,'bob',password, sendAnnounceViaServer(sessionBob,'bob',password, \
bobDomain,bobPort, \ bobDomain,bobPort, \
httpPrefix,outboxPostId, \ httpPrefix,outboxPostId, \
cachedWebfingers,personCache, \ cachedWebfingers, \
True) personCache,True)
for i in range(10): for i in range(10):
if os.path.isdir(outboxPath): if os.path.isdir(outboxPath):
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:
@ -1098,8 +1116,8 @@ def testClientToServer():
assert thrBob.isAlive()==False assert thrBob.isAlive()==False
os.chdir(baseDir) os.chdir(baseDir)
shutil.rmtree(aliceDir) #shutil.rmtree(aliceDir)
shutil.rmtree(bobDir) #shutil.rmtree(bobDir)
def runAllTests(): def runAllTests():
print('Running tests...') print('Running tests...')