LOC-24 add test for /leave functionality
LOC-24 add test for /leave functionality

Moved it before the earlier tests as otherwise the room it tests against will auto-close because of the delay

--- a/server/LocalChat.py
+++ b/server/LocalChat.py
@@ -858,18 +858,19 @@
 
 if __name__ == '__main__':
     
-    # LOC-15
-    testingmode = False
-    if '--testing-mode-enable' in sys.argv:
-        testingmode = True
-    
-    
     # These will be handled properly later
     passw = ''.join(random.SystemRandom().choice(string.ascii_letters + string.digits).encode('utf-8') for _ in range(64))
     bindpoint = "https://127.0.0.1:8090" 
     purgeinterval = 600 # Wipe messages older than 10 mins
     closethresh = 3600 * 6 # Auto-close rooms after 6 hours of inactivity
-
+    
+    # LOC-15
+    testingmode = False
+    if '--testing-mode-enable' in sys.argv:
+        testingmode = True
+        purgeinterval = 30
+        closethresh = 180
+    
     # Create a global instance of the wrapper so that state can be retained
     #
     # We pass in the password we generated for the scheduler thread to use

--- a/tests/run_tests.py
+++ b/tests/run_tests.py
@@ -11,6 +11,7 @@
 import sqlite3
 import traceback
 import json
+import datetime
 
 
 try:
@@ -143,7 +144,8 @@
     test_results = []
     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']
+             'test_nine','test_ten','test_eleven','test_twelve',
+             'test_thirteen','test_fourteen','test_fifteen']
     x = 1
     for test in tests:
         print "Running %s " % (test,)
@@ -719,14 +721,12 @@
     # 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()
@@ -781,7 +781,247 @@
     return [result,isFatal]
 
 
-    
+def test_thirteen(msg):
+    ''' Check that /leave works
+    
+    Essentially, we send a message, wait 240 seconds and then check if it's gone
+    
+    In testing mode, closure is after 3 mins, but we wait an extra minute to make sure the scheduler has run
+    
+    
+    '''
+    result = {'Test' : 'Leave a room','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    newmsg = getClientInstance()
+
+    n = msg.inviteUser('testuser3')
+    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['testuser3'] = {
+        'room':n[0],
+        'pass':"%s:%s" % (n[1],n[2]),
+        'User':n[3]
+        }
+
+    n = newmsg.joinRoom(STORAGE['testuser3']['User'],STORAGE['testuser3']['room'],STORAGE['testuser3']['pass'])
+    
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    STORAGE['testuser3']['clientInstance'] = newmsg
+    
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure They're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser3']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r:
+        result['Notes'] = 'Not Active in DB'
+        return [result,isFatal]
+    
+
+
+    # Now have them leave the room and check they drop out of the DB
+    if not newmsg.leaveRoom():
+        result['Notes'] = 'API reported failure'
+        return [result,isFatal]
+
+
+
+    # They should no longer be in the DB
+    CONN,CURSOR = opendb()
+    
+    # Check the DB to ensure They're now active
+    CURSOR.execute("SELECT * from users where username=? and active=1",(STORAGE['testuser3']['User'],))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r:
+        result['Notes'] = 'We left but are still active in the DB'
+        return [result,isFatal]
+    
+    # Verify the client cleared it's stored info
+    if newmsg.room or newmsg.roompass:
+        result['Notes'] = 'Client still stores room info'
+        return [result,isFatal]
+        
+    
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+def test_fourteen(msg):
+    ''' Check that message purging is actually happening
+    
+    Essentially, we send a message, wait 90 seconds and then check if it's gone
+    
+    '''
+    result = {'Test' : 'Automated message expiration','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    msg.sendMsg('This is a test message')
+    
+    # Open the DB and verify there are messages in the queue now
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages")
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r or r[0] < 1:
+        result['Notes'] = 'Messages not getting into queue'
+        return [result,isFatal]  
+     
+    # Now, we wait
+    time.sleep(90)
+    
+    # Re-open the database and the messages should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages")
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r and r[0] > 0:
+        result['Notes'] = 'Messages not purged'
+        return [result,isFatal]  
+         
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+def test_fifteen(msg):
+    ''' Check that auto room closure happens
+    
+    Essentially, we send a message, wait 240 seconds and then check if it's gone
+    
+    In testing mode, closure is after 3 mins, but we wait an extra minute to make sure the scheduler has run
+    
+    
+    '''
+    result = {'Test' : 'Automated Room Closure','Result' : 'FAIL', 'Notes': '' }
+    isFatal = False
+    
+    newmsg = getClientInstance()
+
+    n = newmsg.createRoom('TestRoom2','testadmin2')
+    if not n:
+        result['Notes'] = 'Empty Response'
+        return [result,isFatal]
+
+    # The client should have given us two passwords
+    if len(n) < 2:
+        result['Notes'] = 'Response too small'
+        return [result,isFatal]
+
+    # Seperate out the return value
+    roompass = n[0]
+    userpass = n[1] # user specific password
+
+    n = newmsg.joinRoom('testadmin2','TestRoom2',
+                    "%s:%s" % (n[0],n[1])
+                    )
+    if not n:
+        result['Notes'] = 'Could not join'
+        return [result,isFatal]
+    
+    # Get the room ID
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT id from rooms where name=?",('TestRoom2',))
+    r = CURSOR.fetchone()
+    CONN.close()    
+    
+    room = r[0]
+    
+    newmsg.sendMsg('This is a test message')
+    
+    # Open the DB and verify there are messages in the queue now
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if not r or r[0] < 1:
+        result['Notes'] = 'Messages not getting into queue'
+        return [result,isFatal]  
+     
+    # Now, we wait
+    time.sleep(240)
+    
+    # Re-open the database and the messages should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from messages where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+    
+    if r and r[0] > 0:
+        result['Notes'] = 'Messages not purged'
+        return [result,isFatal]  
+
+    # The room should be gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from rooms where name=?",('TestRoom2',))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'Room record not deleted'
+        return [result,isFatal]          
+
+    # Check the user records have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from users where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'User records not deleted'
+        return [result,isFatal]          
+    
+    # Check Failure messages have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from failuremsgs where room=?",(room,))
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'Failure Messages not deleted'
+        return [result,isFatal]          
+    
+    # Check Sessions have gone
+    CONN,CURSOR = opendb()
+    CURSOR.execute("SELECT count(*) from sessions where sesskey like 'TestRoom2-%'")
+    r = CURSOR.fetchone()
+    CONN.close()
+
+    if r and r[0] > 0:
+        result['Notes'] = 'User Sessions not deleted'
+        return [result,isFatal]          
+    
+    # Otherwise, we're good
+    result['Result'] = 'Pass'
+    return [result,isFatal]
+
+
+
+
+
 
 
 if __name__ == '__main__':
@@ -798,7 +1038,13 @@
         # I don't like generic catchall exceptions
         # but, we want to make sure we kill the background
         # process if there is one.
+        cmd = "git show | head -n1"
+        ps = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
+        revision = ps.communicate()[0].split(" ")[1]
+        now = datetime.datetime.utcnow().strftime("%Y%m%d%H%M%S")
+        print """Test Run %s-%s""" % (now,revision)
         results = run_tests()
+        
     except Exception as e:
         print traceback.format_exc()
         print e