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.

86 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_ftp
  10. from .mock_server import MultiResponse
  11. from openerp.exceptions import UserError
  12. _logger = logging.getLogger(__name__)
  13. class TestFtpConnection(TestConnection):
  14. def setUp(self):
  15. super(TestFtpConnection, self).setUp()
  16. self.test_file_ftp = ContextualStringIO()
  17. self.test_file_ftp.write('import ftp')
  18. self.test_file_ftp.seek(0)
  19. def test_00_ftp_import(self):
  20. self.task = self.env.ref('external_file_location.ftp_import_task')
  21. with server_mock_ftp(
  22. {'open': self.test_file_ftp,
  23. 'listdir': ['test-import-ftp.txt']}):
  24. self.task.run_import()
  25. search_file = self.env['ir.attachment.metadata'].search(
  26. [('name', '=', 'test-import-ftp.txt')])
  27. self.assertEqual(len(search_file), 1)
  28. self.assertEqual(b64decode(search_file[0].datas), 'import ftp')
  29. def test_01_ftp_export(self):
  30. self.task = self.env.ref('external_file_location.ftp_export_task')
  31. self.ftp_attachment = self.env.ref(
  32. 'external_file_location.ir_attachment_export_file_ftp')
  33. with server_mock_ftp(
  34. {'setcontents': ''}) as FakeFTP:
  35. self.task.run_export()
  36. if FakeFTP:
  37. self.assertEqual('setcontents', FakeFTP[-1]['method'])
  38. self.assertEqual('done', self.ftp_attachment.state)
  39. self.assertEqual(
  40. '/home/user/test/ftp_test_export.txt',
  41. FakeFTP[-1]['args'][0])
  42. self.assertEqual(
  43. 'test ftp file export',
  44. FakeFTP[-1]['kwargs']['data'])
  45. def test_02_ftp_import_md5(self):
  46. md5_file = ContextualStringIO()
  47. md5_file.write(hashlib.md5('import ftp').hexdigest())
  48. md5_file.seek(0)
  49. task = self.env.ref('external_file_location.ftp_import_task')
  50. task.md5_check = True
  51. with server_mock_ftp(
  52. {'open': MultiResponse({
  53. 1: md5_file,
  54. 0: self.test_file_ftp}),
  55. 'listdir': [task.filename]}) as Fakeftp:
  56. task.run_import()
  57. search_file = self.env['ir.attachment.metadata'].search(
  58. (('name', '=', task.filename),))
  59. self.assertEqual(len(search_file), 1)
  60. self.assertEqual(b64decode(search_file[0].datas),
  61. 'import ftp')
  62. self.assertEqual('open', Fakeftp[-1]['method'])
  63. self.assertEqual(hashlib.md5('import ftp').hexdigest(),
  64. search_file.external_hash)
  65. def test_03_ftp_import_md5_corrupt_file(self):
  66. md5_file = ContextualStringIO()
  67. md5_file.write(hashlib.md5('import test ftp corrupted').hexdigest())
  68. md5_file.seek(0)
  69. task = self.env.ref('external_file_location.ftp_import_task')
  70. task.md5_check = True
  71. with server_mock_ftp(
  72. {'open': MultiResponse({
  73. 1: md5_file,
  74. 0: self.test_file_ftp}),
  75. 'listdir': [task.filename]}):
  76. with self.assertRaises(UserError):
  77. task.run_import()