test_complete_ui.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. from __future__ import print_function
  21. import pexpect
  22. import unittest
  23. import os
  24. import shutil
  25. OLD_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8.db')
  26. NEW_DB_PATH = os.path.join(os.path.dirname(__file__), 'pwman.v0.0.8-newdb.db')
  27. _db_warn = ("pwman3 detected that you are using the old database format")
  28. class Ferrum(unittest.TestCase):
  29. def clean_files(self):
  30. lfile = 'convert-test.log'
  31. with open(lfile) as l:
  32. lines = l.readlines()
  33. orig = lines[0].split(':')[-1].strip()
  34. backup = lines[1].split()[-1].strip()
  35. shutil.copy(backup, orig)
  36. # do some cleaning
  37. os.remove(lfile)
  38. os.remove('test-chg_passwd.log')
  39. os.remove(backup)
  40. def test_a_db_warning(self):
  41. "when trying to run with old db, we should see warning"
  42. lfile = 'convert-test.log'
  43. logfile = open(lfile, 'wb')
  44. cmd = os.path.join(os.path.dirname(__file__), '../../scripts/pwman3'
  45. ) + ' -d '+OLD_DB_PATH
  46. child = pexpect.spawn(cmd, logfile=logfile)
  47. rv = child.expect(_db_warn, timeout=5)
  48. if rv != 0:
  49. lfile.seek(0)
  50. print(lfile.readlines())
  51. self.assertEqual(0, rv)
  52. def test_b_run_convert(self):
  53. "invoke pwman with -k option to convert the old data"
  54. lfile = 'convert-test.log'
  55. logfile = open(lfile, 'wb')
  56. cmd = (os.path.join(os.path.dirname(__file__), '../../scripts/pwman3'
  57. ) + ' -k -e Blowfish -d ' + OLD_DB_PATH)
  58. child = pexpect.spawn(cmd, logfile=logfile)
  59. child.expect('[\s|\S]+Please enter your password:', timeout=10)
  60. self.assertEqual(6, child.sendline('12345'))
  61. rv = child.expect('pwman successfully converted the old database')
  62. self.assertEqual(0, rv)
  63. # if successfully converted, reset the converted database
  64. # todo - add test to run auto_convert
  65. def test_c_change_pass(self):
  66. lfile = 'test-chg_passwd.log'
  67. logfile = open(lfile, 'wb')
  68. child = pexpect.spawn(os.path.join(os.path.dirname(__file__),
  69. '../../scripts/pwman3') +
  70. ' -e Blowfish -d '+OLD_DB_PATH, logfile=logfile)
  71. child.sendline('passwd')
  72. child.expect("Please enter your current password:")
  73. child.sendline('12345')
  74. child.sendline('foobar')
  75. child.sendline('foobar')
  76. self.clean_files()
  77. def suite():
  78. loader = unittest.TestLoader()
  79. suite = unittest.TestSuite()
  80. suite.addTest(loader.loadTestsFromTestCase(Ferrum))
  81. return suite
  82. if __name__ == '__main__':
  83. unittest.TextTestRunner(verbosity=2).run(suite())
  84. os.remove(NEW_DB_PATH)