From cecc26ee531450d37954a503acd60fb046a918e3 Mon Sep 17 00:00:00 2001 From: baterflyrity Date: Thu, 8 Feb 2024 17:07:23 +0300 Subject: [PATCH] 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",