test_complete_ui.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. #============================================================================
  17. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. # pylint: disable=I0011
  20. import pexpect
  21. import unittest
  22. import os
  23. import shutil
  24. OLD_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8.db')
  25. NEW_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8-newdb.db')
  26. _db_warn = ("\n*** WARNNING: You are using the old database format"
  27. " which is insecure."
  28. " Please upgrade to the new database "
  29. " format."
  30. " You can upgrade the database now, or you can do it later."
  31. " Check the help (pwman3 -h) or look at the manpage. "
  32. "***")
  33. class Ferrum(unittest.TestCase):
  34. def test_db_warning(self):
  35. "when trying to run with old db, we should see warning"
  36. child = pexpect.spawn(os.path.join(os.path.dirname(__file__),
  37. '../../scripts/pwman3') +
  38. ' -t -d '+OLD_DB_PATH)
  39. self.assertEqual(0, child.expect_exact(_db_warn, timeout=0.5))
  40. def test_run_convert(self):
  41. "invoke pwman with -k option to convert the old data"
  42. lfile = 'test.log'
  43. logfile = open(lfile, 'w')
  44. child = pexpect.spawn(os.path.join(os.path.dirname(__file__),
  45. '../../scripts/pwman3') +
  46. ' -t -k -e Blowfish -d '+OLD_DB_PATH,
  47. logfile=logfile)
  48. child.expect('[\s|\S]+Please enter your password:', timeout=5)
  49. self.assertEqual(6, child.sendline('12345'))
  50. rv = child.expect('pwman successfully converted the old database')
  51. self.assertEqual(0, rv)
  52. # if successfully converted, reset the converted database
  53. if not rv:
  54. with open(lfile) as l:
  55. lines = l.readlines()
  56. orig = lines[0].split(':')[-1].strip()
  57. backup = lines[1].split()[-1].strip()
  58. shutil.copy(backup, orig)
  59. # do some cleaning
  60. os.remove(lfile)
  61. os.remove(backup)
  62. # todo - add test to run auto_convert
  63. def suite():
  64. loader = unittest.TestLoader()
  65. suite = unittest.TestSuite()
  66. suite.addTest(loader.loadTestsFromTestCase(Ferrum))
  67. return suite
  68. if __name__ == '__main__':
  69. unittest.TextTestRunner(verbosity=2).run(suite())
  70. os.remove(NEW_DB_PATH)