db_tests.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import os
  2. import os.path
  3. import sys
  4. from pwman.ui.tools import DummyCallback, DummyCallback2
  5. if 'darwin' in sys.platform: # pragma: no cover
  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: # pragma: no cover
  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, CryptoBadKeyException
  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__), "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. if os.path.exists(os.path.join(os.path.dirname(__file__),
  41. 'testing_config')):
  42. os.remove(os.path.join(os.path.dirname(__file__),
  43. 'testing_config'))
  44. def create(self):
  45. dbver = 0.4
  46. dbtype = config.get_value("Database", "type")
  47. db = pwman.data.factory.create(dbtype, dbver)
  48. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)
  49. class DBTests(unittest.TestCase):
  50. """test everything related to db"""
  51. def setUp(self):
  52. "test that the right db instance was created"
  53. dbver = 0.4
  54. self.dbtype = config.get_value("Database", "type")
  55. self.db = pwman.data.factory.create(self.dbtype, dbver)
  56. self.tester = SetupTester()
  57. self.tester.create()
  58. def test_db_created(self):
  59. "test that the right db instance was created"
  60. # self.db = pwman.data.factory.create(dbtype, dbver)
  61. self.assertIn(self.dbtype, self.db.__class__.__name__)
  62. def test_db_opened(self):
  63. "db was successfuly opened"
  64. # it will have a file name associated
  65. self.assertTrue(hasattr(self.db, '_filename'))
  66. def test_create_node(self):
  67. "test that a node can be successfuly created"
  68. # this method does not test do_new
  69. # which is a UI method, rather we test
  70. # _db.addnodes
  71. username = 'tester'
  72. password = 'Password'
  73. url = 'example.org'
  74. notes = 'some notes'
  75. node = NewNode(username, password, url, notes)
  76. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  77. node.tags = tags
  78. self.db.open()
  79. self.db.addnodes([node])
  80. idx_created = node._id
  81. new_node = self.db.getnodes([idx_created])[0]
  82. for key, attr in {'password': password, 'username': username,
  83. 'url': url, 'notes': notes}.iteritems():
  84. self.assertEquals(attr, getattr(new_node, key))
  85. self.db.close()
  86. def test_tags(self):
  87. enc = CryptoEngine.get()
  88. got_tags = self.tester.cli._tags(enc)
  89. self.assertEqual(2, len(got_tags))
  90. def test_change_pass(self):
  91. self.tester.cli.callback = DummyCallback2
  92. self.assertRaises(CryptoBadKeyException,
  93. self.tester.cli._db.changepassword)
  94. class CLITests(unittest.TestCase):
  95. """
  96. test command line functionallity
  97. """
  98. def setUp(self):
  99. "test that the right db instance was created"
  100. dbver = 0.4
  101. self.dbtype = config.get_value("Database", "type")
  102. self.db = pwman.data.factory.create(self.dbtype, dbver)
  103. self.tester = SetupTester()
  104. self.tester.create()
  105. def test_input(self):
  106. name = self.tester.cli.get_username(reader=lambda: u'alice')
  107. self.assertEqual(name, u'alice')
  108. def test_password(self):
  109. password = self.tester.cli.get_password(None,
  110. reader=lambda x: u'hatman')
  111. self.assertEqual(password, u'hatman')
  112. def test_get_url(self):
  113. url = self.tester.cli.get_url(reader=lambda: u'example.com')
  114. self.assertEqual(url, u'example.com')
  115. def test_get_notes(self):
  116. notes = self.tester.cli.get_notes(reader=lambda:
  117. u'test 123\n test 456')
  118. self.assertEqual(notes, u'test 123\n test 456')
  119. def test_get_tags(self):
  120. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  121. for t in tags:
  122. self.assertIsInstance(t, TagNew)
  123. for t, n in zip(tags, 'looking glass'.split()):
  124. self.assertEqual(t.name.strip(), n)
  125. # creating all the components of the node does
  126. # the node is still not added !
  127. def test_add_new_entry(self):
  128. node = NewNode('alice', 'dough!', 'example.com',
  129. 'lorem impsum')
  130. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  131. node.tags = tags
  132. self.tester.cli._db.addnodes([node])
  133. self.tester.cli._db._cur.execute(
  134. "SELECT ID FROM NODES ORDER BY ID ASC", [])
  135. rows = self.tester.cli._db._cur.fetchall()
  136. # by now the db should have 2 new nodes
  137. # the first one was added by test_create_node in DBTests
  138. # the second was added just now.
  139. # This will pass only when running all the tests than ...
  140. self.assertEqual(len(rows), 2)
  141. def test_get_ids(self):
  142. #used by do_cp or do_open
  143. self.assertEqual([1], self.tester.cli.get_ids('1'))
  144. self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli.get_ids('1-5'))
  145. self.assertListEqual([], self.tester.cli.get_ids('5-1'))
  146. self.assertListEqual([], self.tester.cli.get_ids('5x-1'))
  147. self.assertListEqual([], self.tester.cli.get_ids('5x'))
  148. self.assertListEqual([], self.tester.cli.get_ids('5\\'))
  149. class ConfigTest(unittest.TestCase):
  150. def setUp(self):
  151. "test that the right db instance was created"
  152. dbver = 0.4
  153. self.dbtype = config.get_value("Database", "type")
  154. self.db = pwman.data.factory.create(self.dbtype, dbver)
  155. self.tester = SetupTester()
  156. self.tester.create()
  157. def test_config_write(self):
  158. config.save(os.path.join(os.path.dirname(__file__), 'testing_config'))
  159. def test_config_write_with_none(self):
  160. config._file = os.path.join(os.path.dirname(__file__),
  161. 'testing_config')
  162. config.save()
  163. def test_write_no_permission(self):
  164. # this test will pass if you run as root ...
  165. # assuming you are not doing something like that
  166. self.assertRaises(config.ConfigException, config.save,
  167. '/root/test_config')
  168. def test_add_default(self):
  169. config.add_defaults({'Section1': {'name': 'value'}})
  170. self.assertIn('Section1', config._defaults)