test_base_ui.py 6.6 KB

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