test_mongodb.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. if sys.version_info.major > 2: # pragma: no cover
  22. from urllib.parse import urlparse
  23. else: # pragma: no cover
  24. from urlparse import urlparse
  25. import pymongo
  26. from .test_crypto_engine import give_key, DummyCallback
  27. from pwman.util.crypto_engine import CryptoEngine
  28. from pwman.data.drivers.mongodb import MongoDB
  29. from pwman.data.nodes import Node
  30. # use pwmantest
  31. # db.createUser(
  32. # {
  33. # user: "tester",
  34. # pwd: "12345678",
  35. # roles: [{ role: "dbAdmin", db: "pwmantest" },
  36. # { role: "readWrite", db: "pwmantest" },]
  37. # })
  38. class TestMongoDB(unittest.TestCase):
  39. @classmethod
  40. def setUpClass(cls):
  41. u = u"mongodb://tester:12345678@localhost:27017/pwmantest"
  42. cls.db = MongoDB(urlparse(u))
  43. cls.db._open()
  44. @classmethod
  45. def tearDownClass(cls):
  46. coll = cls.db._db['crypto']
  47. coll.drop()
  48. cls.db._db['counters'].drop()
  49. cls.db._db['nodes'].drop()
  50. cls.db.close()
  51. def test_1_con(self):
  52. self.assertIsInstance(self.db._con, pymongo.Connection)
  53. @unittest.skip("MongoDB creates collections on the fly")
  54. def test_2_create_collections(self):
  55. pass
  56. def test_3a_load_key(self):
  57. secretkey = self.db.loadkey()
  58. self.assertIsNone(secretkey)
  59. def test_3b_load_key(self):
  60. self.db.savekey('SECRET$6$KEY')
  61. secretkey = self.db.loadkey()
  62. self.assertEqual(secretkey, u'SECRET$6$KEY')
  63. @unittest.skip("")
  64. def test_4_save_crypto(self):
  65. self.db.save_crypto_info("TOP", "SECRET")
  66. secretkey = self.db.loadkey()
  67. self.assertEqual(secretkey, 'TOP$6$SECRET')
  68. row = self.db.fetch_crypto_info()
  69. self.assertEqual(row, ('TOP', 'SECRET'))
  70. def test_5_add_node(self):
  71. innode = [u"TBONE", u"S3K43T", u"example.org", u"some note",
  72. [u"bartag", u"footag"]]
  73. kwargs = {
  74. "username": innode[0], "password": innode[1],
  75. "url": innode[2], "notes": innode[3], "tags": innode[4]
  76. }
  77. node = Node(clear_text=True, **kwargs)
  78. self.db.add_node(node)
  79. outnode = self.db.getnodes([1])[0]
  80. no = outnode[1:5]
  81. no.append(outnode[5:])
  82. o = Node.from_encrypted_entries(*no)
  83. self.assertEqual(list(node), list(o))
  84. def test_6_list_nodes(self):
  85. ret = self.db.listnodes()
  86. self.assertEqual(ret, [1])
  87. ret = self.db.listnodes(filter_=b"footag")
  88. def test_6a_list_tags(self):
  89. ret = self.db.listtags()
  90. ce = CryptoEngine.get()
  91. tags = list(map(ce.decrypt, ret))
  92. for tag in tags:
  93. self.assertIn(tag, [b'footag', b'bartag'])
  94. def test_6b_get_nodes(self):
  95. ret = self.db.getnodes([1])
  96. retb = self.db.getnodes([])
  97. self.assertListEqual(ret, retb)
  98. @unittest.skip("tags are created in situ in mongodb")
  99. def test_7_get_or_create_tag(self):
  100. pass
  101. @unittest.skip("tags are removed with their node")
  102. def test_7a_clean_orphans(self):
  103. pass
  104. def test_8_remove_node(self):
  105. self.db.removenodes([1])
  106. n = self.db.listnodes()
  107. self.assertEqual(len(n), 0)
  108. @unittest.skip("No schema migration with mongodb")
  109. def test_9_check_db_version(self):
  110. pass
  111. if __name__ == '__main__':
  112. ce = CryptoEngine.get()
  113. ce.callback = DummyCallback()
  114. ce.changepassword(reader=give_key)
  115. unittest.main(verbosity=2, failfast=True)