Ejemplo n.º 1
0
def test_file_marshalling(fixture):
    """A FileField receives as input a list of UploadedFile objects.  Its marshalling job consists
       of merely changing such a list into a single value, or returning it as is depending on
       the setting of its allow_multiple flag.
    """

    field = FileField()
    obj = fixture.model_object
    field.bind('file_value', obj)

    files = [EmptyStub(), EmptyStub()]

    # Single file only
    field.from_input(files[:1])
    assert obj.file_value == files[0]

    obj.file_value = files[0]
    assert field.as_input() == ''

    # Multiple files
    field = FileField(allow_multiple=True)
    field.bind('file_value', obj)

    field.from_input(files)
    assert obj.file_value == files

    obj.file_value = files
    assert field.as_input() == ''
Ejemplo n.º 2
0
    def file_validation_mime_type(self, fixture):
        """A FileField can also limit the mimetype of files allowed to be uploaded.
        """

        field = FileField(allow_multiple=True, accept=['text/*'])
        obj = fixture.model_object
        field.bind('file_value', obj)

        files = [UploadedFile('file1', b'stuff 1', 'text/html'), UploadedFile('file2', b'stuff 2', 'text/xml')]
        with expected(NoException):
            field.set_user_input(files)

        files = [UploadedFile('file1', b'stuff 3', 'text/html'), UploadedFile('file2', b'stuff 4', 'application/java')]
        with expected(MimeTypeConstraint):
            field.set_user_input(files)
Ejemplo n.º 3
0
    def file_validation_max_files(self, fixture):
        """A maximum can be placed upon the number of files that may be uploaded.
        """

        field = FileField(allow_multiple=True, max_files=1)
        obj = fixture.model_object
        field.bind('file_value', obj)

        files = [UploadedFile('file1', b'stuff 1', '')]
        with expected(NoException):
            field.set_user_input(files)

        files = [UploadedFile('file1', b'stuff 2', ''), UploadedFile('file2', b'stuff 3', '')]
        with expected(MaxFilesConstraint):
            field.set_user_input(files)
Ejemplo n.º 4
0
    def file_validation_size(self, fixture):
        """A FileField can also limit the size if files uploaded.
        """

        field = FileField(allow_multiple=True, max_size_bytes=100)
        obj = fixture.model_object
        field.bind('file_value', obj)

        files = [UploadedFile('file1', b'.'*100, ''), UploadedFile('file2', b'.'*50, '')]
        with expected(NoException):
            field.set_user_input(files)

        files = [UploadedFile('file1', b'.'*100, ''), UploadedFile('file2', b'.'*200, '')]
        with expected(FileSizeConstraint):
            field.set_user_input(files)
Ejemplo n.º 5
0
def test_file_validation(fixture):
    """A FileField needs to check that the right number of files were submitted, depending on the
       setting of allow_multiple and/or required.
    """

    field = FileField()
    obj = fixture.model_object
    field.bind('file_value', obj)

    files = [EmptyStub(), EmptyStub()]

    # Single file only
    with expected(SingleFileConstraint):
        field.set_user_input(files)

    with expected(NoException):
        field.set_user_input(files[:1])

    # Single file that is required
    field = FileField(required=True)
    field.bind('file_value', obj)

    with expected(NoException):
        field.set_user_input(files[:1])

    with expected(RequiredConstraint):
        field.set_user_input([])

    # Multiple files
    field = FileField(allow_multiple=True)
    field.bind('file_value', obj)

    with expected(NoException):
        field.set_user_input(files)