test_postgresql.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. row = self.db.fetch_crypto_info()
  57. self.assertEqual(row, ('TOP', 'SECRET'))
  58. def test_5_add_node(self):
  59. innode = ["TBONE", "S3K43T", "example.org", "some note"]
  60. self.db.add_node(innode)
  61. outnode = self.db.getnodes([1])[0]
  62. self.assertEqual(innode, outnode[1:])
  63. if __name__ == '__main__':
  64. ce = CryptoEngine.get()
  65. ce.callback = DummyCallback()
  66. ce.changepassword(reader=give_key)
  67. unittest.main(verbosity=2, failfast=True)