test_datadownloads.py 13 KB

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