test_datadownloads.py 12 KB

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