test_base_ui.py 6.9 KB

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