Exemplo n.º 1
0
 def test_get_files_from_storage(self):
     content = b'test_get_files_from_storage'
     name = storage.save('test_get_files_from_storage',
                         ContentFile(content))
     files = S3FileMiddleware.get_files_from_storage([name])
     file = next(files)
     assert file.read() == content
Exemplo n.º 2
0
    def test_process_request(self, rf):
        uploaded_file = SimpleUploadedFile('uploaded_file.txt', b'uploaded')
        request = rf.post('/', data={'file': uploaded_file})
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist('file')
        assert request.FILES.get('file').read() == b'uploaded'

        storage.save('s3_file.txt', ContentFile(b's3file'))
        request = rf.post('/',
                          data={
                              'file': '["custom/location/s3_file.txt"]',
                              's3file': '["file"]'
                          })
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist('file')
        assert request.FILES.get('file').read() == b's3file'
Exemplo n.º 3
0
    def test_process_request__no_location(self, rf, settings):
        settings.AWS_LOCATION = ""
        uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
        request = rf.post("/", data={"file": uploaded_file})
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist("file")
        assert request.FILES.get("file").read() == b"uploaded"

        storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
        request = rf.post("/",
                          data={
                              "file": '["tmp/s3file/s3_file.txt"]',
                              "s3file": '["file"]'
                          })
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist("file")
        assert request.FILES.get("file").read() == b"s3file"
Exemplo n.º 4
0
 def test_get_files_from_storage(self):
     content = b"test_get_files_from_storage"
     name = storage.save("tmp/s3file/test_get_files_from_storage",
                         ContentFile(content))
     files = S3FileMiddleware.get_files_from_storage(
         [os.path.join(storage.location, name)])
     file = next(files)
     assert file.read() == content
Exemplo n.º 5
0
    def test_process_request__no_location(self, rf, settings):
        settings.AWS_LOCATION = ''
        uploaded_file = SimpleUploadedFile('uploaded_file.txt', b'uploaded')
        request = rf.post('/', data={'file': uploaded_file})
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist('file')
        assert request.FILES.get('file').read() == b'uploaded'

        storage.save('tmp/s3file/s3_file.txt', ContentFile(b's3file'))
        request = rf.post('/',
                          data={
                              'file': '["tmp/s3file/s3_file.txt"]',
                              's3file': '["file"]'
                          })
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist('file')
        assert request.FILES.get('file').read() == b's3file'
Exemplo n.º 6
0
    def test_process_request(self, rf):
        uploaded_file = SimpleUploadedFile("uploaded_file.txt", b"uploaded")
        request = rf.post("/", data={"file": uploaded_file})
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist("file")
        assert request.FILES.get("file").read() == b"uploaded"

        storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
        request = rf.post(
            "/",
            data={
                "file": '["custom/location/tmp/s3file/s3_file.txt"]',
                "s3file": '["file"]',
            },
        )
        S3FileMiddleware(lambda x: None)(request)
        assert request.FILES.getlist("file")
        assert request.FILES.get("file").read() == b"s3file"
Exemplo n.º 7
0
    def test_value_from_datadict(self, client, upload_file):
        with open(upload_file) as f:
            uploaded_file = storage.save('test.jpg', f)
        response = client.post(reverse('upload'), {
            'file': json.dumps([uploaded_file]),
            's3file': '["file"]',
        })

        assert response.status_code == 201
Exemplo n.º 8
0
 def test_process_request__multiple_files(self, rf):
     storage.save("tmp/s3file/s3_file.txt", ContentFile(b"s3file"))
     storage.save("tmp/s3file/s3_other_file.txt",
                  ContentFile(b"other s3file"))
     request = rf.post(
         "/",
         data={
             "file": [
                 "custom/location/tmp/s3file/s3_file.txt",
                 "custom/location/tmp/s3file/s3_other_file.txt",
             ],
             "s3file": ["file", "other_file"],
         },
     )
     S3FileMiddleware(lambda x: None)(request)
     files = request.FILES.getlist("file")
     assert files[0].read() == b"s3file"
     assert files[1].read() == b"other s3file"
Exemplo n.º 9
0
    def test_value_from_datadict(self, client, upload_file):
        with open(upload_file) as f:
            uploaded_file = storage.save("test.jpg", f)
        response = client.post(
            reverse("upload"),
            {
                "file": json.dumps([uploaded_file]),
                "s3file": '["file"]',
            },
        )

        assert response.status_code == 201