test_sqlite.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute iut 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) 2012 Oz Nahum Tiram <nahumoz@gmail.com>
  18. #============================================================================
  19. import os
  20. import unittest
  21. from pwman.data.drivers.sqlite import SQLite
  22. from pwman.data.nodes import Node
  23. from pwman.util.crypto_engine import CryptoEngine
  24. class TestSQLite(unittest.TestCase):
  25. def setUp(self):
  26. self.db = SQLite('test.db')
  27. self.db._open()
  28. def test_1_create_tables(self):
  29. self.db._create_tables()
  30. self.db._con.commit()
  31. # the method _open calls _create_tables
  32. self.db.save_crypto_info("foo", "bar")
  33. self.db._create_tables()
  34. def test_1a_create_tables(self):
  35. self.db._create_tables()
  36. def test_2_crypto_info(self):
  37. self.db._create_tables()
  38. self.db.save_crypto_info("foo", "bar")
  39. f = self.db.fetch_crypto_info()
  40. self.assertListEqual([u'foo', u'bar'], list(f))
  41. def test_3_add_node(self):
  42. node = Node(clear_text=True,
  43. **{'username': u"alice", 'password': u"secret",
  44. 'url': u"wonderland.com",
  45. 'notes': u"a really great place",
  46. 'tags': [u'foo', u'bar']})
  47. self.db.add_node(node)
  48. rv = self.db._cur.execute("select * from node")
  49. # clearly this fails, while alice is not found in clear text in the
  50. # database!
  51. ce = CryptoEngine.get()
  52. self.assertIn(ce.encrypt(u'alice'), rv.fetchone()[1])
  53. def tearDown(self):
  54. self.db.close()
  55. if __name__ == '__main__':
  56. try:
  57. unittest.main(verbosity=2)
  58. except SystemExit:
  59. os.remove('test.db')