Skip to content

Instantly share code, notes, and snippets.

@lamoboos223
Last active November 13, 2024 10:19
Show Gist options
  • Save lamoboos223/9c68dd72aa7e55e7764e6897216a2090 to your computer and use it in GitHub Desktop.
Save lamoboos223/9c68dd72aa7e55e7764e6897216a2090 to your computer and use it in GitHub Desktop.

Build Project From Source

poetry install --extras all

Activate Virtual Environment (MacOS)

source /Users/<user_name>/Library/Caches/pypoetry/virtualenvs/<virtual_env_id>/bin/activate
# or poetry shell

Format Code

poetry run ./scripts-dev/lint.sh

Run Unit Test

poetry run trial tests

Run Unit Test On Single File

poetry run trial tests/rest/client/test_tags.py

Run Homeserver

python -m synapse.app.homeserver --config-path=homeserver.yaml

Run Worker

python -m synapse.app.generic_worker --config-path=homeserver.yaml --config-path=workers/worker1.yaml
@lamoboos223
Copy link
Author

tests/rest/client/test_tags.py

"""Tests REST events for /tags paths."""

from http import HTTPStatus

import synapse.rest.admin
from synapse.rest.client import login, room, tags

from tests import unittest


class RoomTaggingTestCase(unittest.HomeserverTestCase):
    """Tests /user/$user_id/rooms/$room_id/tags/$tag REST API."""

    servlets = [
        room.register_servlets,
        tags.register_servlets,
        login.register_servlets,
        synapse.rest.admin.register_servlets_for_client_rest_resource,
    ]

    def test_put_tag_checks_room_membership(self) -> None:
        """
        Test that a user can add a tag to a room if they have membership to the room.
        """
        user1_id = self.register_user("user1", "pass")
        user1_tok = self.login(user1_id, "pass")
        room_id = self.helper.create_room_as(user1_id, tok=user1_tok)
        tag = "test_tag"

        # Make the request
        channel = self.make_request(
            "PUT",
            f"/user/{user1_id}/rooms/{room_id}/tags/{tag}",
            content={"order": 0.5},
            access_token=user1_tok,
        )
        # Check that the request was successful
        self.assertEqual(channel.code, HTTPStatus.OK, channel.result)

    def test_put_tag_fails_if_not_in_room(self) -> None:
        """
        Test that a user cannot add a tag to a room if they don't have membership to the room.
        """
        user1_id = self.register_user("user1", "pass")
        user1_tok = self.login(user1_id, "pass")

        room_id = "!nonexistent:test"
        tag = "test_tag"
        # Make the request
        channel = self.make_request(
            "PUT",
            f"/user/{user1_id}/rooms/{room_id}/tags/{tag}",
            content={"order": 0.5},
            access_token=user1_tok,
        )
        # Check that the request failed with the correct error
        self.assertEqual(channel.code, HTTPStatus.FORBIDDEN, channel.result)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment