def test_db_connect_exception(self): db = MySQLdb.connect(server, user, password, database) cursor = db.cursor() call(["sudo", "service", "mysql", "stop"], stdout=stdout_log) with self.assertRaises(MySQLdb.Error): qwifi.main() with self.assertRaises(MySQLdb.OperationalError): qwifi.update_radcheck(db, cursor) with self.assertRaises(MySQLdb.Error): qwifi.cull(db, cursor) call(["sudo", "service", "mysql", "start"], stdout=stdout_log)
def test_cull(self): db = MySQLdb.connect(server, user, password, database) os.system("mysql -u " + user +" -p" + password + " -h " + server + " " + database + " < " + "test.sql" ) db = MySQLdb.connect(server, user, password, database) cursor = db.cursor() #insert a new value to make sure cull only deletes what we want cursor.execute('INSERT INTO radcheck (username) VALUES("testtest");') #update radcheck cursor.execute("INSERT INTO radcheck (username, attribute, op, value) SELECT radcheck.username, 'Auth-Type', ':=', 'Reject' FROM radcheck INNER JOIN radacct ON radcheck.username=radacct.username WHERE radcheck.attribute='Session-Timeout' AND TIMESTAMPDIFF(SECOND, radacct.acctstarttime, NOW()) > radcheck.value AND radacct.acctstoptime is NULL;") #let cull do it's job qwifi.cull(db, cursor) #find all radcheck entries where value = reject (there should be zero) cursor.execute("SELECT * FROM radcheck WHERE value='Reject';") self.assertEqual(len(cursor.fetchall()), 0) #find the rest of the entries in radcheck (there should be four) cursor.execute("SELECT * FROM radcheck;") self.assertEqual(len(cursor.fetchall()), 4) db.close()
def test_cull(self): db = MySQLdb.connect(server, user, password, database) os.system("mysql -u " + user + " -p" + password + " -h " + server + " " + database + " < " + "test.sql") db = MySQLdb.connect(server, user, password, database) cursor = db.cursor() #insert a new value to make sure cull only deletes what we want cursor.execute('INSERT INTO radcheck (username) VALUES("testtest");') #update radcheck cursor.execute( "INSERT INTO radcheck (username, attribute, op, value) SELECT radcheck.username, 'Auth-Type', ':=', 'Reject' FROM radcheck INNER JOIN radacct ON radcheck.username=radacct.username WHERE radcheck.attribute='Session-Timeout' AND TIMESTAMPDIFF(SECOND, radacct.acctstarttime, NOW()) > radcheck.value AND radacct.acctstoptime is NULL;" ) #let cull do it's job qwifi.cull(db, cursor) #find all radcheck entries where value = reject (there should be zero) cursor.execute("SELECT * FROM radcheck WHERE value='Reject';") self.assertEqual(len(cursor.fetchall()), 0) #find the rest of the entries in radcheck (there should be four) cursor.execute("SELECT * FROM radcheck;") self.assertEqual(len(cursor.fetchall()), 4) db.close()