LOC-24 Added tests for kick/ban functionality
LOC-24 Added tests for kick/ban functionality

Test 11 invites a user, has them join and then kicks them (before verifying in the database)
Test 12 has that user rejoin and then bans them (again verifying against the database)

#!/usr/bin/env python #!/usr/bin/env python
# #
# #
# Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py # Interface based upon https://github.com/izderadicka/xmpp-tester/blob/master/commander.py
# #
# apt-get install: # apt-get install:
# python-urwid # python-urwid
# python-gnupg # python-gnupg
   
   
import urwid import urwid
from collections import deque from collections import deque
from threading import Thread from threading import Thread
import threading import threading
   
import json import json
import urllib2 import urllib2
import ssl import ssl
import string import string
import random import random
   
import datetime as dt import datetime as dt
   
import gnupg import gnupg
   
   
   
# We'll get these from the commandline later # We'll get these from the commandline later
USER='ben2' USER='ben2'
SERVER='https://127.0.0.1:8090' SERVER='https://127.0.0.1:8090'
ROOMNAME='BenTest' ROOMNAME='BenTest'
   
   
class msgHandler(object): class msgHandler(object):
def __init__(self): def __init__(self):
self.last = 0 self.last = 0
self.user = False self.user = False
self.server = SERVER self.server = SERVER
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
self.syskey = False self.syskey = False
self.gpg = gnupg.GPG() self.gpg = gnupg.GPG()
def pollForMessage(self): def pollForMessage(self):
if not self.room: if not self.room:
# We're not connected to a room # We're not connected to a room
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"mylast":self.last, "mylast":self.last,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"pollMsg", request = {"action":"pollMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK": if resp == "BROKENLINK":
return resp return resp
if resp['status'] == "unchanged": if resp['status'] == "unchanged":
return False return False
if resp['status'] == "errmessage": if resp['status'] == "errmessage":
# Got an error, we need to stop the poll and then return the text # Got an error, we need to stop the poll and then return the text
self.room = False self.room = False
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return [['reversed',resp['text']]] return [['reversed',resp['text']]]
to_print = [] to_print = []
# Otherwise, process the messages # Otherwise, process the messages
for i in resp["messages"]: for i in resp["messages"]:
self.last = i[0] self.last = i[0]
upstruser = i[3] upstruser = i[3]
try: try:
msgbody = json.loads(self.decrypt(i[1],upstruser)) msgbody = json.loads(self.decrypt(i[1],upstruser))
except: except:
# Means an invalid message was received - LOC-8 # Means an invalid message was received - LOC-8
to_print.append(['error','Received message which could not be decrypted']) to_print.append(['error','Received message which could not be decrypted'])
continue continue
# TODO - We'll need to json decode and extract the sending user's name # TODO - We'll need to json decode and extract the sending user's name
# but not currently including that info in my curl tests. Also means test that part of the next block # but not currently including that info in my curl tests. Also means test that part of the next block
color = "green" color = "green"
   
if upstruser == self.user: if upstruser == self.user:
# One of our own, change the color # One of our own, change the color
color = "blue" color = "blue"
elif upstruser == "SYSTEM": elif upstruser == "SYSTEM":
color = "magenta" color = "magenta"
if msgbody['verb'] == "sysalert": if msgbody['verb'] == "sysalert":
color = 'reversed' color = 'reversed'
elif msgbody['verb'] == 'syswarn': elif msgbody['verb'] == 'syswarn':
color = 'cyan' color = 'cyan'
ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]") ts = dt.datetime.utcfromtimestamp(i[2]).strftime("[%H:%M:%S]")
if msgbody["verb"] == "do": if msgbody["verb"] == "do":
color = 'yellow' color = 'yellow'
line = [ line = [
" ** %s %s **" % (upstruser,msgbody['text']) " ** %s %s **" % (upstruser,msgbody['text'])
] ]
else: else:
if i[4] != "0": if i[4] != "0":
color = 'brown' color = 'brown'
upstruser = 'DM %s' % (upstruser,) upstruser = 'DM %s' % (upstruser,)
line = [ line = [
ts, # timestamp ts, # timestamp
"%s>" % (upstruser,), # To be replaced later "%s>" % (upstruser,), # To be replaced later
msgbody['text'] msgbody['text']
] ]
to_print.append([color,' '.join(line)]) to_print.append([color,' '.join(line)])
return to_print return to_print
def sendMsg(self,line,verb='say'): def sendMsg(self,line,verb='say'):
''' Send a message ''' Send a message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), "msg":self.encrypt(json.dumps(msg)),
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendMsg", request = {"action":"sendMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
def sendDirectMsg(self,line,user,verb='say'): def sendDirectMsg(self,line,user,verb='say'):
''' Send a direct message ''' Send a direct message
''' '''
if not self.room: if not self.room:
# We're not in a room. Abort # We're not in a room. Abort
return False return False
# Otherwise, build a payload # Otherwise, build a payload
msg = { msg = {
'user': self.user, 'user': self.user,
'text': line, 'text': line,
"verb": verb "verb": verb
} }
payload = {"roomName": self.room, payload = {"roomName": self.room,
"msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key "msg":self.encrypt(json.dumps(msg)), # TODO this should use the user's key
"to": user, "to": user,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"sendDirectMsg", request = {"action":"sendDirectMsg",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp['status'] == "ok": if "status" in resp and resp['status'] == "ok":
return True return True
return False return False
   
   
   
   
def joinRoom(self,user,room,passw): def joinRoom(self,user,room,passw):
''' Join a room ''' Join a room
''' '''
# We only want to send the user password section of the password # We only want to send the user password section of the password
p = passw.split(":") p = passw.split(":")
userpass = p[1] userpass = p[1]
payload = {"roomName": room, payload = {"roomName": room,
"userpass": userpass.encode('utf-8'), "userpass": userpass.encode('utf-8'),
"user": user "user": user
} }
request = {"action":"joinRoom", request = {"action":"joinRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = room self.room = room
self.user = user self.user = user
self.last = resp['last'] self.last = resp['last']
self.roompass = p[0] # The room password is the first section of the password self.roompass = p[0] # The room password is the first section of the password
self.sesskey = resp['session'] self.sesskey = resp['session']
self.syskey = resp['syskey'] self.syskey = resp['syskey']
return True return True
   
   
   
   
def leaveRoom(self): def leaveRoom(self):
''' Leave the current room ''' Leave the current room
''' '''
if not self.room: if not self.room:
return False return False
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"leaveRoom", request = {"action":"leaveRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
self.room = False self.room = False
self.user = False self.user = False
self.last = 0 self.last = 0
self.roompass = False self.roompass = False
self.sesskey = False self.sesskey = False
return True return True
   
   
def createRoom(self,room,user=False): def createRoom(self,room,user=False):
''' Create a new room ''' Create a new room
''' '''
if not user and not self.user: if not user and not self.user:
return False return False
if not user: if not user:
user = self.user user = self.user
   
   
# Generate a password for the admin # Generate a password for the admin
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": room, payload = {"roomName": room,
"owner": user, "owner": user,
"pass": passw "pass": passw
} }
request = {"action":"createRoom", request = {"action":"createRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [resp['name'],passw] return [resp['name'],passw]
   
def closeRoom(self): def closeRoom(self):
if not self.room: if not self.room:
return False return False
   
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"closeRoom", request = {"action":"closeRoom",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
   
resp = self.sendRequest(request) resp = self.sendRequest(request)
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
def inviteUser(self,user): def inviteUser(self,user):
''' Invite a user into a room ''' Invite a user into a room
#TODO - Authentication #TODO - Authentication
''' '''
# Generate a password for the new user # Generate a password for the new user
passw = self.genpassw() passw = self.genpassw()
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"invite": user, "invite": user,
"pass": passw, "pass": passw,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":"inviteUser", request = {"action":"inviteUser",
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return [self.room,self.roompass,passw,user] return [self.room,self.roompass,passw,user]
   
   
   
def kickUser(self,user,ban=False): def kickUser(self,user,ban=False):
''' Kick a user out of the room ''' Kick a user out of the room
''' '''
action = 'banUser' action = 'banUser'
if not ban: if not ban:
action = 'kickUser' action = 'kickUser'
payload = {"roomName": self.room, payload = {"roomName": self.room,
"user": self.user, "user": self.user,
"kick": user, "kick": user,
"sesskey": self.sesskey "sesskey": self.sesskey
} }
request = {"action":action, request = {"action":action,
"payload": json.dumps(payload) "payload": json.dumps(payload)
} }
resp = self.sendRequest(request) resp = self.sendRequest(request)
   
if resp == "BROKENLINK" or resp['status'] != "ok": if resp == "BROKENLINK" or resp['status'] != "ok":
return False return False
return True return True
   
   
def sendRequest(self,data): def sendRequest(self,data):
data = json.dumps(data) data = json.dumps(data)
try: try:
# The cert the other end will be considered invalid # The cert the other end will be considered invalid
# #
# Ignore it # Ignore it
ctx = ssl.create_default_context() ctx = ssl.create_default_context()
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
   
req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'}) req = urllib2.Request(self.server, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req,context=ctx) f = urllib2.urlopen(req,context=ctx)
response = f.read() response = f.read()
f.close() f.close()
return json.loads(response) return json.loads(response)
except: except:
return "BROKENLINK" return "BROKENLINK"
   
   
   
def decrypt(self,msg,sender): def decrypt(self,msg,sender):
''' Placeholder ''' Placeholder
''' '''
try: try:
key = self.roompass key = self.roompass
if sender == "SYSTEM": if sender == "SYSTEM":
key = self.syskey key = self.syskey
return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key)) return str(self.gpg.decrypt(msg.decode("base64"),passphrase=key))
except: except:
return False return False
   
def encrypt(self,msg): def encrypt(self,msg):
''' Placeholder ''' Placeholder
''' '''
crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False) crypted = self.gpg.encrypt(msg,None,passphrase=self.roompass,symmetric="AES256",armor=False)
return crypted.data.encode('base64') return crypted.data.encode('base64')
   
   
def hashpw(self,passw): def hashpw(self,passw):
''' Placeholder ''' Placeholder
''' '''
return passw return passw
   
   
def genpassw(self,N=16): def genpassw(self,N=16):
''' Generate a random string of chars to act as a password ''' Generate a random string of chars to act as a password
''' '''
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N)) return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits).encode('utf-8') for _ in range(N))
   
   
   
class NotInRoom(Exception): class NotInRoom(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Message not sent') Exception.__init__(self,'Message not sent')
   
   
class UnableTo(Exception): class UnableTo(Exception):
def __init__(self,action,cmd): def __init__(self,action,cmd):
Exception.__init__(self,'Could not %s: %s' % (action,cmd)) Exception.__init__(self,'Could not %s: %s' % (action,cmd))
   
   
class InvalidCommand(Exception): class InvalidCommand(Exception):
def __init__(self,cmd): def __init__(self,cmd):
Exception.__init__(self,'Command is invalid: %s' % (cmd,)) Exception.__init__(self,'Command is invalid: %s' % (cmd,))
   
   
class Command(object): class Command(object):
""" Base class to manage commands in commander """ Base class to manage commands in commander
similar to cmd.Cmd in standard library similar to cmd.Cmd in standard library
just extend with do_something method to handle your commands""" just extend with do_something method to handle your commands"""
   
def __init__(self,quit_commands=['q','quit','exit'], help_commands=['help','?', 'h']): def __init__(self,quit_commands=['q','quit','exit'], help_commands=['help','?', 'h']):
self._quit_cmd=quit_commands self._quit_cmd=quit_commands
self._help_cmd=help_commands self._help_cmd=help_commands
def __call__(self,line): def __call__(self,line):
global msg global msg
tokens=line.split() tokens=line.split()
cmd=tokens[0].lower() cmd=tokens[0].lower()
args=tokens[1:] args=tokens[1:]
if cmd[0] == "/": if cmd[0] == "/":
# It's a command # It's a command
cmd = cmd[1:] cmd = cmd[1:]
if cmd == "me": if cmd == "me":
#/me [string] #/me [string]
r = msg.sendMsg(' '.join(args),'do') r = msg.sendMsg(' '.join(args),'do')
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
return return
   
if cmd == "ban": if cmd == "ban":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0],True) m = msg.kickUser(args[0],True)
return return
if cmd == "join": if cmd == "join":
# /join [room] [password] [username] # /join [room] [password] [username]
if len(args) < 3: if len(args) < 3:
raise InvalidCommand(line) raise InvalidCommand(line)
return return
if not msg.joinRoom(args[2],args[0],args[1]): if not msg.joinRoom(args[2],args[0],args[1]):
raise UnableTo('join',line) raise UnableTo('join',line)
return return
if cmd == "kick": if cmd == "kick":
# /kick [user] # /kick [user]
if len(args) < 1: if len(args) < 1:
raise InvalidCommand(line) raise InvalidCommand(line)
m = msg.kickUser(args[0]) m = msg.kickUser(args[0])
return return
if cmd == "leave": if cmd == "leave":
# /leave # /leave
if not msg.leaveRoom(): if not msg.leaveRoom():
raise UnableTo('leave',line) raise UnableTo('leave',line)
return return
global c global c
c.output('Left the room','magenta') c.output('Left the room','magenta')
return return
   
   
if cmd == 'msg': if cmd == 'msg':
# /msg ben Hello ben this is a DM # /msg ben Hello ben this is a DM
line = ' '.join(args[1:]) line = ' '.join(args[1:])
r = msg.sendDirectMsg(line,args[0]) r = msg.sendDirectMsg(line,args[0])
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
# Otherwise push a copy of the message to display # Otherwise push a copy of the message to display
# cos we won't get this one back from pollMsg # cos we won't get this one back from pollMsg
global c global c
m = "%s DM %s>%s" % (msg.user,args[0],line) m = "%s DM %s>%s" % (msg.user,args[0],line)
c.output(m,'blue') c.output(m,'blue')
return return
   
if cmd == "room": if cmd == "room":
# /room close [roompass] # /room close [roompass]
if args[0] == "close": if args[0] == "close":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand('/room close [pass]') raise InvalidCommand('/room close [pass]')
if not msg.closeRoom(): if not msg.closeRoom():
raise UnableTo('Close Room','') raise UnableTo('Close Room','')
return return
# /room create [roomname] [[user]] # /room create [roomname] [[user]]
if args[0] == "create": if args[0] == "create":
if len(args) < 3: if len(args) < 3:
args[2] = False args[2] = False
n = msg.createRoom(args[1],args[2]) n = msg.createRoom(args[1],args[2])
if not n: if not n:
raise UnableTo('create room',line) raise UnableTo('create room',line)
return return
# Seperate out the return value # Seperate out the return value
rm = n[0] rm = n[0]
up = n[1] # user specific password up = n[1] # user specific password
global c global c
# Generate a room password # Generate a room password
p = msg.genpassw() p = msg.genpassw()
c.output('Created Room %s' %(rm)) c.output('Created Room %s' %(rm))
c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2])) c.output('To join the room, do /join %s %s:%s %s' %(args[1],p,up,args[2]))
return return
elif args[0] == "invite": elif args[0] == "invite":
if len(args) < 2: if len(args) < 2:
raise InvalidCommand(line) raise InvalidCommand(line)
n = msg.inviteUser(args[1]) n = msg.inviteUser(args[1])
if not n: if not n:
raise UnableTo('invite user',line) raise UnableTo('invite user',line)
return return
global c global c
c.output('User %s may now join room' %(args[1],)) c.output('User %s may now join room' %(args[1],))
c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3])) c.output('To join the room, they should do /join %s %s:%s %s' %(n[0],n[1],n[2],n[3]))
return return
   
if cmd in self._quit_cmd: if cmd in self._quit_cmd:
return Commander.Exit return Commander.Exit
elif cmd in self._help_cmd: elif cmd in self._help_cmd:
return self.help(args[0] if args else None) return self.help(args[0] if args else None)
elif hasattr(self, 'do_'+cmd): elif hasattr(self, 'do_'+cmd):
return getattr(self, 'do_'+cmd)(*args) return getattr(self, 'do_'+cmd)(*args)
else: else:
# If it's not a command, then we're trying to send a message # If it's not a command, then we're trying to send a message
r = msg.sendMsg(line) r = msg.sendMsg(line)
if not r: if not r:
raise NotInRoom(line) raise NotInRoom(line)
def help(self,cmd=None): def help(self,cmd=None):
def std_help(): def std_help():
qc='|'.join(self._quit_cmd) qc='|'.join(self._quit_cmd)
hc ='|'.join(self._help_cmd) hc ='|'.join(self._help_cmd)
res='Type [%s] to quit program\n' % qc res='Type [%s] to quit program\n' % qc
res += """Available commands: res += """Available commands:
/join [room] [password] [username] Join a room /join [room] [password] [username] Join a room
/leave Leave current room /leave Leave current room
/room create [roomname] [roompass] [admin user] New room management /room create [roomname] [roompass] [admin user] New room management
/room invite [user] Invite a user into the current room /room invite [user] Invite a user into the current room
/me [string] Send an 'action' instead of a message /me [string] Send an 'action' instead of a message
Room Admin commands: Room Admin commands:
/kick [user] Kick a user out of the room (they can return) /kick [user] Kick a user out of the room (they can return)
/ban [user] Kick a user out and disinvite them (they cannot return) /ban [user] Kick a user out and disinvite them (they cannot return)
/room close [roompass] Kick all users out and close the room /room close [roompass] Kick all users out and close the room
Once in a room, to send a message, just type it. Once in a room, to send a message, just type it.
""" """
return res return res
if not cmd: if not cmd:
return std_help() return std_help()
else: else:
try: try:
fn=getattr(self,'do_'+cmd) fn=getattr(self,'do_'+cmd)
doc=fn.__doc__ doc=fn.__doc__
return doc or 'No documentation available for %s'%cmd return doc or 'No documentation available for %s'%cmd
except AttributeError: except AttributeError:
return std_help() return std_help()
class FocusMixin(object): class FocusMixin(object):
def mouse_event(self, size, event, button, x, y, focus): def mouse_event(self, size, event, button, x, y, focus):
if focus and hasattr(self, '_got_focus') and self._got_focus: if focus and hasattr(self, '_got_focus') and self._got_focus:
self._got_focus() self._got_focus()
return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus) return super(FocusMixin,self).mouse_event(size, event, button, x, y, focus)
class ListView(FocusMixin, urwid.ListBox): class ListView(FocusMixin, urwid.ListBox):
def __init__(self, model, got_focus, max_size=None): def __init__(self, model, got_focus, max_size=None):
urwid.ListBox.__init__(self,model) urwid.ListBox.__init__(self,model)
self._got_focus=got_focus self._got_focus=got_focus
self.max_size=max_size self.max_size=max_size
self._lock=threading.Lock() self._lock=threading.Lock()
def add(self,line): def add(self,line):
with self._lock: with self._lock:
was_on_end=self.get_focus()[1] == len(self.body)-1 was_on_end=self.get_focus()[1] == len(self.body)-1
if self.max_size and len(self.body)>self.max_size: if self.max_size and len(self.body)>self.max_size:
del self.body[0] del self.body[0]
self.body.append(urwid.Text(line)) self.body.append(urwid.Text(line))
last=len(self.body)-1 last=len(self.body)-1
if was_on_end: if was_on_end:
self.set_focus(last,'above') self.set_focus(last,'above')
   
class Input(FocusMixin, urwid.Edit): class Input(FocusMixin, urwid.Edit):
signals=['line_entered'] signals=['line_entered']
def __init__(self, got_focus=None): def __init__(self, got_focus=None):
urwid.Edit.__init__(self) urwid.Edit.__init__(self)
self.history=deque(maxlen=1000) self.history=deque(maxlen=1000)
self._history_index=-1 self._history_index=-1
self._got_focus=got_focus self._got_focus=got_focus
def keypress(self, size, key): def keypress(self, size, key):
if key=='enter': if key=='enter':
line=self.edit_text.strip() line=self.edit_text.strip()
if line: if line:
urwid.emit_signal(self,'line_entered', line) urwid.emit_signal(self,'line_entered', line)
self.history.append(line) self.history.append(line)
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
if key=='up': if key=='up':
self._history_index-=1 self._history_index-=1
if self._history_index< 0: if self._history_index< 0:
self._history_index= 0 self._history_index= 0
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
if key=='down': if key=='down':
self._history_index+=1 self._history_index+=1
if self._history_index>=len(self.history): if self._history_index>=len(self.history):
self._history_index=len(self.history) self._history_index=len(self.history)
self.edit_text=u'' self.edit_text=u''
else: else:
self.edit_text=self.history[self._history_index] self.edit_text=self.history[self._history_index]
else: else:
urwid.Edit.keypress(self, size, key) urwid.Edit.keypress(self, size, key)
   
   
class Commander(urwid.Frame): class Commander(urwid.Frame):
""" Simple terminal UI with command input on bottom line and display frame above """ Simple terminal UI with command input on bottom line and display frame above
similar to chat client etc. similar to chat client etc.
Initialize with your Command instance to execute commands Initialize with your Command instance to execute commands
and the start main loop Commander.loop(). and the start main loop Commander.loop().
You can also asynchronously output messages with Commander.output('message') """ You can also asynchronously output messages with Commander.output('message') """
   
class Exit(object): class Exit(object):
pass pass
PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY), PALLETE=[('reversed', urwid.BLACK, urwid.LIGHT_GRAY),
('normal', urwid.LIGHT_GRAY, urwid.BLACK), ('normal', urwid.LIGHT_GRAY, urwid.BLACK),
('error', urwid.LIGHT_RED, urwid.BLACK), ('error', urwid.LIGHT_RED, urwid.BLACK),
('green', urwid.DARK_GREEN, urwid.BLACK), ('green', urwid.DARK_GREEN, urwid.BLACK),
('blue', urwid.LIGHT_BLUE, urwid.BLACK), ('blue', urwid.LIGHT_BLUE, urwid.BLACK),
('magenta', urwid.DARK_MAGENTA, urwid.BLACK), ('magenta', urwid.DARK_MAGENTA, urwid.BLACK),
('yellow', urwid.YELLOW, urwid.BLACK), ('yellow', urwid.YELLOW, urwid.BLACK),
('cyan', urwid.LIGHT_CYAN, urwid.BLACK), ('cyan', urwid.LIGHT_CYAN, urwid.BLACK),
('brown', urwid.BROWN, urwid.BLACK), ('brown', urwid.BROWN, urwid.BLACK),
] ]
def __init__(self, title, command_caption='Message: (Tab to switch focus to upper frame, where you can scroll text)', cmd_cb=None, max_size=1000): def __init__(self, title, command_caption='Message: (Tab to switch focus to upper frame, where you can scroll text)', cmd_cb=None, max_size=1000):
self.header=urwid.Text(title) self.header=urwid.Text(title)
self.model=urwid.SimpleListWalker([]) self.model=urwid.SimpleListWalker([])
self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size ) self.body=ListView(self.model, lambda: self._update_focus(False), max_size=max_size )
self.input=Input(lambda: self._update_focus(True)) self.input=Input(lambda: self._update_focus(True))
foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'), foot=urwid.Pile([urwid.AttrMap(urwid.Text(command_caption), 'reversed'),
urwid.AttrMap(self.input,'normal')]) urwid.AttrMap(self.input,'normal')])
urwid.Frame.__init__(self, urwid.Frame.__init__(self,
urwid.AttrWrap(self.body, 'normal'), urwid.AttrWrap(self.body, 'normal'),
urwid.AttrWrap(self.header, 'reversed'), urwid.AttrWrap(self.header, 'reversed'),
foot) foot)
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
urwid.connect_signal(self.input,'line_entered',self.on_line_entered) urwid.connect_signal(self.input,'line_entered',self.on_line_entered)
self._cmd=cmd_cb self._cmd=cmd_cb
self._output_styles=[s[0] for s in self.PALLETE] self._output_styles=[s[0] for s in self.PALLETE]
self.eloop=None self.eloop=None
def loop(self, handle_mouse=False): def loop(self, handle_mouse=False):
self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse) self.eloop=urwid.MainLoop(self, self.PALLETE, handle_mouse=handle_mouse)
self._eloop_thread=threading.current_thread() self._eloop_thread=threading.current_thread()
self.eloop.run() self.eloop.run()
def on_line_entered(self,line): def on_line_entered(self,line):
if self._cmd: if self._cmd:
try: try:
res = self._cmd(line) res = self._cmd(line)
except Exception,e: except Exception,e:
self.output('Error: %s'%e, 'error') self.output('Error: %s'%e, 'error')
return return
if res==Commander.Exit: if res==Commander.Exit:
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
elif res: elif res:
self.output(str(res)) self.output(str(res))
else: else:
if line in ('q','quit','exit'): if line in ('q','quit','exit'):
raise urwid.ExitMainLoop() raise urwid.ExitMainLoop()
else: else:
self.output(line) self.output(line)
def output(self, line, style=None): def output(self, line, style=None):
if style and style in self._output_styles: if style and style in self._output_styles:
line=(style,line) line=(style,line)
self.body.add(line) self.body.add(line)
#since output could be called asynchronously form other threads we need to refresh screen in these cases #since output could be called asynchronously form other threads we need to refresh screen in these cases
if self.eloop and self._eloop_thread != threading.current_thread(): if self.eloop and self._eloop_thread != threading.current_thread():
self.eloop.draw_screen() self.eloop.draw_screen()
def _update_focus(self, focus): def _update_focus(self, focus):
self._focus=focus self._focus=focus
def switch_focus(self): def switch_focus(self):
if self._focus: if self._focus:
self.set_focus('body') self.set_focus('body')
self._focus=False self._focus=False
else: else:
self.set_focus_path(['footer',1]) self.set_focus_path(['footer',1])
self._focus=True self._focus=True
def keypress(self, size, key): def keypress(self, size, key):
if key=='tab': if key=='tab':
self.switch_focus() self.switch_focus()
return urwid.Frame.keypress(self, size, key) return urwid.Frame.keypress(self, size, key)
   
   
if __name__=='__main__': if __name__=='__main__':
msg = msgHandler() msg = msgHandler()
class TestCmd(Command): class TestCmd(Command):
def do_echo(self, *args): def do_echo(self, *args):
'''echo - Just echos all arguments''' '''echo - Just echos all arguments'''
return ' '.join(args) return ' '.join(args)
def do_raise(self, *args): def do_raise(self, *args):
raise Exception('Some Error') raise Exception('Some Error')
c=Commander('LocalChat', cmd_cb=TestCmd()) c=Commander('LocalChat', cmd_cb=TestCmd())
#Test asynch output - e.g. comming from different thread #Test asynch output - e.g. comming from different thread
import time import time
def run(): def run():
state=1 state=1
while True: while True:
time.sleep(1) time.sleep(1)
m = msg.pollForMessage() m = msg.pollForMessage()
if m == "BROKENLINK": if m == "BROKENLINK":
if state == 1: if state == 1:
c.output("Server went away", 'Red') c.output("Server went away", 'Red')
state = 0 state = 0
continue continue
if m: if m:
state = 1 state = 1
for i in m: for i in m:
c.output(i[1], i[0]) c.output(i[1], i[0])
t=Thread(target=run) t=Thread(target=run)
t.daemon=True t.daemon=True
t.start() t.start()
#start main loop #start main loop
c.loop() c.loop()
   
#!/usr/bin/env python #!/usr/bin/env python
# #
# apt-get install: # apt-get install:
# python-psutil # python-psutil
# #
import subprocess import subprocess
import psutil import psutil
import os import os
import sys import sys
import time import time
import sqlite3 import sqlite3
import traceback import traceback
  import json
   
   
try: try:
from subprocess import DEVNULL # py3k from subprocess import DEVNULL # py3k
except ImportError: except ImportError:
import os import os
DEVNULL = open(os.devnull, 'wb') DEVNULL = open(os.devnull, 'wb')
   
   
DB_FILE = "%s/localchat-testing.db" % (os.getcwd(),) DB_FILE = "%s/localchat-testing.db" % (os.getcwd(),)
PARENT_DIR = "%s/.." % (os.path.dirname(os.path.abspath(__file__)),) PARENT_DIR = "%s/.." % (os.path.dirname(os.path.abspath(__file__)),)
sys.path.append('%s/client/' % (PARENT_DIR,)) sys.path.append('%s/client/' % (PARENT_DIR,))
   
import LocalChatClient import LocalChatClient
STORAGE = {} STORAGE = {}
   
   
def restartServer(proc1): def restartServer(proc1):
''' Start the server component ''' Start the server component
If already running (i.e. proc1 != False) kill the existing instance If already running (i.e. proc1 != False) kill the existing instance
''' '''
# If we've already got a process, kill it # If we've already got a process, kill it
if proc1: if proc1:
kill(proc1.pid) kill(proc1.pid)
try: try:
serverloc = "%s/server/LocalChat.py" % (PARENT_DIR,) serverloc = "%s/server/LocalChat.py" % (PARENT_DIR,)
proc1 = subprocess.Popen([serverloc,'--testing-mode-enable'],stderr=subprocess.STDOUT,stdout=DEVNULL) proc1 = subprocess.Popen([serverloc,'--testing-mode-enable'],stderr=subprocess.STDOUT,stdout=DEVNULL)
except Exception as e: except Exception as e:
print "Failed to start server" print "Failed to start server"
print e print e
return False return False
# Give it time to start up # Give it time to start up
time.sleep(5) time.sleep(5)
return proc1 return proc1
   
   
def getClientInstance(): def getClientInstance():
''' Get an instance of the client class ''' Get an instance of the client class
''' '''
return LocalChatClient.msgHandler() return LocalChatClient.msgHandler()
   
   
def exit(proc1,code=0): def exit(proc1,code=0):
''' Tidy up and exit ''' Tidy up and exit
''' '''
if proc1: if proc1:
kill(proc1.pid) kill(proc1.pid)
sys.exit(code) sys.exit(code)
   
   
   
def kill(proc_pid): def kill(proc_pid):
''' Kill the process and it's children ''' Kill the process and it's children
From https://stackoverflow.com/a/25134985 From https://stackoverflow.com/a/25134985
We use this because proc1.kill() doesn't seem to kill of the child thread of the server, even if shell=True We use this because proc1.kill() doesn't seem to kill of the child thread of the server, even if shell=True
''' '''
process = psutil.Process(proc_pid) process = psutil.Process(proc_pid)
for proc in process.children(recursive=True): for proc in process.children(recursive=True):
proc.kill() proc.kill()
process.kill() process.kill()
   
   
   
def make_table(columns, data): def make_table(columns, data):
"""Create an ASCII table and return it as a string. """Create an ASCII table and return it as a string.
   
Pass a list of strings to use as columns in the table and a list of Pass a list of strings to use as columns in the table and a list of
dicts. The strings in 'columns' will be used as the keys to the dicts in dicts. The strings in 'columns' will be used as the keys to the dicts in
'data.' 'data.'
   
   
https://snippets.bentasker.co.uk/page-1705192300-Make-ASCII-Table-Python.html https://snippets.bentasker.co.uk/page-1705192300-Make-ASCII-Table-Python.html
""" """
# Calculate how wide each cell needs to be # Calculate how wide each cell needs to be
cell_widths = {} cell_widths = {}
for c in columns: for c in columns:
lens = [] lens = []
values = [lens.append(len(str(d.get(c, "")))) for d in data] values = [lens.append(len(str(d.get(c, "")))) for d in data]
lens.append(len(c)) lens.append(len(c))
lens.sort() lens.sort()
cell_widths[c] = max(lens) cell_widths[c] = max(lens)
   
# Used for formatting rows of data # Used for formatting rows of data
row_template = "|" + " {} |" * len(columns) row_template = "|" + " {} |" * len(columns)
   
# CONSTRUCT THE TABLE # CONSTRUCT THE TABLE
   
# The top row with the column titles # The top row with the column titles
justified_column_heads = [c.ljust(cell_widths[c]) for c in columns] justified_column_heads = [c.ljust(cell_widths[c]) for c in columns]
header = row_template.format(*justified_column_heads) header = row_template.format(*justified_column_heads)
# The second row contains separators # The second row contains separators
sep = "|" + "-" * (len(header) - 2) + "|" sep = "|" + "-" * (len(header) - 2) + "|"
end = "-" * len(header) end = "-" * len(header)
# Rows of data # Rows of data
rows = [] rows = []
   
for d in data: for d in data:
fields = [str(d.get(c, "")).ljust(cell_widths[c]) for c in columns] fields = [str(d.get(c, "")).ljust(cell_widths[c]) for c in columns]
row = row_template.format(*fields) row = row_template.format(*fields)
rows.append(row) rows.append(row)
rows.append(end) rows.append(end)
return "\n".join([header, sep] + rows) return "\n".join([header, sep] + rows)
   
   
   
  def opendb():
  ''' We'll do this and then close after every query to make sure we don't
  inadvertantly lock the DB and force tests to fail.
   
  '''
  CONN = sqlite3.connect(DB_FILE)
  CURSOR = CONN.cursor()
   
  return [CONN,CURSOR]
   
   
def run_tests(): def run_tests():
# Get an instance of the client # Get an instance of the client
msg = getClientInstance(); msg = getClientInstance();
test_results = [] test_results = []
tests = ['test_one','test_two','test_three'] tests = ['test_one','test_two','test_three','test_four',
  'test_five','test_six','test_seven','test_eight',
  'test_nine','test_ten','test_eleven','test_twelve']
  x = 1
for test in tests: for test in tests:
print "Running %s " % (test,) print "Running %s " % (test,)
stat,isFatal = globals()[test](msg) stat,isFatal = globals()[test](msg)
  stat['No'] = x
test_results.append(stat) test_results.append(stat)
if isFatal and stat['Result'] == 'FAIL': if isFatal and stat['Result'] == 'FAIL':
break break
   
  x = x + 1
   
return test_results return test_results
   
   
   
   
def test_one(msg): def test_one(msg):
''' Create a room and verify that it gets created ''' Create a room and verify that it gets created
''' '''
result = {'Test' : 'Create a Room','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Create a Room','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
# Test 1 - create a room and check it's recorded in the DB # Test 1 - create a room and check it's recorded in the DB
n = msg.createRoom('TestRoom1','testadmin') n = msg.createRoom('TestRoom1','testadmin')
if not n: if not n:
result['Notes'] = 'Empty Response' result['Notes'] = 'Empty Response'
return result return [result,isFatal]
# The client should have given us two passwords # The client should have given us two passwords
if len(n) < 2: if len(n) < 2:
result['Notes'] = 'Response too small' result['Notes'] = 'Response too small'
return result return [result,isFatal]
# Seperate out the return value # Seperate out the return value
roompass = n[0] roompass = n[0]
userpass = n[1] # user specific password userpass = n[1] # user specific password
   
STORAGE['room'] = {"name":"TestRoom1", STORAGE['room'] = {"name":"TestRoom1",
"RoomPass":roompass, "RoomPass":roompass,
"UserPass":userpass, "UserPass":userpass,
'User':'testadmin' 'User':'testadmin'
} }
   
  CONN,CURSOR = opendb()
   
# Check the DB to ensure the room was actually created # Check the DB to ensure the room was actually created
CURSOR.execute("SELECT * from rooms where name=?",('TestRoom1',)) CURSOR.execute("SELECT * from rooms where name=?",('TestRoom1',))
r = CURSOR.fetchone() r = CURSOR.fetchone()
  CONN.close()
if not r: if not r:
result['Notes'] = 'Room not in DB' result['Notes'] = 'Room not in DB'
return result return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,True] return [result,isFatal]
   
   
   
   
def test_two(msg): def test_two(msg):
''' Try joining the previously created room with invalid credentials ''' Try joining the previously created room with invalid credentials
''' '''
result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'], n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong')) "%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
if n: if n:
result['Notes'] = 'Allowed to join with invalid pass' result['Notes'] = 'Allowed to join with invalid pass'
return [result,False] return [result,isFatal]
# Now try with an invalid username # Now try with an invalid username
result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room with invalid creds','Result' : 'FAIL', 'Notes': '' }
n = msg.joinRoom('AlsoWrong',STORAGE['room']['name'],"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong')) n = msg.joinRoom('AlsoWrong',STORAGE['room']['name'],"%s:%s" % (STORAGE['room']['RoomPass'],'BlatantlyWrong'))
if n: if n:
result['Notes'] = 'Invalid user Allowed to join' result['Notes'] = 'Invalid user Allowed to join'
return [result,False] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,'False'] return [result,isFatal]
   
   
   
   
def test_three(msg): def test_three(msg):
''' Join the previously created room ''' Join the previously created room
''' '''
result = {'Test' : 'Join the room','Result' : 'FAIL', 'Notes': '' } result = {'Test' : 'Join the room','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'], n = msg.joinRoom(STORAGE['room']['User'],STORAGE['room']['name'],
"%s:%s" % (STORAGE['room']['RoomPass'],STORAGE['room']['UserPass']) "%s:%s" % (STORAGE['room']['RoomPass'],STORAGE['room']['UserPass'])
) )
if not n: if not n:
result['Notes'] = 'Could not join' result['Notes'] = 'Could not join'
return [result,True] return [result,isFatal]
   
  CONN,CURSOR = opendb()
# Check the DB to ensure we're now active # Check the DB to ensure we're now active
CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['room']['User'],)) CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['room']['User'],))
r = CURSOR.fetchone() r = CURSOR.fetchone()
  CONN.close()
if not r: if not r:
result['Notes'] = 'Not Active in DB' result['Notes'] = 'Not Active in DB'
return [result,True] return [result,isFatal]
# Check we've got a session token # Check we've got a session token
if not msg.sesskey: if not msg.sesskey:
result['Notes'] = 'No Session Key' result['Notes'] = 'No Session Key'
return [result,True] return [result,isFatal]
# Check the client has recorded what it needs to # Check the client has recorded what it needs to
if not msg.room: if not msg.room:
result['Notes'] = 'Client forgot room' result['Notes'] = 'Client forgot room'
return [result,True] return [result,isFatal]
   
if not msg.user: if not msg.user:
result['Notes'] = 'Client forgot user' result['Notes'] = 'Client forgot user'
return [result,True] return [result,isFatal]
   
if not msg.roompass: if not msg.roompass:
result['Notes'] = 'Client forgot roompass' result['Notes'] = 'Client forgot roompass'
return [result,True] return [result,isFatal]
   
if not msg.syskey: if not msg.syskey:
result['Notes'] = 'No SYSTEM key' result['Notes'] = 'No SYSTEM key'
return [result,True] return [result,isFatal]
result['Result'] = 'Pass' result['Result'] = 'Pass'
return [result,True] return [result,isFatal]
   
   
  def test_four(msg):
  ''' When we joined, SYSTEM will have pushed a message. Ensure it's encrypted
  '''
   
  result = {'Test' : 'SYSTEM uses E2E','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
   
   
  CONN,CURSOR = opendb()
   
  CURSOR.execute("SELECT msg FROM messages where user='SYSTEM' ORDER BY ts DESC")
  r = CURSOR.fetchone()
  CONN.close()
   
  if not r:
  result['Notes'] = 'No System Message'
  return [result,isFatal]
   
   
  try:
  json.loads(r[0])
  result['Notes'] = 'System Message not E2E encrypted'
  return [result,isFatal]
  except:
  # This is a good thing in this case!
   
  # Now try and decrypt the message
  m = msg.decrypt(r[0],'SYSTEM')
  if not m:
  result['Notes'] = 'Could not decrypt'
  return [result,isFatal]
   
  # Now check we got valid json
  try:
  j = json.loads(m)
  # Finally
  if "text" not in j or "verb" not in j:
  result['Notes'] = 'Not valid msg payload'
  return [result,isFatal]
   
  # Otherwise, we're good
  result['Result'] = 'Pass'
  return [result,isFatal]
   
  except:
  result['Notes'] = 'Not valid JSON'
  return [result,isFatal]
   
   
  def test_five(msg):
  ''' Send a message and ensure it's encrypted in the DB
  '''
   
  result = {'Test' : 'Ensure payloads are encrypted','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
   
  msg.sendMsg('Hello World')
   
  CONN,CURSOR = opendb()
   
  CURSOR.execute("SELECT msg FROM messages where user=? ORDER BY ts DESC",(STORAGE['room']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if not r:
  result['Notes'] = 'Message not recorded'
  return [result,isFatal]
   
   
  try:
  json.loads(r[0])
  result['Notes'] = 'Message not E2E encrypted'
  return [result,isFatal]
  except:
  # This is a good thing in this case!
   
  # Now try and decrypt the message
  m = msg.decrypt(r[0],STORAGE['room']['User'])
  if not m:
  result['Notes'] = 'Could not decrypt'
  return [result,isFatal]
   
  # Now check we got valid json
  try:
  j = json.loads(m)
  # Finally
  if "text" not in j or "verb" not in j:
  result['Notes'] = 'Not valid msg payload'
  return [result,isFatal]
   
  # Otherwise, we're good
  result['Result'] = 'Pass'
  return [result,isFatal]
   
  except:
  result['Notes'] = 'Not valid JSON'
  return [result,isFatal]
   
   
   
  def test_six(msg):
  ''' Invite a user
  '''
  result = {'Test' : 'Invite a user','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
   
  n = msg.inviteUser('testuser')
  if not n:
  result['Notes'] = 'Could not invite testuser'
  return [result,isFatal]
   
  if len(n) < 4:
  result['Notes'] = 'Client returned too short response'
  return [result,isFatal]
   
  # Otherwise, we've got details for a new user to be able to join
  #
  # Store them for a later test
   
  STORAGE['testuser'] = {
  'room':n[0],
  'pass':"%s:%s" % (n[1],n[2]),
  'User':n[3]
  }
   
  result['Result'] = "Pass"
  return [result,isFatal]
   
   
   
  def test_seven(msg):
  ''' Have the previously invited user join from a new client instance
  '''
  result = {'Test' : 'Join as Invited User','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
   
  usermsg = getClientInstance();
  n = usermsg.joinRoom(STORAGE['testuser']['User'],STORAGE['testuser']['room'],STORAGE['testuser']['pass'])
   
  if not n:
  result['Notes'] = 'Could not join'
  return [result,isFatal]
   
  STORAGE['testuser']['clientInstance'] = usermsg
   
  CONN,CURSOR = opendb()
   
  # Check the DB to ensure we're now active
  CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if not r:
  result['Notes'] = 'Not Active in DB'
  return [result,isFatal]
   
  # Check we've got a session token
  if not msg.sesskey:
  result['Notes'] = 'No Session Key'
  return [result,isFatal]
   
  # Check the client has recorded what it needs to
  if not msg.room:
  result['Notes'] = 'Client forgot room'
  return [result,isFatal]
   
  if not msg.user:
  result['Notes'] = 'Client forgot user'
  return [result,isFatal]
   
  if not msg.roompass:
  result['Notes'] = 'Client forgot roompass'
  return [result,isFatal]
   
  if not msg.syskey:
  result['Notes'] = 'No SYSTEM key'
  return [result,isFatal]
   
   
  result['Result'] = 'Pass'
  return [result,isFatal]
   
   
  def test_eight(msg):
  ''' Have the testuser send a message and ensure that the adminuser receives it
   
  We first call pollMsg to flush the message queue
  '''
   
  result = {'Test' : 'Send a Message','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
   
  # Poll for messages to clear the queue
  f = msg.pollForMessage()
   
  testpayload = 'The admin is a ....'
  # Now send a message as test user
  STORAGE['testuser']['clientInstance'].sendMsg(testpayload)
   
  # Now call pollMsg as admin to ensure we receive the message
  received = msg.pollForMessage()
   
  # Poll as testuser to clear the queue ready for future tests
  f = STORAGE['testuser']['clientInstance'].pollForMessage()
   
  if len(received) < 1:
  result['Notes'] = 'No messages received'
  return [result,isFatal]
   
  if len(received[0]) < 2:
  result['Notes'] = 'Malformed message returned'
  return [result,isFatal]
   
  # Now check the payload
  #
  # We need to trim out the timestamp etc
  r = received[0][1].split('>')
  m = r[1].lstrip()
  if testpayload != m:
  result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m)
  return [result,isFatal]
   
  result['Result'] = 'Pass'
  return [result,isFatal]
   
   
   
  def test_nine(msg):
  ''' Try to maliciously invite SYSTEM (as a precursor to logging in as them)
   
  Essentially a regression test for LOC-5
   
  '''
   
  result = {'Test' : 'LOC-5 Try to invite SYSTEM','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
   
  # Poll for messages to clear the queue
  f = msg.pollForMessage()
   
  # Now have the test user try to invite SYSTEM
  n = STORAGE['testuser']['clientInstance'].inviteUser('SYSTEM')
  if n:
  result['Notes'] = 'Allowed to invite SYSTEM'
  return [result,isFatal]
   
  # Now call pollMsg as admin to ensure we received the warning message
  received = msg.pollForMessage()
   
  # Poll as testuser to clear the queue ready for future tests
  f = STORAGE['testuser']['clientInstance'].pollForMessage()
   
  if len(received) < 1:
  result['Notes'] = 'No warning message received'
  return [result,isFatal]
   
  if len(received[0]) < 2:
  result['Notes'] = 'Malformed message returned'
  return [result,isFatal]
   
  # Now check the payload
  #
  # We need to trim out the timestamp etc
  r = received[0][1].split('>')
  m = r[1].lstrip()
   
  expected = "ALERT: User %s tried to invite SYSTEM" % (STORAGE['testuser']['User'],)
   
  if expected != m:
  result['Notes'] = 'Unexpected message received: %s' % (m)
  return [result,isFatal]
   
  result['Result'] = 'Pass'
  return [result,isFatal]
   
   
  def test_ten(msg):
  ''' Send a direct message and ensure it's received
   
  '''
   
  result = {'Test' : 'Send a Direct Message','Result' : 'FAIL', 'Notes': '' }
  isFatal = False
   
  # Poll for messages to clear the queue
  f = msg.pollForMessage()
  f = STORAGE['testuser']['clientInstance'].pollForMessage()
   
  testpayload = 'Hi Hi'
   
  # Now send a direct message from test user to admin
  n = STORAGE['testuser']['clientInstance'].sendDirectMsg(testpayload,'testadmin')
  if not n:
  result['Notes'] = 'Could not send direct message'
  return [result,isFatal]
   
  # Poll as the admin user to ensure it was received
  received = msg.pollForMessage()
   
  # Poll as testuser to clear the queue ready for future tests
  f = STORAGE['testuser']['clientInstance'].pollForMessage()
   
  if len(received) < 1:
  result['Notes'] = 'No messages received'
  return [result,isFatal]
   
  if len(received[0]) < 2:
  result['Notes'] = 'Malformed message returned'
  return [result,isFatal]
   
  # Now check the payload
  #
  # We need to trim out the timestamp etc
  r = received[0][1].split('>')
  m = r[1].lstrip()
  if testpayload != m:
  result['Notes'] = 'Incorrect message received: (%s)v(%s)' % (testpayload,m)
  return [result,isFatal]
   
  if "DM" not in r[0]:
  result['Notes'] = 'Envelope not marked as DM: %s' % (r[0],)
  return [result,isFatal]
   
  result['Result'] = 'Pass'
  return [result,isFatal]
   
   
   
  def test_eleven(msg):
  ''' Invite a new user and then have admin kick them to ensure they're
  actually kicked out of the room
   
  '''
  result = {'Test' : 'Invite and kick a user','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
   
  n = msg.inviteUser('testuser2')
  if not n:
  result['Notes'] = 'Could not invite testuser2'
  return [result,isFatal]
   
  if len(n) < 4:
  result['Notes'] = 'Client returned too short response'
  return [result,isFatal]
   
  # Otherwise, we've got details for a new user to be able to join
  #
  # Store them for a later test
   
  STORAGE['testuser2'] = {
  'room':n[0],
  'pass':"%s:%s" % (n[1],n[2]),
  'User':n[3]
  }
   
  # Create a new instance so we can join as testuser2
  usermsg = getClientInstance();
  n = usermsg.joinRoom(STORAGE['testuser2']['User'],STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
   
  if not n:
  result['Notes'] = 'User could not join'
  return [result,isFatal]
   
  STORAGE['testuser2']['clientInstance'] = usermsg
   
  # Now have the admin kick (but not ban) them
  msg.kickUser('testuser2',False)
   
   
  # Check that a failure message was written
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if not r:
  result['Notes'] = 'User not notified'
  return [result,isFatal]
   
  # Poll to receive the failure message and verify it's deleted
  msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
   
  if len(msgs) < 1:
  result['Notes'] = "User didn't receive notification"
  return [result,isFatal]
   
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if r:
  result['Notes'] = 'Notification not purged on poll'
  return [result,isFatal]
   
  # Now check that their session was suspended
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT username FROM users where username=? and active=1",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if r:
  result['Notes'] = 'User still considered active in room'
  return [result,isFatal]
   
  # Check if their session still exists
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
  if r:
  result['Notes'] = 'User still has active session'
  return [result,isFatal]
   
  # Otherwise, looks good
  result['Result'] = "Pass"
  return [result,isFatal]
   
   
  def test_twelve(msg):
  ''' Rejoin as the previously invited user, and then ban them
   
   
  '''
  result = {'Test' : 'Ban a user','Result' : 'FAIL', 'Notes': '' }
  isFatal = True
   
  # Flush the failedmessage queue
  f = STORAGE['testuser2']['clientInstance'].pollForMessage()
   
  # Re-join as testuser2
  n = STORAGE['testuser2']['clientInstance'].joinRoom(STORAGE['testuser2']['User'],
  STORAGE['testuser2']['room'],STORAGE['testuser2']['pass'])
   
  if not n:
  result['Notes'] = 'TestUser2 could not join'
  return [result,isFatal]
   
  # Now have the admin ban them
  msg.kickUser('testuser2',True)
   
   
  # Check that a failure message was written
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if not r:
  result['Notes'] = 'User not notified'
  return [result,isFatal]
   
  # Poll to receive the failure message and verify it's deleted
  msgs = STORAGE['testuser2']['clientInstance'].pollForMessage()
   
  if len(msgs) < 1:
  result['Notes'] = "User didn't receive notification"
  return [result,isFatal]
   
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT msg FROM failuremsgs where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if r:
  result['Notes'] = 'Notification not purged on poll'
  return [result,isFatal]
   
  # Now check that their session was suspended
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT username FROM users where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
   
  if r:
  result['Notes'] = 'User still exists for room'
  return [result,isFatal]
   
  # Check if their session still exists
  CONN,CURSOR = opendb()
  CURSOR.execute("SELECT username FROM sessions where username=?",(STORAGE['testuser2']['User'],))
  r = CURSOR.fetchone()
  CONN.close()
  if r:
  result['Notes'] = 'User still has active session'
  return [result,isFatal]
   
  # We don't need this any more
  del STORAGE['testuser2']
   
  # Otherwise, looks good
  result['Result'] = "Pass"
  return [result,isFatal]
   
   
   
   
   
if __name__ == '__main__': if __name__ == '__main__':
# Start the server # Start the server
proc1 = restartServer(False) proc1 = restartServer(False)
   
if not proc1: if not proc1:
# Server start failed. # Server start failed.
# abort, abort, abort # abort, abort, abort
exit(proc1,1) exit(proc1,1)
   
   
   
CONN = sqlite3.connect(DB_FILE)  
CURSOR = CONN.cursor()  
try: try:
# I don't like generic catchall exceptions # I don't like generic catchall exceptions
# but, we want to make sure we kill the background # but, we want to make sure we kill the background
# process if there is one. # process if there is one.
results = run_tests() results = run_tests()
except Exception as e: except Exception as e:
print traceback.format_exc() print traceback.format_exc()
print e print e
exit(proc1,1) exit(proc1,1)
cols = ['Test','Result','Notes'] cols = ['No','Test','Result','Notes']
print make_table(cols,results) print make_table(cols,results)
exit(proc1,0) exit(proc1,0)