test_importer.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. class TestImporter(unittest.TestCase):
  33. @classmethod
  34. def setUpClass(cls):
  35. f = open('import_file.csv', 'w')
  36. f.write(import_example)
  37. f.close()
  38. @classmethod
  39. def tearDownClass(cls):
  40. for item in ('import_file.csv', 'test-importer.db',
  41. 'testfile.conf', 'importdummy.db'):
  42. try:
  43. os.unlink(item)
  44. except OSError:
  45. continue
  46. def setUp(self):
  47. config = {}
  48. db = SQLite('test-importer.db')
  49. Args = namedtuple('args', 'import_file')
  50. self.importer = CSVImporter(Args(import_file=open('import_file.csv')),
  51. config, db)
  52. def test_read_file(self):
  53. lines = self.importer._read_file()
  54. self.assertNotIn(["Username", "URL", "Password", "Notes", " Tags"],
  55. lines)
  56. def test_create_node(self):
  57. # create a node , should be encrypted, but not yet inserted to db
  58. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  59. node = self.importer._create_node(n)
  60. ce = CryptoEngine.get()
  61. self.assertEqual(ce.decrypt(node._username).decode(), u'alice')
  62. self.assertEqual(['foo', 'bar'], [t for t in node.tags])
  63. def test_insert_node(self):
  64. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  65. node = self.importer._create_node(n)
  66. self.importer._open_db()
  67. # do the actual insert of the node to the databse
  68. self.importer._insert_node(node)
  69. def test_runner(self):
  70. # test the whole procees:
  71. """
  72. open csv
  73. open db
  74. for line in csv:
  75. create node
  76. insert node
  77. close db
  78. """
  79. # args need import_file , db,
  80. Args = namedtuple('Args', 'import_file, db')
  81. if os.path.exists('importdummy.db'):
  82. os.unlink('importdummy.db')
  83. f = open('import_file.csv')
  84. args = Args(import_file=f, db='importdummy.db')
  85. f.close()
  86. dbtype, dbver, fname = 'SQLite', 0.6, 'importdummy.db'
  87. db = pwman.data.factory.create(dbtype, dbver, fname)
  88. importer = Importer((args, '', db))
  89. importer.importer.run(callback=DummyCallback)
  90. if __name__ == '__main__':
  91. ce = CryptoEngine.get()
  92. ce.callback = DummyCallback()
  93. ce.changepassword(reader=give_key)
  94. unittest.main(verbosity=2, failfast=True)