Source

django-private-media / private_media / tests / tests.py

import shutil
from django.conf import settings
from django.core.files.storage import default_storage
from django.core.files.uploadedfile import SimpleUploadedFile
from django.http import HttpResponseForbidden
from django.test import TestCase
from models import PrivateStorage


class FileTests(TestCase):
    def setUp(self):
        obj = PrivateStorage()
        obj.normal = SimpleUploadedFile("django_test.txt", "content")
        obj.save()

    def tearDown(self):
        shutil.rmtree(settings.MEDIA_ROOT)

    def test_file_exists(self):
        dirs, files = default_storage.listdir("tests")
        self.assertEqual(
            sorted(files), ["django_test.txt"]
        )

    def test_file_url(self):
        obj = PrivateStorage.objects.get(normal="tests/django_test.txt")
        self.assertEqual(
            obj.normal.url, "/media/Ni8xL25vcm1hbA/tests/django_test.txt")

    def test_file_get_no_auth(self):
        got = self.client.get("/media/Ni8xL25vcm1hbA/tests/django_test.txt")
        self.assertIsInstance(got, HttpResponseForbidden)