db_tests.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. from pwman.util.callback import Callback
  2. import os
  3. import os.path
  4. import sys
  5. class DummyCallback(Callback):
  6. def getinput(self, question):
  7. return '12345'
  8. def getsecret(self, question):
  9. return '12345'
  10. class DummyCallback2(Callback):
  11. def getinput(self, question):
  12. return 'newsecret'
  13. def getsecret(self, question):
  14. return 'newsecret'
  15. if 'darwin' in sys.platform: # pragma: no cover
  16. from pwman.ui.mac import PwmanCliMac as PwmanCliOld
  17. from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
  18. OSX = True
  19. elif 'win' in sys.platform: # pragma: no cover
  20. from pwman.ui.cli import PwmanCli
  21. from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
  22. OSX = False
  23. else:
  24. from pwman.ui.cli import PwmanCliOld
  25. from pwman.ui.cli import PwmanCliNew
  26. OSX = False
  27. import pwman.util.config as config
  28. import pwman.data.factory
  29. from pwman.data.nodes import NewNode
  30. from pwman.data.tags import Tag, TagNew
  31. from pwman.util.crypto import CryptoEngine, CryptoBadKeyException
  32. from pwman import which, default_config
  33. import unittest
  34. _saveconfig = False
  35. default_config['Database'] = {'type': 'SQLite',
  36. 'filename':
  37. os.path.join(os.path.dirname(__file__),
  38. "test.pwman.db")
  39. }
  40. class SetupTester(object):
  41. def __init__(self):
  42. config.set_defaults(default_config)
  43. if not OSX:
  44. self.xselpath = which("xsel")
  45. config.set_value("Global", "xsel", self.xselpath)
  46. else:
  47. self.xselpath = "xsel"
  48. def clean(self):
  49. if os.path.exists(config.get_value('Database', 'filename')):
  50. os.remove(config.get_value('Database', 'filename'))
  51. if os.path.exists(os.path.join(os.path.dirname(__file__),
  52. 'testing_config')):
  53. os.remove(os.path.join(os.path.dirname(__file__),
  54. 'testing_config'))
  55. def create(self):
  56. dbver = 0.4
  57. dbtype = config.get_value("Database", "type")
  58. db = pwman.data.factory.create(dbtype, dbver)
  59. self.cli = PwmanCliNew(db, self.xselpath, DummyCallback)
  60. class DBTests(unittest.TestCase):
  61. """test everything related to db"""
  62. def setUp(self):
  63. "test that the right db instance was created"
  64. dbver = 0.4
  65. self.dbtype = config.get_value("Database", "type")
  66. self.db = pwman.data.factory.create(self.dbtype, dbver)
  67. self.tester = SetupTester()
  68. self.tester.create()
  69. def test_db_created(self):
  70. "test that the right db instance was created"
  71. # self.db = pwman.data.factory.create(dbtype, dbver)
  72. self.assertIn(self.dbtype, self.db.__class__.__name__)
  73. def test_db_opened(self):
  74. "db was successfuly opened"
  75. # it will have a file name associated
  76. self.assertTrue(hasattr(self.db, '_filename'))
  77. def test_create_node(self):
  78. "test that a node can be successfuly created"
  79. # this method does not test do_new
  80. # which is a UI method, rather we test
  81. # _db.addnodes
  82. username = 'tester'
  83. password = 'Password'
  84. url = 'example.org'
  85. notes = 'some notes'
  86. node = NewNode(username, password, url, notes)
  87. tags = [Tag(tn) for tn in ['testing1', 'testing2']]
  88. node.tags = tags
  89. self.db.open()
  90. self.db.addnodes([node])
  91. idx_created = node._id
  92. new_node = self.db.getnodes([idx_created])[0]
  93. for key, attr in {'password': password, 'username': username,
  94. 'url': url, 'notes': notes}.iteritems():
  95. self.assertEquals(attr, getattr(new_node, key))
  96. self.db.close()
  97. def test_tags(self):
  98. enc = CryptoEngine.get()
  99. got_tags = self.tester.cli._tags(enc)
  100. self.assertEqual(2, len(got_tags))
  101. def test_change_pass(self):
  102. self.tester.cli.callback = DummyCallback2
  103. self.assertRaises(CryptoBadKeyException,
  104. self.tester.cli._db.changepassword)
  105. class CLITests(unittest.TestCase):
  106. """
  107. test command line functionallity
  108. """
  109. def setUp(self):
  110. "test that the right db instance was created"
  111. dbver = 0.4
  112. self.dbtype = config.get_value("Database", "type")
  113. self.db = pwman.data.factory.create(self.dbtype, dbver)
  114. self.tester = SetupTester()
  115. self.tester.create()
  116. def test_input(self):
  117. name = self.tester.cli.get_username(reader=lambda: u'alice')
  118. self.assertEqual(name, u'alice')
  119. def test_password(self):
  120. password = self.tester.cli.get_password(None,
  121. reader=lambda x: u'hatman')
  122. self.assertEqual(password, u'hatman')
  123. def test_random_password(self):
  124. password = self.tester.cli.get_password(None, length=7)
  125. self.assertEqual(len(password), 7)
  126. def test_get_url(self):
  127. url = self.tester.cli.get_url(reader=lambda: u'example.com')
  128. self.assertEqual(url, u'example.com')
  129. def test_get_notes(self):
  130. notes = self.tester.cli.get_notes(reader=lambda:
  131. u'test 123\n test 456')
  132. self.assertEqual(notes, u'test 123\n test 456')
  133. def test_get_tags(self):
  134. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  135. for t in tags:
  136. self.assertIsInstance(t, TagNew)
  137. for t, n in zip(tags, 'looking glass'.split()):
  138. self.assertEqual(t.name.strip(), n)
  139. # creating all the components of the node does
  140. # the node is still not added !
  141. def test_add_new_entry(self):
  142. node = NewNode('alice', 'dough!', 'example.com',
  143. 'lorem impsum')
  144. tags = self.tester.cli.get_tags(reader=lambda: u'looking glass')
  145. node.tags = tags
  146. self.tester.cli._db.addnodes([node])
  147. self.tester.cli._db._cur.execute(
  148. "SELECT ID FROM NODES ORDER BY ID ASC", [])
  149. rows = self.tester.cli._db._cur.fetchall()
  150. # by now the db should have 2 new nodes
  151. # the first one was added by test_create_node in DBTests
  152. # the second was added just now.
  153. # This will pass only when running all the tests than ...
  154. self.assertEqual(len(rows), 2)
  155. def test_get_ids(self):
  156. #used by do_cp or do_open,
  157. # this spits many time could not understand your input
  158. self.assertEqual([1], self.tester.cli.get_ids('1'))
  159. self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli.get_ids('1-5'))
  160. self.assertListEqual([], self.tester.cli.get_ids('5-1'))
  161. self.assertListEqual([], self.tester.cli.get_ids('5x-1'))
  162. self.assertListEqual([], self.tester.cli.get_ids('5x'))
  163. self.assertListEqual([], self.tester.cli.get_ids('5\\'))
  164. class ConfigTest(unittest.TestCase):
  165. def setUp(self):
  166. "test that the right db instance was created"
  167. dbver = 0.4
  168. self.dbtype = config.get_value("Database", "type")
  169. self.db = pwman.data.factory.create(self.dbtype, dbver)
  170. self.tester = SetupTester()
  171. self.tester.create()
  172. def test_config_write(self):
  173. config.save(os.path.join(os.path.dirname(__file__), 'testing_config'))
  174. def test_config_write_with_none(self):
  175. config._file = os.path.join(os.path.dirname(__file__),
  176. 'testing_config')
  177. config.save()
  178. def test_write_no_permission(self):
  179. # this test will pass if you run as root ...
  180. # assuming you are not doing something like that
  181. self.assertRaises(config.ConfigException, config.save,
  182. '/root/test_config')
  183. def test_add_default(self):
  184. config.add_defaults({'Section1': {'name': 'value'}})
  185. self.assertIn('Section1', config._defaults)