class DownloadContentTestCase(TestCase): """ Test case for the downloadcontent endpoint. """ def setUp(self): provision_device() self.client = Client() self.hash = hashlib.md5("DUMMYDATA".encode()).hexdigest() self.extension = file_formats.PDF self.filename = "{}.{}".format(self.hash, self.extension) self.title = "abc123!@#$%^&*();'[],./?><" self.contentnode = ContentNode(title=self.title) self.available = True self.preset = format_presets.DOCUMENT self.local_file = LocalFile(id=self.hash, extension=self.extension, available=self.available) self.file = File( local_file=self.local_file, available=self.available, contentnode=self.contentnode, preset=self.preset, ) self.path = get_content_storage_file_path(self.filename) path_dir = os.path.dirname(self.path) if not os.path.exists(path_dir): os.makedirs(path_dir) tempfile = open(self.path, "w") tempfile.write("test") tempfile.close() def test_generate_download_filename(self): self.assertEqual( self.file.get_download_filename(), "abc123._Document.{}".format(self.extension), ) def test_generate_download_url(self): self.assertEqual( self.file.get_download_url(), "/downloadcontent/{}/{}".format(self.filename, self.file.get_download_filename()), ) def test_download_existing_file(self): response = self.client.get(self.file.get_download_url()) self.assertEqual(response.status_code, 200) def test_download_non_existing_file(self): bad_download_url = self.file.get_download_url().replace( self.file.get_download_url()[25:25], "aaaaa") response = self.client.get(bad_download_url) self.assertEqual(response.status_code, 404) def test_download_headers(self): response = self.client.get(self.file.get_download_url()) self.assertEqual(response["Content-Type"], mimetypes.guess_type(self.filename)[0]) self.assertEqual(response["Content-Disposition"], "attachment;") self.assertEqual(response["Content-Length"], str(os.path.getsize(self.path)))
def insert_into_default_db(self): ContentNode.objects.bulk_create(self._django_nodes) ChannelMetadata.objects.create(**self.channel) LocalFile.objects.bulk_create( (LocalFile(**l) for l in self.localfiles.values())) File.objects.bulk_create((File(**f) for f in self.files.values()))
class UnavailableContentDeletion(TransactionTestCase): def setUp(self): super(UnavailableContentDeletion, self).setUp() # create an unavailable contentnode self.unavailable_contentnode = ContentNode( title="wow", available=False, id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, channel_id=uuid.uuid4().hex, ) self.unavailable_contentnode.save() self.hash = hashlib.md5("wow".encode()).hexdigest() self.extension = file_formats.PDF # with an associated stored local file self.stored_local_file = LocalFile(id=self.hash, extension=self.extension, available=True, file_size=1000000) self.stored_local_file.save() self.file = File( local_file=self.stored_local_file, contentnode=self.unavailable_contentnode, preset=format_presets.DOCUMENT, id=uuid.uuid4().hex, ) self.file.save() # actually store a dummy local file self.filename = "{}.{}".format(self.hash, self.extension) self.path = get_content_storage_file_path(self.filename) path_dir = os.path.dirname(self.path) if not os.path.exists(path_dir): os.makedirs(path_dir) tempfile = open(self.path, "w") tempfile.write("wow") tempfile.close() def delete_content(self): num_deleted = 0 freed_bytes = 0 for deleted, file in LocalFile.objects.delete_unused_files(): if deleted: num_deleted += 1 freed_bytes += file.file_size return num_deleted, freed_bytes def test_delete_unavailable_stored_files(self): self.assertEqual(LocalFile.objects.get_unused_files().count(), 1) deleted, freed_bytes = self.delete_content() self.assertEqual(deleted, 1) self.assertEqual(freed_bytes, self.stored_local_file.file_size) self.assertEqual(os.path.exists(self.path), False) self.assertEqual(LocalFile.objects.get_unused_files().count(), 0) def test_dont_delete_used_stored_files(self): available_contentnode = ContentNode.objects.create( title="wow", available=True, id=uuid.uuid4().hex, content_id=uuid.uuid4().hex, channel_id=uuid.uuid4().hex, ) File.objects.create( local_file=self.stored_local_file, contentnode=available_contentnode, preset=format_presets.DOCUMENT, id=uuid.uuid4().hex, ) self.assertEqual(LocalFile.objects.get_unused_files().count(), 0) deleted, freed_bytes = self.delete_content() self.assertEqual(deleted, 0) def tearDown(self): call_command("flush", interactive=False) super(UnavailableContentDeletion, self).tearDown()