Skip to content

Commit

Permalink
Strip whitespace from game title and enforce minimum length
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Jan 1, 2024
1 parent aab63c5 commit b4bb165
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@ def name(self, value: str):
"""
Verifies that names only contain ascii characters.
"""
value = value.strip()

if not value.isascii():
raise ValueError("Name must be ascii!")
raise ValueError("Game title must be ascii!")

if not value:
raise ValueError("Game title must not be empty!")

self.set_name_unchecked(value)

Expand Down
2 changes: 2 additions & 0 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,8 @@ async def command_game_host(self, message):
title = message.get("title") or f"{self.player.login}'s game"
if not title.isascii():
raise ClientError("Title must contain only ascii characters.")
if not title.strip():
raise ClientError("Title must not be empty.")

mod = message.get("mod") or FeaturedModType.FAF
mapname = message.get("mapname") or "scmp_007"
Expand Down
50 changes: 50 additions & 0 deletions tests/integration_tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,56 @@ async def test_host_missing_fields(lobby_server):
assert msg["featured_mod"] == "faf"


@fast_forward(10)
async def test_host_game_name_only_spaces(lobby_server):
player_id, session, proto = await connect_and_sign_in(
("test", "test_password"),
lobby_server
)

await read_until_command(proto, "game_info")

await proto.send_message({
"command": "game_host",
"mod": "",
"visibility": "public",
"title": " ",
})

msg = await read_until_command(proto, "notice", timeout=10)

assert msg == {
"command": "notice",
"style": "error",
"text": "Title must not be empty.",
}


@fast_forward(10)
async def test_host_game_name_non_ascii(lobby_server):
player_id, session, proto = await connect_and_sign_in(
("test", "test_password"),
lobby_server
)

await read_until_command(proto, "game_info")

await proto.send_message({
"command": "game_host",
"mod": "",
"visibility": "public",
"title": "ÇÒÖL GÃMÊ"
})

msg = await read_until_command(proto, "notice", timeout=10)

assert msg == {
"command": "notice",
"style": "error",
"text": "Title must contain only ascii characters."
}


async def test_play_game_while_queueing(lobby_server):
player_id, session, proto = await connect_and_sign_in(
("test", "test_password"),
Expand Down
6 changes: 6 additions & 0 deletions tests/unit_tests/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,12 @@ async def test_name_sanitization(game, players):
with pytest.raises(ValueError):
game.name = "Hello ⏴⏵⏶⏷⏸⏹⏺⏻♿"

with pytest.raises(ValueError):
game.name = " \n\n\t"

with pytest.raises(ValueError):
game.name = ""

game.name = "A" * 256
assert game.name == "A" * 128

Expand Down

0 comments on commit b4bb165

Please sign in to comment.