test_postgresql.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. # no password required, for testing in travis
  36. u = "postgresql:///pwman"
  37. # password required, for all other hosts
  38. #u = "postgresql://<user>:<pass>@localhost/pwman"
  39. self.db = PostgresqlDatabase(u)
  40. self.db._open()
  41. @classmethod
  42. def tearDownClass(self):
  43. self.db._cur.execute("DROP TABLE LOOKUP")
  44. self.db._cur.execute("DROP TABLE TAG")
  45. self.db._cur.execute("DROP TABLE NODE")
  46. self.db._cur.execute("DROP TABLE DBVERSION")
  47. self.db._cur.execute("DROP TABLE CRYPTO")
  48. self.db._con.commit()
  49. def test_1_con(self):
  50. self.assertIsInstance(self.db._cur, pg._psycopg.cursor)
  51. def test_2_create_tables(self):
  52. self.db._create_tables()
  53. # invoking this method a second time should not raise an exception
  54. self.db._create_tables()
  55. def test_3_load_key(self):
  56. self.db.savekey('SECRET$6$KEY')
  57. secretkey = self.db.loadkey()
  58. self.assertEqual(secretkey, 'SECRET$6$KEY')
  59. def test_4_save_crypto(self):
  60. self.db.save_crypto_info("TOP", "SECRET")
  61. secretkey = self.db.loadkey()
  62. self.assertEqual(secretkey, 'TOP$6$SECRET')
  63. row = self.db.fetch_crypto_info()
  64. self.assertEqual(row, ('TOP', 'SECRET'))
  65. def test_5_add_node(self):
  66. innode = ["TBONE", "S3K43T", "example.org", "some note",
  67. ["footag", "bartag"]]
  68. self.db.add_node(innode)
  69. outnode = self.db.getnodes([1])[0]
  70. self.assertEqual(innode[:-1] + [t for t in innode[-1]], outnode[1:])
  71. def test_6_list_nodes(self):
  72. ret = self.db.listnodes()
  73. self.assertEqual(ret, [1])
  74. ret = self.db.listnodes("footag")
  75. self.assertEqual(ret, [1])
  76. def test_6a_list_tags(self):
  77. ret = self.db.listtags()
  78. self.assertListEqual(ret, ['footag', 'bartag'])
  79. def test_7_get_or_create_tag(self):
  80. s = self.db._get_or_create_tag("SECRET")
  81. s1 = self.db._get_or_create_tag("SECRET")
  82. self.assertEqual(s, s1)
  83. def test_7a_clean_orphans(self):
  84. self.db._clean_orphans()
  85. rv = self.db._get_tag("SECRET")
  86. self.assertIsNone(rv)
  87. def test_8_remove_node(self):
  88. self.db.removenodes([1])
  89. n = self.db.listnodes()
  90. self.assertEqual(len(n), 0)
  91. if __name__ == '__main__':
  92. ce = CryptoEngine.get()
  93. ce.callback = DummyCallback()
  94. ce.changepassword(reader=give_key)
  95. unittest.main(verbosity=2, failfast=True)