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