db_tests.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, TagNew
  20. from pwman.util.crypto import CryptoEngine
  21. from pwman import which, default_config
  22. import unittest
  23. _saveconfig = False
  24. default_config['Database'] = {'type': 'SQLite',
  25. 'filename':
  26. os.path.join(os.path.dirname(__file__),
  27. "test.pwman.db")
  28. }
  29. class SetupTester(object):
  30. def __init__(self):
  31. config.set_defaults(default_config)
  32. if not OSX:
  33. self.xselpath = which("xsel")
  34. config.set_value("Global", "xsel", self.xselpath)
  35. else:
  36. self.xselpath = "xsel"
  37. def clean(self):
  38. if os.path.exists(config.get_value('Database', 'filename')):
  39. os.remove(config.get_value('Database', 'filename'))
  40. def create(self):
  41. dbver = 0.4
  42. dbtype = config.get_value("Database", "type")
  43. db = pwman.data.factory.create(dbtype, dbver)
  44. self.cli = PwmanCliNew(db, self.xselpath)
  45. class DBTests(unittest.TestCase):
  46. """test everything related to db"""
  47. def setUp(self):
  48. "test that the right db instance was created"
  49. dbver = 0.4
  50. self.dbtype = config.get_value("Database", "type")
  51. self.db = pwman.data.factory.create(self.dbtype, dbver)
  52. self.tester = SetupTester()
  53. self.tester.create()
  54. def test_db_created(self):
  55. "test that the right db instance was created"
  56. # self.db = pwman.data.factory.create(dbtype, dbver)
  57. self.assertIn(self.dbtype, self.db.__class__.__name__)
  58. def test_db_opened(self):
  59. "db was successfuly opened"
  60. # it will have a file name associated
  61. self.assertTrue(hasattr(self.db, '_filename'))
  62. def test_create_node(self):
  63. "test that a node can be successfuly created"
  64. # this method does not test do_new
  65. # which is a UI method, rather we test
  66. # _db.addnodes
  67. username = 'tester'
  68. password = 'Password'
  69. url = 'example.org'
  70. notes = 'some notes'
  71. node = NewNode(username, password, url, notes)
  72. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  73. node.tags = tags
  74. self.db.open()
  75. self.db.addnodes([node])
  76. idx_created = node._id
  77. new_node = self.db.getnodes([idx_created])[0]
  78. for key, attr in {'password': password, 'username': username,
  79. 'url': url, 'notes': notes}.iteritems():
  80. self.assertEquals(attr, getattr(new_node, key))
  81. self.db.close()
  82. def test_tags(self):
  83. enc = CryptoEngine.get()
  84. got_tags = self.tester.cli._tags(enc)
  85. self.assertEqual(2, len(got_tags))
  86. class CLITests(unittest.TestCase):
  87. """
  88. test command line functionallity
  89. """
  90. def setUp(self):
  91. "test that the right db instance was created"
  92. dbver = 0.4
  93. self.dbtype = config.get_value("Database", "type")
  94. self.db = pwman.data.factory.create(self.dbtype, dbver)
  95. self.tester = SetupTester()
  96. self.tester.create()
  97. def test_input(self):
  98. name = self.tester.cli.get_username(reader=lambda: u'alice')
  99. self.assertEqual(name, u'alice')
  100. def test_password(self):
  101. password = self.tester.cli.get_password(None,
  102. reader=lambda x: u'hatman')
  103. self.assertEqual(password, u'hatman')
  104. def test_get_url(self):
  105. url = self.tester.cli.get_url(reader=lambda: u'example.com')
  106. self.assertEqual(url, u'example.com')
  107. def test_get_notes(self):
  108. notes = self.tester.cli.get_notes(reader=lambda:
  109. u'test 123\n test 456')
  110. self.assertEqual(notes, u'test 123\n test 456')
  111. def test_get_tags(self):
  112. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  113. for t in tags:
  114. self.assertIsInstance(t, TagNew)
  115. for t, n in zip(tags, 'looking glass'.split()):
  116. self.assertEqual(t.name.strip(), n)
  117. # creating all the components of the node does
  118. # the node is still not added !
  119. def test_add_new_entry(self):
  120. node = NewNode('alice', 'dough!', 'example.com',
  121. 'lorem impsum')
  122. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  123. node.tags = tags
  124. self.tester.cli._db.addnodes([node])
  125. self.tester.cli._db._cur.execute(
  126. "SELECT ID FROM NODES ORDER BY ID ASC", [])
  127. rows = self.tester.cli._db._cur.fetchall()
  128. # by now the db should have 2 new nodes
  129. # the first one was added by test_create_node in DBTests
  130. # the second was added just now.
  131. # This will pass only when running all the tests than ...
  132. self.assertEqual(len(rows), 2)
  133. #TODO: implement a test for get_ids