test_base_ui.py 3.7 KB

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