db_tests.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. testdb = os.path.join(os.path.dirname(__file__), "test.pwman.db")
  49. class CLITests(unittest.TestCase):
  50. """
  51. test command line functionallity
  52. """
  53. def setUp(self):
  54. "test that the right db instance was created"
  55. self.dbtype = 'SQLite'
  56. self.db = factory.create(self.dbtype, __DB_FORMAT__, testdb)
  57. self.tester = SetupTester(__DB_FORMAT__, testdb)
  58. self.tester.create()
  59. def test_input(self):
  60. name = self.tester.cli.get_username(reader=lambda: u'alice')
  61. self.assertEqual(name, u'alice')
  62. def test_password(self):
  63. password = self.tester.cli.get_password(None,
  64. reader=lambda x: u'hatman')
  65. self.assertEqual(password, u'hatman')
  66. def test_random_password(self):
  67. password = self.tester.cli.get_password(None, length=7)
  68. self.assertEqual(len(password), 7)
  69. def test_leet_password(self):
  70. password = self.tester.cli.get_password(None, leetify=True,
  71. reader=lambda x: u'HAtman')
  72. # python3 compatability
  73. if sys.version_info.major < 3:
  74. self.assertRegexpMatches(password,
  75. ("(H|h)?(A|a|4)?(T|t|\+)?(m|M|\|"
  76. "\/\|)?(A|a|4)?(N|n|\|\\|)?"))
  77. else:
  78. self.assertRegex(password, ("(H|h)?(A|a|4)?(T|t|\+)?(m|M|\|"
  79. "\/\|)?(A|a|4)?(N|n|\|\\|)?"))
  80. def test_get_url(self):
  81. url = self.tester.cli.get_url(reader=lambda: u'example.com')
  82. self.assertEqual(url, u'example.com')
  83. def test_get_notes(self):
  84. notes = self.tester.cli.get_notes(reader=lambda:
  85. u'test 123\n test 456')
  86. self.assertEqual(notes, u'test 123\n test 456')
  87. def test_get_ids(self):
  88. # used by do_cp or do_open,
  89. # this spits many time could not understand your input
  90. self.assertEqual([1], self.tester.cli.get_ids('1'))
  91. self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli.get_ids('1-5'))
  92. self.assertListEqual([], self.tester.cli.get_ids('5-1'))
  93. self.assertListEqual([], self.tester.cli.get_ids('5x-1'))
  94. self.assertListEqual([], self.tester.cli.get_ids('5x'))
  95. self.assertListEqual([], self.tester.cli.get_ids('5\\'))
  96. def test_edit(self):
  97. node = self.tester.cli._db.getnodes([2])[0]
  98. menu = CMDLoop()
  99. menu.add(CliMenuItem("Username", self.tester.cli.get_username,
  100. node.username,
  101. node.username))
  102. menu.add(CliMenuItem("Password", self.tester.cli.get_password,
  103. node.password,
  104. node.password))
  105. menu.add(CliMenuItem("Url", self.tester.cli.get_url,
  106. node.url,
  107. node.url))
  108. menunotes = CliMenuItem("Notes",
  109. self.tester.cli.get_notes(reader=lambda:
  110. u'bla bla'),
  111. node.notes,
  112. node.notes)
  113. menu.add(menunotes)
  114. menu.add(CliMenuItem("Tags", self.tester.cli.get_tags,
  115. node.tags,
  116. node.tags))
  117. dummy_stdin = StringIO('4\n\nX')
  118. class dummy_stdin(object):
  119. def __init__(self):
  120. self.idx = -1
  121. self.ans = ['4', 'some fucking notes', 'X']
  122. def __call__(self, msg):
  123. self.idx += 1
  124. return self.ans[self.idx]
  125. dstin = dummy_stdin()
  126. menu.run(node, reader=dstin)
  127. self.tester.cli._db.editnode(2, node)
  128. def test_get_pass_conf(self):
  129. numerics, leet, s_chars = get_pass_conf(self.tester.cli.config)
  130. self.assertFalse(numerics)
  131. self.assertFalse(leet)
  132. self.assertFalse(s_chars)
  133. def test_do_tags(self):
  134. self.tester.cli.do_filter('bank')
  135. def test_do_forget(self):
  136. self.tester.cli.do_forget('')
  137. def test_do_auth(self):
  138. crypto = CryptoEngine.get()
  139. rv = crypto.authenticate('12345')
  140. self.assertTrue(rv)
  141. self.assertFalse(crypto.authenticate('WRONG'))
  142. def test_do_clear(self):
  143. self.tester.cli.do_clear('')
  144. def test_do_exit(self):
  145. self.assertTrue(self.tester.cli.do_exit(''))
  146. class FactoryTest(unittest.TestCase):
  147. def test_factory_check_db_ver(self):
  148. self.assertEqual(factory.check_db_version('SQLite', testdb), 0.5)
  149. def test_factory_check_db_file(self):
  150. factory.create('SQLite', version='0.3', filename='baz.db')
  151. self.assertEqual(factory.check_db_version('SQLite', 'baz.db'), 0.3)
  152. os.unlink('baz.db')
  153. #def test_factory_create(self):
  154. # db = factory.create('SQLite', filename='foo.db')
  155. # db._open()
  156. # self.assertTrue(os.path.exists('foo.db'))
  157. # db.close()
  158. # os.unlink('foo.db')
  159. # self.assertIsInstance(db, SQLiteDatabaseNewForm)
  160. # self.assertRaises(DatabaseException, factory.create, 'UNKNOWN')
  161. if __name__ == '__main__':
  162. # make sure we use local pwman
  163. sys.path.insert(0, os.getcwd())
  164. # check if old DB exists, if so remove it.
  165. # excuted only once when invoked upon import or
  166. # upon run
  167. SetupTester().clean()
  168. unittest.main(verbosity=1, failfast=True)