test_base_ui.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. from StringIO import StringIO
  27. import sys
  28. testdb = os.path.join(os.path.dirname(__file__), "test-baseui.pwman.db")
  29. class TestBaseUI(unittest.TestCase):
  30. def setUp(self):
  31. "test that the right db instance was created"
  32. dbver = __DB_FORMAT__
  33. self.dbtype = 'SQLite'
  34. self.db = factory.create(self.dbtype, dbver, testdb)
  35. self.tester = SetupTester(dbver, testdb)
  36. self.tester.create()
  37. def test_false(self):
  38. self.assertFalse(False)
  39. def test_get_tags(self):
  40. sys.stdin = StringIO("foo bar baz\n")
  41. tags = self.tester.cli._get_tags(reader=lambda: "foo bar baz")
  42. self.assertListEqual(['foo', 'bar', 'baz'], tags)
  43. sys.stdin = sys.__stdin__
  44. def test_do_newn(self):
  45. sys.stdin = StringIO(("alice\nsecret\nexample.com\nsome notes"
  46. "\nfoo bar baz"))
  47. _node = self.tester.cli.do_newn('')
  48. self.assertListEqual(['foo', 'bar', 'baz'], _node.tags)
  49. sys.stdin = sys.__stdin__
  50. nodeid = self.tester.cli._db.listnodes()
  51. self.assertListEqual([1], nodeid)
  52. nodes = self.tester.cli._db.getnodes(nodeid)
  53. ce = CryptoEngine.get()
  54. user = ce.decrypt(nodes[0][1])
  55. self.assertTrue(user, 'alice')
  56. tags = nodes[0][5:]
  57. for idx, t in enumerate(['foo', 'bar', 'baz']):
  58. self.assertTrue(t, tags[idx])
  59. if __name__ == '__main__':
  60. ce = CryptoEngine.get()
  61. ce.callback = DummyCallback()
  62. ce.changepassword(reader=give_key)
  63. try:
  64. unittest.main(verbosity=2, failfast=True)
  65. except SystemExit:
  66. #os.remove(testdb)
  67. pass