test_importer.py 3.9 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-2017 Oz Nahum Tiram <oz.tiram@gmail.com>
  18. # ============================================================================
  19. import os
  20. import unittest
  21. import sys
  22. from collections import namedtuple
  23. import pwman.data.factory
  24. from pwman.util.crypto_engine import CryptoEngine
  25. from pwman.exchange.importer import CSVImporter, Importer
  26. from pwman.data.drivers.sqlite import SQLite
  27. from .test_crypto_engine import give_key, DummyCallback
  28. import_example = """
  29. Username;URL;Password;Notes;Tags
  30. alice;wonderland.com;secert;scratch;foo,bar
  31. hatman;behindthemirror.com;pa33w0rd;scratch;foo,bar
  32. """
  33. class TestImporter(unittest.TestCase):
  34. @classmethod
  35. def setUpClass(cls):
  36. f = open('import_file.csv', 'w')
  37. f.write(import_example)
  38. f.close()
  39. @classmethod
  40. def tearDownClass(cls):
  41. for item in ('import_file.csv', 'test-importer.db',
  42. 'testfile.conf', 'importdummy.db'):
  43. try:
  44. os.unlink(item)
  45. except OSError:
  46. continue
  47. def setUp(self):
  48. config = {}
  49. db = SQLite('test-importer.db')
  50. Args = namedtuple('args', 'file_delim')
  51. args = Args(file_delim=['import_file.csv', ';'])
  52. self.importer = CSVImporter(args,
  53. config, db)
  54. def test_1_read_file(self):
  55. lines = self.importer._read_file()
  56. self.assertNotIn(["Username", "URL", "Password", "Notes", " Tags"],
  57. lines)
  58. def test_2_create_node(self):
  59. # create a node , should be encrypted, but not yet inserted to db
  60. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  61. node = self.importer._create_node(n)
  62. ce = CryptoEngine.get()
  63. self.assertEqual(ce.decrypt(node._username).decode(), u'alice')
  64. self.assertEqual([b'foo', b'bar'], [t for t in node.tags])
  65. def test_3_insert_node(self):
  66. self.importer._open_db()
  67. n = "alice;wonderland.com;secert;scratch;foo,bar".split(";")
  68. node = self.importer._create_node(n)
  69. # do the actual insert of the node to the databse
  70. self.importer._insert_node(node)
  71. def test_4_runner(self):
  72. # test the whole procees:
  73. """
  74. open csv
  75. open db
  76. for line in csv:
  77. create node
  78. insert node
  79. close db
  80. """
  81. # args need import_file , db,
  82. Args = namedtuple('Args', 'file_delim, db')
  83. if os.path.exists('importdummy.db'):
  84. os.unlink('importdummy.db')
  85. args = Args(file_delim=['import_file.csv', ';'], db='importdummy.db')
  86. p = os.getcwd()
  87. if sys.platform.startswith("win"):
  88. p = p.strip("C:\\")
  89. print(os.getcwd())
  90. db = pwman.data.factory.createdb('sqlite:///' + p +
  91. '/importdummy.db', 0.6)
  92. importer = Importer((args, '', db))
  93. importer.importer.run(callback=DummyCallback)
  94. if __name__ == '__main__':
  95. ce = CryptoEngine.get()
  96. ce.callback = DummyCallback()
  97. ce.changepassword(reader=give_key)
  98. unittest.main(verbosity=2, failfast=True)