test_postgresql.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 unittest
  20. from pwman.data.drivers.postgresql import PostgresqlDatabase
  21. from pwman.util.crypto_engine import CryptoEngine
  22. from .test_crypto_engine import give_key, DummyCallback
  23. import psycopg2 as pg
  24. class TestPostGresql(unittest.TestCase):
  25. @classmethod
  26. def setUpClass(self):
  27. secret = open('secret.txt').readline().strip()
  28. u = "postgresql://oz123:%s@localhost/pwman" % secret
  29. self.db = PostgresqlDatabase(u)
  30. self.db._open()
  31. @classmethod
  32. def tearDownClass(self):
  33. self.db._cur.execute("DROP TABLE LOOKUP")
  34. self.db._cur.execute("DROP TABLE TAG")
  35. self.db._cur.execute("DROP TABLE NODE")
  36. self.db._cur.execute("DROP TABLE DBVERSION")
  37. self.db._cur.execute("DROP TABLE CRYPTO")
  38. self.db._con.commit()
  39. def test_1_con(self):
  40. self.assertIsInstance(self.db._cur, pg._psycopg.cursor)
  41. def test_2_create_tables(self):
  42. self.db._create_tables()
  43. # invoking this method a second time should not raise an exception
  44. self.db._create_tables()
  45. def test_3_load_key(self):
  46. self.db.savekey('SECRET$6$KEY')
  47. secretkey = self.db.loadkey()
  48. self.assertEqual(secretkey, 'SECRET$6$KEY')
  49. def test_4_save_crypto(self):
  50. self.db.save_crypto_info("TOP", "SECRET")
  51. secretkey = self.db.loadkey()
  52. self.assertEqual(secretkey, 'TOP$6$SECRET')
  53. row = self.db.fetch_crypto_info()
  54. self.assertEqual(row, ('TOP', 'SECRET'))
  55. def test_5_add_node(self):
  56. innode = ["TBONE", "S3K43T", "example.org", "some note"]
  57. self.db.add_node(innode)
  58. outnode = self.db.getnodes([1])[0]
  59. self.assertEqual(innode, outnode[1:])
  60. def test_6_list_nodes(self):
  61. self.db.listnodes()
  62. def test_7_get_or_create_tag(self):
  63. s = self.db._get_or_create_tag("SECRET")
  64. s1 = self.db._get_or_create_tag("SECRET")
  65. self.assertEqual(s, s1)
  66. def test_7a_clean_orphans(self):
  67. self.db._clean_orphans()
  68. rv = self.db._get_tag("SECRET")
  69. self.assertIsNone(rv)
  70. if __name__ == '__main__':
  71. ce = CryptoEngine.get()
  72. ce.callback = DummyCallback()
  73. ce.changepassword(reader=give_key)
  74. unittest.main(verbosity=2, failfast=True)