db_tests.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import os
  2. import os.path
  3. import sys
  4. if 'darwin' in sys.platform:
  5. from pwman.ui.mac import PwmanCliMac as PwmanCliOld
  6. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  7. OSX = True
  8. elif 'win' in sys.platform:
  9. from pwman.ui.cli import PwmanCli
  10. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  11. OSX = False
  12. else:
  13. from pwman.ui.cli import PwmanCliOld
  14. from pwman.ui.cli import PwmanCliNew
  15. OSX = False
  16. import pwman.util.config as config
  17. import pwman.data.factory
  18. from pwman.data.nodes import NewNode
  19. from pwman.data.tags import Tag
  20. from pwman.util.crypto import CryptoEngine
  21. import unittest
  22. def which(cmd):
  23. _, cmdname = os.path.split(cmd)
  24. for path in os.environ["PATH"].split(os.pathsep):
  25. cmd = os.path.join(path, cmdname)
  26. if os.path.isfile(cmd) and os.access(cmd, os.X_OK):
  27. return cmd
  28. return None
  29. _saveconfig = False
  30. default_config = {'Global': {'umask': '0100', 'colors': 'yes',
  31. 'cls_timeout': '5'
  32. },
  33. 'Database': {'type': 'SQLite',
  34. 'filename':
  35. os.path.join(os.path.dirname(__file__),
  36. "test.pwman.db")},
  37. 'Encryption': {'algorithm': 'AES'},
  38. 'Readline': {'history': os.path.join("tests",
  39. "history")}
  40. }
  41. class SetupTester(object):
  42. def __init__(self):
  43. config.set_defaults(default_config)
  44. if not OSX:
  45. self.xselpath = which("xsel")
  46. config.set_value("Global", "xsel", self.xselpath)
  47. else:
  48. self.xselpath = "xsel"
  49. def clean(self):
  50. if os.path.exists(config.get_value('Database', 'filename')):
  51. os.remove(config.get_value('Database', 'filename'))
  52. def create(self):
  53. dbver = 0.4
  54. dbtype = config.get_value("Database", "type")
  55. db = pwman.data.factory.create(dbtype, dbver)
  56. self.cli = PwmanCliNew(db, self.xselpath)
  57. class DBTests(unittest.TestCase):
  58. """test everything related to db"""
  59. def setUp(self):
  60. "test that the right db instance was created"
  61. dbver = 0.4
  62. self.dbtype = config.get_value("Database", "type")
  63. self.db = pwman.data.factory.create(self.dbtype, dbver)
  64. self.tester = SetupTester()
  65. self.tester.create()
  66. def test_db_created(self):
  67. "test that the right db instance was created"
  68. # self.db = pwman.data.factory.create(dbtype, dbver)
  69. self.assertIn(self.dbtype, self.db.__class__.__name__)
  70. def test_db_opened(self):
  71. "db was successfuly opened"
  72. # it will have a file name associated
  73. self.assertTrue(hasattr(self.db, '_filename'))
  74. def test_create_node(self):
  75. "test that a node can be successfuly created"
  76. # this method does not test do_new
  77. # which is a UI method, rather we test
  78. # _db.addnodes
  79. username = 'tester'
  80. password = 'Password'
  81. url = 'example.org'
  82. notes = 'some notes'
  83. node = NewNode(username, password, url, notes)
  84. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  85. node.tags = tags
  86. self.db.open()
  87. self.db.addnodes([node])
  88. idx_created = node._id
  89. new_node = self.db.getnodes([idx_created])[0]
  90. for key, attr in {'password': password, 'username': username,
  91. 'url': url, 'notes': notes}.iteritems():
  92. self.assertEquals(attr, eval('new_node.' + key))
  93. self.db.close()
  94. def test_tags(self):
  95. enc = CryptoEngine.get()
  96. got_tags = self.tester.cli._tags(enc)
  97. self.assertEqual(2, len(got_tags))
  98. #def tearDown(self):
  99. # self.tester.clean()
  100. class CLITests(unittest.TestCase):
  101. """test command line functionallity"""
  102. def test(self):
  103. self.assertTrue(True)