Esempio n. 1
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)
Esempio 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)
Esempio 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)
Esempio 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)