test_postgresql.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. ##
  25. # testing on linux host
  26. # su - postgres
  27. # psql
  28. # postgres=# create user $YOUR_USERNAME;
  29. # postgres=# grant ALL ON DATABASE pwman to $YOUR_USERNAME;
  30. #
  31. ##
  32. class TestPostGresql(unittest.TestCase):
  33. @classmethod
  34. def setUpClass(self):
  35. u = "postgresql:///pwman"
  36. self.db = PostgresqlDatabase(u)
  37. self.db._open()
  38. @classmethod
  39. def tearDownClass(self):
  40. self.db._cur.execute("DROP TABLE LOOKUP")
  41. self.db._cur.execute("DROP TABLE TAG")
  42. self.db._cur.execute("DROP TABLE NODE")
  43. self.db._cur.execute("DROP TABLE DBVERSION")
  44. self.db._cur.execute("DROP TABLE CRYPTO")
  45. self.db._con.commit()
  46. def test_1_con(self):
  47. self.assertIsInstance(self.db._cur, pg._psycopg.cursor)
  48. def test_2_create_tables(self):
  49. self.db._create_tables()
  50. # invoking this method a second time should not raise an exception
  51. self.db._create_tables()
  52. def test_3_load_key(self):
  53. self.db.savekey('SECRET$6$KEY')
  54. secretkey = self.db.loadkey()
  55. self.assertEqual(secretkey, 'SECRET$6$KEY')
  56. def test_4_save_crypto(self):
  57. self.db.save_crypto_info("TOP", "SECRET")
  58. secretkey = self.db.loadkey()
  59. self.assertEqual(secretkey, 'TOP$6$SECRET')
  60. row = self.db.fetch_crypto_info()
  61. self.assertEqual(row, ('TOP', 'SECRET'))
  62. def test_5_add_node(self):
  63. innode = ["TBONE", "S3K43T", "example.org", "some note"]
  64. self.db.add_node(innode)
  65. outnode = self.db.getnodes([1])[0]
  66. self.assertEqual(innode, outnode[1:])
  67. def test_6_list_nodes(self):
  68. self.db.listnodes()
  69. def test_7_get_or_create_tag(self):
  70. s = self.db._get_or_create_tag("SECRET")
  71. s1 = self.db._get_or_create_tag("SECRET")
  72. self.assertEqual(s, s1)
  73. def test_7a_clean_orphans(self):
  74. self.db._clean_orphans()
  75. rv = self.db._get_tag("SECRET")
  76. self.assertIsNone(rv)
  77. def test_8_remove_node(self):
  78. self.db.removenodes([1])
  79. n = self.db.listnodes()
  80. self.assertEqual(len(n), 0)
  81. if __name__ == '__main__':
  82. ce = CryptoEngine.get()
  83. ce.callback = DummyCallback()
  84. ce.changepassword(reader=give_key)
  85. unittest.main(verbosity=2, failfast=True)