Skip to content

Commit

Permalink
Perform client time sync on login (#1004)
Browse files Browse the repository at this point in the history
* Perform client time sync on login

Add current_time to the command welcome sent to the client on login.

Closes #889

* 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.

* Fix E261 at least two spaces before inline comment

Fix E261 at least two spaces before inline comment

* Fix line comment

I dont know wtf with that linter so just remove comments at all...

* Migrate from datetime to server.timing

Co-authored-by: Askaholic <[email protected]>

* Migrate to server.timing

* Add fixed_time(...) context manager for mocking server.timing.

* Migrate from real-time login tests to fixed_time(...) context mock.

* Fix flake8 alerts.

* FDowngrade datetime to python 3.10.

* Fixed typo in test_server_login_token_valid.

* Changed fixed_time from context manager to callable fixture. See docstring.

* Remove redundant imports.

* Fix typos.

* Fix typos.

* Revert "Fix typos."

This reverts commit 3202247.

* Fix typos.

---------

Co-authored-by: Askaholic <[email protected]>
  • Loading branch information
baterflyrity and Askaholic authored Feb 24, 2024
1 parent 98271c4 commit 971ea3e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -693,6 +694,7 @@ async def on_player_login(
await self.send({
"command": "welcome",
"me": self.player.to_dict(),
"current_time": datetime_now().isoformat(),

# For backwards compatibility for old clients. For now.
"id": self.player.id,
Expand Down
32 changes: 32 additions & 0 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import datetime
import hashlib
import json
import logging
Expand Down Expand Up @@ -240,6 +241,37 @@ def lobby_contexts_proxy(lobby_setup_proxy):
return contexts


@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.:
def test_time(fixed_time):
assert server.lobbyconnection.datetime_now().timestamp == 0.
fixed_time(1)
assert server.lobbyconnection.datetime_now().timestamp == 1.
"""

def fix_time(iso_utc_time: str | float | int | datetime.datetime = 0):
"""
Fix server.timing value.
: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,
# it is used in almost every tests, so renaming it is a large task.
@pytest.fixture(params=("qstream", "json"))
Expand Down
12 changes: 8 additions & 4 deletions tests/integration_tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ 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)
await perform_login(proto, ("Rhiza", "puff_the_magic_dragon"))
msg = await proto.read_message()
Expand All @@ -118,6 +118,7 @@ async def test_server_login_valid(lobby_server):
assert msg == {
"command": "welcome",
"me": me,
"current_time": "1970-01-01T00:00:00+00:00",
"id": 3,
"login": "Rhiza"
}
Expand All @@ -137,7 +138,7 @@ 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)
await perform_login(proto, ("test", "test_password"))
msg = await proto.read_message()
Expand All @@ -163,6 +164,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",
"id": 1,
"login": "test"
}
Expand All @@ -182,7 +184,7 @@ 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)
await perform_login(proto, ("moderator", "moderator"))
msg = await proto.read_message()
Expand All @@ -207,6 +209,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",
"id": 20,
"login": "moderator"
}
Expand Down Expand Up @@ -261,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",
Expand Down Expand Up @@ -305,6 +308,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",
"id": 3,
"login": "Rhiza"
}
Expand Down

0 comments on commit 971ea3e

Please sign in to comment.