test_importer.py 3.5 KB

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