Unit test for adding new follow to calendar

merge-requests/8/head
Bob Mottram 2020-09-03 13:16:24 +01:00
parent 8d642fe267
commit 949f54f5f3
3 changed files with 30 additions and 12 deletions

View File

@ -42,14 +42,19 @@ def receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
indicating whether to receive calendar events from that account
"""
# check that a following file exists
if ':' in domain:
domain = domain.split(':')[0]
followingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/following.txt'
if not os.path.isfile(followingFilename):
print("WARN: following.txt doesn't exist for " +
nickname + '@' + domain)
return
handle = followingNickname + '@' + followingDomain
# check that you are following this handle
if handle + '\n' not in open(followingFilename).read():
print('WARN: ' + handle + ' is not in ' + followingFilename)
return
calendarFilename = baseDir + '/accounts/' + \
@ -59,17 +64,22 @@ def receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
# a set of handles
followingHandles = ''
if os.path.isfile(calendarFilename):
print('Calendar file exists')
with open(calendarFilename, 'r') as calendarFile:
followingHandles = calendarFile.read()
else:
# create a new calendar file from the following file
print('Creating calendar file ' + calendarFilename)
followingHandles = ''
with open(followingFilename, 'r') as followingFile:
followingHandles = followingFile.read()
if add:
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
fp.write(followingHandles + handle + '\n')
# already in the calendar file?
if handle + '\n' in followingHandles:
print(handle + ' exists in followingCalendar.txt')
if add:
# already added
return
@ -78,6 +88,7 @@ def receiveCalendarEvents(baseDir: str, nickname: str, domain: str,
with open(calendarFilename, 'w+') as fp:
fp.write(followingHandles)
else:
print(handle + ' not in followingCalendar.txt')
# not already in the calendar file
if add:
# append to the list of handles

View File

@ -777,12 +777,21 @@ def testFollowBetweenServers():
bobDomain + '/followers.txt'):
if os.path.isfile(aliceDir + '/accounts/alice@' +
aliceDomain + '/following.txt'):
break
if os.path.isfile(aliceDir + '/accounts/alice@' +
aliceDomain + '/followingCalendar.txt'):
break
time.sleep(1)
assert validInbox(bobDir, 'bob', bobDomain)
assert validInboxFilenames(bobDir, 'bob', bobDomain,
aliceDomain, alicePort)
assert 'alice@' + aliceDomain in open(bobDir + '/accounts/bob@' +
bobDomain + '/followers.txt').read()
assert 'bob@' + bobDomain in open(aliceDir + '/accounts/alice@' +
aliceDomain + '/following.txt').read()
assert 'bob@' + bobDomain in open(aliceDir + '/accounts/alice@' +
aliceDomain +
'/followingCalendar.txt').read()
print('\n\n*********************************************************')
print('Alice sends a message to Bob')
@ -828,11 +837,6 @@ def testFollowBetweenServers():
thrBob.join()
assert thrBob.isAlive() is False
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
time.sleep(4)
assert len([name for name in os.listdir(queuePath)

View File

@ -369,24 +369,27 @@ def followPerson(baseDir: str, nickname: str, domain: str,
content = f.read()
f.seek(0, 0)
f.write(handleToFollow + '\n' + content)
if debug:
print('DEBUG: follow added')
print('DEBUG: follow added')
except Exception as e:
print('WARN: Failed to write entry to follow file ' +
filename + ' ' + str(e))
else:
# first follow
if debug:
print('DEBUG: creating new following file to follow ' +
handleToFollow)
print('DEBUG: ' + handle +
' creating new following file to follow ' + handleToFollow +
', filename is ' + filename)
with open(filename, 'w+') as f:
f.write(handleToFollow + '\n')
# Default to adding new follows to the calendar.
# Possibly this could be made optional
if followFile == 'following.txt':
if followFile.endswith('following.txt'):
# if following a person add them to the list of
# calendar follows
print('DEBUG: adding ' +
followNickname + '@' + followDomain + ' to calendar of ' +
nickname + '@' + domain)
addPersonToCalendar(baseDir, nickname, domain,
followNickname, followDomain)
return True