test_base_ui.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_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_do_list(self):
  64. self.tester.cli.do_list('')
  65. if __name__ == '__main__':
  66. ce = CryptoEngine.get()
  67. ce.callback = DummyCallback()
  68. ce.changepassword(reader=give_key)
  69. try:
  70. unittest.main(verbosity=2, failfast=True)
  71. except SystemExit:
  72. #os.remove(testdb)
  73. pass