def test_forum_cache(app, authed_client): forum = Forum.from_pk(1) cache.cache_model(forum, timeout=60) forum = Forum.from_pk(1) assert forum.name == 'Pulsar' assert forum.description == 'Stuff about pulsar' assert cache.ttl(forum.cache_key) < 61
def test_forum_get_from_category_cached(app, authed_client): cache.set(Forum.__cache_key_of_category__.format(id=2), ['1', '5'], timeout=60) Forum.from_pk(1) Forum.from_pk(5) # noqa: cache these forums = Forum.from_category(2) assert len(forums) == 2 for forum in forums: if forum.name == 'Yacht Funding' and forum.id == 5: break else: raise AssertionError('A real forum not called')
def delete_forum(id: int) -> flask.Response: """ This is the endpoint for forum deletion . The ``forums_forums_modify`` permission is required to access this endpoint. All threads in a deleted forum will also be deleted. .. :quickref: Forum; Delete a forum. **Example response**: .. parsed-literal:: { "status": "success", "response": "Forum 1 (Pulsar) has been deleted." } :>json str response: The deleted forum message :statuscode 200: Deletion successful :statuscode 400: Deletion unsuccessful :statuscode 404: Forum does not exist """ forum = Forum.from_pk(id, _404=True) forum.deleted = True ForumThread.update_many(pks=ForumThread.get_ids_from_forum(forum.id), update={'deleted': True}) return flask.jsonify(f'Forum {id} ({forum.name}) has been deleted.')
def test_edit_forum(app, authed_client): add_permissions(app, 'forums_view', 'forums_forums_modify') response = authed_client.put( '/forums/1', data=json.dumps({ 'name': 'Bite', 'description': 'Very New Description', 'category_id': 4, }), ) check_json_response( response, { 'id': 1, 'name': 'Bite', 'description': 'Very New Description' }, ) print(response.get_json()) assert response.get_json()['response']['category']['id'] == 4 forum = Forum.from_pk(1) assert forum.id == 1 assert forum.name == 'Bite' assert forum.description == 'Very New Description' assert forum.category_id == 4
def modify_forum( id: int, name: Optional_[str] = None, category_id: Optional_[int] = None, description: Union[str, bool, None] = False, position: Optional_[int] = None, ) -> flask.Response: """ This is the endpoint for forum editing. The ``forums_forums_modify`` permission is required to access this endpoint. The name, category, description, and position of a forum can be changed here. .. :quickref: Forum; Edit a forum. **Example request**: .. parsed-literal:: PUT /forums/6 HTTP/1.1 { "name": "Support", "description": "The place for **very** confused share bears.", "position": 99 } **Example response**: .. parsed-literal:: { "status": "success", "response": "<Forum>" } :>json dict response: The edited forum :statuscode 200: Editing successful :statuscode 400: Editing unsuccessful :statuscode 404: Forum does not exist """ forum = Forum.from_pk(id, _404=True) if name: forum.name = name if category_id and ForumCategory.is_valid(category_id, error=True): forum.category_id = category_id if description is not False: assert not isinstance(description, bool) forum.description = description if position is not None: forum.position = position db.session.commit() return flask.jsonify(forum)
def alter_forum_subscription(forum_id: int) -> flask.Response: """ This is the endpoint for forum subscription. The ``forums_subscriptions_modify`` permission is required to access this endpoint. A POST request creates a subscription, whereas a DELETE request removes a subscription. .. :quickref: ForumSubscription; Subscribe to a forum. **Example response**: .. parsed-literal:: { "status": "success", "response": "Successfully subscribed to forum 2." } :>json str response: Success or failure message :statuscode 200: Subscription alteration successful :statuscode 400: Subscription alteration unsuccessful :statuscode 404: Forum thread does not exist """ forum = Forum.from_pk(forum_id, _404=True) subscription = ForumSubscription.from_attrs( user_id=flask.g.user.id, forum_id=forum.id ) if flask.request.method == 'POST': if subscription: raise APIException( f'You are already subscribed to forum {forum_id}.' ) ForumSubscription.new(user_id=flask.g.user.id, forum_id=forum_id) return flask.jsonify(f'Successfully subscribed to forum {forum_id}.') else: # method = DELETE if not subscription: raise APIException(f'You are not subscribed to forum {forum_id}.') db.session.delete(subscription) db.session.commit() cache.delete( ForumSubscription.__cache_key_users__.format(forum_id=forum_id) ) cache.delete( ForumSubscription.__cache_key_of_user__.format( user_id=flask.g.user.id ) ) return flask.jsonify( f'Successfully unsubscribed from forum {forum_id}.' )
def test_edit_forum_skips(app, authed_client): add_permissions(app, 'forums_view', 'forums_forums_modify') response = authed_client.put('/forums/1', data=json.dumps({'position': 0})) check_json_response( response, { 'id': 1, 'name': 'Pulsar', 'description': 'Stuff about pulsar', 'position': 0, }, ) forum = Forum.from_pk(1) assert forum.position == 0
def test_serialize_no_perms(app, authed_client): forum = Forum.from_pk(1) data = NewJSONEncoder().default(forum) check_dictionary( data, { 'id': 1, 'name': 'Pulsar', 'description': 'Stuff about pulsar', 'position': 1, 'thread_count': 1, }, ) assert 'category' in data and data['category']['id'] == 1 assert 'threads' in data and len(data['threads']) == 1
def test_serialize_nested(app, authed_client): add_permissions(app, 'forums_forums_modify') forum = Forum.from_pk(1) data = forum.serialize(nested=True) check_dictionary( data, { 'id': 1, 'name': 'Pulsar', 'description': 'Stuff about pulsar', 'position': 1, 'thread_count': 1, 'deleted': False, }, ) assert ('last_updated_thread' in data and 'id' in data['last_updated_thread'])
def test_serialize_very_detailed(app, authed_client): add_permissions(app, 'forums_forums_modify') forum = Forum.from_pk(1) data = NewJSONEncoder().default(forum) check_dictionary( data, { 'id': 1, 'name': 'Pulsar', 'description': 'Stuff about pulsar', 'position': 1, 'thread_count': 1, 'deleted': False, }, ) assert 'category' in data and data['category']['id'] == 1 assert 'threads' in data and len(data['threads']) == 1
def view_forum(id: int, page: int = 1, limit: int = 50, include_dead: bool = False) -> flask.Response: """ This endpoint allows users to view details about a forum and its threads. .. :quickref: Forum; View a forum. **Example response**: .. parsed-literal:: { "status": "success", "response": [ "<Forum>", "<Forum>" ] } :>json list response: A list of forums :statuscode 200: View successful :statuscode 403: User does not have permission to view forum :statuscode 404: Forum does not exist """ forum = Forum.from_pk( id, _404=True, include_dead=flask.g.user.has_permission('forums_forums_modify'), ) forum.set_threads( page, limit, include_dead and flask.g.user.has_permission('forums_threads_modify_advanced'), ) return flask.jsonify(forum)
def test_forum_from_pk(app, authed_client): forum = Forum.from_pk(1) assert forum.name == 'Pulsar' assert forum.description == 'Stuff about pulsar'
def test_forum_threads_setter(app, authed_client): forum = Forum.from_pk(2) forum.set_threads(page=1, limit=1) threads = forum.threads assert len(threads) == 1 assert threads[0].topic == 'Using PHP'
def test_forum_threads_with_deleted(app, authed_client): forum = Forum.from_pk(1) threads = forum.threads assert len(threads) == 1 assert threads[0].topic != 'New Site Borked'
def test_forum_last_updated_thread_from_cache(app, authed_client): cache.set(Forum.__cache_key_last_updated__.format(id=2), 4) forum = Forum.from_pk(2) assert forum.last_updated_thread.id == 4
def test_forum_last_updated_thread(app, authed_client): forum = Forum.from_pk(2) assert forum.last_updated_thread.id == 3
def test_forum_thread_count_from_cache(app, authed_client): cache.set(Forum.__cache_key_thread_count__.format(id=2), 40) assert Forum.from_pk(2).thread_count == 40
def test_forum_thread_count(app, authed_client, forum_id, count): assert Forum.from_pk(forum_id).thread_count == count
def test_forum_threads(app, authed_client): forum = Forum.from_pk(2) threads = forum.threads assert len(threads) == 2 assert any(t.topic == 'Using PHP' for t in threads)
def test_forum_no_permission(app, authed_client): db.engine.execute( "DELETE FROM users_permissions WHERE permission LIKE 'forumaccess%%'") with pytest.raises(_403Exception): Forum.from_pk(1, error=True)