db_tests.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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': os.path.join("tests", "pwman.db")},
  35. 'Encryption': {'algorithm': 'AES'},
  36. 'Readline': {'history': os.path.join("tests",
  37. "history")}
  38. }
  39. class SetupTester(object):
  40. def __init__(self):
  41. config.set_defaults(default_config)
  42. if not OSX:
  43. xselpath = which("xsel")
  44. config.set_value("Global", "xsel", xselpath)
  45. else:
  46. xselpath = "xsel"
  47. dbver = 0.4
  48. dbtype = config.get_value("Database", "type")
  49. db = pwman.data.factory.create(dbtype, dbver)
  50. self.cli = PwmanCliNew(db, xselpath)
  51. class DBTests(unittest.TestCase):
  52. """test everything related to db"""
  53. def setUp(self):
  54. "test that the right db instance was created"
  55. dbver = 0.4
  56. self.dbtype = config.get_value("Database", "type")
  57. self.db = pwman.data.factory.create(self.dbtype, dbver)
  58. self.tester = SetupTester()
  59. def test_db_created(self):
  60. "test that the right db instance was created"
  61. # self.db = pwman.data.factory.create(dbtype, dbver)
  62. self.assertIn(self.dbtype, self.db.__class__.__name__)
  63. def test_db_opened(self):
  64. "db was successfuly opened"
  65. # it will have a file name associated
  66. self.assertTrue(hasattr(self.db, '_filename'))
  67. def test_create_node(self):
  68. "test that a node can be successfuly created"
  69. # this method does not test do_new
  70. # which is a UI method, rather we test
  71. # _db.addnodes
  72. username = 'tester'
  73. password = 'Password'
  74. url = 'example.org'
  75. notes = 'some notes'
  76. node = NewNode(username, password, url, notes)
  77. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  78. node.tags = tags
  79. self.db.open()
  80. self.db.addnodes([node])
  81. idx_created = node._id
  82. new_node = self.db.getnodes([idx_created])[0]
  83. for key, attr in {'password': password, 'username': username,
  84. 'url': url, 'notes': notes}.iteritems():
  85. self.assertEquals(attr, eval('new_node.' + key))
  86. self.db.close()
  87. def test_tags(self):
  88. enc = CryptoEngine.get()
  89. got_tags = self.tester.cli._tags(enc)
  90. self.assertEqual(2, len(got_tags))
  91. class CLITests(unittest.TestCase):
  92. """test command line functionallity"""
  93. def test(self):
  94. self.assertTrue(True)