test_mongodb.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import pymongo
  23. from pwman.data.drivers.mongodb import MongoDB
  24. # use pwmantest
  25. # db.createUser(
  26. # {
  27. # user: "tester",
  28. # pwd: "12345678",
  29. # roles: [{ role: "dbAdmin", db: "pwmantest" },
  30. # { role: "readWrite", db: "pwmantest" },]
  31. # })
  32. from pwman.util.crypto_engine import CryptoEngine
  33. class TestMongoDB(unittest.TestCase):
  34. @classmethod
  35. def setUpClass(cls):
  36. u = u"mongodb://tester:12345678@localhost:27017/pwmantest"
  37. cls.db = MongoDB(u)
  38. cls.db._open()
  39. @classmethod
  40. def tearDownClass(cls):
  41. coll = cls.db._db['crypto']
  42. coll.drop()
  43. cls.db._db['counters'].drop()
  44. cls.db._db['nodes'].drop()
  45. cls.db.close()
  46. def test_1_con(self):
  47. self.assertIsInstance(self.db._con, pymongo.Connection)
  48. @unittest.skip("MongoDB creates collections on the fly")
  49. def test_2_create_collections(self):
  50. pass
  51. def test_3_load_key(self):
  52. self.db.savekey('SECRET$6$KEY')
  53. secretkey = self.db.loadkey()
  54. self.assertEqual(secretkey, u'SECRET$6$KEY')
  55. @unittest.skip("")
  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 = [u"TBONE", u"S3K43T", u"example.org", u"some note",
  64. [u"bartag", u"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. @unittest.skip("tags are created in situ in mongodb")
  81. def test_7_get_or_create_tag(self):
  82. pass
  83. @unittest.skip("tags are removed with their node")
  84. def test_7a_clean_orphans(self):
  85. pass
  86. def test_8_remove_node(self):
  87. self.db.removenodes([1])
  88. n = self.db.listnodes()
  89. self.assertEqual(len(n), 0)
  90. @unittest.skip("No schema migration with mongodb")
  91. def test_9_check_db_version(self):
  92. pass
  93. if __name__ == '__main__':
  94. ce = CryptoEngine.get()
  95. ce.callback = DummyCallback()
  96. ce.changepassword(reader=give_key)
  97. unittest.main(verbosity=2, failfast=True)