def test_validation_mesh():
    mime, mt = mv.validate('', mime='model/mesh', url='foo.stl')
    assert mime == 'model/mesh'
    assert mt == 'models'
    mime, mt = mv.validate('', mime='model/mesh', url='foo.stl', type='models')
    assert mime == 'model/mesh'
    assert mt == 'models'
def test_validation_zip(zippath):
    mime, mt = mv.validate(zippath.open('rb').read(1024), type="debugfile")
    assert mime == 'application/zip'
    assert mt == 'debugfile'

    mime, mt = mv.validate(zippath.open('rb').read(1024), type="datasets")
    assert mime == 'application/zip'
    assert mt == 'datasets'

    with pytest.raises(mv.UnknownBucketError):
        mime, mt = mv.validate(zippath.open('rb').read(1024))
def test_validation_png(pngpath):
    with pytest.raises(mv.UnknownBucketError):
        mime, mt = mv.validate(pngpath.open('rb').read(1024))

    with pytest.raises(mv.UnknownBucketError):
        mime, mt = mv.validate(pngpath.open('rb').read(1024),
                               mime=u'image/png')

    with pytest.raises(mv.MimeNotAllowedError):
        mime, mt = mv.validate(pngpath.open('rb').read(1024), type='images')

    with pytest.raises(mv.MimeNotAllowedError):
        mime, mt = mv.validate(pngpath.open('rb').read(1024),
                               mime=u'image/png',
                               type='images')
def test_validation_jpg(jpgpath):
    """Test validation when submitted with the incorrect mime type image/jpg

    We've decided on the policy that instead this should be kicked
    back to the provider for correction.

    """
    with pytest.raises(mv.UnknownBucketError):
        mime, mt = mv.validate(jpgpath.open('rb').read(1024),
                               mime=u'image/jpg')

    with pytest.raises(mv.MimeNotAllowedError):
        mime, mt = mv.validate(jpgpath.open('rb').read(1024),
                               mime=u'image/jpg',
                               type='images')
Beispiel #5
0
    def fromobj(cls, obj, **attrs):
        obj.seek(0)
        attrs.setdefault('last_status', 200)
        attrs.setdefault('last_check', datetime.now())
        attrs.setdefault('derivatives', False)

        mo = cls(**attrs)
        if not mo.detected_mime or not mo.bucket:
            mo.detected_mime, mo.bucket = validate(
                obj.read(1024), url=mo.url,
                type=mo.type or mo.bucket,
                mime=mo.mime or mo.detected_mime)

        if mo.type and not mo.bucket:
            mo.bucket = mo.type
        if mo.bucket and not mo.type:
            mo.type = mo.bucket
        if not mo.mime:
            mo.mime = mo.detected_mime

        obj.seek(0)
        mo.etag = calcFileHash(obj, op=False, return_size=False)
        if attrs.get('etag'):
            if mo.etag != attrs.get('etag'):
                raise EtagMismatchError(attrs.get('etag'), mo.etag)

        obj.seek(0)
        return mo
def test_validation_jpeg(jpgpath):
    mime, mt = mv.validate(jpgpath.open('rb').read(1024))
    assert mime == u'image/jpeg'
    assert mt == u'images'

    mime, mt = mv.validate(jpgpath.open('rb').read(1024), mime=u'image/jpeg')
    assert mime == u'image/jpeg'
    assert mt == u'images'

    mime, mt = mv.validate(jpgpath.open('rb').read(1024), type='images')
    assert mime == u'image/jpeg'
    assert mt == u'images'

    mime, mt = mv.validate(jpgpath.open('rb').read(1024),
                           mime=u'image/jpeg',
                           type='images')
    assert mime == u'image/jpeg'
    assert mt == u'images'
def test_mime_mismatch(jpgpath, zippath):
    with pytest.raises(mv.UnknownBucketError):
        mv.validate(jpgpath.open('rb').read(1024), mime='application/zip')

    with pytest.raises(mv.MimeMismatchError):
        mv.validate(zippath.open('rb').read(1024), mime='image/jpeg')

    with pytest.raises(mv.MimeMismatchError):
        mv.validate(jpgpath.open('rb').read(1024),
                    type='datasets',
                    mime='application/zip')

    with pytest.raises(mv.MimeMismatchError):
        mv.validate(zippath.open('rb').read(1024),
                    mime='image/jpeg',
                    type='images')
def test_validation_bad_type(jpgpath):
    with pytest.raises(mv.InvalidBucketError):
        mv.validate(jpgpath.open('rb').read(1024), type="foo")
    with pytest.raises(mv.MimeNotAllowedError):
        mv.validate(jpgpath.open('rb').read(1024), type="sounds")
    with pytest.raises(mv.MimeNotAllowedError):
        mv.validate(jpgpath.open('rb').read(1024),
                    type='images',
                    mime='image/png')