From 60d684a8ad3441c89f947a7aad4efffe291f4de6 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 3 Feb 2024 23:06:55 +0300 Subject: [PATCH 01/17] Perform client time sync on login Add current_time to the command welcome sent to the client on login. Closes #889 --- server/lobbyconnection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index bf028d8b8..2fc294dfc 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -693,6 +693,7 @@ async def on_player_login( await self.send({ "command": "welcome", "me": self.player.to_dict(), + "current_time": datetime.utcnow().timestamp(), # For backwards compatibility for old clients. For now. "id": self.player.id, From 04c1bbc10eb5b9407c89d20736de88787933ead3 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 3 Feb 2024 23:45:43 +0300 Subject: [PATCH 02/17] Update test_login.py for client login sync Update tests to suit #60d684a8ad3441c89f947a7aad4efffe291f4de6: Add current_time to the command welcome sent to the client on login. --- tests/integration_tests/test_login.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 179d71214..142020a0d 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -1,3 +1,4 @@ +from datetime import datetime from time import time import jwt @@ -10,6 +11,8 @@ read_until_command ) +TIMEOUT = 10 # login timeout in seconds + async def test_server_login_invalid(lobby_server): proto = await connect_client(lobby_server) @@ -118,9 +121,12 @@ async def test_server_login_valid(lobby_server): assert msg == { "command": "welcome", "me": me, + "current_time": msg.get("current_time", None), "id": 3, "login": "Rhiza" } + assert isinstance(msg.get("current_time", None), float) + assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT msg = await proto.read_message() assert msg == { "command": "player_info", @@ -163,9 +169,12 @@ async def test_server_login_valid_admin(lobby_server): assert msg == { "command": "welcome", "me": me, + "current_time": msg.get("current_time", None), "id": 1, "login": "test" } + assert isinstance(msg.get("current_time", None), float) + assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT msg = await proto.read_message() assert msg == { "command": "player_info", @@ -207,9 +216,12 @@ async def test_server_login_valid_moderator(lobby_server): assert msg == { "command": "welcome", "me": me, + "current_time": msg.get("current_time", None), "id": 20, "login": "moderator" } + assert isinstance(msg.get("current_time", None), float) + assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT msg = await proto.read_message() assert msg == { "command": "player_info", @@ -305,9 +317,12 @@ async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): assert msg == { "command": "welcome", "me": me, + "current_time": msg.get("current_time", None), "id": 3, "login": "Rhiza" } + assert isinstance(msg.get("current_time", None), float) + assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT msg = await proto.read_message() assert msg == { "command": "player_info", From fb9378e4c5b16eff5cf7d10bef0b75c19cc26250 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 3 Feb 2024 23:47:27 +0300 Subject: [PATCH 03/17] Fix E261 at least two spaces before inline comment Fix E261 at least two spaces before inline comment --- tests/integration_tests/test_login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 142020a0d..79aca38fe 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -11,7 +11,7 @@ read_until_command ) -TIMEOUT = 10 # login timeout in seconds +TIMEOUT = 10 # login timeout in seconds async def test_server_login_invalid(lobby_server): From eea6c98ae1b420046a25bb78d8a424c5e083af5a Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 3 Feb 2024 23:48:46 +0300 Subject: [PATCH 04/17] Fix line comment I dont know wtf with that linter so just remove comments at all... --- tests/integration_tests/test_login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 79aca38fe..68fdff858 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -11,7 +11,7 @@ read_until_command ) -TIMEOUT = 10 # login timeout in seconds +TIMEOUT = 10 async def test_server_login_invalid(lobby_server): From e8e862384a1f934723a8967b7a1276fbeff636d3 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sun, 4 Feb 2024 22:57:54 +0300 Subject: [PATCH 05/17] Migrate from datetime to server.timing Co-authored-by: Askaholic --- server/lobbyconnection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index 2fc294dfc..852b907fb 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -693,7 +693,7 @@ async def on_player_login( await self.send({ "command": "welcome", "me": self.player.to_dict(), - "current_time": datetime.utcnow().timestamp(), + "current_time": datetime_now().isoformat(), # For backwards compatibility for old clients. For now. "id": self.player.id, From 4a2a14d6d1c54663e46e3af8516a6ed0c4655d7d Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sun, 4 Feb 2024 23:01:03 +0300 Subject: [PATCH 06/17] Migrate to server.timing --- server/lobbyconnection.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index 852b907fb..ad87aeca7 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -18,6 +18,7 @@ import server.metrics as metrics from server.db import FAFDatabase +from server.timing import datetime_now from .config import TRACE, config from .db.models import ( From 3fca2e9173aae3a1d4bb47b0d6beb28b90464274 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:06:27 +0300 Subject: [PATCH 07/17] Add fixed_time(...) context manager for mocking server.timing. --- tests/integration_tests/conftest.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 4008037bb..c409a766b 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -1,10 +1,12 @@ import asyncio +import datetime import hashlib import json import logging import textwrap from collections import defaultdict -from typing import Any, Callable, Optional +from contextlib import contextmanager +from typing import Any, Callable, ContextManager, Optional from unittest import mock import aio_pika @@ -240,6 +242,26 @@ def lobby_contexts_proxy(lobby_setup_proxy): return contexts +@contextmanager +def fixed_time(iso_utc_time: str | float | int | datetime.datetime = 0) -> ContextManager[datetime.datetime]: + """ + Context manager to fix server.timing value. Yields native datetime object of fixed time. + + :param iso_utc_time: UTC time to use. Can be isoformat, timestamp or native object. + """ + if isinstance(iso_utc_time, str): + iso_utc_time = datetime.datetime.fromisoformat(iso_utc_time) + elif isinstance(iso_utc_time, (float, int)): + iso_utc_time = datetime.datetime.fromtimestamp(iso_utc_time, datetime.UTC) + + def mock_datetime_now() -> datetime: + return iso_utc_time + + with pytest.MonkeyPatch.context() as mp: + mp.setattr('server.lobbyconnection.datetime_now', mock_datetime_now) + yield iso_utc_time + + # TODO: This fixture is poorly named since it returns a ServerContext, however, # it is used in almost every tests, so renaming it is a large task. @pytest.fixture(params=("qstream", "json")) From cecc26ee531450d37954a503acd60fb046a918e3 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:07:23 +0300 Subject: [PATCH 08/17] Migrate from real-time login tests to fixed_time(...) context mock. --- tests/integration_tests/test_login.py | 265 +++++++++++++------------- 1 file changed, 130 insertions(+), 135 deletions(-) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 68fdff858..9fe914521 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -7,12 +7,11 @@ from .conftest import ( connect_and_sign_in, connect_client, + fixed_time, perform_login, read_until_command ) -TIMEOUT = 10 - async def test_server_login_invalid(lobby_server): proto = await connect_client(lobby_server) @@ -97,36 +96,35 @@ async def test_server_ban_revoked_or_expired(lobby_server, user): async def test_server_login_valid(lobby_server): proto = await connect_client(lobby_server) - await perform_login(proto, ("Rhiza", "puff_the_magic_dragon")) - msg = await proto.read_message() - me = { - "id": 3, - "login": "Rhiza", - "clan": "123", - "country": "", - "ratings": { - "global": { - "rating": [1650.0, 62.52], - "number_of_games": 2 + with fixed_time(): + await perform_login(proto, ("Rhiza", "puff_the_magic_dragon")) + msg = await proto.read_message() + me = { + "id": 3, + "login": "Rhiza", + "clan": "123", + "country": "", + "ratings": { + "global": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + }, + "ladder_1v1": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + } }, - "ladder_1v1": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - } - }, - "global_rating": [1650.0, 62.52], - "ladder_rating": [1650.0, 62.52], - "number_of_games": 2 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": msg.get("current_time", None), - "id": 3, - "login": "Rhiza" - } - assert isinstance(msg.get("current_time", None), float) - assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT + "global_rating": [1650.0, 62.52], + "ladder_rating": [1650.0, 62.52], + "number_of_games": 2 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": '1970-01-01T00:00:00+00:00', + "id": 3, + "login": "Rhiza" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -145,36 +143,35 @@ async def test_server_login_valid(lobby_server): async def test_server_login_valid_admin(lobby_server): proto = await connect_client(lobby_server) - await perform_login(proto, ("test", "test_password")) - msg = await proto.read_message() - me = { - "id": 1, - "login": "test", - "clan": "678", - "country": "", - "ratings": { - "global": { - "rating": [2000.0, 125.0], - "number_of_games": 5 + with fixed_time(): + await perform_login(proto, ("test", "test_password")) + msg = await proto.read_message() + me = { + "id": 1, + "login": "test", + "clan": "678", + "country": "", + "ratings": { + "global": { + "rating": [2000.0, 125.0], + "number_of_games": 5 + }, + "ladder_1v1": { + "rating": [2000.0, 125.0], + "number_of_games": 5 + } }, - "ladder_1v1": { - "rating": [2000.0, 125.0], - "number_of_games": 5 - } - }, - "global_rating": [2000.0, 125.0], - "ladder_rating": [2000.0, 125.0], - "number_of_games": 5, - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": msg.get("current_time", None), - "id": 1, - "login": "test" - } - assert isinstance(msg.get("current_time", None), float) - assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT + "global_rating": [2000.0, 125.0], + "ladder_rating": [2000.0, 125.0], + "number_of_games": 5, + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": '1970-01-01T00:00:00+00:00', + "id": 1, + "login": "test" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -193,35 +190,34 @@ async def test_server_login_valid_admin(lobby_server): async def test_server_login_valid_moderator(lobby_server): proto = await connect_client(lobby_server) - await perform_login(proto, ("moderator", "moderator")) - msg = await proto.read_message() - me = { - "id": 20, - "login": "moderator", - "country": "", - "ratings": { - "global": { - "rating": [1500, 500], - "number_of_games": 0 + with fixed_time(): + await perform_login(proto, ("moderator", "moderator")) + msg = await proto.read_message() + me = { + "id": 20, + "login": "moderator", + "country": "", + "ratings": { + "global": { + "rating": [1500, 500], + "number_of_games": 0 + }, + "ladder_1v1": { + "rating": [1500, 500], + "number_of_games": 0 + } }, - "ladder_1v1": { - "rating": [1500, 500], - "number_of_games": 0 - } - }, - "global_rating": [1500, 500], - "ladder_rating": [1500, 500], - "number_of_games": 0 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": msg.get("current_time", None), - "id": 20, - "login": "moderator" - } - assert isinstance(msg.get("current_time", None), float) - assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT + "global_rating": [1500, 500], + "ladder_rating": [1500, 500], + "number_of_games": 0 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": '1970-01-01T00:00:00+00:00', + "id": 20, + "login": "moderator" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -275,54 +271,53 @@ async def test_server_login_double(lobby_server): async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): proto = await connect_client(lobby_server) - await proto.send_message({ - "command": "auth", - "version": "1.0.0-dev", - "user_agent": "faf-client", - "token": jwt.encode({ - "sub": 3, - "user_name": "Rhiza", - "scp": ["lobby"], - "exp": int(time() + 1000), - "authorities": [], - "non_locked": True, - "jti": "", - "client_id": "" - }, jwk_priv_key, algorithm="RS256", headers={"kid": jwk_kid}), - "unique_id": "some_id" - }) - - msg = await proto.read_message() - assert msg["command"] == "irc_password" - msg = await proto.read_message() - me = { - "id": 3, - "login": "Rhiza", - "clan": "123", - "country": "", - "ratings": { - "global": { - "rating": [1650.0, 62.52], - "number_of_games": 2 + with fixed_time: + await proto.send_message({ + "command": "auth", + "version": "1.0.0-dev", + "user_agent": "faf-client", + "token": jwt.encode({ + "sub": 3, + "user_name": "Rhiza", + "scp": ["lobby"], + "exp": int(time() + 1000), + "authorities": [], + "non_locked": True, + "jti": "", + "client_id": "" + }, jwk_priv_key, algorithm="RS256", headers={"kid": jwk_kid}), + "unique_id": "some_id" + }) + + msg = await proto.read_message() + assert msg["command"] == "irc_password" + msg = await proto.read_message() + me = { + "id": 3, + "login": "Rhiza", + "clan": "123", + "country": "", + "ratings": { + "global": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + }, + "ladder_1v1": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + } }, - "ladder_1v1": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - } - }, - "global_rating": [1650.0, 62.52], - "ladder_rating": [1650.0, 62.52], - "number_of_games": 2 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": msg.get("current_time", None), - "id": 3, - "login": "Rhiza" - } - assert isinstance(msg.get("current_time", None), float) - assert abs(msg.get("current_time", -1) - datetime.utcnow().timestamp()) <= TIMEOUT + "global_rating": [1650.0, 62.52], + "ladder_rating": [1650.0, 62.52], + "number_of_games": 2 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": '1970-01-01T00:00:00+00:00', + "id": 3, + "login": "Rhiza" + } msg = await proto.read_message() assert msg == { "command": "player_info", From f14e12c147a2a70c45597a160407ed35d5638380 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:09:57 +0300 Subject: [PATCH 09/17] Fix flake8 alerts. --- tests/integration_tests/conftest.py | 2 +- tests/integration_tests/test_login.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index c409a766b..9105153fc 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -258,7 +258,7 @@ def mock_datetime_now() -> datetime: return iso_utc_time with pytest.MonkeyPatch.context() as mp: - mp.setattr('server.lobbyconnection.datetime_now', mock_datetime_now) + mp.setattr("server.lobbyconnection.datetime_now", mock_datetime_now) yield iso_utc_time diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 9fe914521..4dde64923 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -1,4 +1,3 @@ -from datetime import datetime from time import time import jwt @@ -121,7 +120,7 @@ async def test_server_login_valid(lobby_server): assert msg == { "command": "welcome", "me": me, - "current_time": '1970-01-01T00:00:00+00:00', + "current_time": "1970-01-01T00:00:00+00:00", "id": 3, "login": "Rhiza" } @@ -168,7 +167,7 @@ async def test_server_login_valid_admin(lobby_server): assert msg == { "command": "welcome", "me": me, - "current_time": '1970-01-01T00:00:00+00:00', + "current_time": "1970-01-01T00:00:00+00:00", "id": 1, "login": "test" } @@ -214,7 +213,7 @@ async def test_server_login_valid_moderator(lobby_server): assert msg == { "command": "welcome", "me": me, - "current_time": '1970-01-01T00:00:00+00:00', + "current_time": "1970-01-01T00:00:00+00:00", "id": 20, "login": "moderator" } @@ -314,7 +313,7 @@ async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): assert msg == { "command": "welcome", "me": me, - "current_time": '1970-01-01T00:00:00+00:00', + "current_time": "1970-01-01T00:00:00+00:00", "id": 3, "login": "Rhiza" } From 7c5c8746afd5227ea95da7a7b45d979496b7cd97 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:28:42 +0300 Subject: [PATCH 10/17] FDowngrade datetime to python 3.10. --- tests/integration_tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 9105153fc..4d4b58e08 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -252,7 +252,7 @@ def fixed_time(iso_utc_time: str | float | int | datetime.datetime = 0) -> Conte if isinstance(iso_utc_time, str): iso_utc_time = datetime.datetime.fromisoformat(iso_utc_time) elif isinstance(iso_utc_time, (float, int)): - iso_utc_time = datetime.datetime.fromtimestamp(iso_utc_time, datetime.UTC) + iso_utc_time = datetime.datetime.fromtimestamp(iso_utc_time, datetime.timezone.utc) def mock_datetime_now() -> datetime: return iso_utc_time From d0663eb584962175c503d6775d57736d5fe83b77 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:38:01 +0300 Subject: [PATCH 11/17] Fixed typo in test_server_login_token_valid. --- tests/integration_tests/test_login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 4dde64923..f5db9cafd 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -270,7 +270,7 @@ async def test_server_login_double(lobby_server): async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): proto = await connect_client(lobby_server) - with fixed_time: + with fixed_time(): await proto.send_message({ "command": "auth", "version": "1.0.0-dev", From 6ddd09f00fa70d293b2b61cb7ccf9f3015c3a868 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:03:07 +0300 Subject: [PATCH 12/17] Changed fixed_time from context manager to callable fixture. See docstring. --- tests/integration_tests/conftest.py | 37 ++-- tests/integration_tests/test_login.py | 261 +++++++++++++------------- 2 files changed, 152 insertions(+), 146 deletions(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 4d4b58e08..8e6e648ae 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -242,24 +242,35 @@ def lobby_contexts_proxy(lobby_setup_proxy): return contexts -@contextmanager -def fixed_time(iso_utc_time: str | float | int | datetime.datetime = 0) -> ContextManager[datetime.datetime]: +@pytest.fuxture +def fixed_time(monkeypatch): """ - Context manager to fix server.timing value. Yields native datetime object of fixed time. + Fixture to fix server.timing value. By default, fixes all timings at 1970-01-01T00:00:00+00:00. Additionally, returned function can be called unbound times to change timing value, e.g.: - :param iso_utc_time: UTC time to use. Can be isoformat, timestamp or native object. + def test_time(fixed_time): + assert server.lobbyconnection.datetime_now().timestamp == 0. + fixed_time(1) + assert server.lobbyconnection.datetime_now().timestamp == 1. """ - if isinstance(iso_utc_time, str): - iso_utc_time = datetime.datetime.fromisoformat(iso_utc_time) - elif isinstance(iso_utc_time, (float, int)): - iso_utc_time = datetime.datetime.fromtimestamp(iso_utc_time, datetime.timezone.utc) - def mock_datetime_now() -> datetime: - return iso_utc_time + def fix_time(iso_utc_time: str | float | int | datetime.datetime = 0): + """ + Fix server.timing value. - with pytest.MonkeyPatch.context() as mp: - mp.setattr("server.lobbyconnection.datetime_now", mock_datetime_now) - yield iso_utc_time + :param iso_utc_time: UTC time to use. Can be isoformat, timestamp or native object. + """ + if isinstance(iso_utc_time, str): + iso_utc_time = datetime.datetime.fromisoformat(iso_utc_time) + elif isinstance(iso_utc_time, (float, int)): + iso_utc_time = datetime.datetime.fromtimestamp(iso_utc_time, datetime.timezone.utc) + + def mock_datetime_now() -> datetime: + return iso_utc_time + + monkeypatch.setattr("server.lobbyconnection.datetime_now", mock_datetime_now) + + fix_time() + return fix_time # TODO: This fixture is poorly named since it returns a ServerContext, however, diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index f5db9cafd..48b26378f 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -6,7 +6,6 @@ from .conftest import ( connect_and_sign_in, connect_client, - fixed_time, perform_login, read_until_command ) @@ -93,37 +92,36 @@ async def test_server_ban_revoked_or_expired(lobby_server, user): assert msg["login"] == user -async def test_server_login_valid(lobby_server): +async def test_server_login_valid(lobby_server, fixed_time): proto = await connect_client(lobby_server) - with fixed_time(): - await perform_login(proto, ("Rhiza", "puff_the_magic_dragon")) - msg = await proto.read_message() - me = { - "id": 3, - "login": "Rhiza", - "clan": "123", - "country": "", - "ratings": { - "global": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - }, - "ladder_1v1": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - } + await perform_login(proto, ("Rhiza", "puff_the_magic_dragon")) + msg = await proto.read_message() + me = { + "id": 3, + "login": "Rhiza", + "clan": "123", + "country": "", + "ratings": { + "global": { + "rating": [1650.0, 62.52], + "number_of_games": 2 }, - "global_rating": [1650.0, 62.52], - "ladder_rating": [1650.0, 62.52], - "number_of_games": 2 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": "1970-01-01T00:00:00+00:00", - "id": 3, - "login": "Rhiza" - } + "ladder_1v1": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + } + }, + "global_rating": [1650.0, 62.52], + "ladder_rating": [1650.0, 62.52], + "number_of_games": 2 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": "1970-01-01T00:00:00+00:00", + "id": 3, + "login": "Rhiza" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -140,37 +138,36 @@ async def test_server_login_valid(lobby_server): } -async def test_server_login_valid_admin(lobby_server): +async def test_server_login_valid_admin(lobby_server, fixed_time): proto = await connect_client(lobby_server) - with fixed_time(): - await perform_login(proto, ("test", "test_password")) - msg = await proto.read_message() - me = { - "id": 1, - "login": "test", - "clan": "678", - "country": "", - "ratings": { - "global": { - "rating": [2000.0, 125.0], - "number_of_games": 5 - }, - "ladder_1v1": { - "rating": [2000.0, 125.0], - "number_of_games": 5 - } + await perform_login(proto, ("test", "test_password")) + msg = await proto.read_message() + me = { + "id": 1, + "login": "test", + "clan": "678", + "country": "", + "ratings": { + "global": { + "rating": [2000.0, 125.0], + "number_of_games": 5 }, - "global_rating": [2000.0, 125.0], - "ladder_rating": [2000.0, 125.0], - "number_of_games": 5, - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": "1970-01-01T00:00:00+00:00", - "id": 1, - "login": "test" - } + "ladder_1v1": { + "rating": [2000.0, 125.0], + "number_of_games": 5 + } + }, + "global_rating": [2000.0, 125.0], + "ladder_rating": [2000.0, 125.0], + "number_of_games": 5, + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": "1970-01-01T00:00:00+00:00", + "id": 1, + "login": "test" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -187,36 +184,35 @@ async def test_server_login_valid_admin(lobby_server): } -async def test_server_login_valid_moderator(lobby_server): +async def test_server_login_valid_moderator(lobby_server, fixed_time): proto = await connect_client(lobby_server) - with fixed_time(): - await perform_login(proto, ("moderator", "moderator")) - msg = await proto.read_message() - me = { - "id": 20, - "login": "moderator", - "country": "", - "ratings": { - "global": { - "rating": [1500, 500], - "number_of_games": 0 - }, - "ladder_1v1": { - "rating": [1500, 500], - "number_of_games": 0 - } + await perform_login(proto, ("moderator", "moderator")) + msg = await proto.read_message() + me = { + "id": 20, + "login": "moderator", + "country": "", + "ratings": { + "global": { + "rating": [1500, 500], + "number_of_games": 0 }, - "global_rating": [1500, 500], - "ladder_rating": [1500, 500], - "number_of_games": 0 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": "1970-01-01T00:00:00+00:00", - "id": 20, - "login": "moderator" - } + "ladder_1v1": { + "rating": [1500, 500], + "number_of_games": 0 + } + }, + "global_rating": [1500, 500], + "ladder_rating": [1500, 500], + "number_of_games": 0 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": "1970-01-01T00:00:00+00:00", + "id": 20, + "login": "moderator" + } msg = await proto.read_message() assert msg == { "command": "player_info", @@ -270,53 +266,52 @@ async def test_server_login_double(lobby_server): async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): proto = await connect_client(lobby_server) - with fixed_time(): - await proto.send_message({ - "command": "auth", - "version": "1.0.0-dev", - "user_agent": "faf-client", - "token": jwt.encode({ - "sub": 3, - "user_name": "Rhiza", - "scp": ["lobby"], - "exp": int(time() + 1000), - "authorities": [], - "non_locked": True, - "jti": "", - "client_id": "" - }, jwk_priv_key, algorithm="RS256", headers={"kid": jwk_kid}), - "unique_id": "some_id" - }) - - msg = await proto.read_message() - assert msg["command"] == "irc_password" - msg = await proto.read_message() - me = { - "id": 3, - "login": "Rhiza", - "clan": "123", - "country": "", - "ratings": { - "global": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - }, - "ladder_1v1": { - "rating": [1650.0, 62.52], - "number_of_games": 2 - } + await proto.send_message({ + "command": "auth", + "version": "1.0.0-dev", + "user_agent": "faf-client", + "token": jwt.encode({ + "sub": 3, + "user_name": "Rhiza", + "scp": ["lobby"], + "exp": int(time() + 1000), + "authorities": [], + "non_locked": True, + "jti": "", + "client_id": "" + }, jwk_priv_key, algorithm="RS256", headers={"kid": jwk_kid}), + "unique_id": "some_id" + }) + + msg = await proto.read_message() + assert msg["command"] == "irc_password" + msg = await proto.read_message() + me = { + "id": 3, + "login": "Rhiza", + "clan": "123", + "country": "", + "ratings": { + "global": { + "rating": [1650.0, 62.52], + "number_of_games": 2 }, - "global_rating": [1650.0, 62.52], - "ladder_rating": [1650.0, 62.52], - "number_of_games": 2 - } - assert msg == { - "command": "welcome", - "me": me, - "current_time": "1970-01-01T00:00:00+00:00", - "id": 3, - "login": "Rhiza" - } + "ladder_1v1": { + "rating": [1650.0, 62.52], + "number_of_games": 2 + } + }, + "global_rating": [1650.0, 62.52], + "ladder_rating": [1650.0, 62.52], + "number_of_games": 2 + } + assert msg == { + "command": "welcome", + "me": me, + "current_time": "1970-01-01T00:00:00+00:00", + "id": 3, + "login": "Rhiza" + } msg = await proto.read_message() assert msg == { "command": "player_info", From 6d0bfc03193efadc41c853e050796d2c07b06e23 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:04:38 +0300 Subject: [PATCH 13/17] Remove redundant imports. --- tests/integration_tests/conftest.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 8e6e648ae..90708140e 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -5,8 +5,7 @@ import logging import textwrap from collections import defaultdict -from contextlib import contextmanager -from typing import Any, Callable, ContextManager, Optional +from typing import Any, Callable, Optional from unittest import mock import aio_pika From 28c4ee53ed90618332e42b36f62c24c238b412f4 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:07:11 +0300 Subject: [PATCH 14/17] Fix typos. --- tests/integration_tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 90708140e..60f2c5b89 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -241,7 +241,7 @@ def lobby_contexts_proxy(lobby_setup_proxy): return contexts -@pytest.fuxture +@pytest.fixture def fixed_time(monkeypatch): """ Fixture to fix server.timing value. By default, fixes all timings at 1970-01-01T00:00:00+00:00. Additionally, returned function can be called unbound times to change timing value, e.g.: From 320224779e4140c5d5bc282ee02a7409bc8d3b51 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:08:09 +0300 Subject: [PATCH 15/17] Fix typos. --- tests/integration_tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 60f2c5b89..3b0865870 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -247,9 +247,9 @@ def fixed_time(monkeypatch): Fixture to fix server.timing value. By default, fixes all timings at 1970-01-01T00:00:00+00:00. Additionally, returned function can be called unbound times to change timing value, e.g.: def test_time(fixed_time): - assert server.lobbyconnection.datetime_now().timestamp == 0. + assert server.lobbyconnection.datetime_now().timestamp == 0 fixed_time(1) - assert server.lobbyconnection.datetime_now().timestamp == 1. + assert server.lobbyconnection.datetime_now().timestamp == 1 """ def fix_time(iso_utc_time: str | float | int | datetime.datetime = 0): From f786f605805b5f26600354047f3078c601b1bbf5 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:09:12 +0300 Subject: [PATCH 16/17] Revert "Fix typos." This reverts commit 320224779e4140c5d5bc282ee02a7409bc8d3b51. --- tests/integration_tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py index 3b0865870..60f2c5b89 100644 --- a/tests/integration_tests/conftest.py +++ b/tests/integration_tests/conftest.py @@ -247,9 +247,9 @@ def fixed_time(monkeypatch): Fixture to fix server.timing value. By default, fixes all timings at 1970-01-01T00:00:00+00:00. Additionally, returned function can be called unbound times to change timing value, e.g.: def test_time(fixed_time): - assert server.lobbyconnection.datetime_now().timestamp == 0 + assert server.lobbyconnection.datetime_now().timestamp == 0. fixed_time(1) - assert server.lobbyconnection.datetime_now().timestamp == 1 + assert server.lobbyconnection.datetime_now().timestamp == 1. """ def fix_time(iso_utc_time: str | float | int | datetime.datetime = 0): From ac34ebde67a99589bf591dd60b7ea30fef10e1f9 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Sat, 24 Feb 2024 13:15:38 +0300 Subject: [PATCH 17/17] Fix typos. --- tests/integration_tests/test_login.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_tests/test_login.py b/tests/integration_tests/test_login.py index 48b26378f..d51a9205d 100644 --- a/tests/integration_tests/test_login.py +++ b/tests/integration_tests/test_login.py @@ -264,7 +264,7 @@ async def test_server_login_double(lobby_server): } -async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid): +async def test_server_login_token_valid(lobby_server, jwk_priv_key, jwk_kid, fixed_time): proto = await connect_client(lobby_server) await proto.send_message({ "command": "auth",