Skip to content

Commit

Permalink
Enforces secure chat improvements (#678)
Browse files Browse the repository at this point in the history
* Set `enforces_secure_chat` to `None` if not provided

* Improve tests for `enforces_secure_chat`

We should use consistent style across tests.

* Add docstring

* Remove trailing whitespaces
  • Loading branch information
PerchunPak authored Nov 29, 2023
1 parent 1a2badd commit 8b74acb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
12 changes: 10 additions & 2 deletions mcstatus/status_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ class JavaStatusResponse(BaseStatusResponse):
"""
players: JavaStatusPlayers
version: JavaStatusVersion
enforces_secure_chat: bool
enforces_secure_chat: bool | None
"""Whether the server enforces secure chat (every message is signed up with a key).
.. seealso::
`Signed Chat explanation <https://gist.github.com/kennytv/ed783dd244ca0321bbd882c347892874>`_,
`22w17a changelog, where this was added <https://www.minecraft.net/nl-nl/article/minecraft-snapshot-22w17a>`_.
.. versionadded:: 11.1.0
"""
icon: str | None
"""The icon of the server. In `Base64 <https://en.wikipedia.org/wiki/Base64>`_ encoded PNG image format.
Expand All @@ -133,7 +141,7 @@ def build(cls, raw: RawJavaResponse, latency: float = 0) -> Self:
players=JavaStatusPlayers.build(raw["players"]),
version=JavaStatusVersion.build(raw["version"]),
motd=Motd.parse(raw["description"], bedrock=False),
enforces_secure_chat=raw.get("enforcesSecureChat", False),
enforces_secure_chat=raw.get("enforcesSecureChat"),
icon=raw.get("favicon"),
latency=latency,
)
Expand Down
11 changes: 4 additions & 7 deletions tests/status_response/test_java.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class TestJavaStatusResponse(BaseStatusResponseTest):
"players": {"max": 20, "online": 0},
"version": {"name": "1.8-pre1", "protocol": 44},
"description": "A Minecraft Server",
"enforcesSecureChat": True,
"favicon": "data:image/png;base64,foo",
}

Expand All @@ -19,26 +20,22 @@ class TestJavaStatusResponse(BaseStatusResponseTest):
("version", JavaStatusVersion("1.8-pre1", 44)),
("motd", Motd.parse("A Minecraft Server", bedrock=False)),
("latency", 0),
("enforces_secure_chat", True),
("icon", "data:image/png;base64,foo"),
("raw", RAW),
]
OPTIONAL_FIELDS = [("favicon", "icon")], {
OPTIONAL_FIELDS = [("favicon", "icon"), ("enforcesSecureChat", "enforces_secure_chat")], {
"players": {"max": 20, "online": 0},
"version": {"name": "1.8-pre1", "protocol": 44},
"description": "A Minecraft Server",
"enforcesSecureChat": True,
"favicon": "data:image/png;base64,foo",
}

@pytest.fixture(scope="class")
def build(self):
return JavaStatusResponse.build(self.RAW) # type: ignore # dict[str, Unknown] cannot be assigned to TypedDict

@pytest.mark.parametrize("value", (True, False, object()))
def test_enforces_secure_chat(self, value):
raw = self.RAW.copy()
raw["enforcesSecureChat"] = value
assert JavaStatusResponse.build(raw, 0).enforces_secure_chat is value # type: ignore # dict[str, Unknown] cannot be assigned to TypedDict


@BaseStatusResponseTest.construct
class TestJavaStatusPlayers(BaseStatusResponseTest):
Expand Down

0 comments on commit 8b74acb

Please sign in to comment.