test_mysql.py 4.6 KB

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