Skip to content

Commit

Permalink
Use modern project layout and CI conf
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Velten committed Feb 5, 2024
1 parent 08e0191 commit 061441b
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 185 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tests

on:
push:
branches: ["main"]
pull_request:


jobs:
check-code-style:
name: Check code style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: python -m pip install tox
- run: tox -e check_codestyle

# check-types:
# name: Check types with Mypy
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: actions/setup-python@v4
# with:
# python-version: "3.11"
# - run: python -m pip install tox
# - run: tox -e check_types

unit-tests:
name: Unit tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.11"
- run: python -m pip install tox
- run: tox -e py
21 changes: 0 additions & 21 deletions .github/workflows/tests.yaml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ _trial_temp/
/build/
/dist/
/*.egg-info/
/.venv/
83 changes: 83 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[project]
name = "room_access_rules"
description = "Custom room access rules for Tchap."
readme = "README.md"
dynamic = ["version"]

requires-python = ">=3.8"

classifiers = [
"License :: OSI Approved :: Apache Software License"
]

dependencies = [
"attrs"
]

[project.optional-dependencies]
dev = [
# for tests
"pydantic >= 1.7.4, < 2.0",
"matrix-synapse == 1.98.0",
"tox",
"twisted",
"aiounittest",
# for type checking
"mypy == 1.6.1",
# for linting
"black == 23.10.0",
"ruff == 0.1.1",
]

[project.urls]
repository = "https://github.com/tchapgouv/synapse-room-access-rules"

[build-system]
requires = ["setuptools", "setuptools_scm", "wheel"]
build-backend = "setuptools.build_meta"

[tool.setuptools_scm]

[tool.mypy]
strict = true

[tool.ruff]
line-length = 88

# See https://docs.astral.sh/ruff/rules/#error-e
# for error codes. The ones we ignore are:
# E501: Line too long (black enforces this for us)
# E731: do not assign a lambda expression, use a def
#
# flake8-bugbear compatible checks. Its error codes are described at
# https://docs.astral.sh/ruff/rules/#flake8-bugbear-b
# B023: Functions defined inside a loop must not use variables redefined in the loop
ignore = [
"B023",
"E501",
"E731",
]
select = [
# pycodestyle
"E",
"W",
# pyflakes
"F",
# flake8-bugbear
"B0",
# flake8-comprehensions
"C4",
# flake8-2020
"YTT",
# flake8-slots
"SLOT",
# flake8-debugger
"T10",
# flake8-pie
"PIE",
# flake8-executable
"EXE",
# isort
"I",
]

31 changes: 2 additions & 29 deletions room_access_rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import Any, Dict, List, Optional, Tuple

import attr
from synapse.api.constants import EventTypes, JoinRules, Membership, RoomCreationPreset
from synapse.events import EventBase
from synapse.module_api import ModuleApi, UserID
from synapse.module_api.errors import ConfigError, SynapseError
Expand All @@ -27,34 +28,6 @@
ACCESS_RULES_TYPE = "im.vector.room.access_rules"


class EventTypes:
Member = "m.room.member"
Tombstone = "m.room.tombstone"
JoinRules = "m.room.join_rules"
PowerLevels = "m.room.power_levels"
ThirdPartyInvite = "m.room.third_party_invite"
RoomHistoryVisibility = "m.room.history_visibility"
CanonicalAlias = "m.room.canonical_alias"
RoomAvatar = "m.room.avatar"
RoomEncryption = "m.room.encryption"
Topic = "m.room.topic"
Name = "m.room.name"
ServerACL = "m.room.server_acl"


class JoinRules:
PUBLIC = "public"


class Membership:
INVITE = "invite"
JOIN = "join"


class RoomCreationPreset:
PUBLIC_CHAT = "public_chat"


class AccessRules:
DIRECT = "direct"
RESTRICTED = "restricted"
Expand Down Expand Up @@ -364,7 +337,7 @@ async def check_event_allowed(
self._is_power_level_content_allowed(
event.content, rule, on_room_creation=False
),
None
None,
)

if (
Expand Down
19 changes: 19 additions & 0 deletions scripts-dev/lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Runs linting scripts and type checking
# black - opinionated code formatter
# ruff - lints, finds mistakes, and sorts import statements
# mypy - checks type annotations

set -e

files=(
"room_access_rules"
"tests"
)

# Print out the commands being run
set -x

black "${files[@]}"
ruff --fix "${files[@]}"
mypy "${files[@]}"
57 changes: 0 additions & 57 deletions setup.py

This file was deleted.

15 changes: 10 additions & 5 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
from typing import Any, Dict, Optional
from unittest.mock import Mock

import attr
from synapse.module_api import ModuleApi, UserID

from room_access_rules import RoomAccessRules, ACCESS_RULES_TYPE
from room_access_rules import ACCESS_RULES_TYPE, RoomAccessRules

PUBLIC_ROOM_ID = "!public:example.com"


class MockHttpClient:
async def get_json(self, uri, args):
return {"hs": args["address"].split('@')[1]}
return {"hs": args["address"].split("@")[1]}


class MockPublicRoomListManager:
Expand All @@ -41,6 +41,7 @@ def __init__(self, user_id: str):
@attr.s(auto_attribs=True)
class MockEvent:
"""Mocks an event. Only exposes properties the module uses."""

sender: str
type: str
content: dict
Expand Down Expand Up @@ -69,15 +70,19 @@ def new_access_rules_event(sender: str, room_id: str, rule: str) -> MockEvent:
)


def create_module(config_override={}) -> RoomAccessRules:
def create_module(
config_override: Optional[Dict[str, Any]] = None, server_name: str = "example.com"
) -> RoomAccessRules:
# Create a mock based on the ModuleApi spec, but override some mocked functions
# because some capabilities are needed for running the tests.
module_api = Mock(spec=ModuleApi)
module_api.http_client = MockHttpClient()
module_api.public_room_list_manager = MockPublicRoomListManager()

if config_override is None:
config_override = {}
config_override["id_server"] = "example.com"

config = RoomAccessRules.parse_config(config_override)

return RoomAccessRules(config, module_api)
return RoomAccessRules(config, module_api)
Loading

0 comments on commit 061441b

Please sign in to comment.