test_mongodb.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import unittest
  2. import sys
  3. from .test_crypto_engine import give_key, DummyCallback
  4. if sys.version_info.major > 2: # pragma: no cover
  5. from urllib.parse import urlparse
  6. else: # pragma: no cover
  7. from urlparse import urlparse
  8. import pymongo
  9. from pwman.data.drivers.mongodb import MongoDB
  10. from pwman.util.crypto_engine import CryptoEngine
  11. class TestMongoDB(unittest.TestCase):
  12. @classmethod
  13. def setUpClass(cls):
  14. u = "mongodb://pwman:123456@localhost:27017/pwmantest"
  15. u = urlparse(u)
  16. cls.db = MongoDB(u)
  17. cls.db._open()
  18. @classmethod
  19. def tearDownClass(cls):
  20. # Drop collections here
  21. pass
  22. def test_1_con(self):
  23. self.assertFalse(True)
  24. def test_2_create_collections(self):
  25. pass
  26. def test_3_load_key(self):
  27. self.db.savekey('SECRET$6$KEY')
  28. secretkey = self.db.loadkey()
  29. self.assertEqual(secretkey, 'SECRET$6$KEY')
  30. def test_4_save_crypto(self):
  31. self.db.save_crypto_info("TOP", "SECRET")
  32. secretkey = self.db.loadkey()
  33. self.assertEqual(secretkey, 'TOP$6$SECRET')
  34. row = self.db.fetch_crypto_info()
  35. self.assertEqual(row, ('TOP', 'SECRET'))
  36. def test_5_add_node(self):
  37. innode = ["TBONE", "S3K43T", "example.org", "some note",
  38. ["bartag", "footag"]]
  39. self.db.add_node(innode)
  40. outnode = self.db.getnodes([1])[0]
  41. self.assertEqual(innode[:-1] + [t for t in innode[-1]], outnode[1:])
  42. def test_6_list_nodes(self):
  43. ret = self.db.listnodes()
  44. self.assertEqual(ret, [1])
  45. ret = self.db.listnodes("footag")
  46. self.assertEqual(ret, [1])
  47. def test_6a_list_tags(self):
  48. ret = self.db.listtags()
  49. self.assertListEqual(ret, ['bartag', 'footag'])
  50. def test_6b_get_nodes(self):
  51. ret = self.db.getnodes([1])
  52. retb = self.db.getnodes([])
  53. self.assertListEqual(ret, retb)
  54. def test_7_get_or_create_tag(self):
  55. s = self.db._get_or_create_tag("SECRET")
  56. s1 = self.db._get_or_create_tag("SECRET")
  57. self.assertEqual(s, s1)
  58. def test_7a_clean_orphans(self):
  59. self.db._clean_orphans()
  60. rv = self.db._get_tag("SECRET")
  61. self.assertIsNone(rv)
  62. def test_8_remove_node(self):
  63. self.db.removenodes([1])
  64. n = self.db.listnodes()
  65. self.assertEqual(len(n), 0)
  66. def test_9_check_db_version(self):
  67. dburi = "mysql://pwman:123456@localhost:3306/pwmantest"
  68. v = self.db.check_db_version(urlparse(dburi))
  69. self.assertEqual(v, '0.6')
  70. self.db._cur.execute("DROP TABLE DBVERSION")
  71. self.db._con.commit()
  72. v = self.db.check_db_version(urlparse(dburi))
  73. self.assertEqual(v, None)
  74. self.db._cur.execute("CREATE TABLE DBVERSION("
  75. "VERSION TEXT NOT NULL) ")
  76. self.db._con.commit()
  77. if __name__ == '__main__':
  78. ce = CryptoEngine.get()
  79. ce.callback = DummyCallback()
  80. ce.changepassword(reader=give_key)
  81. unittest.main(verbosity=2, failfast=True)