test_datadownloads.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import os
  2. from django.core.files import File
  3. from ...categories.models import Category
  4. from ...threads.models import 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. """
  137. function creates data download for user with username changed by deleted user
  138. """
  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. def test_prepare_download_with_post(self):
  147. """function creates data download for user with post"""
  148. category = Category.objects.get(slug="first-category")
  149. post_thread(category, poster=self.user)
  150. self.assert_download_is_valid()
  151. def test_prepare_download_with_owm_post_edit(self):
  152. """function creates data download for user with own post edit"""
  153. category = Category.objects.get(slug="first-category")
  154. thread = post_thread(category, poster=self.user)
  155. post = thread.first_post
  156. post.edits_record.create(
  157. category=category,
  158. thread=thread,
  159. editor=self.user,
  160. editor_name=self.user.username,
  161. editor_slug=self.user.slug,
  162. edited_from="edited from",
  163. edited_to="edited to",
  164. )
  165. self.assert_download_is_valid()
  166. def test_prepare_download_with_other_users_post_edit(self):
  167. """function creates data download for user with other user's post edit"""
  168. category = Category.objects.get(slug="first-category")
  169. thread = post_thread(category)
  170. post = thread.first_post
  171. post.edits_record.create(
  172. category=category,
  173. thread=thread,
  174. editor=self.user,
  175. editor_name=self.user.username,
  176. editor_slug=self.user.slug,
  177. edited_from="edited from",
  178. edited_to="edited to",
  179. )
  180. self.assert_download_is_valid()
  181. def test_prepare_download_with_own_post_edit_by_staff(self):
  182. """function creates data download for user with post edited by staff"""
  183. category = Category.objects.get(slug="first-category")
  184. thread = post_thread(category, poster=self.user)
  185. post = thread.first_post
  186. staff_user = self.get_superuser()
  187. post.edits_record.create(
  188. category=category,
  189. thread=thread,
  190. editor=staff_user,
  191. editor_name=staff_user.username,
  192. editor_slug=staff_user.slug,
  193. edited_from="edited from",
  194. edited_to="edited to",
  195. )
  196. self.assert_download_is_valid()
  197. def test_prepare_download_with_poll(self):
  198. """function creates data download for user with poll"""
  199. category = Category.objects.get(slug="first-category")
  200. thread = post_thread(category, poster=self.user)
  201. post_poll(thread, self.user)
  202. self.assert_download_is_valid()
  203. class RequestUserDataDownloadTests(AuthenticatedUserTestCase):
  204. def test_util_creates_data_download_for_user_with_them_as_requester(self):
  205. """request_user_data_download created valid data download for user"""
  206. data_download = request_user_data_download(self.user)
  207. self.assertEqual(data_download.user, self.user)
  208. self.assertEqual(data_download.requester, self.user)
  209. self.assertEqual(data_download.requester_name, self.user.username)
  210. self.assertEqual(data_download.status, DataDownload.STATUS_PENDING)
  211. def test_util_creates_data_download_for_user_explicit_requester(self):
  212. """
  213. request_user_data_download created valid data download
  214. for user with other requester
  215. """
  216. requester = self.get_superuser()
  217. data_download = request_user_data_download(self.user, requester)
  218. self.assertEqual(data_download.user, self.user)
  219. self.assertEqual(data_download.requester, requester)
  220. self.assertEqual(data_download.requester_name, requester.username)
  221. self.assertEqual(data_download.status, DataDownload.STATUS_PENDING)
  222. class UserHasRequestedDataDownloadTests(AuthenticatedUserTestCase):
  223. def test_util_returns_false_for_no_download(self):
  224. """
  225. user_has_data_download_request returns false if user has no requests in progress
  226. """
  227. self.assertFalse(user_has_data_download_request(self.user))
  228. def test_util_returns_false_for_ready_download(self):
  229. """user_has_data_download_request returns false if user has ready download"""
  230. data_download = request_user_data_download(self.user)
  231. data_download.status = DataDownload.STATUS_READY
  232. data_download.save()
  233. self.assertFalse(user_has_data_download_request(self.user))
  234. def test_util_returns_false_for_expired_download(self):
  235. """user_has_data_download_request returns false if user has expired download"""
  236. data_download = request_user_data_download(self.user)
  237. data_download.status = DataDownload.STATUS_EXPIRED
  238. data_download.save()
  239. self.assertFalse(user_has_data_download_request(self.user))
  240. def test_util_returns_true_for_pending_download(self):
  241. """user_has_data_download_request returns true if user has pending download"""
  242. data_download = request_user_data_download(self.user)
  243. data_download.status = DataDownload.STATUS_PENDING
  244. data_download.save()
  245. self.assertTrue(user_has_data_download_request(self.user))
  246. def test_util_returns_true_for_processing_download(self):
  247. """
  248. user_has_data_download_request returns true if user has processing download
  249. """
  250. data_download = request_user_data_download(self.user)
  251. data_download.status = DataDownload.STATUS_PROCESSING
  252. data_download.save()
  253. self.assertTrue(user_has_data_download_request(self.user))