db_tests.py 5.9 KB

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