test_dataarchive.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. import os
  3. from collections import OrderedDict
  4. from django.core.files import File
  5. from misago.conf import settings
  6. from misago.users.dataarchive import DataArchive
  7. from misago.users.testutils import AuthenticatedUserTestCase
  8. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  9. TEST_AVATAR_PATH = os.path.join(TESTFILES_DIR, 'avatar.png')
  10. DATA_DOWNLOADS_WORKING_DIR = settings.MISAGO_USER_DATA_DOWNLOADS_WORKING_DIR
  11. class DataArchiveTests(AuthenticatedUserTestCase):
  12. def test_enter_without_dirs(self):
  13. """data archive doesn't touch filesystem on init"""
  14. archive = DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR)
  15. self.assertEqual(archive.user, self.user)
  16. self.assertEqual(archive.working_dir_path, DATA_DOWNLOADS_WORKING_DIR)
  17. self.assertIsNone(archive.tmp_dir_path)
  18. self.assertIsNone(archive.data_dir_path)
  19. def test_context_life_cycle(self):
  20. """object creates valid tmp directory on enter and cleans on exit"""
  21. tmp_dir_path = None
  22. data_dir_path = None
  23. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  24. self.assertTrue(os.path.exists(archive.tmp_dir_path))
  25. self.assertTrue(os.path.exists(archive.data_dir_path))
  26. working_dir = str(DATA_DOWNLOADS_WORKING_DIR)
  27. tmp_dir_path = str(archive.tmp_dir_path)
  28. data_dir_path = str(archive.data_dir_path)
  29. self.assertTrue(tmp_dir_path.startswith(working_dir))
  30. self.assertTrue(data_dir_path.startswith(working_dir))
  31. self.assertTrue(data_dir_path.startswith(working_dir))
  32. self.assertTrue(data_dir_path.startswith(tmp_dir_path))
  33. self.assertIsNone(archive.tmp_dir_path)
  34. self.assertIsNone(archive.data_dir_path)
  35. self.assertFalse(os.path.exists(tmp_dir_path))
  36. self.assertFalse(os.path.exists(data_dir_path))
  37. def test_add_text_str(self):
  38. """add_dict method creates text file with string"""
  39. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  40. data_to_write = u"Hello, łorld!"
  41. file_path = archive.add_text('testfile', data_to_write)
  42. self.assertTrue(os.path.isfile(file_path))
  43. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  44. self.assertEqual(file_path, valid_output_path)
  45. with open(file_path, 'r', encoding="utf-8") as fp:
  46. saved_data = fp.read().strip()
  47. self.assertEqual(saved_data, data_to_write)
  48. def test_add_text_int(self):
  49. """add_dict method creates text file with int"""
  50. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  51. data_to_write = 1234
  52. file_path = archive.add_text('testfile', data_to_write)
  53. self.assertTrue(os.path.isfile(file_path))
  54. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  55. self.assertEqual(file_path, valid_output_path)
  56. with open(file_path, 'r', encoding="utf-8") as fp:
  57. saved_data = fp.read().strip()
  58. self.assertEqual(saved_data, str(data_to_write))
  59. def test_add_text_path(self):
  60. """add_dict method creates text file under path"""
  61. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  62. data_to_write = u"Hello, łorld!"
  63. file_path = archive.add_text(
  64. 'testfile', data_to_write, path=['avatars', 'tmp'])
  65. self.assertTrue(os.path.isfile(file_path))
  66. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  67. self.assertEqual(file_path, valid_output_path)
  68. data_dir_path = str(archive.data_dir_path)
  69. self.assertTrue(str(valid_output_path).startswith(data_dir_path))
  70. self.assertIn('/avatars/tmp/', str(valid_output_path))
  71. def test_add_dict(self):
  72. """add_dict method creates text file from dict"""
  73. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  74. data_to_write = {'first': u"łorld!", 'second': u"łup!"}
  75. file_path = archive.add_dict('testfile', data_to_write)
  76. self.assertTrue(os.path.isfile(file_path))
  77. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  78. self.assertEqual(file_path, valid_output_path)
  79. with open(file_path, 'r', encoding="utf-8") as fp:
  80. saved_data = fp.read().strip()
  81. self.assertEqual(saved_data, u"first: łorld!\nsecond: łup!")
  82. def test_add_dict_ordered(self):
  83. """add_dict method creates text file form ordered dict"""
  84. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  85. data_to_write = OrderedDict((('first', u"łorld!"), ('second', u"łup!")))
  86. file_path = archive.add_dict('testfile', data_to_write)
  87. self.assertTrue(os.path.isfile(file_path))
  88. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  89. self.assertEqual(file_path, valid_output_path)
  90. with open(file_path, 'r', encoding="utf-8") as fp:
  91. saved_data = fp.read().strip()
  92. self.assertEqual(saved_data, u"first: łorld!\nsecond: łup!")
  93. def test_add_dict_path(self):
  94. """add_dict method creates text file under path"""
  95. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  96. data_to_write = {'first': u"łorld!", 'second': u"łup!"}
  97. file_path = archive.add_dict(
  98. 'testfile', data_to_write, path=['avatars', 'tmp']))
  99. self.assertTrue(os.path.isfile(file_path))
  100. valid_output_path = os.path.join(archive.data_dir_path, 'testfile.txt')
  101. self.assertEqual(file_path, valid_output_path)
  102. data_dir_path = str(archive.data_dir_path)
  103. self.assertTrue(str(valid_output_path).startswith(data_dir_path))
  104. self.assertIn('/avatars/tmp/', str(valid_output_path))
  105. def test_add_model_file(self):
  106. """add_model_file method adds model file"""
  107. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  108. self.user.avatar_tmp = File(avatar)
  109. self.user.save()
  110. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  111. file_path = archive.add_model_file(self.user.avatar_tmp)
  112. self.assertTrue(os.path.isfile(file_path))
  113. data_dir_path = str(archive.data_dir_path)
  114. self.assertTrue(str(file_path).startswith(data_dir_path))
  115. def test_add_model_file_empty(self):
  116. """add_model_file method is noop if model field is empty"""
  117. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  118. file_path = archive.add_model_file(self.user.avatar_tmp)
  119. self.assertIsNone(file_path)
  120. self.assertFalse(os.listdir(archive.data_dir_path))
  121. def test_add_model_file_path(self):
  122. """add_model_file method adds model file under path"""
  123. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  124. self.user.avatar_tmp = File(avatar)
  125. self.user.save()
  126. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  127. file_path = archive.add_model_file(
  128. self.user.avatar_tmp, path=['avatars', 'tmp'])
  129. self.assertTrue(os.path.isfile(file_path))
  130. data_dir_path = str(archive.data_dir_path)
  131. self.assertTrue(str(file_path).startswith(data_dir_path))
  132. self.assertIn('/avatars/tmp/', str(file_path))
  133. def test_get_file(self):
  134. """get_file returns django file"""
  135. django_file = None
  136. with open(TEST_AVATAR_PATH, 'rb') as avatar:
  137. self.user.avatar_tmp = File(avatar)
  138. self.user.save()
  139. with DataArchive(self.user, DATA_DOWNLOADS_WORKING_DIR) as archive:
  140. archive.add_model_file(self.user.avatar_tmp)
  141. django_file = archive.get_file()
  142. archive_path = archive.file_path
  143. self.assertIsNotNone(archive_path)
  144. self.assertEqual(django_file.name, archive.file_path)
  145. self.assertFalse(django_file.closed)
  146. self.assertIsNone(archive.file)
  147. self.assertIsNone(archive.file_path)
  148. self.assertTrue(django_file.closed)