Fixing unit tests

merge-requests/30/head
Bob Mottram 2020-02-19 13:34:24 +00:00
parent 55dcdd546d
commit 4bd90ca3a4
2 changed files with 4 additions and 313 deletions

View File

@ -5476,20 +5476,21 @@ def runDaemon(mediaInstance: bool,maxRecentPosts: int, \
print('Invalid domain: ' + domain)
return
serverAddress = ('', proxyPort)
if unitTest:
serverAddress = (domain, proxyPort)
pubHandler = partial(PubServerUnitTest)
else:
serverAddress = ('', proxyPort)
pubHandler = partial(PubServer)
try:
httpd = ThreadingHTTPServer(serverAddress, pubHandler)
except Exception as e:
if e.errno==98:
print('ERROR: HTTP Server address is already in use. '+str(serverAddress))
print('ERROR: HTTP server address is already in use. '+str(serverAddress))
return False
print('ERROR: HTTP Server failed to start. '+str(e))
print('ERROR: HTTP server failed to start. '+str(e))
return False
httpd.useBlurHash=useBlurHash

310
tests.py
View File

@ -551,316 +551,6 @@ def testPostMessageBetweenServers():
shutil.rmtree(aliceDir)
shutil.rmtree(bobDir)
def testFollowBetweenServersWithCapabilities():
print('Testing sending a follow request from one server to another')
global testServerAliceRunning
global testServerBobRunning
global testServerEveRunning
testServerAliceRunning = False
testServerBobRunning = False
testServerEveRunning = False
httpPrefix='http'
useTor=False
federationList=[]
baseDir=os.getcwd()
if os.path.isdir(baseDir+'/.tests'):
shutil.rmtree(baseDir+'/.tests')
os.mkdir(baseDir+'/.tests')
ocapAlways=True
# create the servers
aliceDir=baseDir+'/.tests/alice'
aliceDomain='127.0.0.52'
alicePort=61935
aliceSendThreads=[]
aliceAddress=aliceDomain+':'+str(alicePort)
bobDir=baseDir+'/.tests/bob'
bobDomain='127.0.0.78'
bobPort=61936
bobSendThreads=[]
bobAddress=bobDomain+':'+str(bobPort)
global thrAlice
if thrAlice:
while thrAlice.isAlive():
thrAlice.stop()
time.sleep(1)
thrAlice.kill()
thrAlice = \
threadWithTrace(target=createServerAlice, \
args=(aliceDir,aliceDomain,alicePort,bobAddress, \
federationList,False,False, \
ocapAlways,aliceSendThreads),daemon=True)
global thrBob
if thrBob:
while thrBob.isAlive():
thrBob.stop()
time.sleep(1)
thrBob.kill()
thrBob = \
threadWithTrace(target=createServerBob, \
args=(bobDir,bobDomain,bobPort,aliceAddress, \
federationList,False,False, \
ocapAlways,bobSendThreads),daemon=True)
eveDir=baseDir+'/.tests/eve'
eveDomain='127.0.0.55'
evePort=61937
eveSendThreads=[]
global thrEve
if thrEve:
while thrEve.isAlive():
thrEve.stop()
time.sleep(1)
thrEve.kill()
thrEve = \
threadWithTrace(target=createServerEve, \
args=(eveDir,eveDomain,evePort, \
federationList,False,False, \
False,eveSendThreads),daemon=True)
thrAlice.start()
thrBob.start()
thrEve.start()
assert thrAlice.isAlive()==True
assert thrBob.isAlive()==True
assert thrEve.isAlive()==True
# wait for all servers to be running
ctr=0
while not (testServerAliceRunning and testServerBobRunning and testServerEveRunning):
time.sleep(1)
ctr+=1
if ctr>60:
break
print('Alice online: '+str(testServerAliceRunning))
print('Bob online: '+str(testServerBobRunning))
print('Eve online: '+str(testServerEveRunning))
assert ctr<=60
time.sleep(1)
# In the beginning all was calm and there were no follows
print('*********************************************************')
print('Alice sends a follow request to Bob')
print('Both are strictly enforcing object capabilities')
os.chdir(aliceDir)
sessionAlice = createSession(useTor)
inReplyTo=None
inReplyToAtomUri=None
subject=None
alicePostLog = []
followersOnly=False
saveToFile=True
clientToServer=False
ccUrl=None
alicePersonCache={}
aliceCachedWebfingers={}
alicePostLog=[]
sendResult = \
sendFollowRequest(sessionAlice,aliceDir, \
'alice',aliceDomain,alicePort,httpPrefix, \
'bob',bobDomain,bobPort,httpPrefix, \
clientToServer,federationList, \
aliceSendThreads,alicePostLog, \
aliceCachedWebfingers,alicePersonCache, \
True,__version__)
print('sendResult: '+str(sendResult))
bobCapsFilename=bobDir+'/accounts/bob@'+bobDomain+'/ocap/accept/'+httpPrefix+':##'+aliceDomain+':'+str(alicePort)+'#users#alice.json'
aliceCapsFilename=aliceDir+'/accounts/alice@'+aliceDomain+'/ocap/granted/'+httpPrefix+':##'+bobDomain+':'+str(bobPort)+'#users#bob.json'
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'):
if os.path.isfile(bobCapsFilename):
if os.path.isfile(aliceCapsFilename):
break
time.sleep(1)
bobCapsJson=loadJson(bobCapsFilename,0)
if bobCapsJson:
if not bobCapsJson.get('capability'):
print("Unexpected format for Bob's capabilities")
pprint(bobCapsJson)
assert False
assert validInbox(bobDir,'bob',bobDomain)
assert validInboxFilenames(bobDir,'bob',bobDomain,aliceDomain,alicePort)
print('\n\n*********************************************************')
print('Eve tries to send to Bob')
sessionEve = createSession(useTor)
evePostLog = []
evePersonCache={}
eveCachedWebfingers={}
evePostLog=[]
useBlurhash=False
sendResult = \
sendPost(__version__, \
sessionEve,eveDir,'eve', eveDomain, evePort, \
'bob', bobDomain, bobPort, ccUrl, \
httpPrefix, 'Eve message', followersOnly, \
saveToFile, clientToServer,None,None,None, \
useBlurhash, federationList, eveSendThreads, \
evePostLog, eveCachedWebfingers, \
evePersonCache,inReplyTo, inReplyToAtomUri, subject)
print('sendResult: '+str(sendResult))
queuePath=bobDir+'/accounts/bob@'+bobDomain+'/queue'
inboxPath=bobDir+'/accounts/bob@'+bobDomain+'/inbox'
eveMessageArrived=False
for i in range(10):
time.sleep(1)
if os.path.isdir(inboxPath):
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>1:
eveMessageArrived=True
print('Eve message sent to Bob!')
break
# capabilities should have prevented delivery
assert eveMessageArrived==False
print('Message from Eve to Bob was correctly rejected by object capabilities')
print('\n\n*********************************************************')
print('Alice sends a message to Bob')
alicePostLog = []
alicePersonCache={}
aliceCachedWebfingers={}
alicePostLog=[]
useBlurhash=False
sendResult = \
sendPost(__version__, \
sessionAlice,aliceDir,'alice', aliceDomain, alicePort, \
'bob', bobDomain, bobPort, ccUrl, \
httpPrefix, 'Alice message', followersOnly, saveToFile, \
clientToServer,None,None,None,useBlurhash, federationList, \
aliceSendThreads, alicePostLog, aliceCachedWebfingers, \
alicePersonCache,inReplyTo, inReplyToAtomUri, subject)
print('sendResult: '+str(sendResult))
queuePath=bobDir+'/accounts/bob@'+bobDomain+'/queue'
inboxPath=bobDir+'/accounts/bob@'+bobDomain+'/inbox'
aliceMessageArrived=False
for i in range(20):
time.sleep(1)
if os.path.isdir(inboxPath):
if len([name for name in os.listdir(inboxPath) if os.path.isfile(os.path.join(inboxPath, name))])>0:
aliceMessageArrived=True
print('Alice message sent to Bob!')
break
assert aliceMessageArrived==True
print('Message from Alice to Bob succeeded, since it was granted capabilities')
print('\n\n*********************************************************')
print("\nBob changes Alice's capabilities so that she can't reply on his posts")
bobCapsFilename= \
bobDir+'/accounts/bob@'+bobDomain+'/ocap/accept/'+ \
httpPrefix+':##'+aliceDomain+':'+str(alicePort)+'#users#alice.json'
aliceCapsFilename= \
aliceDir+'/accounts/alice@'+aliceDomain+'/ocap/granted/'+ \
httpPrefix+':##'+bobDomain+':'+str(bobPort)+'#users#bob.json'
sessionBob = createSession(useTor)
bobPostLog = []
bobPersonCache={}
bobCachedWebfingers={}
print("Bob's capabilities for Alice:")
bobCapsJson=loadJson(bobCapsFilename,0)
if bobCapsJson:
pprint(bobCapsJson)
assert bobCapsJson.get('capability')
assert "inbox:noreply" not in bobCapsJson['capability']
print("Alice's capabilities granted by Bob")
aliceCapsJson=loadJson(aliceCapsFilename,0)
if aliceCapsJson:
pprint(aliceCapsJson)
assert aliceCapsJson.get('capability')
assert "inbox:noreply" not in aliceCapsJson['capability']
newCapabilities=["inbox:write","objects:read","inbox:noreply"]
sendCapabilitiesUpdate(sessionBob,bobDir,httpPrefix, \
'bob',bobDomain,bobPort, \
httpPrefix+'://'+aliceDomain+':'+\
str(alicePort)+'/users/alice',
newCapabilities, \
bobSendThreads, bobPostLog, \
bobCachedWebfingers,bobPersonCache, \
federationList,True,__version__)
bobChanged=False
bobNewCapsJson=None
for i in range(20):
time.sleep(1)
bobNewCapsJson=loadJson(bobCapsFilename,0)
if bobNewCapsJson:
assert bobNewCapsJson.get('capability')
if "inbox:noreply" in bobNewCapsJson['capability']:
print("Bob's capabilities were changed")
pprint(bobNewCapsJson)
bobChanged=True
break
assert bobChanged
aliceChanged=False
aliceNewCapsJson=None
for i in range(20):
time.sleep(1)
aliceNewCapsJson=loadJson(aliceCapsFilename,0)
if aliceNewCapsJson:
assert aliceNewCapsJson.get('capability')
if "inbox:noreply" in aliceNewCapsJson['capability']:
print("Alice's granted capabilities were changed")
pprint(aliceNewCapsJson)
aliceChanged=True
break
assert aliceChanged
# check that the capabilities id has changed
assert bobNewCapsJson['id']!=bobCapsJson['id']
assert aliceNewCapsJson['id']!=aliceCapsJson['id']
# stop the servers
thrAlice.kill()
thrAlice.join()
assert thrAlice.isAlive()==False
thrBob.kill()
thrBob.join()
assert thrBob.isAlive()==False
thrEve.kill()
thrEve.join()
assert thrEve.isAlive()==False
assert os.path.isfile(bobDir+'/accounts/bob@'+bobDomain+ \
'/ocap/accept/'+httpPrefix+':##'+ \
aliceDomain+':'+str(alicePort)+ \
'#users#alice.json')
assert os.path.isfile(aliceDir+'/accounts/alice@'+ \
aliceDomain+'/ocap/granted/'+ \
httpPrefix+':##'+bobDomain+':'+ \
str(bobPort)+'#users#bob.json')
assert 'alice@'+aliceDomain in open(bobDir+'/accounts/bob@'+bobDomain+'/followers.txt').read()
assert 'bob@'+bobDomain in open(aliceDir+'/accounts/alice@'+aliceDomain+'/following.txt').read()
# queue item removed
assert len([name for name in os.listdir(queuePath) if os.path.isfile(os.path.join(queuePath, name))])==0
os.chdir(baseDir)
shutil.rmtree(baseDir+'/.tests')
def testFollowBetweenServers():
print('Testing sending a follow request from one server to another')