db_tests.py 6.6 KB

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