test_datadownloads.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import os
  2. from django.core.files import File
  3. from misago.threads.models import Attachment, AttachmentType
  4. from misago.users.audittrail import create_user_audit_trail
  5. from misago.users.datadownloads import (
  6. expire_user_data_download, prepare_user_data_download, request_user_data_download,
  7. user_has_data_download_request
  8. )
  9. from misago.users.models import DataDownload
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  12. TEST_FILE_PATH = os.path.join(TESTFILES_DIR, 'avatar.png')
  13. class ExpireUserDataDownloadTests(AuthenticatedUserTestCase):
  14. def test_util_marks_download_as_expired(self):
  15. """expire_user_data_download changed data download status to expired"""
  16. data_download = request_user_data_download(self.user)
  17. data_download.status = DataDownload.STATUS_READY
  18. with open(TEST_FILE_PATH, 'rb') as download_file:
  19. data_download.file = File(download_file)
  20. data_download.save()
  21. expire_user_data_download(data_download)
  22. self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
  23. def test_util_deletes_file(self):
  24. """expire_user_data_download deleted file associated with download"""
  25. data_download = request_user_data_download(self.user)
  26. data_download.status = DataDownload.STATUS_READY
  27. with open(TEST_FILE_PATH, 'rb') as download_file:
  28. data_download.file = File(download_file)
  29. data_download.save()
  30. download_file_path = data_download.file.path
  31. expire_user_data_download(data_download)
  32. self.assertFalse(data_download.file)
  33. self.assertFalse(os.path.isdir(download_file_path))
  34. def test_util_expires_download_without_file(self):
  35. """expire_user_data_download handles missing download file"""
  36. data_download = request_user_data_download(self.user)
  37. data_download.status = DataDownload.STATUS_READY
  38. expire_user_data_download(data_download)
  39. self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
  40. class PrepareUserDataDownload(AuthenticatedUserTestCase):
  41. def setUp(self):
  42. super(PrepareUserDataDownload, self).setUp()
  43. self.download = request_user_data_download(self.user)
  44. def assert_download_is_valid(self):
  45. result = prepare_user_data_download(self.download)
  46. self.assertTrue(result)
  47. self.download.refresh_from_db()
  48. self.assertTrue(self.download.file)
  49. def test_prepare_basic_download(self):
  50. """function creates data download for basic user account"""
  51. self.assert_download_is_valid()
  52. def test_prepare_download_with_profle_fields(self):
  53. """function creates data download for user with profile fields"""
  54. self.user.profile_fields = {'real_name': "Bob Boberthon!"}
  55. self.user.save()
  56. self.assert_download_is_valid()
  57. def test_prepare_download_with_tmp_avatar(self):
  58. """function creates data download for user with tmp avatar"""
  59. with open(TEST_FILE_PATH, 'rb') as test_file:
  60. self.user.avatar_tmp = File(test_file)
  61. self.user.save()
  62. self.assert_download_is_valid()
  63. def test_prepare_download_with_src_avatar(self):
  64. """function creates data download for user with src avatar"""
  65. with open(TEST_FILE_PATH, 'rb') as test_file:
  66. self.user.avatar_src = File(test_file)
  67. self.user.save()
  68. self.assert_download_is_valid()
  69. def test_prepare_download_with_avatar_set(self):
  70. """function creates data download for user with avatar set"""
  71. with open(TEST_FILE_PATH, 'rb') as test_file:
  72. self.user.avatar_set.create(size=100, image=File(test_file))
  73. self.assert_download_is_valid()
  74. def test_prepare_download_with_file_attachment(self):
  75. """function creates data download for user with file attachment"""
  76. filetype = AttachmentType.objects.create(
  77. name="Test extension",
  78. extensions='png',
  79. mimetypes='image/png',
  80. )
  81. with open(TEST_FILE_PATH, 'rb') as test_file:
  82. self.user.attachment_set.create(
  83. secret='test',
  84. filetype=filetype,
  85. uploader_name=self.user.username,
  86. uploader_slug=self.user.slug,
  87. filename='test.png',
  88. size=1000,
  89. file=File(test_file),
  90. )
  91. self.assert_download_is_valid()
  92. def test_prepare_download_with_image_attachment(self):
  93. """function creates data download for user with image attachment"""
  94. filetype = AttachmentType.objects.create(
  95. name="Test extension",
  96. extensions='png',
  97. mimetypes='image/png',
  98. )
  99. with open(TEST_FILE_PATH, 'rb') as test_file:
  100. self.user.attachment_set.create(
  101. secret='test',
  102. filetype=filetype,
  103. uploader_name=self.user.username,
  104. uploader_slug=self.user.slug,
  105. filename='test.png',
  106. size=1000,
  107. image=File(test_file),
  108. )
  109. self.assert_download_is_valid()
  110. def test_prepare_download_with_thumbnail_attachment(self):
  111. """function creates data download for user with thumbnail attachment"""
  112. filetype = AttachmentType.objects.create(
  113. name="Test extension",
  114. extensions='png',
  115. mimetypes='image/png',
  116. )
  117. with open(TEST_FILE_PATH, 'rb') as test_file:
  118. self.user.attachment_set.create(
  119. secret='test',
  120. filetype=filetype,
  121. uploader_name=self.user.username,
  122. uploader_slug=self.user.slug,
  123. filename='test.png',
  124. size=1000,
  125. thumbnail=File(test_file),
  126. )
  127. self.assert_download_is_valid()
  128. def test_prepare_download_with_self_username_change(self):
  129. """function creates data download for user that changed their username"""
  130. self.user.record_name_change(self.user, 'aerith', 'alice')
  131. self.assert_download_is_valid()
  132. def test_prepare_download_with_username_changed_by_staff(self):
  133. """function creates data download for user with username changed by staff"""
  134. staff_user = self.get_superuser()
  135. self.user.record_name_change(staff_user, 'aerith', 'alice')
  136. self.assert_download_is_valid()
  137. def test_prepare_download_with_username_changed_by_deleted_user(self):
  138. """function creates data download for user with username changed by deleted user"""
  139. self.user.record_name_change(self.user, 'aerith', 'alice')
  140. self.user.namechanges.update(changed_by=None)
  141. self.assert_download_is_valid()
  142. def test_prepare_download_with_audit_trail(self):
  143. """function creates data download for user with audit trail"""
  144. create_user_audit_trail(self.user, '127.0.0.1', self.user)
  145. self.assert_download_is_valid()
  146. class RequestUserDataDownloadTests(AuthenticatedUserTestCase):
  147. def test_util_creates_data_download_for_user_with_them_as_requester(self):
  148. """request_user_data_download created valid data download for user"""
  149. data_download = request_user_data_download(self.user)
  150. self.assertEqual(data_download.user, self.user)
  151. self.assertEqual(data_download.requester, self.user)
  152. self.assertEqual(data_download.requester_name, self.user.username)
  153. self.assertEqual(data_download.status, DataDownload.STATUS_PENDING)
  154. def test_util_creates_data_download_for_user_explicit_requester(self):
  155. """request_user_data_download created valid data download for user with other requester"""
  156. requester = self.get_superuser()
  157. data_download = request_user_data_download(self.user, requester)
  158. self.assertEqual(data_download.user, self.user)
  159. self.assertEqual(data_download.requester, requester)
  160. self.assertEqual(data_download.requester_name, requester.username)
  161. self.assertEqual(data_download.status, DataDownload.STATUS_PENDING)
  162. class UserHasRequestedDataDownloadTests(AuthenticatedUserTestCase):
  163. def test_util_returns_false_for_no_download(self):
  164. """user_has_data_download_request returns false if user has no requests in progress"""
  165. self.assertFalse(user_has_data_download_request(self.user))
  166. def test_util_returns_false_for_ready_download(self):
  167. """user_has_data_download_request returns false if user has ready download"""
  168. data_download = request_user_data_download(self.user)
  169. data_download.status = DataDownload.STATUS_READY
  170. data_download.save()
  171. self.assertFalse(user_has_data_download_request(self.user))
  172. def test_util_returns_false_for_expired_download(self):
  173. """user_has_data_download_request returns false if user has expired download"""
  174. data_download = request_user_data_download(self.user)
  175. data_download.status = DataDownload.STATUS_EXPIRED
  176. data_download.save()
  177. self.assertFalse(user_has_data_download_request(self.user))
  178. def test_util_returns_true_for_pending_download(self):
  179. """user_has_data_download_request returns true if user has pending download"""
  180. data_download = request_user_data_download(self.user)
  181. data_download.status = DataDownload.STATUS_PENDING
  182. data_download.save()
  183. self.assertTrue(user_has_data_download_request(self.user))
  184. def test_util_returns_true_for_processing_download(self):
  185. """user_has_data_download_request returns true if user has processing download"""
  186. data_download = request_user_data_download(self.user)
  187. data_download.status = DataDownload.STATUS_PROCESSING
  188. data_download.save()
  189. self.assertTrue(user_has_data_download_request(self.user))