test_tools.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import os.path
  3. import shutil
  4. import sys
  5. if sys.version_info.major > 2: # pragma: no cover
  6. from urllib.parse import urlparse
  7. else:
  8. from urlparse import urlparse
  9. from pwman.data import factory
  10. from pwman.util import config
  11. from pwman.util.callback import Callback
  12. def get_ui_platform(platform): # pragma: no cover
  13. if 'darwin' in platform:
  14. from pwman.ui.mac import PwmanCliMac as PwmanCli
  15. OSX = True
  16. elif 'win' in platform:
  17. from pwman.ui.win import PwmanCliWin as PwmanCli
  18. OSX = False
  19. else:
  20. from pwman.ui.cli import PwmanCli
  21. OSX = False
  22. return PwmanCli, OSX
  23. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  24. class DummyCallback(Callback):
  25. def getinput(self, question):
  26. return b'12345'
  27. def getsecret(self, question):
  28. return b'12345'
  29. class DummyCallback2(Callback):
  30. def getinput(self, question):
  31. return b'newsecret'
  32. def getsecret(self, question):
  33. return b'wrong'
  34. class DummyCallback3(Callback):
  35. def getinput(self, question):
  36. return b'newsecret'
  37. def getsecret(self, question):
  38. ans = b'12345'
  39. return ans
  40. class DummyCallback4(Callback):
  41. def getinput(self, question):
  42. return b'newsecret'
  43. def getsecret(self, question):
  44. return b'newsecret'
  45. db = ".".join(("pwman","test", sys.version.split(" " ,1)[0], "db"))
  46. testdb = os.path.abspath(os.path.join(os.path.dirname(__file__), db))
  47. config.default_config['Database'] = {'type': 'sqlite',
  48. 'filename': testdb,
  49. 'dburi': os.path.join(
  50. 'sqlite:///', testdb)
  51. }
  52. dc = """
  53. [Global]
  54. xsel = /usr/bin/xsel
  55. colors = yes
  56. umask = 0100
  57. cls_timeout = 5
  58. [Database]
  59. type = SQLite
  60. """
  61. class SetupTester(object):
  62. def __init__(self, dbver=None, filename=None, dburi=None):
  63. f = open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w')
  64. f.write(dc)
  65. f.close()
  66. self.configp = config.Config(os.path.join(os.path.dirname(__file__),
  67. "test.conf"),
  68. config.default_config)
  69. self.configp.set_value('Database', 'dburi',
  70. 'sqlite://' + testdb
  71. )
  72. if not OSX:
  73. self.xselpath = shutil.which("xsel") or ""
  74. self.configp.set_value("Global", "xsel", self.xselpath)
  75. else:
  76. self.xselpath = "xsel"
  77. self.dbver = dbver
  78. self.dburi = self.configp.get_value('Database', 'dburi')
  79. def clean(self):
  80. dbfile = self.configp.get_value('Database', 'filename')
  81. dburi = urlparse(self.configp.get_value('Database', 'dburi')).path
  82. try:
  83. if os.path.exists(dbfile):
  84. os.remove(dbfile)
  85. if os.path.exists(dburi):
  86. os.remove(dburi)
  87. except PermissionError:
  88. pass
  89. if os.path.exists(os.path.join(os.path.dirname(__file__),
  90. 'testing_config')):
  91. os.remove(os.path.join(os.path.dirname(__file__),
  92. 'testing_config'))
  93. if os.path.exists(os.path.join(os.path.dirname(__file__),
  94. 'test.conf')):
  95. os.remove(os.path.join(os.path.dirname(__file__),
  96. 'test.conf'))
  97. def create(self):
  98. db = factory.createdb(self.dburi, self.dbver)
  99. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
  100. config_parser=self.configp)