test_postgresql.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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) 2015 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import os
  20. import unittest
  21. import sys
  22. from pwman.data.drivers.postgresql import PostgresqlDatabase
  23. from pwman.data.nodes import Node
  24. from pwman.util.crypto_engine import CryptoEngine
  25. from .test_crypto_engine import give_key, DummyCallback
  26. import psycopg2 as pg
  27. class TestPostGresql(unittest.TestCase):
  28. @classmethod
  29. def setUpClass(self):
  30. secret = open('secret.txt').readline().strip()
  31. u = "postgresql://oz123:%s@localhost/pwman" % secret
  32. self.db = PostgresqlDatabase(u)
  33. self.db._open()
  34. @classmethod
  35. def tearDownClass(self):
  36. self.db._cur.execute("DROP TABLE LOOKUP")
  37. self.db._cur.execute("DROP TABLE TAG")
  38. self.db._cur.execute("DROP TABLE NODE")
  39. self.db._cur.execute("DROP TABLE DBVERSION")
  40. self.db._cur.execute("DROP TABLE CRYPTO")
  41. self.db._con.commit()
  42. def test_1_con(self):
  43. self.assertIsInstance(self.db._cur, pg._psycopg.cursor)
  44. def test_2_create_tables(self):
  45. self.db._create_tables()
  46. # invoking this method a second time should not raise an exception
  47. self.db._create_tables()
  48. def test_3_load_key(self):
  49. self.db.savekey('SECRET$6$KEY')
  50. secretkey = self.db.loadkey()
  51. self.assertEqual(secretkey, 'SECRET$6$KEY')
  52. def test_4_save_crypto(self):
  53. self.db.save_crypto_info("TOP", "SECRET")
  54. secretkey = self.db.loadkey()
  55. self.assertEqual(secretkey, 'TOP$6$SECRET')
  56. def test_5_add_node(self):
  57. self.db.add_node(("TBONE", "S3K43T", "example.org", "some note"))
  58. if __name__ == '__main__':
  59. ce = CryptoEngine.get()
  60. ce.callback = DummyCallback()
  61. ce.changepassword(reader=give_key)
  62. unittest.main(verbosity=2, failfast=True)