test_importer.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.util.crypto_engine import CryptoEngine
  22. from collections import namedtuple
  23. from .test_crypto_engine import give_key, DummyCallback
  24. from pwman.exchange.importer import CSVImporter
  25. from pwman.data.drivers.sqlite import SQLite
  26. import_example = """
  27. Username;URL;Password;Notes;Tags
  28. alice;wonderland.com;secert;scratch;foo,bar
  29. hatman;behindthemirror.com;pa33w0rd;scratch;foo,bar
  30. """
  31. with open('import_file.csv', 'w') as f:
  32. f.write(import_example)
  33. class TestImporter(unittest.TestCase):
  34. def setUp(self):
  35. config = {}
  36. db = SQLite('test-importer.db')
  37. Args = namedtuple('args', 'import_file')
  38. self.importer = CSVImporter(Args(import_file='import_file.csv'),
  39. config, db)
  40. def test_read_file(self):
  41. lines = self.importer._read_file()
  42. self.assertNotIn(["Username", "URL", "Password", "Notes", " Tags"],
  43. lines)
  44. def test_create_node(self):
  45. # create a node , should be encrypted, but not yet inserted to db
  46. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  47. node = self.importer._create_node(n)
  48. ce = CryptoEngine.get()
  49. self.assertEqual(ce.decrypt(node._username).decode(), u'alice')
  50. self.assertEqual(['foo', 'bar'], [t for t in node.tags])
  51. def test_insert_node(self):
  52. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  53. node = self.importer._create_node(n)
  54. self.importer._open_db()
  55. # do the actual insert of the node to the databse
  56. self.importer._insert_node(node)
  57. def test_runner(self):
  58. # test the whole procees:
  59. """
  60. open csv
  61. open db
  62. for line in csv:
  63. create node
  64. insert node
  65. close db
  66. """
  67. pass
  68. if __name__ == '__main__':
  69. ce = CryptoEngine.get()
  70. ce.callback = DummyCallback()
  71. ce.changepassword(reader=give_key)
  72. try:
  73. unittest.main(verbosity=2, failfast=True)
  74. except SystemExit:
  75. os.remove('import_file.csv')