Пример #1
0
 def test_non_zip_file_url_reversal(self):
     file = LocalFile(id=self.hash,
                      extension="otherextension",
                      available=True)
     filename = file.get_filename()
     self.assertEqual(
         file.get_storage_url(),
         "/content/storage/{}/{}/{}".format(filename[0], filename[1],
                                            filename),
     )
Пример #2
0
    def test_zip_file_url_reversal(self):
        from kolibri.utils.conf import OPTIONS

        path_prefix = OPTIONS["Deployment"]["ZIP_CONTENT_URL_PATH_PREFIX"]

        if path_prefix != "/":
            path_prefix = "/" + path_prefix
        file = LocalFile(id=self.hash,
                         extension=self.extension,
                         available=True)
        self.assertEqual(
            file.get_storage_url(),
            "{}zipcontent/{}/".format(path_prefix, self.filename),
        )
Пример #3
0
    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()
Пример #4
0
 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()))
Пример #5
0
    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()
Пример #6
0
 def test_zip_file_url_reversal(self):
     file = LocalFile(id=self.hash,
                      extension=self.extension,
                      available=True)
     self.assertEqual(file.get_storage_url(),
                      "/zipcontent/{}/".format(self.filename))
Пример #7
0
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()