test_postgresql.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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-2017 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import unittest
  20. import sys
  21. from .test_crypto_engine import give_key, DummyCallback
  22. if sys.version_info.major > 2: # pragma: no cover
  23. from urllib.parse import urlparse
  24. else: # pragma: no cover
  25. from urlparse import urlparse
  26. import psycopg2 as pg
  27. from pwman.data.drivers.postgresql import PostgresqlDatabase
  28. from pwman.util.crypto_engine import CryptoEngine
  29. ##
  30. # testing on linux host
  31. # su - postgres
  32. # psql
  33. # postgres=# CREATE USER tester WITH PASSWORD '123456';
  34. # postgres=# grant ALL ON DATABASE pwman to tester;
  35. #
  36. ##
  37. class TestPostGresql(unittest.TestCase):
  38. @classmethod
  39. def setUpClass(self):
  40. u = "postgresql://tester:123456@localhost/pwman"
  41. u = urlparse(u)
  42. # password required, for all hosts
  43. # u = "postgresql://<user>:<pass>@localhost/pwman"
  44. self.db = PostgresqlDatabase(u)
  45. self.db._open()
  46. @classmethod
  47. def tearDownClass(self):
  48. self.db._cur.execute("DROP TABLE LOOKUP")
  49. self.db._cur.execute("DROP TABLE TAG")
  50. self.db._cur.execute("DROP TABLE NODE")
  51. self.db._cur.execute("DROP TABLE DBVERSION")
  52. self.db._cur.execute("DROP TABLE CRYPTO")
  53. self.db._con.commit()
  54. def test_1_con(self):
  55. self.assertIsInstance(self.db._cur, pg._psycopg.cursor)
  56. def test_2_create_tables(self):
  57. self.db._create_tables()
  58. # invoking this method a second time should not raise an exception
  59. self.db._create_tables()
  60. def test_3_load_key(self):
  61. self.db.savekey('SECRET$6$KEY')
  62. secretkey = self.db.loadkey()
  63. self.assertEqual(secretkey, b'SECRET$6$KEY')
  64. def test_4_save_crypto(self):
  65. self.db.save_crypto_info(b"TOP", b"SECRET")
  66. secretkey = self.db.loadkey()
  67. self.assertEqual(secretkey, b'TOP$6$SECRET')
  68. row = self.db.fetch_crypto_info()
  69. self.assertEqual(list(map(bytearray, row)),
  70. [bytearray(b'TOP'), bytearray(b'SECRET')])
  71. def test_5_add_node(self):
  72. innode = [b"TBONE", b"S3K43T", b"example.org", b"some note",
  73. [b"footag", b"bartag"]]
  74. self.db.add_node(innode)
  75. outnode = self.db.getnodes([1])[0]
  76. self.assertEqual(innode[:-1] + [t for t in innode[-1]], outnode[1:])
  77. def test_6_list_nodes(self):
  78. ret1 = self.db.listnodes()
  79. self.assertEqual(ret1, [1])
  80. ret2 = self.db.listnodes(b"footag")
  81. self.assertEqual(ret2, [1])
  82. def test_6a_list_tags(self):
  83. ret = self.db.listtags()
  84. self.assertListEqual(ret, [b'footag', b'bartag'])
  85. def test_6b_get_nodes(self):
  86. ret = self.db.getnodes([1])
  87. retb = self.db.getnodes([])
  88. self.assertListEqual(ret, retb)
  89. def test_7_get_or_create_tag(self):
  90. s = self.db._get_or_create_tag(b"SECRET")
  91. s1 = self.db._get_or_create_tag(b"SECRET")
  92. self.assertEqual(s, s1)
  93. def test_7a_clean_orphans(self):
  94. self.db._clean_orphans()
  95. rv = self.db._get_tag("SECRET")
  96. self.assertIsNone(rv)
  97. def test_8_remove_node(self):
  98. self.db.removenodes([1])
  99. n = self.db.listnodes()
  100. self.assertEqual(len(n), 0)
  101. def test_9_check_db_version(self):
  102. dburi = "postgresql://tester:123456@localhost/pwman"
  103. v = self.db.check_db_version(dburi)
  104. self.assertEqual(str(v), '0.6')
  105. self.db._cur.execute("DROP TABLE DBVERSION")
  106. self.db._con.commit()
  107. v = self.db.check_db_version(dburi)
  108. self.assertEqual(str(v), '0.6')
  109. self.db._cur.execute("CREATE TABLE DBVERSION("
  110. "VERSION TEXT NOT NULL DEFAULT {}"
  111. ")".format('0.6'))
  112. self.db._con.commit()
  113. if __name__ == '__main__':
  114. ce = CryptoEngine.get()
  115. ce.callback = DummyCallback()
  116. ce.changepassword(reader=give_key)
  117. unittest.main(verbosity=2, failfast=True)