Ejemplo n.º 1
0
def test_update_post_thumbnail_to_default(tmpdir, config_injector, read_asset,
                                          post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
        'secret': 'test',
    })
    post = post_factory(id=1)
    db.session.add(post)
    if is_existing:
        db.session.flush()
    assert post.post_id
    generated_path = (
        '{}/data/generated-thumbnails/1_244c8840887984c4.jpg'.format(tmpdir))
    source_path = ('{}/data/posts/custom-thumbnails/1_244c8840887984c4.dat'.
                   format(tmpdir))
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    posts.update_post_thumbnail(post, None)
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert not os.path.exists(source_path)
Ejemplo n.º 2
0
 def put(self, ctx, post_id):
     post = posts.get_post_by_id(post_id)
     if ctx.has_file('content'):
         auth.verify_privilege(ctx.user, 'posts:edit:content')
         posts.update_post_content(post, ctx.get_file('content'))
     if ctx.has_param('tags'):
         auth.verify_privilege(ctx.user, 'posts:edit:tags')
         posts.update_post_tags(post, ctx.get_param_as_list('tags'))
     if ctx.has_param('safety'):
         auth.verify_privilege(ctx.user, 'posts:edit:safety')
         posts.update_post_safety(post, ctx.get_param_as_string('safety'))
     if ctx.has_param('source'):
         auth.verify_privilege(ctx.user, 'posts:edit:source')
         posts.update_post_source(post, ctx.get_param_as_string('source'))
     elif ctx.has_param('contentUrl'):
         posts.update_post_source(post, ctx.get_param_as_string('contentUrl'))
     if ctx.has_param('relations'):
         auth.verify_privilege(ctx.user, 'posts:edit:relations')
         posts.update_post_relations(post, ctx.get_param_as_list('relations'))
     if ctx.has_param('notes'):
         auth.verify_privilege(ctx.user, 'posts:edit:notes')
         posts.update_post_notes(post, ctx.get_param_as_list('notes'))
     if ctx.has_param('flags'):
         auth.verify_privilege(ctx.user, 'posts:edit:flags')
         posts.update_post_flags(post, ctx.get_param_as_list('flags'))
     if ctx.has_file('thumbnail'):
         auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
         posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
     post.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return posts.serialize_post_with_details(post, ctx.user)
Ejemplo n.º 3
0
def test_update_post_thumbnail_to_default(
        tmpdir, config_injector, read_asset, post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    db.session.add(post)
    if is_existing:
        db.session.flush()
        assert post.post_id
    else:
        assert not post.post_id
    generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg'
    source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat'
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    posts.update_post_thumbnail(post, None)
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert not os.path.exists(source_path)
Ejemplo n.º 4
0
def test_update_post_content_for_new_post(
        tmpdir, config_injector, post_factory, read_asset, is_existing,
        input_file, expected_mime_type, expected_type, output_file_name):
    with patch('szurubooru.func.util.get_sha1'):
        util.get_sha1.return_value = 'crc'
        config_injector({
            'data_dir': str(tmpdir.mkdir('data')),
            'thumbnails': {
                'post_width': 300,
                'post_height': 300,
            },
        })
        output_file_path = str(tmpdir) + '/data/posts/' + output_file_name
        post = post_factory()
        db.session.add(post)
        if is_existing:
            db.session.flush()
            assert post.post_id
        else:
            assert not post.post_id
        assert not os.path.exists(output_file_path)
        content = read_asset(input_file)
        posts.update_post_content(post, content)
        assert not os.path.exists(output_file_path)
        db.session.flush()
        assert post.mime_type == expected_mime_type
        assert post.type == expected_type
        assert post.checksum == 'crc'
        assert os.path.exists(output_file_path)
        if post.type in (model.Post.TYPE_IMAGE, model.Post.TYPE_ANIMATION):
            image_hash.delete_image.assert_called_once_with(post.post_id)
            image_hash.add_image.assert_called_once_with(post.post_id, content)
        else:
            image_hash.delete_image.assert_not_called()
            image_hash.add_image.assert_not_called()
Ejemplo n.º 5
0
def test_merge_posts_replaces_content(
        post_factory, config_injector, tmpdir, read_asset):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'data_url': 'example.com',
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    source_post = post_factory()
    target_post = post_factory()
    content = read_asset('png.png')
    db.session.add_all([source_post, target_post])
    db.session.commit()
    posts.update_post_content(source_post, content)
    db.session.flush()
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png'))
    assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.dat'))
    assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png'))
    posts.merge_posts(source_post, target_post, True)
    db.session.flush()
    assert posts.try_get_post_by_id(source_post.post_id) is None
    post = posts.get_post_by_id(target_post.post_id)
    assert post is not None
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png'))
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png'))
Ejemplo n.º 6
0
def test_update_post_thumbnail_to_new_one(tmpdir, config_injector, read_asset,
                                          post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory(id=1)
    db.session.add(post)
    if is_existing:
        db.session.flush()
    assert post.post_id
    generated_path = '{}/data/generated-thumbnails/1.jpg'.format(tmpdir)
    source_path = '{}/data/posts/custom-thumbnails/1.dat'.format(tmpdir)
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert os.path.exists(source_path)
    with open(source_path, 'rb') as handle:
        assert handle.read() == read_asset('jpeg.jpg')
Ejemplo n.º 7
0
def test_update_post_thumbnail_with_broken_thumbnail(
        tmpdir, config_injector, read_asset, post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    db.session.add(post)
    if is_existing:
        db.session.flush()
        assert post.post_id
    else:
        assert not post.post_id
    generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg'
    source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat'
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('png-broken.png'))
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert os.path.exists(source_path)
    with open(source_path, 'rb') as handle:
        assert handle.read() == read_asset('png-broken.png')
    with open(generated_path, 'rb') as handle:
        image = images.Image(handle.read())
        assert image.width == 1
        assert image.height == 1
Ejemplo n.º 8
0
def test_update_post_thumbnail_with_broken_thumbnail(
        tmpdir, config_injector, read_asset, post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    db.session.add(post)
    if is_existing:
        db.session.flush()
        assert post.post_id
    else:
        assert not post.post_id
    generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg'
    source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat'
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('png-broken.png'))
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert os.path.exists(source_path)
    with open(source_path, 'rb') as handle:
        assert handle.read() == read_asset('png-broken.png')
    with open(generated_path, 'rb') as handle:
        image = images.Image(handle.read())
        assert image.width == 1
        assert image.height == 1
Ejemplo n.º 9
0
def test_merge_posts_replaces_content(
        post_factory, config_injector, tmpdir, read_asset):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'data_url': 'example.com',
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    source_post = post_factory()
    target_post = post_factory()
    content = read_asset('png.png')
    db.session.add_all([source_post, target_post])
    db.session.commit()
    posts.update_post_content(source_post, content)
    db.session.flush()
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png'))
    assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.dat'))
    assert not os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png'))
    posts.merge_posts(source_post, target_post, True)
    db.session.flush()
    assert posts.try_get_post_by_id(source_post.post_id) is None
    post = posts.get_post_by_id(target_post.post_id)
    assert post is not None
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/1.png'))
    assert os.path.exists(os.path.join(str(tmpdir), 'data/posts/2.png'))
Ejemplo n.º 10
0
def test_update_post_thumbnail_to_default(
        tmpdir, config_injector, read_asset, post_factory, is_existing):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    db.session.add(post)
    if is_existing:
        db.session.flush()
        assert post.post_id
    else:
        assert not post.post_id
    generated_path = str(tmpdir) + '/data/generated-thumbnails/1.jpg'
    source_path = str(tmpdir) + '/data/posts/custom-thumbnails/1.dat'
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    posts.update_post_thumbnail(post, None)
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert not os.path.exists(source_path)
Ejemplo n.º 11
0
def test_update_post_content_with_broken_content(tmpdir, config_injector,
                                                 post_factory, read_asset,
                                                 allow_broken_uploads):
    # the rationale behind this behavior is to salvage user upload even if the
    # server software thinks it's broken. chances are the server is wrong,
    # especially about flash movies.
    config_injector({
        "data_dir": str(tmpdir.mkdir("data")),
        "thumbnails": {
            "post_width": 300,
            "post_height": 300,
        },
        "secret": "test",
        "allow_broken_uploads": allow_broken_uploads,
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    if allow_broken_uploads:
        posts.update_post_content(post, read_asset("png-broken.png"))
        db.session.flush()
        assert post.canvas_width is None
        assert post.canvas_height is None
    else:
        with pytest.raises(posts.InvalidPostContentError):
            posts.update_post_content(post, read_asset("png-broken.png"))
        db.session.flush()
Ejemplo n.º 12
0
def test_update_post_thumbnail_to_default(tmpdir, config_injector, read_asset,
                                          post_factory, is_existing):
    config_injector({
        "data_dir": str(tmpdir.mkdir("data")),
        "thumbnails": {
            "post_width": 300,
            "post_height": 300,
        },
        "secret": "test",
        "allow_broken_uploads": False,
    })
    post = post_factory(id=1)
    db.session.add(post)
    if is_existing:
        db.session.flush()
    assert post.post_id
    generated_path = ("{}/data/generated-thumbnails/".format(tmpdir) +
                      "1_244c8840887984c4.jpg")
    source_path = ("{}/data/posts/custom-thumbnails/".format(tmpdir) +
                   "1_244c8840887984c4.dat")
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset("png.png"))
    posts.update_post_thumbnail(post, read_asset("jpeg.jpg"))
    posts.update_post_thumbnail(post, None)
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert not os.path.exists(source_path)
Ejemplo n.º 13
0
def test_update_post_content_convert_heif_to_png_when_processing(
    tmpdir, config_injector, read_asset, post_factory, filename
):
    config_injector(
        {
            "data_dir": str(tmpdir.mkdir("data")),
            "thumbnails": {
                "post_width": 300,
                "post_height": 300,
            },
            "secret": "test",
            "allow_broken_uploads": False,
        }
    )
    post = post_factory(id=1)
    db.session.add(post)
    posts.update_post_content(post, read_asset(filename))
    posts.update_post_thumbnail(post, read_asset(filename))
    db.session.flush()
    generated_path = (
        "{}/data/generated-thumbnails/".format(tmpdir)
        + "1_244c8840887984c4.jpg"
    )
    source_path = (
        "{}/data/posts/custom-thumbnails/".format(tmpdir)
        + "1_244c8840887984c4.dat"
    )
    assert os.path.exists(source_path)
    assert os.path.exists(generated_path)
Ejemplo n.º 14
0
def test_merge_posts_replaces_content(post_factory, config_injector, tmpdir,
                                      read_asset):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'data_url': 'example.com',
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
        'secret': 'test',
    })
    source_post = post_factory(id=1)
    target_post = post_factory(id=2)
    content = read_asset('png.png')
    db.session.add_all([source_post, target_post])
    db.session.commit()
    posts.update_post_content(source_post, content)
    db.session.flush()
    source_path = (os.path.join(
        '{}/data/posts/1_244c8840887984c4.png'.format(tmpdir)))
    target_path1 = (os.path.join(
        '{}/data/posts/2_49caeb3ec1643406.png'.format(tmpdir)))
    target_path2 = (os.path.join(
        '{}/data/posts/2_49caeb3ec1643406.dat'.format(tmpdir)))
    assert os.path.exists(source_path)
    assert not os.path.exists(target_path1)
    assert not os.path.exists(target_path2)
    posts.merge_posts(source_post, target_post, True)
    db.session.flush()
    assert posts.try_get_post_by_id(source_post.post_id) is None
    post = posts.get_post_by_id(target_post.post_id)
    assert post is not None
    assert os.path.exists(source_path)
    assert os.path.exists(target_path1)
    assert not os.path.exists(target_path2)
Ejemplo n.º 15
0
def test_update_post_content_with_invalid_content(config_injector,
                                                  input_content):
    config_injector({
        "allow_broken_uploads": True,
    })
    post = model.Post()
    with pytest.raises(posts.InvalidPostContentError):
        posts.update_post_content(post, input_content)
Ejemplo n.º 16
0
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response:
    post = _get_post(params)
    versions.verify_version(post, ctx)
    versions.bump_version(post)
    if ctx.has_file("content"):
        auth.verify_privilege(ctx.user, "posts:edit:content")
        posts.update_post_content(
            post,
            ctx.get_file(
                "content",
                use_video_downloader=auth.has_privilege(
                    ctx.user, "uploads:use_downloader"),
            ),
        )
    if ctx.has_param("tags"):
        auth.verify_privilege(ctx.user, "posts:edit:tags")
        new_tags = posts.update_post_tags(post,
                                          ctx.get_param_as_string_list("tags"))
        if len(new_tags):
            auth.verify_privilege(ctx.user, "tags:create")
            db.session.flush()
            for tag in new_tags:
                snapshots.create(tag, ctx.user)
    if ctx.has_param("safety"):
        auth.verify_privilege(ctx.user, "posts:edit:safety")
        posts.update_post_safety(post, ctx.get_param_as_string("safety"))
    if ctx.has_param("source"):
        auth.verify_privilege(ctx.user, "posts:edit:source")
        posts.update_post_source(post, ctx.get_param_as_string("source"))
    elif ctx.has_param("contentUrl"):
        posts.update_post_source(post, ctx.get_param_as_string("contentUrl"))
    if ctx.has_param("relations"):
        auth.verify_privilege(ctx.user, "posts:edit:relations")
        posts.update_post_relations(post,
                                    ctx.get_param_as_int_list("relations"))
    if ctx.has_param("notes"):
        auth.verify_privilege(ctx.user, "posts:edit:notes")
        posts.update_post_notes(post, ctx.get_param_as_list("notes"))
    if ctx.has_param("flags"):
        auth.verify_privilege(ctx.user, "posts:edit:flags")
        posts.update_post_flags(post, ctx.get_param_as_string_list("flags"))
    if ctx.has_file("thumbnail"):
        auth.verify_privilege(ctx.user, "posts:edit:thumbnail")
        posts.update_post_thumbnail(post, ctx.get_file("thumbnail"))
    post.last_edit_time = datetime.utcnow()
    ctx.session.flush()
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    return _serialize_post(ctx, post)
Ejemplo n.º 17
0
def test_update_post_content_to_existing_content(tmpdir, config_injector,
                                                 post_factory, read_asset):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    db.session.flush()
    posts.update_post_content(post, read_asset('png.png'))
    with pytest.raises(posts.PostAlreadyUploadedError):
        posts.update_post_content(another_post, read_asset('png.png'))
Ejemplo n.º 18
0
def test_update_post_content_for_new_post(
    tmpdir,
    config_injector,
    post_factory,
    read_asset,
    is_existing,
    input_file,
    expected_mime_type,
    expected_type,
    output_file_name,
):
    with patch("szurubooru.func.util.get_sha1"), patch(
        "szurubooru.func.util.get_md5"
    ):
        util.get_sha1.return_value = "crc"
        util.get_md5.return_value = "md5"
        config_injector(
            {
                "data_dir": str(tmpdir.mkdir("data")),
                "thumbnails": {
                    "post_width": 300,
                    "post_height": 300,
                },
                "secret": "test",
                "allow_broken_uploads": False,
            }
        )
        output_file_path = "{}/data/posts/{}".format(tmpdir, output_file_name)
        post = post_factory(id=1)
        db.session.add(post)
        if is_existing:
            db.session.flush()
        assert post.post_id
        assert not os.path.exists(output_file_path)
        content = read_asset(input_file)
        posts.update_post_content(post, content)
        assert not os.path.exists(output_file_path)
        db.session.flush()
        assert post.mime_type == expected_mime_type
        assert post.type == expected_type
        assert post.checksum == "crc"
        assert post.checksum_md5 == "md5"
        assert os.path.exists(output_file_path)
        if post.type in (model.Post.TYPE_IMAGE, model.Post.TYPE_ANIMATION):
            assert db.session.query(model.PostSignature).count() == 1
        else:
            assert db.session.query(model.PostSignature).count() == 0
Ejemplo n.º 19
0
def test_update_post_content_to_existing_content(
        tmpdir, config_injector, post_factory, read_asset):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'data_url': 'example.com',
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    posts.update_post_content(post, read_asset('png.png'))
    db.session.flush()
    with pytest.raises(posts.PostAlreadyUploadedError):
        posts.update_post_content(another_post, read_asset('png.png'))
Ejemplo n.º 20
0
def test_update_post_content_leaving_custom_thumbnail(
        tmpdir, config_injector, read_asset, post_factory):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory(id=1)
    db.session.add(post)
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    posts.update_post_content(post, read_asset('png.png'))
    db.session.flush()
    assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat')
    assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg')
Ejemplo n.º 21
0
def test_update_post_content_leaves_custom_thumbnail(tmpdir, config_injector,
                                                     read_asset, post_factory):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory(id=1)
    db.session.add(post)
    db.session.flush()
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('jpeg.jpg'))
    posts.update_post_content(post, read_asset('png.png'))
    assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat')
    assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg')
Ejemplo n.º 22
0
def test_update_post_content_with_broken_content(
        tmpdir, config_injector, post_factory, read_asset):
    # the rationale behind this behavior is to salvage user upload even if the
    # server software thinks it's broken. chances are the server is wrong,
    # especially about flash movies.
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    posts.update_post_content(post, read_asset('png-broken.png'))
    db.session.flush()
    assert post.canvas_width is None
    assert post.canvas_height is None
Ejemplo n.º 23
0
def test_update_post_content_to_existing_content(tmpdir, config_injector,
                                                 post_factory, read_asset):
    config_injector({
        "data_dir": str(tmpdir.mkdir("data")),
        "data_url": "example.com",
        "thumbnails": {
            "post_width": 300,
            "post_height": 300,
        },
        "secret": "test",
        "allow_broken_uploads": False,
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    posts.update_post_content(post, read_asset("png.png"))
    db.session.flush()
    with pytest.raises(posts.PostAlreadyUploadedError):
        posts.update_post_content(another_post, read_asset("png.png"))
Ejemplo n.º 24
0
def test_update_post_content_broken_content(tmpdir, config_injector,
                                            post_factory, read_asset):
    # the rationale behind this behavior is to salvage user upload even if the
    # server software thinks it's broken. chances are the server is wrong,
    # especially about flash movies.
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory()
    another_post = post_factory()
    db.session.add_all([post, another_post])
    db.session.flush()
    posts.update_post_content(post, read_asset('png-broken.png'))
    assert post.canvas_width is None
    assert post.canvas_height is None
Ejemplo n.º 25
0
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response:
    post = _get_post(params)
    versions.verify_version(post, ctx)
    versions.bump_version(post)
    if ctx.has_file('content'):
        auth.verify_privilege(ctx.user, 'posts:edit:content')
        posts.update_post_content(post, ctx.get_file('content'))
    if ctx.has_param('tags'):
        auth.verify_privilege(ctx.user, 'posts:edit:tags')
        new_tags = posts.update_post_tags(
            post, ctx.get_param_as_string_list('tags'))
        if len(new_tags):
            auth.verify_privilege(ctx.user, 'tags:create')
            db.session.flush()
            for tag in new_tags:
                snapshots.create(tag, ctx.user)
    if ctx.has_param('safety'):
        auth.verify_privilege(ctx.user, 'posts:edit:safety')
        posts.update_post_safety(post, ctx.get_param_as_string('safety'))
    if ctx.has_param('source'):
        auth.verify_privilege(ctx.user, 'posts:edit:source')
        posts.update_post_source(post, ctx.get_param_as_string('source'))
    elif ctx.has_param('contentUrl'):
        posts.update_post_source(post, ctx.get_param_as_string('contentUrl'))
    if ctx.has_param('relations'):
        auth.verify_privilege(ctx.user, 'posts:edit:relations')
        posts.update_post_relations(
            post, ctx.get_param_as_int_list('relations'))
    if ctx.has_param('notes'):
        auth.verify_privilege(ctx.user, 'posts:edit:notes')
        posts.update_post_notes(post, ctx.get_param_as_list('notes'))
    if ctx.has_param('flags'):
        auth.verify_privilege(ctx.user, 'posts:edit:flags')
        posts.update_post_flags(post, ctx.get_param_as_string_list('flags'))
    if ctx.has_file('thumbnail'):
        auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
        posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
    post.last_edit_time = datetime.utcnow()
    ctx.session.flush()
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    tags.export_to_json()
    return _serialize_post(ctx, post)
Ejemplo n.º 26
0
def update_post(ctx: rest.Context, params: Dict[str, str]) -> rest.Response:
    post = _get_post(params)
    versions.verify_version(post, ctx)
    versions.bump_version(post)
    if ctx.has_file('content'):
        auth.verify_privilege(ctx.user, 'posts:edit:content')
        posts.update_post_content(post, ctx.get_file('content'))
    if ctx.has_param('tags'):
        auth.verify_privilege(ctx.user, 'posts:edit:tags')
        new_tags = posts.update_post_tags(
            post, ctx.get_param_as_string_list('tags'))
        if len(new_tags):
            auth.verify_privilege(ctx.user, 'tags:create')
            db.session.flush()
            for tag in new_tags:
                snapshots.create(tag, ctx.user)
    if ctx.has_param('safety'):
        auth.verify_privilege(ctx.user, 'posts:edit:safety')
        posts.update_post_safety(post, ctx.get_param_as_string('safety'))
    if ctx.has_param('source'):
        auth.verify_privilege(ctx.user, 'posts:edit:source')
        posts.update_post_source(post, ctx.get_param_as_string('source'))
    elif ctx.has_param('contentUrl'):
        posts.update_post_source(post, ctx.get_param_as_string('contentUrl'))
    if ctx.has_param('relations'):
        auth.verify_privilege(ctx.user, 'posts:edit:relations')
        posts.update_post_relations(
            post, ctx.get_param_as_int_list('relations'))
    if ctx.has_param('notes'):
        auth.verify_privilege(ctx.user, 'posts:edit:notes')
        posts.update_post_notes(post, ctx.get_param_as_list('notes'))
    if ctx.has_param('flags'):
        auth.verify_privilege(ctx.user, 'posts:edit:flags')
        posts.update_post_flags(post, ctx.get_param_as_string_list('flags'))
    if ctx.has_file('thumbnail'):
        auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
        posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
    post.last_edit_time = datetime.utcnow()
    ctx.session.flush()
    snapshots.modify(post, ctx.user)
    ctx.session.commit()
    return _serialize_post(ctx, post)
Ejemplo n.º 27
0
def test_merge_posts_replaces_content(
    post_factory, config_injector, tmpdir, read_asset
):
    config_injector(
        {
            "data_dir": str(tmpdir.mkdir("data")),
            "data_url": "example.com",
            "delete_source_files": False,
            "thumbnails": {
                "post_width": 300,
                "post_height": 300,
            },
            "secret": "test",
        }
    )
    source_post = post_factory(id=1)
    target_post = post_factory(id=2)
    content = read_asset("png.png")
    db.session.add_all([source_post, target_post])
    db.session.commit()
    posts.update_post_content(source_post, content)
    db.session.flush()
    source_path = os.path.join(
        "{}/data/posts/1_244c8840887984c4.png".format(tmpdir)
    )
    target_path1 = os.path.join(
        "{}/data/posts/2_49caeb3ec1643406.png".format(tmpdir)
    )
    target_path2 = os.path.join(
        "{}/data/posts/2_49caeb3ec1643406.dat".format(tmpdir)
    )
    assert os.path.exists(source_path)
    assert not os.path.exists(target_path1)
    assert not os.path.exists(target_path2)
    posts.merge_posts(source_post, target_post, True)
    db.session.flush()
    assert posts.try_get_post_by_id(source_post.post_id) is None
    post = posts.get_post_by_id(target_post.post_id)
    assert post is not None
    assert os.path.exists(source_path)
    assert os.path.exists(target_path1)
    assert not os.path.exists(target_path2)
Ejemplo n.º 28
0
def test_update_post_content_for_new_post(
        tmpdir,
        config_injector,
        post_factory,
        read_asset,
        is_existing,
        input_file,
        expected_mime_type,
        expected_type,
        output_file_name):
    with patch('szurubooru.func.util.get_sha1'):
        util.get_sha1.return_value = 'crc'
        config_injector({
            'data_dir': str(tmpdir.mkdir('data')),
            'thumbnails': {
                'post_width': 300,
                'post_height': 300,
            },
        })
        output_file_path = str(tmpdir) + '/data/posts/' + output_file_name
        post = post_factory()
        db.session.add(post)
        if is_existing:
            db.session.flush()
            assert post.post_id
        else:
            assert not post.post_id
        assert not os.path.exists(output_file_path)
        content = read_asset(input_file)
        posts.update_post_content(post, content)
        assert not os.path.exists(output_file_path)
        db.session.flush()
        assert post.mime_type == expected_mime_type
        assert post.type == expected_type
        assert post.checksum == 'crc'
        assert os.path.exists(output_file_path)
        if post.type in (db.Post.TYPE_IMAGE, db.Post.TYPE_ANIMATION):
            image_hash.delete_image.assert_called_once_with(post.post_id)
            image_hash.add_image.assert_called_once_with(post.post_id, content)
        else:
            image_hash.delete_image.assert_not_called()
            image_hash.add_image.assert_not_called()
Ejemplo n.º 29
0
def test_update_post_thumbnail_with_broken_thumbnail(
    tmpdir, config_injector, read_asset, post_factory, is_existing
):
    config_injector(
        {
            "data_dir": str(tmpdir.mkdir("data")),
            "thumbnails": {
                "post_width": 300,
                "post_height": 300,
            },
            "secret": "test",
            "allow_broken_uploads": False,
        }
    )
    post = post_factory(id=1)
    db.session.add(post)
    if is_existing:
        db.session.flush()
    assert post.post_id
    generated_path = (
        "{}/data/generated-thumbnails/".format(tmpdir)
        + "1_244c8840887984c4.jpg"
    )
    source_path = (
        "{}/data/posts/custom-thumbnails/".format(tmpdir)
        + "1_244c8840887984c4.dat"
    )
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    posts.update_post_content(post, read_asset("png.png"))
    posts.update_post_thumbnail(post, read_asset("png-broken.png"))
    assert not os.path.exists(generated_path)
    assert not os.path.exists(source_path)
    db.session.flush()
    assert os.path.exists(generated_path)
    assert os.path.exists(source_path)
    with open(source_path, "rb") as handle:
        assert handle.read() == read_asset("png-broken.png")
    with open(generated_path, "rb") as handle:
        image = images.Image(handle.read())
        assert image.width == 1
        assert image.height == 1
Ejemplo n.º 30
0
def test_update_post_content(tmpdir, config_injector, post_factory, read_asset,
                             input_file, expected_mime_type, expected_type,
                             output_file_name):
    with unittest.mock.patch('szurubooru.func.util.get_md5',
                             return_value='crc'):
        config_injector({
            'data_dir': str(tmpdir.mkdir('data')),
            'thumbnails': {
                'post_width': 300,
                'post_height': 300,
            },
        })
        post = post_factory(id=1)
        db.session.add(post)
        db.session.flush()
        posts.update_post_content(post, read_asset(input_file))
    assert post.mime_type == expected_mime_type
    assert post.type == expected_type
    assert post.checksum == 'crc'
    assert os.path.exists(str(tmpdir) + '/data/posts/' + output_file_name)
Ejemplo n.º 31
0
def test_update_post_thumbnail_broken_thumbnail(tmpdir, config_injector,
                                                read_asset, post_factory):
    config_injector({
        'data_dir': str(tmpdir.mkdir('data')),
        'thumbnails': {
            'post_width': 300,
            'post_height': 300,
        },
    })
    post = post_factory(id=1)
    db.session.add(post)
    db.session.flush()
    posts.update_post_content(post, read_asset('png.png'))
    posts.update_post_thumbnail(post, read_asset('png-broken.png'))
    assert os.path.exists(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat')
    assert os.path.exists(str(tmpdir) + '/data/generated-thumbnails/1.jpg')
    with open(str(tmpdir) + '/data/posts/custom-thumbnails/1.dat',
              'rb') as handle:
        assert handle.read() == read_asset('png-broken.png')
    with open(str(tmpdir) + '/data/generated-thumbnails/1.jpg',
              'rb') as handle:
        image = images.Image(handle.read())
        assert image.width == 1
        assert image.height == 1
Ejemplo n.º 32
0
 def put(self, ctx, post_id):
     post = posts.get_post_by_id(post_id)
     if ctx.has_file('content'):
         auth.verify_privilege(ctx.user, 'posts:edit:content')
         posts.update_post_content(post, ctx.get_file('content'))
     if ctx.has_param('tags'):
         auth.verify_privilege(ctx.user, 'posts:edit:tags')
         posts.update_post_tags(post, ctx.get_param_as_list('tags'))
     if ctx.has_param('safety'):
         auth.verify_privilege(ctx.user, 'posts:edit:safety')
         posts.update_post_safety(post, ctx.get_param_as_string('safety'))
     if ctx.has_param('source'):
         auth.verify_privilege(ctx.user, 'posts:edit:source')
         posts.update_post_source(post, ctx.get_param_as_string('source'))
     elif ctx.has_param('contentUrl'):
         posts.update_post_source(post,
                                  ctx.get_param_as_string('contentUrl'))
     if ctx.has_param('relations'):
         auth.verify_privilege(ctx.user, 'posts:edit:relations')
         posts.update_post_relations(post,
                                     ctx.get_param_as_list('relations'))
     if ctx.has_param('notes'):
         auth.verify_privilege(ctx.user, 'posts:edit:notes')
         posts.update_post_notes(post, ctx.get_param_as_list('notes'))
     if ctx.has_param('flags'):
         auth.verify_privilege(ctx.user, 'posts:edit:flags')
         posts.update_post_flags(post, ctx.get_param_as_list('flags'))
     if ctx.has_file('thumbnail'):
         auth.verify_privilege(ctx.user, 'posts:edit:thumbnail')
         posts.update_post_thumbnail(post, ctx.get_file('thumbnail'))
     post.last_edit_time = datetime.datetime.now()
     ctx.session.flush()
     snapshots.save_entity_modification(post, ctx.user)
     ctx.session.commit()
     tags.export_to_json()
     return posts.serialize_post_with_details(post, ctx.user)
Ejemplo n.º 33
0
def test_update_post_invalid_content(input_content):
    post = db.Post()
    with pytest.raises(posts.InvalidPostContentError):
        posts.update_post_content(post, input_content)
Ejemplo n.º 34
0
def test_update_post_content_with_invalid_content(input_content):
    post = db.Post()
    with pytest.raises(posts.InvalidPostContentError):
        posts.update_post_content(post, input_content)