| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | #============================================================================# This file is part of Pwman3.## Pwman3 is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2# as published by the Free Software Foundation;## Pwman3 is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with Pwman3; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA#============================================================================# Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>#============================================================================# pylint: disable=I0011import pexpectimport unittestimport osimport shutilOLD_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8.db')NEW_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8-newdb.db')_db_warn = ("pwman3 detected that you are using the old database format")class Ferrum(unittest.TestCase):    def clean_files(self):        lfile = 'convert-test.log'        with open(lfile) as l:            lines = l.readlines()            orig = lines[0].split(':')[-1].strip()            backup = lines[1].split()[-1].strip()        shutil.copy(backup, orig)        # do some cleaning        os.remove(lfile)        os.remove('test-chg_passwd.log')        os.remove(backup)    def test_a_db_warning(self):        "when trying to run with old db, we should see warning"        lfile = 'convert-test.log'        logfile = open(lfile, 'w')        child = pexpect.spawn(os.path.join(os.path.dirname(__file__),                                           '../../scripts/pwman3') +                              ' -d '+OLD_DB_PATH, logfile=logfile)        rv = child.expect(_db_warn, timeout=5)        if rv != 0:            lfile.seek(0)            print lfile.readlines()        self.assertEqual(0, rv)    def test_b_run_convert(self):        "invoke pwman with -k option to convert the old data"        lfile = 'convert-test.log'        logfile = open(lfile, 'w')        child = pexpect.spawn(os.path.join(os.path.dirname(__file__),                                           '../../scripts/pwman3') +                              ' -k -e Blowfish -d '+OLD_DB_PATH,                              logfile=logfile)        child.expect('[\s|\S]+Please enter your password:', timeout=10)        self.assertEqual(6, child.sendline('12345'))        rv = child.expect('pwman successfully converted the old database')        self.assertEqual(0, rv)        # if successfully converted, reset the converted database        # todo - add test to run auto_convert    def test_c_change_pass(self):        lfile = 'test-chg_passwd.log'        logfile = open(lfile, 'w')        child = pexpect.spawn(os.path.join(os.path.dirname(__file__),                                           '../../scripts/pwman3') +                              ' -e Blowfish -d '+OLD_DB_PATH, logfile=logfile)        child.sendline('passwd')        child.expect("Please enter your current password:")        child.sendline('12345')        child.sendline('foobar')        child.sendline('foobar')        self.clean_files()def suite():    loader = unittest.TestLoader()    suite = unittest.TestSuite()    suite.addTest(loader.loadTestsFromTestCase(Ferrum))    return suiteif __name__ == '__main__':    unittest.TextTestRunner(verbosity=2).run(suite())    os.remove(NEW_DB_PATH)
 |