test_ui.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from pwman_tstlib import *
  2. initTest(__file__)
  3. import importlib
  4. import os
  5. import pathlib
  6. import shutil
  7. import tempfile
  8. class Test_UI(TestCase):
  9. """User interface.
  10. """
  11. def __fakeClearScreen(self):
  12. print("CLEAR SCREEN")
  13. def __fakeReadPassphrase(self, prompt, verify=False):
  14. print("READ PASSPHRASE")
  15. return "test"
  16. def setUp(self):
  17. import libpwman.util
  18. self.__origClearScreen = libpwman.util.clearScreen
  19. self.__origReadPassphrase = libpwman.util.readPassphrase
  20. libpwman.util.clearScreen = self.__fakeClearScreen
  21. libpwman.util.readPassphrase = self.__fakeReadPassphrase
  22. import libpwman
  23. importlib.reload(libpwman.ui)
  24. self.dbFile = tempfile.NamedTemporaryFile(suffix="_" + self.id())
  25. self.dbFileSecondary = tempfile.NamedTemporaryFile(suffix="_secondary_" + self.id())
  26. print("DB file:", self.dbFile.name)
  27. print("Secondary DB file:", self.dbFileSecondary.name)
  28. shutil.copy(pathlib.Path("tests", "test_database_v1.db"),
  29. self.dbFile.name)
  30. self.ui = libpwman.PWMan(filename=self.dbFile.name,
  31. passphrase="test")
  32. def tearDown(self):
  33. try:
  34. self.dbFile.close()
  35. except FileNotFoundError as e:
  36. pass
  37. try:
  38. self.dbFileSecondary.close()
  39. except FileNotFoundError as e:
  40. pass
  41. self.dbFile = None
  42. self.dbFileSecondary = None
  43. import libpwman.util
  44. libpwman.util.clearScreen = self.__origClearScreen
  45. def test_base(self):
  46. import libpwman
  47. self.ui.do_help("")
  48. self.assertRaises(libpwman.PWMan.Quit,
  49. lambda: self.ui.do_quit(""))
  50. self.ui.do_cls("")
  51. self.ui.do_database("") # No params: db listing.
  52. self.ui.do_database("main") # Re-select of main shall succeed.
  53. self.ui.do_commit("")
  54. self.ui.do_masterp("")
  55. self.ui.do_dbdump("")
  56. self.assertRaises(libpwman.PWMan.CommandError,
  57. lambda: self.ui.do_dbimport("/does/not/exist"))
  58. self.ui.do_drop("")
  59. self.ui.do_list("")
  60. self.ui.do_list("testcat1 testtitle1 totpkey")
  61. self.ui.do_find("test")
  62. self.ui.do_totp("testcat1 testtitle1")
  63. self.ui.do_diff("")
  64. self.ui.do_new("a b c")
  65. self.ui.do_edit_user("a b c")
  66. self.ui.do_edit_pw("a b c")
  67. self.ui.do_edit_bulk("a b c")
  68. self.ui.do_edit_totp("a b GEZDGNAK 6 SHA1")
  69. self.ui.do_edit_attr("a b c d")
  70. self.ui.do_move("a b c d")
  71. self.ui.do_remove("c d")
  72. def test_multidb_closemain(self):
  73. import libpwman
  74. self.assertRaises(libpwman.PWMan.Quit,
  75. lambda: self.ui.do_close(""))
  76. def test_multidb(self):
  77. import libpwman
  78. try:
  79. os.unlink(self.dbFileSecondary.name)
  80. except FileNotFoundError as e:
  81. pass
  82. # main db
  83. self.ui.do_new("cat0 ent0 user0 pw0")
  84. self.ui.do_new("cat1 ent1 user1 pw1")
  85. self.ui.do_new("bigcat ent10 user10 pw10")
  86. self.ui.do_new("bigcat ent11 user11 pw11")
  87. self.ui.do_new("bigcat ent12 user12 pw12")
  88. self.ui.do_new("bigcat ent13 user13 pw13")
  89. # secondary db
  90. self.ui.do_database("-f %s secondary" % self.dbFileSecondary.name)
  91. self.ui.do_new("cat2 ent2 user2 pw2")
  92. self.ui.do_database("main")
  93. self.ui.do_move("-d secondary cat0 ent0 cat0")
  94. self.assertRaises(libpwman.PWMan.CommandError,
  95. lambda: self.ui.do_move("-d secondary cat0 ent0 cat0"))
  96. self.ui.do_move("-s secondary -d main cat0 ent0 cat0")
  97. self.assertRaises(libpwman.PWMan.CommandError,
  98. lambda: self.ui.do_move("-s secondary -d main cat0 ent0 cat0"))
  99. self.ui.do_move("-d secondary bigcat")
  100. # self.assertRaises(libpwman.PWMan.CommandError,
  101. # lambda: self.ui.do_move("-d secondary bigcat"))
  102. # self.ui.do_move("-s secondary -d main bigcat bigcat2")
  103. # self.assertRaises(libpwman.PWMan.CommandError,
  104. # lambda: self.ui.do_move("-s secondary -d main bigcat bigcat2"))
  105. self.ui.do_commit("-a")