test_media_storage.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from synapse.rest.media.v1._base import FileInfo
  17. from synapse.rest.media.v1.media_storage import MediaStorage
  18. from synapse.rest.media.v1.filepath import MediaFilePaths
  19. from synapse.rest.media.v1.storage_provider import FileStorageProviderBackend
  20. from mock import Mock
  21. from tests import unittest
  22. import os
  23. import shutil
  24. import tempfile
  25. class MediaStorageTests(unittest.TestCase):
  26. def setUp(self):
  27. self.test_dir = tempfile.mkdtemp(prefix="synapse-tests-")
  28. self.primary_base_path = os.path.join(self.test_dir, "primary")
  29. self.secondary_base_path = os.path.join(self.test_dir, "secondary")
  30. hs = Mock()
  31. hs.config.media_store_path = self.primary_base_path
  32. storage_providers = [FileStorageProviderBackend(
  33. hs, self.secondary_base_path
  34. )]
  35. self.filepaths = MediaFilePaths(self.primary_base_path)
  36. self.media_storage = MediaStorage(
  37. self.primary_base_path, self.filepaths, storage_providers,
  38. )
  39. def tearDown(self):
  40. shutil.rmtree(self.test_dir)
  41. @defer.inlineCallbacks
  42. def test_ensure_media_is_in_local_cache(self):
  43. media_id = "some_media_id"
  44. test_body = "Test\n"
  45. # First we create a file that is in a storage provider but not in the
  46. # local primary media store
  47. rel_path = self.filepaths.local_media_filepath_rel(media_id)
  48. secondary_path = os.path.join(self.secondary_base_path, rel_path)
  49. os.makedirs(os.path.dirname(secondary_path))
  50. with open(secondary_path, "w") as f:
  51. f.write(test_body)
  52. # Now we run ensure_media_is_in_local_cache, which should copy the file
  53. # to the local cache.
  54. file_info = FileInfo(None, media_id)
  55. local_path = yield self.media_storage.ensure_media_is_in_local_cache(file_info)
  56. self.assertTrue(os.path.exists(local_path))
  57. # Asserts the file is under the expected local cache directory
  58. self.assertEquals(
  59. os.path.commonprefix([self.primary_base_path, local_path]),
  60. self.primary_base_path,
  61. )
  62. with open(local_path) as f:
  63. body = f.read()
  64. self.assertEqual(test_body, body)