test_datadownloads.py 12 KB

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