test_base_ui.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 pwman.util.crypto_engine import CryptoEngine
  22. from .test_crypto_engine import give_key, DummyCallback
  23. from pwman.data.database import __DB_FORMAT__
  24. from .test_tools import (SetupTester)
  25. from pwman.data import factory
  26. try:
  27. from StringIO import StringIO
  28. except ImportError:
  29. from io import StringIO
  30. import sys
  31. testdb = os.path.join(os.path.dirname(__file__), "test-baseui.pwman.db")
  32. class TestBaseUI(unittest.TestCase):
  33. def setUp(self):
  34. "test that the right db instance was created"
  35. dbver = __DB_FORMAT__
  36. self.dbtype = 'SQLite'
  37. self.db = factory.create(self.dbtype, dbver, testdb)
  38. self.tester = SetupTester(dbver, testdb)
  39. self.tester.create()
  40. def test_false(self):
  41. self.assertFalse(False)
  42. def test_get_tags(self):
  43. sys.stdin = StringIO("foo bar baz\n")
  44. tags = self.tester.cli._get_tags(reader=lambda: "foo bar baz")
  45. self.assertListEqual(['foo', 'bar', 'baz'], tags)
  46. sys.stdin = sys.__stdin__
  47. def test_1_do_new(self):
  48. sys.stdin = StringIO(("alice\nsecret\nexample.com\nsome notes"
  49. "\nfoo bar baz"))
  50. _node = self.tester.cli.do_new('')
  51. sys.stdin = sys.__stdin__
  52. self.assertListEqual(['foo', 'bar', 'baz'], [t.decode() for t
  53. in _node.tags])
  54. nodeid = self.tester.cli._db.listnodes()
  55. self.assertListEqual([1], nodeid)
  56. nodes = self.tester.cli._db.getnodes(nodeid)
  57. ce = CryptoEngine.get()
  58. user = ce.decrypt(nodes[0][1])
  59. self.assertTrue(user, 'alice')
  60. tags = nodes[0][5:]
  61. for idx, t in enumerate(['foo', 'bar', 'baz']):
  62. self.assertTrue(t, tags[idx])
  63. def test_2_do_list(self):
  64. self.output = StringIO()
  65. self.saved_stdout = sys.stdout
  66. sys.stdout = self.output
  67. self.tester.cli.do_list('')
  68. self.tester.cli.do_list('foo')
  69. self.tester.cli.do_list('bar')
  70. sys.stdout = self.saved_stdout
  71. self.output.getvalue()
  72. def test_3_do_export(self):
  73. self.tester.cli.do_export("{'filename':'foo.csv'}")
  74. with open('foo.csv') as f:
  75. l = f.readlines()
  76. self.assertIn('alice;example.com;secret;some notes;foo,bar,baz', l[1])
  77. if __name__ == '__main__':
  78. ce = CryptoEngine.get()
  79. ce.callback = DummyCallback()
  80. ce.changepassword(reader=give_key)
  81. try:
  82. unittest.main(verbosity=2, failfast=True)
  83. except SystemExit:
  84. #os.remove(testdb)
  85. pass