test_tools.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import os.path
  3. import sys
  4. if sys.version_info.major > 2: # pragma: no cover
  5. from urllib.parse import urlparse
  6. else:
  7. from urlparse import urlparse
  8. from pwman.data import factory
  9. from pwman.util import config
  10. from pwman import which
  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. config.default_config['Database'] = {'type': 'sqlite',
  46. 'filename':
  47. os.path.join(os.path.dirname(__file__),
  48. "test.pwman.db"),
  49. 'dburi': os.path.join(
  50. 'sqlite:///',
  51. os.path.dirname(__file__),
  52. "test.pwman.db")
  53. }
  54. dc = """
  55. [Global]
  56. xsel = /usr/bin/xsel
  57. colors = yes
  58. umask = 0100
  59. cls_timeout = 5
  60. [Database]
  61. type = SQLite
  62. """
  63. class SetupTester(object):
  64. def __init__(self, dbver=None, filename=None, dburi=None):
  65. f = open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w')
  66. f.write(dc)
  67. f.close()
  68. self.configp = config.Config(os.path.join(os.path.dirname(__file__),
  69. "test.conf"),
  70. config.default_config)
  71. self.configp.set_value('Database', 'dburi',
  72. 'sqlite://' + os.path.join(
  73. os.path.abspath(os.path.dirname(__file__)),
  74. "test.pwman.db")
  75. )
  76. if not OSX:
  77. self.xselpath = which("xsel")
  78. self.configp.set_value("Global", "xsel", self.xselpath)
  79. else:
  80. self.xselpath = "xsel"
  81. self.dbver = dbver
  82. self.dburi = self.configp.get_value('Database', 'dburi')
  83. def clean(self):
  84. dbfile = self.configp.get_value('Database', 'filename')
  85. dburi = urlparse(self.configp.get_value('Database', 'dburi')).path
  86. if os.path.exists(dbfile):
  87. os.remove(dbfile)
  88. if os.path.exists(dburi):
  89. os.remove(dburi)
  90. if os.path.exists(os.path.join(os.path.dirname(__file__),
  91. 'testing_config')):
  92. os.remove(os.path.join(os.path.dirname(__file__),
  93. 'testing_config'))
  94. if os.path.exists(os.path.join(os.path.dirname(__file__),
  95. 'test.conf')):
  96. os.remove(os.path.join(os.path.dirname(__file__),
  97. 'test.conf'))
  98. def create(self):
  99. db = factory.createdb(self.dburi, self.dbver)
  100. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
  101. config_parser=self.configp)