예제 #1
0
class SessionsDBTest(unittest.TestCase):

    def setUp(self):
        self._connection = DatabaseConnection()
        
        # read data from config file, we don't want to be modifying this code  all the time
        config = ConfigParser.ConfigParser()
        config.readfp(open('../test/test-db.cfg'))
        DBHOST = config.get("Database", "host")
        DB = config.get("Database", "database")
        DBUSER = config.get("Database", "user")
        DBPASS = config.get("Database", "pass")
        
        self._connection.connect(DBHOST, DBUSER, DBPASS, DB)
        self._userDB = UsersDB(self._connection)
        
        self._sessionsDB = SessionsDB(self._connection)
        self._userDB.addUser('user1','123')
        
    def tearDown(self):
        self._connection._execute('TRUNCATE sessions')
        self._connection._execute('TRUNCATE users')
        self._connection._commit()
        self._connection.disconnect()

    def testSession(self):
        sessionId = '440f23c58848769685e481ff270b046659f40b7c'
        self._sessionsDB.createSession(sessionId, 'user1', '216.27.61.137', datetime.datetime.now())
        session = self._sessionsDB.getSession(sessionId)
        self.assertEqual( session['username'], 'user1')
        self.assertEqual( self._sessionsDB.getUserFromSession(sessionId), 'user1')
        self.assertEqual( session['ip_address'], '216.27.61.137')        
        self._sessionsDB.endSession(sessionId)
        self.assertEqual( None, self._sessionsDB.getSession(sessionId))
    
    def testRemoveExpiredSessions(self):
        sessionIds = ('440f23c58848769685e481ff270b046659f40b7a','440f23c58848769685e481ff270b046659f40b7b', '440f23c58848769685e481ff270b046659f40b7c')
        self._sessionsDB.createSession(sessionIds[0], 'user1', '216.27.61.137', datetime.datetime.now() + datetime.timedelta(hours = 2))
        self._sessionsDB.createSession(sessionIds[1], 'user1', '216.27.61.138', datetime.datetime.now() - datetime.timedelta(hours = 2))
        self._sessionsDB.createSession(sessionIds[2], 'user1', '216.27.61.139', datetime.datetime.now() - datetime.timedelta(hours = 2))
        self._sessionsDB.removeExpiredSessions()
        
        self.assertEqual( sessionIds[0], self._sessionsDB.getSession(sessionIds[0])['session_id'])
        self.assertEqual( None, self._sessionsDB.getSession(sessionIds[1]))
        self.assertEqual( None, self._sessionsDB.getSession(sessionIds[2]))
예제 #2
0
class UsersDBTest(unittest.TestCase):
    def setUp(self):
        self._connection = DatabaseConnection()
        
        # read data from config file, we don't want to be modifying this code  all the time
        config = ConfigParser.ConfigParser()
        config.readfp(open('../test/test-db.cfg'))
        DBHOST = config.get("Database", "host")
        DB = config.get("Database", "database")
        DBUSER = config.get("Database", "user")
        DBPASS = config.get("Database", "pass")
        
        self._connection.connect(DBHOST, DBUSER, DBPASS, DB)
        self._userDB = UsersDB.UsersDB(self._connection)
        self._userDB.addUser("_TestUser" , "123")
        
    def tearDown(self):
        self._connection._execute('TRUNCATE users_files')
        self._connection._execute('TRUNCATE files')
        self._connection._execute('TRUNCATE users')
        self._connection._commit()
        self._connection.disconnect()
                
    def testUser(self):
        # note how getUser addUser and removeUser are all used to test each other
        self._userDB.addUser("_TestUser2" , "456")
        self.assertEqual({'username': '******', 'salt': 'abcdefg', 'quota': 0L, 'password_hash': '456'}, self._userDB.getUser('_TestUser2'))
        self._userDB.removeUser("_TestUser2")
        self.assertEqual(None, self._userDB.getUser("_TestUser2"))
    
    def testAuthenticate(self):
        self.assertEqual(True, self._userDB.authenticate("_TestUser", "123"))
        self.assertEqual(False, self._userDB.authenticate("_TestUser", "wrongpass"))
        self.assertEqual(False, self._userDB.authenticate("_wronguser", "wrongpass"))
    
    def testUserExists(self):    
        self.assertEqual(True, self._userDB.userExists("_TestUser"))
        self.assertEqual(False, self._userDB.userExists("_RandomUser"))
    
    def testUpdateUsername(self):
        self._userDB.updateUsername('NewName','_TestUser')
        self.assertEqual({'username': '******', 'salt': 'abcdefg', 'quota': 0L, 'password_hash': '123'},self._userDB.getUser("NewName"))        

    def testUpdatePassword(self):
        self._userDB.updatePassword('new_pass', '_TestUser', '123')
        self.assertEqual({'username': '******', 'salt': 'abcdefg', 'quota': 0L, 'password_hash': 'new_pass'},self._userDB.getUser("_TestUser"))
        
    def testUserQuota(self):
        self._userDB.setUserQuota(100 * 1024 * 1024 * 1024, "_TestUser")
        self.assertEqual(100 * 1024 * 1024 * 1024, self._userDB.getUserQuota("_TestUser"))

    def testGetSpaceRemaining(self):
        quota = 100 * 1024 * 1024 * 1024
        self._userDB.setUserQuota(quota, "_TestUser")
        fileDB = FilesDB.FilesDB(self._connection)
        fileDB.addFile("_TestUser", "/home/canada/32-c.bill", "/mnt/HD_a2/_TestUser/cRaZyHaShVaLuE/32-c.bill", 2048, "_TestUser", datetime.datetime(2011, 3, 26, 15, 6, 17), 1, "2b61cdf97336e06740df")
        fileDB.addFile("_TestUser", "/home/canada/human-rights.bill", "/mnt/HD_a2/_TestUser/cRaZyHaShVaLuE/human-rights.bill", 2048, "_TestUser", datetime.datetime(2011, 3, 26, 15, 6, 17), 1, "2b61chq97336e06740df")
        self.assertEqual(quota - (2 * 2048), self._userDB.getSpaceRemaining("_TestUser"))
        
    def testGetAllUser(self):
        userDB = self._userDB
        userTuples = (  {'username': "******",'password_hash': '123', 'quota': 0, 'salt': "abcdefg"}
                      , {'username': "******",'password_hash': '223', 'quota': 0, 'salt': "abcdefg"}
                      , {'username': "******",'password_hash': '333', 'quota': 0, 'salt': "abcdefg"}
                      , {'username': "******",'password_hash': '443', 'quota': 0, 'salt': "abcdefg"})
        self._addUsers(userDB, userTuples)
        userTuplesFromDB = userDB.getAllUser()
        userTuples += ({'username': "******",'password_hash': '123', 'quota': 0, 'salt': "abcdefg"},)
        self.assertEqual( userTuples, userTuplesFromDB)
        
    def _addUsers(self, userDB, userTuples):
        index = 0
        while (index < len( userTuples ) ):
            user = userTuples[index]
            userDB.addUser(user['username'] , user['password_hash'])
            index += 1
예제 #3
0
class FilesDBTest(unittest.TestCase):
    def setUp(self):
        self._connection = DatabaseConnection()
        
        # read data from config file, we don't want to be modifying this code  all the time
        config = ConfigParser.ConfigParser()
        config.readfp(open('../test/test-db.cfg'))
        DBHOST = config.get("Database", "host")
        DB = config.get("Database", "database")
        DBUSER = config.get("Database", "user")
        DBPASS = config.get("Database", "pass")
        
        self._connection.connect(DBHOST, DBUSER, DBPASS, DB)
        self._userDB = UsersDB(self._connection)
        
        self._fileDB = FilesDB(self._connection)
        self._userDB.addUser("_TestUser" , "123")
        self._fileDB.addFile("_TestUser","folder2/testFile1.txt", "/mnt/HD_a2/_TestUser/f3bfa/testFile1.txt", 35, "_TestUser",datetime.datetime(2011, 3, 24, 15, 6, 16),1, "2b61cdf97336e06720df")
        self._fileDB.addFile("_TestUser","folder2/testFile2.txt", "/mnt/HD_a2/_TestUser/f3bfa/testFile2.txt", 35, "_TestUser",datetime.datetime(2011, 3, 24, 15, 6, 16),1, "4bd950f32db28f05d972")
        
    def tearDown(self):
        self._connection._execute('TRUNCATE files_history')
        self._connection._execute('TRUNCATE users_files')
        self._connection._execute('TRUNCATE files')
        self._connection._execute('TRUNCATE users')
        self._connection._commit()
        self._connection.disconnect() 

    def testFile(self):
        fileId = self._fileDB.addFile("_TestUser","folder5/testFile1.txt", "/mnt/HD_a2/_TestUser/f3bfa/testFile1.txt", 35, "_TestUser", datetime.datetime(2011, 3, 26, 15, 6, 17), 1, "2b61cdf97336e06720df")
        self.assertEqual({'username': "******",
                          'file_id': fileId, \
                          'f.file_id': fileId, \
                          'client_path': "folder5/testFile1.txt", \
                          'server_path': "/mnt/HD_a2/_TestUser/f3bfa/testFile1.txt", \
                          'size': 35, \
                          'last_author': "_TestUser", \
                          'last_modified': datetime.datetime(2011, 3, 26, 15, 6, 17), \
                          'version': 1, \
                          'permission_level': 0, \
                          'directory': 0, \
                          'checksum': "2b61cdf97336e06720df"}, \
                        self._fileDB.getFile("_TestUser", "folder5/testFile1.txt"))
        self._fileDB.removeFile("_TestUser", "folder5/testFile1.txt")
        self.assertEqual(None, self._fileDB.getFile("_TestUser", "folder5/testFile1.txt"))

    def testRemoveFile(self):
        newFile = self._fileDB.getFile("_TestUser" , "folder2/testFile1.txt")
        
        oldAuthor = newFile['last_author']
        newFile['last_author'] = "_TestUser123"
                
        self._fileDB.updateFile("_TestUser", "folder2/testFile1.txt", newFile) 
        data = self._fileDB.getFileVersion(newFile['file_id'], newFile['version']) 
        self.assertEqual( data['last_author'] , oldAuthor )
         
        self._fileDB.removeFile("_TestUser" , "folder2/testFile1.txt" )
        data = self._fileDB.getFile("_TestUser" , "folder2/testFile1.txt")
        self.assertEqual( data , None )
        data = self._fileDB.getFileVersion(newFile['file_id'], 1) 
        self.assertEqual( data , None)  
    
    def testGetFilesInDir(self):
        files = self._fileDB.getFilesInDir("folder2/", "_TestUser")
        self.assertEqual(files[0]['client_path'], "folder2/testFile1.txt")
        self.assertEqual(files[1]['client_path'], "folder2/testFile2.txt")

    def testGetAllFiles(self):
        files = self._fileDB.getAllFiles("_TestUser")
        self.assertEqual(files[0]['client_path'], "folder2/testFile1.txt")
        self.assertEqual(files[1]['client_path'], "folder2/testFile2.txt")
        
    def testUpdateFiles(self):
        newFile = self._fileDB.getFile("_TestUser" , "folder2/testFile1.txt")
        newFile['last_author'] = "_TestUser123"
        
        self._fileDB.updateFile("_TestUser", "folder2/testFile1.txt", newFile)
        data = self._fileDB.getFile("_TestUser" , "folder2/testFile1.txt")   
        self.assertEqual( "_TestUser123" , data['last_author'] )     
            
    def testGetServerPath(self):
        file = self._fileDB.getFile("_TestUser","folder2/testFile1.txt")
        self.assertEqual(file['server_path'], self._fileDB.getServerPath(file['file_id']))
        
    def testGetClientPath(self):
        file = self._fileDB.getFile("_TestUser","folder2/testFile1.txt")
        self.assertEqual(file['client_path'], self._fileDB.getClientPath(file['file_id']))
        
    def testGetChecksum(self):
        file = self._fileDB.getFile("_TestUser","folder2/testFile1.txt")
        self.assertEqual(file['checksum'], self._fileDB.getChecksum(file['file_id']))
        
    def testGetLastModified(self):
        file = self._fileDB.getFile("_TestUser","folder2/testFile1.txt")
        self.assertEqual(file['last_modified'], self._fileDB.getLastModified(file['file_id']))
        
    def testPermissions(self):
        file = self._fileDB.getFile("_TestUser", "folder2/testFile1.txt")
        self._fileDB.setPermission("_TestUser", file['file_id'], 0)
        self.assertTrue(self._fileDB.getPermission("_TestUser", file['file_id']) == 0)
        randomFileID = 3145156
        self.assertTrue(self._fileDB.getPermission("_TestUser", randomFileID) == None)
        
    def testGetFileHistory(self):
        file = self._fileDB.getFile("_TestUser", "folder2/testFile1.txt")
        # test before we add anything
        fileVersions = self._fileDB.getFileHistory(file['file_id'])
        self.assertTrue(fileVersions[0]['version'] == 1 and fileVersions[0]['size'] == 35 and \
                        len(fileVersions) == 1)
        
        # after update test to see new version listed
        file['size'] = 45
        file['checksum'] = '2b61cdf97336e06720dj'
        self._fileDB.updateFile("_TestUser" , "folder2/testFile1.txt", file)
        fileVersions = self._fileDB.getFileHistory(file['file_id'])
        self.assertTrue(fileVersions[0]['version'] == 2 and fileVersions[0]['size'] == 45 and \
                        fileVersions[1]['version'] == 1 and fileVersions[1]['size'] == 35 and \
                        len(fileVersions) == 2)