test_tools.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class DummyCallback2(Callback):
  16. def getinput(self, question):
  17. return u'newsecret'
  18. def getsecret(self, question):
  19. return u'wrong'
  20. class DummyCallback3(Callback):
  21. def getinput(self, question):
  22. return u'newsecret'
  23. def getsecret(self, question):
  24. ans = '12345'
  25. return ans
  26. class DummyCallback4(Callback):
  27. def getinput(self, question):
  28. return u'newsecret'
  29. def getsecret(self, question):
  30. return u'newsecret'
  31. default_config['Database'] = {'type': 'SQLite',
  32. 'filename':
  33. os.path.join(os.path.dirname(__file__),
  34. "test.pwman.db")
  35. }
  36. with open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w') as f:
  37. f.write("""
  38. [Global]
  39. xsel = /usr/bin/xsel
  40. colors = yes
  41. umask = 0100
  42. cls_timeout = 5
  43. [Database]
  44. type = SQLite
  45. """)
  46. class SetupTester(object):
  47. def __init__(self, dbver=None, filename=None):
  48. self.configp = config.Config(os.path.join(os.path.dirname(__file__),
  49. "test.conf"),
  50. default_config)
  51. self.configp.set_value('Database', 'filename',
  52. os.path.join(os.path.dirname(__file__),
  53. "test.pwman.db"))
  54. if not OSX:
  55. self.xselpath = which("xsel")
  56. self.configp.set_value("Global", "xsel", self.xselpath)
  57. else:
  58. self.xselpath = "xsel"
  59. self.dbver = dbver
  60. self.filename = filename
  61. def clean(self):
  62. if os.path.exists(self.configp.get_value('Database', 'filename')):
  63. os.remove(self.configp.get_value('Database', 'filename'))
  64. if os.path.exists(os.path.join(os.path.dirname(__file__),
  65. 'testing_config')):
  66. os.remove(os.path.join(os.path.dirname(__file__),
  67. 'testing_config'))
  68. def create(self):
  69. dbtype = 'SQLite'
  70. db = factory.create(dbtype, self.dbver, self.filename)
  71. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
  72. config_parser=self.configp)