Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 8 additions & 20 deletions server/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
Common exception definitions
"""

import humanize

from server.timing import datetime_now
from datetime import timezone


class ClientError(Exception):
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Expand All @@ -31,23 +29,13 @@ def __init__(self, ban_expiry, ban_reason, *args, **kwargs):
self.ban_expiry = ban_expiry
self.ban_reason = ban_reason

def message(self):
return (
f"You are banned from FAF {self._ban_duration_text()}. <br>"
f"Reason: <br>{self.ban_reason}<br><br>"
"<i>If you would like to appeal this ban, please send an email to: "
"moderation@faforever.com</i>"
)

def _ban_duration_text(self):
ban_duration = self.ban_expiry - datetime_now()
if ban_duration.days > 365 * 100:
return "forever"
humanized_ban_duration = humanize.precisedelta(
ban_duration,
minimum_unit="hours"
)
return f"for {humanized_ban_duration}"
def to_payload(self):
expiry_utc = self.ban_expiry.astimezone(timezone.utc)
return {
"command": "banned",
"expires_at": expiry_utc.isoformat(),
"reason": self.ban_reason,
}


class AuthenticationError(Exception):
Expand Down
8 changes: 2 additions & 6 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,8 @@ async def on_message_received(self, message):
"text": e.message
})
except BanError as e:
await self.send({
"command": "notice",
"style": "error",
"text": e.message()
})
await self.abort(e.message())
await self.send(e.to_payload())
await self.abort("Banned user blocked from lobby session")
except ClientError as e:
self._logger.warning(
"ClientError[%s]: %s",
Expand Down
2 changes: 1 addition & 1 deletion server/protocol/qdatastream.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def pack_message(*args: str) -> bytes:
raise NotImplementedError("Only string serialization is supported")

msg += QDataStreamProtocol.pack_qstring(arg)
return QDataStreamProtocol.pack_block(msg)
return QDataStreamProtocol.pack_block(bytes(msg))

@staticmethod
def encode_message(message: dict) -> bytes:
Expand Down
25 changes: 7 additions & 18 deletions tests/integration_tests/test_login.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from time import time

import jwt
Expand Down Expand Up @@ -38,15 +39,9 @@ async def test_server_ban(lobby_server, user):
proto = await connect_client(lobby_server)
await perform_login(proto, user)
msg = await proto.read_message()
assert msg == {
"command": "notice",
"style": "error",
"text": (
"You are banned from FAF forever. <br>Reason: <br>Test permanent ban"
"<br><br><i>If you would like to appeal this ban, please send an "
"email to: moderation@faforever.com</i>"
)
}
assert msg["command"] == "banned"
assert msg["reason"] == "Test permanent ban"
assert datetime.fromisoformat(msg["expires_at"]).astimezone(timezone.utc).year >= 2500


@pytest.mark.parametrize("user", [
Expand All @@ -73,15 +68,9 @@ async def test_server_ban_token(lobby_server, user, jwk_priv_key, jwk_kid):
"unique_id": "some_id"
})
msg = await proto.read_message()
assert msg == {
"command": "notice",
"style": "error",
"text": (
"You are banned from FAF forever. <br>Reason: <br>Test permanent ban"
"<br><br><i>If you would like to appeal this ban, please send an "
"email to: moderation@faforever.com</i>"
)
}
assert msg["command"] == "banned"
assert msg["reason"] == "Test permanent ban"
assert datetime.fromisoformat(msg["expires_at"]).astimezone(timezone.utc).year >= 2500


@pytest.mark.parametrize("user", ["ban_revoked", "ban_expired"])
Expand Down
14 changes: 4 additions & 10 deletions tests/integration_tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ async def test_game_host_name_non_ascii(lobby_server):
"command": "game_host",
"mod": "",
"visibility": "public",
"title": "ÇÒÖL GÃMÊ"
"title": "ÇÖL GÂMÊ"
})

msg = await read_until_command(proto, "notice", timeout=10)
Expand Down Expand Up @@ -845,15 +845,9 @@ async def test_server_ban_prevents_hosting(lobby_server, database, command):
await proto.send_message({"command": command})

msg = await proto.read_message()
assert msg == {
"command": "notice",
"style": "error",
"text": (
"You are banned from FAF forever. <br>Reason: <br>Test live ban<br>"
"<br><i>If you would like to appeal this ban, please send an email "
"to: moderation@faforever.com</i>"
)
}
assert msg["command"] == "banned"
assert msg["reason"] == "Test live ban"
assert msg["expires_at"].endswith("+00:00")


@fast_forward(5)
Expand Down
18 changes: 7 additions & 11 deletions tests/unit_tests/test_lobbyconnection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from hashlib import sha256
from unittest import mock

Expand Down Expand Up @@ -1303,18 +1302,15 @@ async def test_abort_connection_if_banned(
lobbyconnection.player.id = 203
with pytest.raises(BanError) as banned_error:
await lobbyconnection.abort_connection_if_banned()
assert banned_error.value.message() == (
"You are banned from FAF forever. <br>Reason: <br>Test permanent ban"
"<br><br><i>If you would like to appeal this ban, please send an email "
"to: moderation@faforever.com</i>"
)
assert banned_error.value.to_payload()["command"] == "banned"
assert banned_error.value.to_payload()["reason"] == "Test permanent ban"
assert banned_error.value.to_payload()["expires_at"].endswith("+00:00")

# test user who is banned for another 46 hours
lobbyconnection.player.id = 204
with pytest.raises(BanError) as banned_error:
await lobbyconnection.abort_connection_if_banned()
assert re.match(
r"You are banned from FAF for 1 day and 2[12]\.[0-9]+ hours. <br>"
"Reason: <br>Test ongoing ban with 46 hours left",
banned_error.value.message()
)
payload = banned_error.value.to_payload()
assert payload["command"] == "banned"
assert payload["reason"] == "Test ongoing ban with 46 hours left"
assert payload["expires_at"].endswith("+00:00")
Loading