test_tools.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.ui import get_ui_platform
  12. from pwman.util.callback import Callback
  13. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  14. class DummyCallback(Callback):
  15. def getinput(self, question):
  16. return u'12345'
  17. def getsecret(self, question):
  18. return u'12345'
  19. class DummyCallback2(Callback):
  20. def getinput(self, question):
  21. return u'newsecret'
  22. def getsecret(self, question):
  23. return u'wrong'
  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. class DummyCallback4(Callback):
  31. def getinput(self, question):
  32. return u'newsecret'
  33. def getsecret(self, question):
  34. return u'newsecret'
  35. config.default_config['Database'] = {'type': 'SQLite',
  36. 'filename':
  37. os.path.join(os.path.dirname(__file__),
  38. "test.pwman.db"),
  39. 'dburi': os.path.join(
  40. 'sqlite:///',
  41. os.path.dirname(__file__),
  42. "test.pwman.db")
  43. }
  44. dc = """
  45. [Global]
  46. xsel = /usr/bin/xsel
  47. colors = yes
  48. umask = 0100
  49. cls_timeout = 5
  50. [Database]
  51. type = SQLite
  52. """
  53. class SetupTester(object):
  54. def __init__(self, dbver=None, filename=None, dburi=None):
  55. f = open(os.path.join(os.path.dirname(__file__), 'test.conf'), 'w')
  56. f.write(dc)
  57. f.close()
  58. self.configp = config.Config(os.path.join(os.path.dirname(__file__),
  59. "test.conf"),
  60. config.default_config)
  61. self.configp.set_value('Database', 'filename',
  62. os.path.join(os.path.dirname(__file__),
  63. "test.pwman.db"))
  64. self.configp.set_value('Database', 'dburi',
  65. os.path.join('sqlite:///',
  66. os.path.dirname(__file__),
  67. "test.pwman.db"))
  68. if not OSX:
  69. self.xselpath = which("xsel")
  70. self.configp.set_value("Global", "xsel", self.xselpath)
  71. else:
  72. self.xselpath = "xsel"
  73. self.dbver = dbver
  74. self.filename = filename
  75. self.dburi = dburi
  76. def clean(self):
  77. dbfile = self.configp.get_value('Database', 'filename')
  78. dburi = urlparse(self.configp.get_value('Database', 'dburi')).path
  79. if os.path.exists(dbfile):
  80. os.remove(dbfile)
  81. if os.path.exists(dburi):
  82. os.remove(dburi)
  83. if os.path.exists(os.path.join(os.path.dirname(__file__),
  84. 'testing_config')):
  85. os.remove(os.path.join(os.path.dirname(__file__),
  86. 'testing_config'))
  87. if os.path.exists(os.path.join(os.path.dirname(__file__),
  88. 'test.conf')):
  89. os.remove(os.path.join(os.path.dirname(__file__),
  90. 'test.conf'))
  91. def create(self):
  92. dbtype = 'SQLite'
  93. db = factory.create(dbtype, self.dbver, self.filename)
  94. #db = factory.createdb(self.dburi, self.dbver)
  95. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
  96. config_parser=self.configp)