|
@@ -3,7 +3,8 @@ import os
|
|
|
from django.core.files import File
|
|
|
|
|
|
from misago.users.datadownloads import (
|
|
|
- expire_user_data_download, request_user_data_download, user_has_data_download_request
|
|
|
+ expire_user_data_download, prepare_user_data_download, request_user_data_download,
|
|
|
+ user_has_data_download_request
|
|
|
)
|
|
|
from misago.users.models import DataDownload
|
|
|
from misago.users.testutils import AuthenticatedUserTestCase
|
|
@@ -13,42 +14,91 @@ TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfi
|
|
|
TEST_FILE_PATH = os.path.join(TESTFILES_DIR, 'avatar.png')
|
|
|
|
|
|
|
|
|
-class UserHasRequestedDataDownloadTests(AuthenticatedUserTestCase):
|
|
|
- def test_util_returns_false_for_no_download(self):
|
|
|
- """user_has_data_download_request returns false if user has no requests in progress"""
|
|
|
- self.assertFalse(user_has_data_download_request(self.user))
|
|
|
-
|
|
|
- def test_util_returns_false_for_ready_download(self):
|
|
|
- """user_has_data_download_request returns false if user has ready download"""
|
|
|
+class ExpireUserDataDownloadTests(AuthenticatedUserTestCase):
|
|
|
+ def test_util_marks_download_as_expired(self):
|
|
|
+ """expire_user_data_download changed data download status to expired"""
|
|
|
data_download = request_user_data_download(self.user)
|
|
|
data_download.status = DataDownload.STATUS_READY
|
|
|
- data_download.save()
|
|
|
|
|
|
- self.assertFalse(user_has_data_download_request(self.user))
|
|
|
+ with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
+ data_download.file = File(download_file)
|
|
|
+ data_download.save()
|
|
|
|
|
|
- def test_util_returns_false_for_expired_download(self):
|
|
|
- """user_has_data_download_request returns false if user has expired download"""
|
|
|
- data_download = request_user_data_download(self.user)
|
|
|
- data_download.status = DataDownload.STATUS_EXPIRED
|
|
|
- data_download.save()
|
|
|
-
|
|
|
- self.assertFalse(user_has_data_download_request(self.user))
|
|
|
+ expire_user_data_download(data_download)
|
|
|
|
|
|
- def test_util_returns_true_for_pending_download(self):
|
|
|
- """user_has_data_download_request returns true if user has pending download"""
|
|
|
+ self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
|
|
|
+
|
|
|
+ def test_util_deletes_file(self):
|
|
|
+ """expire_user_data_download deleted file associated with download"""
|
|
|
data_download = request_user_data_download(self.user)
|
|
|
- data_download.status = DataDownload.STATUS_PENDING
|
|
|
- data_download.save()
|
|
|
-
|
|
|
- self.assertTrue(user_has_data_download_request(self.user))
|
|
|
+ data_download.status = DataDownload.STATUS_READY
|
|
|
|
|
|
- def test_util_returns_true_for_processing_download(self):
|
|
|
- """user_has_data_download_request returns true if user has processing download"""
|
|
|
+ with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
+ data_download.file = File(download_file)
|
|
|
+ data_download.save()
|
|
|
+
|
|
|
+ download_file_path = data_download.file.path
|
|
|
+
|
|
|
+ expire_user_data_download(data_download)
|
|
|
+
|
|
|
+ self.assertFalse(data_download.file)
|
|
|
+ self.assertFalse(os.path.isdir(download_file_path))
|
|
|
+
|
|
|
+ def test_util_expires_download_without_file(self):
|
|
|
+ """expire_user_data_download handles missing download file"""
|
|
|
data_download = request_user_data_download(self.user)
|
|
|
- data_download.status = DataDownload.STATUS_PROCESSING
|
|
|
- data_download.save()
|
|
|
-
|
|
|
- self.assertTrue(user_has_data_download_request(self.user))
|
|
|
+ data_download.status = DataDownload.STATUS_READY
|
|
|
+
|
|
|
+ expire_user_data_download(data_download)
|
|
|
+
|
|
|
+ self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
|
|
|
+
|
|
|
+
|
|
|
+class PrepareUserDataDownload(AuthenticatedUserTestCase):
|
|
|
+ def setUp(self):
|
|
|
+ super(PrepareUserDataDownload, self).setUp()
|
|
|
+ self.download = request_user_data_download(self.user)
|
|
|
+
|
|
|
+ def assert_download_is_valid(self):
|
|
|
+ result = prepare_user_data_download(self.download)
|
|
|
+ self.assertTrue(result)
|
|
|
+
|
|
|
+ self.download.refresh_from_db()
|
|
|
+ self.assertTrue(self.download.file)
|
|
|
+
|
|
|
+ def test_prepare_basic_download(self):
|
|
|
+ """function creates data download for basic user account"""
|
|
|
+ self.assert_download_is_valid()
|
|
|
+
|
|
|
+ def test_prepare_download_with_profle_fields(self):
|
|
|
+ """function creates data download for user with profile fields"""
|
|
|
+ self.user.profile_fields = {'real_name': "Bob Boberthon!"}
|
|
|
+ self.user.save()
|
|
|
+
|
|
|
+ self.assert_download_is_valid()
|
|
|
+
|
|
|
+ def test_prepare_download_with_tmp_avatar(self):
|
|
|
+ """function creates data download for user with tmp avatar"""
|
|
|
+ with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
+ self.user.avatar_tmp = File(download_file)
|
|
|
+ self.user.save()
|
|
|
+
|
|
|
+ self.assert_download_is_valid()
|
|
|
+
|
|
|
+ def test_prepare_download_with_src_avatar(self):
|
|
|
+ """function creates data download for user with src avatar"""
|
|
|
+ with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
+ self.user.avatar_src = File(download_file)
|
|
|
+ self.user.save()
|
|
|
+
|
|
|
+ self.assert_download_is_valid()
|
|
|
+
|
|
|
+ def test_prepare_download_with_avatar_set(self):
|
|
|
+ """function creates data download for user with avatar set"""
|
|
|
+ with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
+ self.user.avatar_set.create(size=100, image=File(download_file))
|
|
|
+
|
|
|
+ self.assert_download_is_valid()
|
|
|
|
|
|
|
|
|
class RequestUserDataDownloadTests(AuthenticatedUserTestCase):
|
|
@@ -72,41 +122,39 @@ class RequestUserDataDownloadTests(AuthenticatedUserTestCase):
|
|
|
self.assertEqual(data_download.status, DataDownload.STATUS_PENDING)
|
|
|
|
|
|
|
|
|
-class ExpireUserDataDownloadTests(AuthenticatedUserTestCase):
|
|
|
- def test_util_marks_download_as_expired(self):
|
|
|
- """expire_user_data_download changed data download status to expired"""
|
|
|
- data_download = request_user_data_download(self.user)
|
|
|
- data_download.status = DataDownload.STATUS_READY
|
|
|
-
|
|
|
- with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
- data_download.file = File(download_file)
|
|
|
- data_download.save()
|
|
|
-
|
|
|
- expire_user_data_download(data_download)
|
|
|
-
|
|
|
- self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
|
|
|
+class UserHasRequestedDataDownloadTests(AuthenticatedUserTestCase):
|
|
|
+ def test_util_returns_false_for_no_download(self):
|
|
|
+ """user_has_data_download_request returns false if user has no requests in progress"""
|
|
|
+ self.assertFalse(user_has_data_download_request(self.user))
|
|
|
|
|
|
- def test_util_deletes_file(self):
|
|
|
- """expire_user_data_download deleted file associated with download"""
|
|
|
+ def test_util_returns_false_for_ready_download(self):
|
|
|
+ """user_has_data_download_request returns false if user has ready download"""
|
|
|
data_download = request_user_data_download(self.user)
|
|
|
data_download.status = DataDownload.STATUS_READY
|
|
|
+ data_download.save()
|
|
|
|
|
|
- with open(TEST_FILE_PATH, 'rb') as download_file:
|
|
|
- data_download.file = File(download_file)
|
|
|
- data_download.save()
|
|
|
-
|
|
|
- download_file_path = data_download.file.path
|
|
|
-
|
|
|
- expire_user_data_download(data_download)
|
|
|
-
|
|
|
- self.assertFalse(data_download.file)
|
|
|
- self.assertFalse(os.path.isdir(download_file_path))
|
|
|
+ self.assertFalse(user_has_data_download_request(self.user))
|
|
|
|
|
|
- def test_util_expires_download_without_file(self):
|
|
|
- """expire_user_data_download handles missing download file"""
|
|
|
+ def test_util_returns_false_for_expired_download(self):
|
|
|
+ """user_has_data_download_request returns false if user has expired download"""
|
|
|
data_download = request_user_data_download(self.user)
|
|
|
- data_download.status = DataDownload.STATUS_READY
|
|
|
+ data_download.status = DataDownload.STATUS_EXPIRED
|
|
|
+ data_download.save()
|
|
|
+
|
|
|
+ self.assertFalse(user_has_data_download_request(self.user))
|
|
|
|
|
|
- expire_user_data_download(data_download)
|
|
|
+ def test_util_returns_true_for_pending_download(self):
|
|
|
+ """user_has_data_download_request returns true if user has pending download"""
|
|
|
+ data_download = request_user_data_download(self.user)
|
|
|
+ data_download.status = DataDownload.STATUS_PENDING
|
|
|
+ data_download.save()
|
|
|
+
|
|
|
+ self.assertTrue(user_has_data_download_request(self.user))
|
|
|
|
|
|
- self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
|
|
|
+ def test_util_returns_true_for_processing_download(self):
|
|
|
+ """user_has_data_download_request returns true if user has processing download"""
|
|
|
+ data_download = request_user_data_download(self.user)
|
|
|
+ data_download.status = DataDownload.STATUS_PROCESSING
|
|
|
+ data_download.save()
|
|
|
+
|
|
|
+ self.assertTrue(user_has_data_download_request(self.user))
|