test_base_ui.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import os
  20. import unittest
  21. from io import StringIO, BytesIO
  22. from pwman.util.crypto_engine import CryptoEngine
  23. from .test_crypto_engine import give_key, DummyCallback
  24. from pwman.data.database import __DB_FORMAT__
  25. from .test_tools import (SetupTester)
  26. from pwman.data import factory
  27. from pwman.data.nodes import Node
  28. db = ".".join(("test-baseui","pwman", sys.version.split(" " ,1)[0], "db"))
  29. testdb = os.path.abspath(os.path.join(os.path.dirname(__file__), db))
  30. print(testdb)
  31. class dummy_stdin(object):
  32. def __init__(self):
  33. self.idx = -1
  34. self.ans = ['4', 'some fucking notes', 'X']
  35. def __call__(self, msg):
  36. self.idx += 1
  37. return self.ans[self.idx]
  38. class TestBaseUI(unittest.TestCase):
  39. @classmethod
  40. def tearDownClass(cls):
  41. for item in (testdb, 'foo.csv', 'pwman-export.csv'):
  42. try:
  43. os.unlink(item)
  44. except OSError:
  45. continue
  46. SetupTester().clean()
  47. def setUp(self):
  48. "test that the right db instance was created"
  49. dbver = __DB_FORMAT__
  50. self.db = factory.createdb('sqlite://' + testdb, dbver)
  51. self.tester = SetupTester(dbver, testdb)
  52. self.tester.create()
  53. def tearDown(self):
  54. #self.tester.cli.do_exit("")
  55. pass
  56. def test_get_tags(self):
  57. sys.stdin = StringIO("foo bar baz\n")
  58. tags = self.tester.cli._get_tags(reader=lambda: "foo bar baz")
  59. self.assertListEqual(['foo', 'bar', 'baz'], tags)
  60. sys.stdin = sys.__stdin__
  61. def test_1_do_new(self):
  62. sys.stdin = BytesIO((b"alice\nsecret\nexample.com\nsome notes"
  63. b"\nfoo bar baz"))
  64. _node = self.tester.cli._do_new('')
  65. sys.stdin = sys.__stdin__
  66. self.assertListEqual([b'foo', b'bar', b'baz'], [t for t
  67. in _node.tags])
  68. nodeid = self.tester.cli._db.listnodes()
  69. self.assertListEqual([1], nodeid)
  70. nodes = self.tester.cli._db.getnodes(nodeid)
  71. ce = CryptoEngine.get()
  72. user = ce.decrypt(nodes[0][1])
  73. self.assertTrue(user, 'alice')
  74. tags = nodes[0][5:]
  75. for idx, t in enumerate(['foo', 'bar', 'baz']):
  76. self.assertTrue(t, tags[idx])
  77. def test_2_do_list(self):
  78. self.output = StringIO()
  79. sys.stdout = self.output
  80. self.tester.cli.do_list('')
  81. self.tester.cli.do_list('foo')
  82. self.tester.cli.do_list('bar')
  83. sys.stdout = sys.__stdout__
  84. self.output.getvalue()
  85. def test_3_do_export(self):
  86. self.tester.cli.do_export("{'filename':'foo.csv'}")
  87. with open('foo.csv') as f:
  88. l = f.readlines()
  89. # on windows there is an extra empty line in the exported file
  90. self.assertIn('alice;example.com;secret;some notes;foo,bar,baz\n', l)
  91. def test_3a_do_export(self):
  92. self.tester.cli.do_export("f")
  93. with open('pwman-export.csv') as f:
  94. l = f.readlines()
  95. self.assertIn('alice;example.com;secret;some notes;foo,bar,baz\n', l)
  96. def test_4_do_forget(self):
  97. self.tester.cli.do_forget('')
  98. ce = CryptoEngine.get()
  99. self.assertIsNone(ce._cipher)
  100. def test_5_do_print(self):
  101. v = StringIO()
  102. sys.stdout = v
  103. self.tester.cli.do_print('1')
  104. self.assertIn('\x1b[31mUsername:\x1b[0m alice', v.getvalue())
  105. self.tester.cli.do_print('a')
  106. self.assertIn("print accepts only a single ID ...", v.getvalue())
  107. sys.stdout = sys.__stdout__
  108. def test_6_do_tags(self):
  109. v = StringIO()
  110. sys.stdout = v
  111. self.tester.cli.do_tags('1')
  112. v = v.getvalue()
  113. for t in ['foo', 'bar', 'baz']:
  114. t in v
  115. sys.stdout = sys.__stdout__
  116. print(v)
  117. def test_7_get_ids(self):
  118. # used by do_cp or do_open,
  119. # this spits many time could not understand your input
  120. self.assertEqual([1], self.tester.cli._get_ids('1'))
  121. self.assertListEqual([1, 2, 3, 4, 5], self.tester.cli._get_ids('1-5'))
  122. self.assertListEqual([], self.tester.cli._get_ids('5-1'))
  123. self.assertListEqual([], self.tester.cli._get_ids('5x-1'))
  124. self.assertListEqual([], self.tester.cli._get_ids('5x'))
  125. self.assertListEqual([], self.tester.cli._get_ids('5\\'))
  126. def test_8_do_edit_1(self):
  127. node = self.tester.cli._db.getnodes([1])[0]
  128. node = node[1:5] + [node[5:]]
  129. node = Node.from_encrypted_entries(*node)
  130. sys.stdin = StringIO(("1\nfoo\nx\n"))
  131. self.tester.cli.do_edit('1')
  132. v = StringIO()
  133. sys.stdin = sys.__stdin__
  134. sys.stdout = v
  135. self.tester.cli.do_print('1')
  136. self.assertIn('\x1b[31mUsername:\x1b[0m foo', v.getvalue())
  137. def test_8_do_edit_2(self):
  138. node = self.tester.cli._db.getnodes([1])[0]
  139. node = node[1:5] + [node[5:]]
  140. node = Node.from_encrypted_entries(*node)
  141. sys.stdin = StringIO(("2\ns3kr3t\nx\n"))
  142. self.tester.cli.do_edit('1')
  143. v = StringIO()
  144. sys.stdin = sys.__stdin__
  145. sys.stdout = v
  146. self.tester.cli.do_print('1')
  147. self.assertIn('\x1b[31mPassword:\x1b[0m s3kr3t', v.getvalue())
  148. def test_9_do_delete(self):
  149. self.assertIsNone(self.tester.cli._do_rm('x'))
  150. sys.stdin = StringIO("y\n")
  151. self.tester.cli.do_rm('1')
  152. sys.stdin = sys.__stdin__
  153. sys.stdout = StringIO()
  154. self.tester.cli.do_ls('')
  155. self.assertNotIn('alice', sys.stdout.getvalue())
  156. sys.stdout = sys.__stdout__
  157. def test_10_do_info(self):
  158. self.output = StringIO()
  159. sys.stdout = self.output
  160. self.tester.cli.do_info(b'')
  161. self.assertIn("test.pwman.db", sys.stdout.getvalue())
  162. if __name__ == '__main__':
  163. ce = CryptoEngine.get()
  164. ce.callback = DummyCallback()
  165. ce.changepassword(reader=give_key)
  166. unittest.main(verbosity=2, failfast=True)