test_nodes.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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=b'foo', password=b's3kr3t',
  26. url=b'example.com', notes=b'just a reminder to self',
  27. tags=[b'baz', b'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': b'baz', 'password': b'n3ws3k43t',
  38. 'notes': b'i have changed the password',
  39. 'url': b'newexample.com', 'tags': [b'tag1', b'tag2']}
  40. for k in new_node:
  41. setattr(self.node, k, new_node[k])
  42. for attribute in ['username', 'password', 'url', 'notes']:
  43. self.assertEqual(bytearray(getattr(self.node, attribute), 'utf-8'), new_node[attribute])
  44. self.assertEqual(bytearray(getattr(self.node, 'username'), 'utf-8'), new_node['username'])
  45. self.assertEqual(bytearray(getattr(self.node, 'password'), 'utf-8'), new_node['password'])
  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