test_tools.py 2.4 KB

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