test_nodes.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 unittest
  20. from pwman.util.crypto_engine import CryptoEngine
  21. from pwman.data.nodes import Node
  22. from .test_crypto_engine import give_key, DummyCallback
  23. class TestNode(unittest.TestCase):
  24. def setUp(self):
  25. self.node = Node(username=u'foo', password=u's3kr3t',
  26. url=u'example.com', notes=u'just a reminder to self',
  27. tags=[u'baz', u'baz'])
  28. def test_do_encdict(self):
  29. ce = CryptoEngine.get()
  30. for k, v in self.node.to_encdict().items():
  31. if k == 'user':
  32. self.assertEqual(ce.decrypt(v).decode(), getattr(self.node,
  33. 'username'))
  34. elif k != 'tags':
  35. self.assertEqual(ce.decrypt(v).decode(), getattr(self.node, k))
  36. def test_setters(self):
  37. new_node = {'username': 'baz', 'password': 'n3ws3k43t',
  38. 'notes': 'i have changed the password',
  39. 'url': 'newexample.com', 'tags': ['tag1', 'tag2']}
  40. for k in new_node:
  41. setattr(self.node, k, new_node[k])
  42. self.assertEqual(getattr(self.node, 'username'), new_node['username'])
  43. self.assertEqual(getattr(self.node, 'password'), new_node['password'])
  44. self.assertEqual(getattr(self.node, 'url'), new_node['url'])
  45. self.assertEqual(getattr(self.node, 'notes'), new_node['notes'])
  46. self.assertEqual(getattr(self.node, 'tags'), new_node['tags'])
  47. if __name__ == '__main__':
  48. ce = CryptoEngine.get()
  49. ce.callback = DummyCallback()
  50. ce.changepassword(reader=give_key)
  51. try:
  52. unittest.main(verbosity=2, failfast=True)
  53. except SystemExit:
  54. pass