Exemplo n.º 1
0
def test_trying_to_update_non_existing(context_factory, user_factory):
    with pytest.raises(posts.PostNotFoundError):
        api.PostDetailApi().put(
            context_factory(
                input='whatever',
                user=user_factory(rank=db.User.RANK_REGULAR)),
            1)
Exemplo n.º 2
0
def test_uploading_from_url_with_source_specified(
        config_injector, context_factory, post_factory, user_factory):
    config_injector({
        'privileges': {
            'posts:edit:content': db.User.RANK_REGULAR,
            'posts:edit:source': db.User.RANK_REGULAR,
        },
    })
    post = post_factory()
    db.session.add(post)
    db.session.flush()
    with unittest.mock.patch('szurubooru.func.net.download'), \
        unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
        unittest.mock.patch('szurubooru.func.snapshots.save_entity_modification'), \
        unittest.mock.patch('szurubooru.func.posts.serialize_post_with_details'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_source'):
        net.download.return_value = b'content'
        api.PostDetailApi().put(
            context_factory(
                input={'contentUrl': 'example.com', 'source': 'example2.com'},
                user=user_factory(rank=db.User.RANK_REGULAR)),
            post.post_id)
        net.download.assert_called_once_with('example.com')
        posts.update_post_content.assert_called_once_with(post, b'content')
        posts.update_post_source.assert_called_once_with(post, 'example2.com')
Exemplo n.º 3
0
def create_app():
    ''' Create a WSGI compatible App object. '''
    validate_config()
    falcon.responders.create_method_not_allowed = create_method_not_allowed

    coloredlogs.install(fmt='[%(asctime)-15s] %(name)s %(message)s')
    if config.config['debug']:
        logging.getLogger('szurubooru').setLevel(logging.INFO)
        logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

    app = falcon.API(request_type=api.Request,
                     middleware=[
                         middleware.RequireJson(),
                         middleware.ContextAdapter(),
                         middleware.DbSession(),
                         middleware.Authenticator(),
                     ])

    app.add_error_handler(errors.AuthError, _on_auth_error)
    app.add_error_handler(errors.IntegrityError, _on_integrity_error)
    app.add_error_handler(errors.ValidationError, _on_validation_error)
    app.add_error_handler(errors.SearchError, _on_search_error)
    app.add_error_handler(errors.NotFoundError, _on_not_found_error)
    app.add_error_handler(errors.ProcessingError, _on_processing_error)

    app.add_route('/users/', api.UserListApi())
    app.add_route('/user/{user_name}', api.UserDetailApi())
    app.add_route('/password-reset/{user_name}', api.PasswordResetApi())

    app.add_route('/tag-categories/', api.TagCategoryListApi())
    app.add_route('/tag-category/{category_name}', api.TagCategoryDetailApi())
    app.add_route('/tags/', api.TagListApi())
    app.add_route('/tag/{tag_name}', api.TagDetailApi())
    app.add_route('/tag-merge/', api.TagMergeApi())
    app.add_route('/tag-siblings/{tag_name}', api.TagSiblingsApi())

    app.add_route('/posts/', api.PostListApi())
    app.add_route('/post/{post_id}', api.PostDetailApi())
    app.add_route('/post/{post_id}/score', api.PostScoreApi())
    app.add_route('/post/{post_id}/favorite', api.PostFavoriteApi())

    app.add_route('/comments/', api.CommentListApi())
    app.add_route('/comment/{comment_id}', api.CommentDetailApi())
    app.add_route('/comment/{comment_id}/score', api.CommentScoreApi())

    app.add_route('/info/', api.InfoApi())
    app.add_route('/featured-post/', api.PostFeatureApi())
    app.add_route('/snapshots/', api.SnapshotListApi())

    return app
Exemplo n.º 4
0
def test_ctx(
        tmpdir, config_injector, context_factory, post_factory, user_factory):
    config_injector({
        'data_dir': str(tmpdir),
        'privileges': {
            'posts:delete': db.User.RANK_REGULAR,
        },
    })
    ret = util.dotdict()
    ret.context_factory = context_factory
    ret.user_factory = user_factory
    ret.post_factory = post_factory
    ret.api = api.PostDetailApi()
    return ret
Exemplo n.º 5
0
def test_ctx(tmpdir, context_factory, config_injector, user_factory,
             post_factory):
    config_injector({
        'data_dir': str(tmpdir),
        'data_url': 'http://example.com',
        'privileges': {
            'posts:list': db.User.RANK_REGULAR,
            'posts:view': db.User.RANK_REGULAR,
        },
        'thumbnails': {
            'avatar_width': 200
        },
    })
    ret = util.dotdict()
    ret.context_factory = context_factory
    ret.user_factory = user_factory
    ret.post_factory = post_factory
    ret.list_api = api.PostListApi()
    ret.detail_api = api.PostDetailApi()
    return ret
Exemplo n.º 6
0
def test_trying_to_create_without_privileges(
        config_injector,
        context_factory,
        post_factory,
        user_factory,
        files,
        input,
        privilege):
    config_injector({
        'privileges': {privilege: db.User.RANK_REGULAR},
    })
    post = post_factory()
    db.session.add(post)
    db.session.flush()
    with pytest.raises(errors.AuthError):
        api.PostDetailApi().put(
            context_factory(
                input=input,
                files=files,
                user=user_factory(rank=db.User.RANK_ANONYMOUS)),
            post.post_id)
Exemplo n.º 7
0
def test_post_updating(
        config_injector, context_factory, post_factory, user_factory, fake_datetime):
    config_injector({
        'privileges': {
            'posts:edit:tags': db.User.RANK_REGULAR,
            'posts:edit:content': db.User.RANK_REGULAR,
            'posts:edit:safety': db.User.RANK_REGULAR,
            'posts:edit:source': db.User.RANK_REGULAR,
            'posts:edit:relations': db.User.RANK_REGULAR,
            'posts:edit:notes': db.User.RANK_REGULAR,
            'posts:edit:flags': db.User.RANK_REGULAR,
            'posts:edit:thumbnail': db.User.RANK_REGULAR,
        },
    })
    auth_user = user_factory(rank=db.User.RANK_REGULAR)
    post = post_factory()
    db.session.add(post)
    db.session.flush()

    with unittest.mock.patch('szurubooru.func.posts.create_post'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_tags'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_content'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_thumbnail'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_safety'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_source'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_relations'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_notes'), \
        unittest.mock.patch('szurubooru.func.posts.update_post_flags'), \
        unittest.mock.patch('szurubooru.func.posts.serialize_post_with_details'), \
        unittest.mock.patch('szurubooru.func.tags.export_to_json'), \
        unittest.mock.patch('szurubooru.func.snapshots.save_entity_modification'):

        posts.serialize_post_with_details.return_value = 'serialized post'

        with fake_datetime('1997-01-01'):
            result = api.PostDetailApi().put(
                context_factory(
                    input={
                        'safety': 'safe',
                        'tags': ['tag1', 'tag2'],
                        'relations': [1, 2],
                        'source': 'source',
                        'notes': ['note1', 'note2'],
                        'flags': ['flag1', 'flag2'],
                    },
                    files={
                        'content': 'post-content',
                        'thumbnail': 'post-thumbnail',
                    },
                    user=auth_user),
                post.post_id)

        assert result == 'serialized post'
        posts.create_post.assert_not_called()
        posts.update_post_tags.assert_called_once_with(post, ['tag1', 'tag2'])
        posts.update_post_content.assert_called_once_with(post, 'post-content')
        posts.update_post_thumbnail.assert_called_once_with(post, 'post-thumbnail')
        posts.update_post_safety.assert_called_once_with(post, 'safe')
        posts.update_post_source.assert_called_once_with(post, 'source')
        posts.update_post_relations.assert_called_once_with(post, [1, 2])
        posts.update_post_notes.assert_called_once_with(post, ['note1', 'note2'])
        posts.update_post_flags.assert_called_once_with(post, ['flag1', 'flag2'])
        posts.serialize_post_with_details.assert_called_once_with(post, auth_user)
        tags.export_to_json.assert_called_once_with()
        snapshots.save_entity_modification.assert_called_once_with(post, auth_user)
        assert post.last_edit_time == datetime.datetime(1997, 1, 1)