db_tests.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
  18. # ============================================================================
  19. from pwman.data import factory
  20. #from pwman.data.drivers.sqlite import DatabaseException
  21. from pwman.util.config import get_pass_conf
  22. #from pwman.util.generator import leetlist
  23. from pwman.util.crypto_engine import CryptoEngine
  24. from pwman.ui.tools import CMDLoop, CliMenuItem
  25. # from pwman import (parser_options, get_conf_options, get_conf, set_umask)
  26. from pwman.data.database import __DB_FORMAT__
  27. import sys
  28. import unittest
  29. if sys.version_info.major > 2:
  30. from io import StringIO
  31. else:
  32. from StringIO import StringIO
  33. import os
  34. import os.path
  35. dummyfile = """
  36. [Encryption]
  37. [Readline]
  38. [Global]
  39. xsel = /usr/bin/xsel
  40. colors = yes
  41. umask = 0100
  42. cls_timeout = 5
  43. [Database]
  44. """
  45. _saveconfig = False
  46. from .test_tools import (SetupTester) # DummyCallback2,
  47. # DummyCallback3, DummyCallback4)
  48. db = ".".join(("pwman","test", sys.version.split(" " ,1)[0], "db"))
  49. testdb = os.path.abspath(os.path.join(os.path.dirname(__file__), db))
  50. class CLITests(unittest.TestCase):
  51. """
  52. test command line functionallity
  53. """
  54. def setUp(self):
  55. "test that the right db instance was created"
  56. self.dbtype = 'SQLite'
  57. self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
  58. self.tester = SetupTester(__DB_FORMAT__, testdb)
  59. self.tester.create()
  60. def test_input(self):
  61. name = self.tester.cli.get_username(reader=lambda: u'alice')
  62. self.assertEqual(name, u'alice')
  63. def test_password(self):
  64. password = self.tester.cli.get_password(None,
  65. reader=lambda x: u'hatman')
  66. self.assertEqual(password, u'hatman')
  67. def test_random_password(self):
  68. password = self.tester.cli.get_password(None, length=7)
  69. self.assertEqual(len(password), 7)
  70. def test_leet_password(self):
  71. password = self.tester.cli.get_password(None, leetify=True,
  72. reader=lambda x: u'HAtman')
  73. # python3 compatability
  74. if sys.version_info.major < 3:
  75. self.assertRegexpMatches(password,
  76. ("(H|h)?(A|a|4)?(T|t|\+)?(m|M|\|"
  77. "\/\|)?(A|a|4)?(N|n|\|\\|)?"))
  78. else:
  79. self.assertRegex(password, ("(H|h)?(A|a|4)?(T|t|\+)?(m|M|\|"
  80. "\/\|)?(A|a|4)?(N|n|\|\\|)?"))
  81. def test_get_url(self):
  82. url = self.tester.cli.get_url(reader=lambda: u'example.com')
  83. self.assertEqual(url, u'example.com')
  84. def test_get_notes(self):
  85. notes = self.tester.cli.get_notes(reader=lambda:
  86. u'test 123\n test 456')
  87. self.assertEqual(notes, u'test 123\n test 456')
  88. def test_get_ids(self):
  89. # used by do_cp or do_open,
  90. # this spits many time could not understand your input
  91. self.assertEqual([1], self.tester.cli.get_ids('1'))
  92. self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli.get_ids('1-5'))
  93. self.assertListEqual([], self.tester.cli.get_ids('5-1'))
  94. self.assertListEqual([], self.tester.cli.get_ids('5x-1'))
  95. self.assertListEqual([], self.tester.cli.get_ids('5x'))
  96. self.assertListEqual([], self.tester.cli.get_ids('5\\'))
  97. def test_edit(self):
  98. node = self.tester.cli._db.getnodes([2])[0]
  99. menu = CMDLoop()
  100. menu.add(CliMenuItem("Username", self.tester.cli.get_username,
  101. node.username,
  102. node.username))
  103. menu.add(CliMenuItem("Password", self.tester.cli.get_password,
  104. node.password,
  105. node.password))
  106. menu.add(CliMenuItem("Url", self.tester.cli.get_url,
  107. node.url,
  108. node.url))
  109. menunotes = CliMenuItem("Notes",
  110. self.tester.cli.get_notes(reader=lambda:
  111. u'bla bla'),
  112. node.notes,
  113. node.notes)
  114. menu.add(menunotes)
  115. menu.add(CliMenuItem("Tags", self.tester.cli.get_tags,
  116. node.tags,
  117. node.tags))
  118. dummy_stdin = StringIO('4\n\nX')
  119. class dummy_stdin(object):
  120. def __init__(self):
  121. self.idx = -1
  122. self.ans = ['4', 'some fucking notes', 'X']
  123. def __call__(self, msg):
  124. self.idx += 1
  125. return self.ans[self.idx]
  126. dstin = dummy_stdin()
  127. menu.run(node, reader=dstin)
  128. self.tester.cli._db.editnode(2, node)
  129. def test_get_pass_conf(self):
  130. numerics, leet, s_chars = get_pass_conf(self.tester.cli.config)
  131. self.assertFalse(numerics)
  132. self.assertFalse(leet)
  133. self.assertFalse(s_chars)
  134. def test_do_tags(self):
  135. self.tester.cli.do_filter('bank')
  136. def test_do_forget(self):
  137. self.tester.cli.do_forget('')
  138. def test_do_auth(self):
  139. crypto = CryptoEngine.get()
  140. rv = crypto.authenticate('12345')
  141. self.assertTrue(rv)
  142. self.assertFalse(crypto.authenticate('WRONG'))
  143. def test_do_clear(self):
  144. self.tester.cli.do_clear('')
  145. def test_do_exit(self):
  146. self.assertTrue(self.tester.cli.do_exit(''))
  147. class FactoryTest(unittest.TestCase):
  148. def test_factory_check_db_ver(self):
  149. self.assertEqual(factory.check_db_version('SQLite', testdb), 0.5)
  150. def test_factory_check_db_file(self):
  151. factory.create('SQLite', version='0.3', filename='baz.db')
  152. self.assertEqual(factory.check_db_version('SQLite', 'baz.db'), 0.3)
  153. os.unlink('baz.db')
  154. #def test_factory_create(self):
  155. # db = factory.create('SQLite', filename='foo.db')
  156. # db._open()
  157. # self.assertTrue(os.path.exists('foo.db'))
  158. # db.close()
  159. # os.unlink('foo.db')
  160. # self.assertIsInstance(db, SQLiteDatabaseNewForm)
  161. # self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
  162. if __name__ == '__main__':
  163. # make sure we use local pwman
  164. sys.path.insert(0, os.getcwd())
  165. # check if old DB exists, if so remove it.
  166. # excuted only once when invoked upon import or
  167. # upon run
  168. SetupTester().clean()
  169. unittest.main(verbosity=1, failfast=True)