test_mysql.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. 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. if sys.version_info.major > 2:
  27. from pymysql import connections
  28. else:
  29. from MySQLdb import connections
  30. from pwman.data.drivers.mysql import MySQLDatabase
  31. from pwman.util.crypto_engine import CryptoEngine
  32. class TestMySQLDatabase(unittest.TestCase):
  33. @classmethod
  34. def setUpClass(self):
  35. u = "mysql://pwman:123456@localhost:3306/pwmantest"
  36. u = urlparse(u)
  37. # password required, for all hosts
  38. # u = "mysql://<user>:<pass>@localhost/pwmantest"
  39. self.db = MySQLDatabase(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._con, connections.Connection)
  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. ["bartag", "footag"]]
  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, ['bartag', 'footag'])
  79. def test_6b_get_nodes(self):
  80. ret = self.db.getnodes([1])
  81. retb = self.db.getnodes([])
  82. self.assertListEqual(ret, retb)
  83. def test_7_get_or_create_tag(self):
  84. s = self.db._get_or_create_tag("SECRET")
  85. s1 = self.db._get_or_create_tag("SECRET")
  86. self.assertEqual(s, s1)
  87. def test_7a_clean_orphans(self):
  88. self.db._clean_orphans()
  89. rv = self.db._get_tag("SECRET")
  90. self.assertIsNone(rv)
  91. def test_8_remove_node(self):
  92. self.db.removenodes([1])
  93. n = self.db.listnodes()
  94. self.assertEqual(len(n), 0)
  95. def test_9_check_db_version(self):
  96. dburi = "mysql://pwman:123456@localhost:3306/pwmantest"
  97. v = self.db.check_db_version(urlparse(dburi))
  98. self.assertEqual(v, '0.6')
  99. self.db._cur.execute("DROP TABLE DBVERSION")
  100. self.db._con.commit()
  101. v = self.db.check_db_version(urlparse(dburi))
  102. self.assertEqual(v, None)
  103. self.db._cur.execute("CREATE TABLE DBVERSION("
  104. "VERSION TEXT NOT NULL) ")
  105. self.db._con.commit()
  106. if __name__ == '__main__':
  107. ce = CryptoEngine.get()
  108. ce.callback = DummyCallback()
  109. ce.changepassword(reader=give_key)
  110. unittest.main(verbosity=2, failfast=True)