test_sqlite.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import os
  20. import unittest
  21. from pwman.data.drivers.sqlite import SQLite
  22. from pwman.data.nodes import Node
  23. from pwman.util.crypto_engine import CryptoEngine
  24. class TestSQLite(unittest.TestCase):
  25. def setUp(self):
  26. self.db = SQLite('test.db')
  27. self.db._open()
  28. def test_1_create_tables(self):
  29. self.db._create_tables()
  30. self.db._con.commit()
  31. # the method _open calls _create_tables
  32. self.db.save_crypto_info("foo", "bar")
  33. self.db._create_tables()
  34. def test_1a_create_tables(self):
  35. self.db._create_tables()
  36. def test_2_crypto_info(self):
  37. self.db._create_tables()
  38. self.db.save_crypto_info("foo", "bar")
  39. f = self.db.fetch_crypto_info()
  40. self.assertListEqual([u'foo', u'bar'], list(f))
  41. def test_3_add_node(self):
  42. node = Node(clear_text=True,
  43. **{'username': u"alice", 'password': u"secret",
  44. 'url': u"wonderland.com",
  45. 'notes': u"a really great place",
  46. 'tags': [u'foo', u'bar']})
  47. self.db.add_node(node)
  48. rv = self.db._cur.execute("select * from node")
  49. # clearly this fails, while alice is not found in clear text in the
  50. # database!
  51. ce = CryptoEngine.get()
  52. res = rv.fetchone()
  53. self.assertIn(ce.encrypt(u'alice'), res[1])
  54. def test_4_test_tags(self):
  55. node = Node(clear_text=True,
  56. **{'username': u"alice", 'password': u"secret",
  57. 'url': u"wonderland.com",
  58. 'notes': u"a really great place",
  59. 'tags': [u'foo', u'bar']})
  60. ce = CryptoEngine.get()
  61. self.db._get_or_create_tag(node._tags[0])
  62. self.assertEqual(1, self.db._get_or_create_tag(node._tags[0]))
  63. self.assertEqual(3, self.db._get_or_create_tag(ce.encrypt('baz')))
  64. def test_5_test_lookup(self):
  65. self.db._cur.execute('SELECT * FROM LOOKUP')
  66. rows = self.db._cur.fetchall()
  67. self.assertEqual(2, len(rows))
  68. def test_6_listnodes(self):
  69. node = Node(clear_text=True,
  70. **{'username': u"hatman", 'password': u"secret",
  71. 'url': u"wonderland.com",
  72. 'notes': u"a really great place",
  73. 'tags': [u'baz', u'bar']})
  74. self.db.add_node(node)
  75. ids = self.db.listnodes()
  76. self.assertEqual(2, len(ids))
  77. def test_7_listnodes_w_filter(self):
  78. ce = CryptoEngine.get()
  79. tag = ce.encrypt(u'bar')
  80. rv = self.db.listnodes(tag)
  81. self.assertEqual(len(rv), 2)
  82. tag = ce.encrypt(u'baz')
  83. rv = self.db.listnodes(tag)
  84. self.assertEqual(len(rv), 1)
  85. def test_8_getnodes(self):
  86. nodes = self.db.getnodes([1, 2])
  87. self.assertEqual(len(nodes), 2)
  88. def test_9_editnode(self):
  89. # delibertly insert clear text into the database
  90. node = {'user': 'transparent', 'password': 'notsecret'}
  91. self.db.editnode(2, **node)
  92. self.db._cur.execute('SELECT USER, PASSWORD FROM NODE WHERE ID=2')
  93. rv = self.db._cur.fetchone()
  94. self.assertEqual(rv, (u'transparent', u'notsecret'))
  95. def tearDown(self):
  96. self.db.close()
  97. if __name__ == '__main__':
  98. try:
  99. unittest.main(verbosity=2)
  100. except SystemExit:
  101. os.remove('test.db')