You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.4 KiB

  1. # coding: utf-8
  2. # @ 2015 Valentin CHEMIERE @ Akretion
  3. # ©2016 @author Mourad EL HADJ MIMOUNE <mourad.elhadj.mimoune@akretion.com>
  4. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  5. import logging
  6. from base64 import b64decode
  7. import hashlib
  8. from .common import TestConnection, ContextualStringIO
  9. from .mock_server import server_mock_sftp
  10. from .mock_server import MultiResponse
  11. from openerp.exceptions import UserError
  12. _logger = logging.getLogger(__name__)
  13. class TestSftpConnection(TestConnection):
  14. def setUp(self):
  15. super(TestSftpConnection, self).setUp()
  16. self.test_file_sftp = ContextualStringIO()
  17. self.test_file_sftp.write('import sftp')
  18. self.test_file_sftp.seek(0)
  19. def test_00_sftp_import(self):
  20. task = self.env.ref('external_file_location.sftp_import_task')
  21. with server_mock_sftp(
  22. {'open': self.test_file_sftp,
  23. 'listdir': [task.filename]}):
  24. task.run_import()
  25. search_file = self.env['ir.attachment.metadata'].search(
  26. [('name', '=', task.filename)])
  27. self.assertEqual(len(search_file), 1)
  28. self.assertEqual(b64decode(search_file[0].datas), 'import sftp')
  29. def test_01_sftp_export(self):
  30. self.task = self.env.ref('external_file_location.sftp_export_task')
  31. self.sftp_attachment = self.env.ref(
  32. 'external_file_location.ir_attachment_export_file_sftp')
  33. with server_mock_sftp(
  34. {'setcontents': ''}) as FakeSFTP:
  35. self.task.run_export()
  36. if FakeSFTP:
  37. self.assertEqual('setcontents', FakeSFTP[-1]['method'])
  38. self.assertEqual(
  39. '/home/user/test/sftp_test_export.txt',
  40. FakeSFTP[-1]['args'][0])
  41. self.assertEqual(
  42. 'test sftp file export',
  43. FakeSFTP[-1]['kwargs']['data'])
  44. def test_02_sftp_import_md5(self):
  45. md5_file = ContextualStringIO()
  46. md5_file.write(hashlib.md5('import sftp').hexdigest())
  47. md5_file.seek(0)
  48. task = self.env.ref('external_file_location.sftp_import_task')
  49. task.md5_check = True
  50. with server_mock_sftp(
  51. {'open': MultiResponse({
  52. 1: md5_file,
  53. 0: self.test_file_sftp}),
  54. 'listdir': [task.filename]}) as FakeSFTP:
  55. task.run_import()
  56. search_file = self.env['ir.attachment.metadata'].search(
  57. (('name', '=', task.filename),))
  58. self.assertEqual(len(search_file), 1)
  59. self.assertEqual(b64decode(search_file[0].datas),
  60. 'import sftp')
  61. self.assertEqual('open', FakeSFTP[-1]['method'])
  62. self.assertEqual(hashlib.md5('import sftp').hexdigest(),
  63. search_file.external_hash)
  64. def test_03_sftp_import_md5_corrupt_file(self):
  65. md5_file = ContextualStringIO()
  66. md5_file.write(hashlib.md5('import test sftp corrupted').hexdigest())
  67. md5_file.seek(0)
  68. task = self.env.ref('external_file_location.sftp_import_task')
  69. task.md5_check = True
  70. with server_mock_sftp(
  71. {'open': MultiResponse({
  72. 1: md5_file,
  73. 0: self.test_file_sftp}),
  74. 'listdir': [task.filename]}):
  75. with self.assertRaises(UserError):
  76. task.run_import()