Esempio n. 1
0
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
Esempio n. 2
0
def test_new_forum_failure(app, authed_client, category_id):
    with pytest.raises(APIException):
        Forum.new(
            name='NewForum',
            description=None,
            category_id=category_id,
            position=100,
        )
Esempio n. 3
0
 def inject_forums(self):
     logging.info('Injecting forums...')
     with open('utils/dbinjector/forums.csv', newline='') as csv_file:
         csv_reader = csv.reader(csv_file, delimiter=',')
         for row in csv_reader:
             forum = Forum()
             forum.name = row[0]
             forum.save()
Esempio n. 4
0
def test_new_forum(app, authed_client):
    forum = Forum.new(name='NewForum',
                      description=None,
                      category_id=2,
                      position=100)
    assert forum.name == 'NewForum'
    assert forum.description is None
    assert forum.position == 100
    assert Forum.from_cache(forum.cache_key).id == forum.id == 7
Esempio n. 5
0
def test_subscribe_forum_deletes_cache_keys(app, authed_client):
    add_permissions(app, ForumPermissions.MODIFY_SUBSCRIPTIONS)
    Forum.from_subscribed_user(1)
    ForumSubscription.user_ids_from_forum(5)
    response = authed_client.post('/subscriptions/forums/5')
    assert response.status_code == 200
    assert not cache.get(
        ForumSubscription.__cache_key_users__.format(forum_id=5)
    )
    assert not cache.get(
        ForumSubscription.__cache_key_of_user__.format(user_id=1)
    )
    assert ForumSubscription.user_ids_from_forum(5) == [3, 4, 1]
Esempio n. 6
0
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')
Esempio n. 7
0
def post(request):
    if request.method == "POST":
        question = request.POST.get("question", "")
        description = request.POST.get("description", "")
        author = request.user.username
        tags = request.POST.get("tags", "")
        newQues = Forum(question=question,
                        description=description,
                        author=author,
                        tags=tags)
        newQues.save()
        return HttpResponseRedirect('/forums/')
    else:
        raise Http404
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
Esempio n. 9
0
def view_forum_subscriptions() -> flask.Response:
    """
    This is the endpoint to view forum and thread subscriptions. The
    ``forums_view_subscriptions`` permission is required to access this endpoint.

    .. :quickref: ForumSubscription; View forum subscriptions.

    **Example response**:

    .. parsed-literal::

       {
         "status": "success",
         "response": {
            "forum_subscriptions": [
              "<Forum>",
              "<Forum>"
            ],
            "thread_subscriptions": [
              "<ForumThread>",
              "<ForumThread>"
            ]
          }
        }

    :>json dict response: The forum and thread subscriptions

    :statuscode 200: The forum subscriptions
    """
    return flask.jsonify(Forum.from_subscribed_user(flask.g.user.id))
Esempio n. 10
0
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.')
Esempio n. 11
0
def test_forum_get_from_category(app, authed_client):
    forums = Forum.from_category(1)
    assert len([f for f in forums if f]) == 2

    for forum in forums:
        if forum.name == 'Bugs' and forum.id == 2:
            break
    else:
        raise AssertionError('A real forum not called')
Esempio n. 12
0
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)
Esempio n. 13
0
def test_thread_model():
    forum = Forum(title='Test Forum', description='This is a Test Forum')
    forum.save()
    user = get_user_model().objects.create_user(
        username='******',
        email='*****@*****.**',
        password='******',
    )
    thread = Thread(title='A new thread',
                    text='The text of the thread',
                    forum=forum,
                    user=user)
    thread.save()
    assert thread.title == 'A new thread'
    assert thread.text == 'The text of the thread'
    assert thread.forum == forum
    assert thread.user == user
    assert thread.added
    assert thread.edited
    assert str(
        thread) == f'Thread: {thread.title} - (started by {thread.user})'
Esempio n. 14
0
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
Esempio n. 15
0
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}.'
        )
Esempio n. 16
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
Esempio n. 17
0
def create_forum():
    request_data = request.json
    fields_missing = require_values(request_data, ["title", "forumType", "parent"])
    if len(fields_missing):
        return response.errors({"fields_missing": fields_missing})
    invalid_values = {}
    if request_data["forumType"] not in Forum.ForumTypes.values:
        invalid_values[
            "forum_type"
        ] = f"forumTypes must be in [{Forum.ForumTypes.values}])"
    if not int(request_data["parent"]):
        invalid_values["parent"] = f"parent must be an integer"
    try:
        parent: Forum = get_objects_by_id(
            request_data["parent"], Forum, CacheKeys.FORUM_DETAILS.value
        )
    except Forum.DoesNotExist:
        invalid_values["parent"] = f"parent \"{request_data['parent']}\" does not exist"

    game = None
    if "game_id" in request_data:
        game: Game = get_objects_by_id(
            request_data["game_id"], Game, CacheKeys.GAME_DETAILS.value
        )
        if not game:
            invalid_values[
                "game_id"
            ] = f"game_id \"{request_data['game_id']}\" does not exist"
    if invalid_values:
        return response.errors({"invalid_values": invalid_values})

    forum_values = {
        "title": request_data["title"],
        "forumType": request_data["forumType"],
        "parent": parent,
    }
    if "description" in request_data:
        forum_values["description"] = request_data["description"]
    if game:
        forum_values["game"] = game

    forum = Forum(**forum_values)
    forum.save()
    forum.generate_heritage()
    forum.save()
    return response.success({"forum": {"id": forum.id, "title": forum.title}})
Esempio n. 18
0
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
Esempio n. 19
0
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'])
Esempio n. 20
0
def create_forum(name: str,
                 category_id: int,
                 description: str = None,
                 position: int = 0) -> flask.Response:
    """
    This is the endpoint for forum creation. The ``forums_forums_modify`` permission
    is required to access this endpoint.

    .. :quickref: Forum; Create a forum.

    **Example request**:

    .. parsed-literal::

       POST /forums HTTP/1.1

       {
         "name": "Support",
         "description": "The place for confused share bears.",
         "position": 6
       }

    **Example response**:

    .. parsed-literal::

       {
         "status": "success",
         "response": "<Forum>"
       }

    :>json dict response: The newly created forum

    :statuscode 200: Creation successful
    :statuscode 400: Creation unsuccessful
    """
    forum = Forum.new(
        name=name,
        category_id=category_id,
        description=description,
        position=position,
    )
    return flask.jsonify(forum)
Esempio n. 21
0
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)
Esempio n. 22
0
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
Esempio n. 23
0
def test_forum_thread_count(app, authed_client, forum_id, count):
    assert Forum.from_pk(forum_id).thread_count == count
Esempio n. 24
0
def test_forum_last_updated_thread(app, authed_client):
    forum = Forum.from_pk(2)
    assert forum.last_updated_thread.id == 3
Esempio n. 25
0
def test_forum_model(add_forum):
    forum = Forum(title='Test Forum', description='This is a Test Forum')
    forum.save()
    assert forum.title == 'Test Forum'
    assert forum.description == 'This is a Test Forum'
    assert str(forum) == f'Forum: {forum.title}'
Esempio n. 26
0
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
Esempio n. 27
0
def test_forum_get_from_category_no_permissions(app, authed_client):
    db.engine.execute(
        "DELETE FROM users_permissions WHERE permission LIKE 'forumaccess%%'")
    forums = Forum.from_category(1)
    assert len(forums) == 0
Esempio n. 28
0
def forum(**kwargs):
    if 'name' not in kwargs:
        kwargs['name'] = u'test forum'
    if 'slug' not in kwargs:
        kwargs['slug'] = u'testforum'
    return Forum(**kwargs)
Esempio n. 29
0
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)
Esempio n. 30
0
def test_forum_from_pk(app, authed_client):
    forum = Forum.from_pk(1)
    assert forum.name == 'Pulsar'
    assert forum.description == 'Stuff about pulsar'