db_tests.py 8.4 KB

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