test_mysql.py 4.4 KB

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