test_base_ui.py 6.8 KB

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