test_mongodb.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it 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 pymongo
  27. from pwman.data.drivers.mongodb import MongoDB
  28. # use pwmantest
  29. # db.createUser(
  30. # {
  31. # user: "tester",
  32. # pwd: "12345678",
  33. # roles: [{ role: "dbAdmin", db: "pwmantest" },
  34. # { role: "readWrite", db: "pwmantest" },]
  35. # })
  36. from pwman.util.crypto_engine import CryptoEngine
  37. class TestMongoDB(unittest.TestCase):
  38. @classmethod
  39. def setUpClass(cls):
  40. u = u"mongodb://tester:12345678@localhost:27017/pwmantest"
  41. cls.db = MongoDB(u)
  42. cls.db._open()
  43. @classmethod
  44. def tearDownClass(cls):
  45. # Drop collections here
  46. pass
  47. def test_1_con(self):
  48. self.assertIsInstance(self.db._con, pymongo.Connection)
  49. @unittest.skip("")
  50. def test_2_create_collections(self):
  51. pass
  52. @unittest.skip("")
  53. def test_3_load_key(self):
  54. self.db.savekey('SECRET$6$KEY')
  55. secretkey = self.db.loadkey()
  56. self.assertEqual(secretkey, 'SECRET$6$KEY')
  57. @unittest.skip("")
  58. def test_4_save_crypto(self):
  59. self.db.save_crypto_info("TOP", "SECRET")
  60. secretkey = self.db.loadkey()
  61. self.assertEqual(secretkey, 'TOP$6$SECRET')
  62. row = self.db.fetch_crypto_info()
  63. self.assertEqual(row, ('TOP', 'SECRET'))
  64. @unittest.skip("")
  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. @unittest.skip("")
  72. def test_6_list_nodes(self):
  73. ret = self.db.listnodes()
  74. self.assertEqual(ret, [1])
  75. ret = self.db.listnodes("footag")
  76. self.assertEqual(ret, [1])
  77. @unittest.skip("")
  78. def test_6a_list_tags(self):
  79. ret = self.db.listtags()
  80. self.assertListEqual(ret, ['bartag', 'footag'])
  81. @unittest.skip("")
  82. def test_6b_get_nodes(self):
  83. ret = self.db.getnodes([1])
  84. retb = self.db.getnodes([])
  85. self.assertListEqual(ret, retb)
  86. @unittest.skip("")
  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. @unittest.skip("")
  92. def test_7a_clean_orphans(self):
  93. self.db._clean_orphans()
  94. rv = self.db._get_tag("SECRET")
  95. self.assertIsNone(rv)
  96. @unittest.skip("")
  97. def test_8_remove_node(self):
  98. self.db.removenodes([1])
  99. n = self.db.listnodes()
  100. self.assertEqual(len(n), 0)
  101. @unittest.skip("")
  102. def test_9_check_db_version(self):
  103. dburi = "mysql://pwman:123456@localhost:3306/pwmantest"
  104. v = self.db.check_db_version(urlparse(dburi))
  105. self.assertEqual(v, '0.6')
  106. self.db._cur.execute("DROP TABLE DBVERSION")
  107. self.db._con.commit()
  108. v = self.db.check_db_version(urlparse(dburi))
  109. self.assertEqual(v, None)
  110. self.db._cur.execute("CREATE TABLE DBVERSION("
  111. "VERSION TEXT NOT NULL) ")
  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)