test_tools.py 2.4 KB

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