test_tools.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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', 'dburi',
  62. 'sqlite://' + os.path.join(os.path.abspath(
  63. os.path.dirname(__file__)),
  64. "test.pwman.db")
  65. )
  66. if not OSX:
  67. self.xselpath = which("xsel")
  68. self.configp.set_value("Global", "xsel", self.xselpath)
  69. else:
  70. self.xselpath = "xsel"
  71. self.dbver = dbver
  72. self.dburi = self.configp.get_value('Database', 'dburi')
  73. def clean(self):
  74. dbfile = self.configp.get_value('Database', 'filename')
  75. dburi = urlparse(self.configp.get_value('Database', 'dburi')).path
  76. if os.path.exists(dbfile):
  77. os.remove(dbfile)
  78. if os.path.exists(dburi):
  79. os.remove(dburi)
  80. if os.path.exists(os.path.join(os.path.dirname(__file__),
  81. 'testing_config')):
  82. os.remove(os.path.join(os.path.dirname(__file__),
  83. 'testing_config'))
  84. if os.path.exists(os.path.join(os.path.dirname(__file__),
  85. 'test.conf')):
  86. os.remove(os.path.join(os.path.dirname(__file__),
  87. 'test.conf'))
  88. def create(self):
  89. db = factory.createdb(self.dburi, self.dbver)
  90. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback,
  91. config_parser=self.configp)