test_tools.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from pwman.data import factory
  2. from pwman.util import config
  3. from pwman import which, default_config
  4. from pwman.ui import get_ui_platform
  5. import os
  6. import os.path
  7. import sys
  8. from pwman.util.callback import Callback
  9. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  10. class DummyCallback(Callback):
  11. def getsecret(self, question):
  12. return u'12345'
  13. def getnewsecret(self, question):
  14. return u'12345'
  15. class DummyCallback2(Callback):
  16. def getinput(self, question):
  17. return u'newsecret'
  18. def getsecret(self, question):
  19. return u'wrong'
  20. def getnewsecret(self, question):
  21. return u'newsecret'
  22. class DummyCallback3(Callback):
  23. def getinput(self, question):
  24. return u'newsecret'
  25. def getsecret(self, question):
  26. ans = '12345'
  27. return ans
  28. def getnewsecret(self, question):
  29. return u'newsecret'
  30. class DummyCallback4(Callback):
  31. def getinput(self, question):
  32. return u'newsecret'
  33. def getsecret(self, question):
  34. return u'newsecret'
  35. def getnewsecret(self, question):
  36. return u'newsecret'
  37. default_config['Database'] = {'type': 'SQLite',
  38. 'filename':
  39. os.path.join(os.path.dirname(__file__),
  40. "test.pwman.db")
  41. }
  42. class SetupTester(object):
  43. def __init__(self, dbver=None, filename=None):
  44. config.set_defaults(default_config)
  45. if not OSX:
  46. self.xselpath = which("xsel")
  47. config.set_value("Global", "xsel", self.xselpath)
  48. else:
  49. self.xselpath = "xsel"
  50. self.dbver = dbver
  51. self.filename = filename
  52. def clean(self):
  53. if os.path.exists(config.get_value('Database', 'filename')):
  54. os.remove(config.get_value('Database', 'filename'))
  55. if os.path.exists(os.path.join(os.path.dirname(__file__),
  56. 'testing_config')):
  57. os.remove(os.path.join(os.path.dirname(__file__),
  58. 'testing_config'))
  59. def create(self):
  60. dbtype = config.get_value("Database", "type")
  61. if self.filename:
  62. db = factory.create(dbtype, self.dbver, self.filename)
  63. else:
  64. db = factory.create(dbtype, self.dbver)
  65. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)