Skip to content

Commit

Permalink
Changed fixed_time from context manager to callable fixture. See docs…
Browse files Browse the repository at this point in the history
…tring.
  • Loading branch information
baterflyrity committed Feb 24, 2024
1 parent d0663eb commit 6ddd09f
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 146 deletions.
37 changes: 24 additions & 13 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
261 changes: 128 additions & 133 deletions tests/integration_tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from .conftest import (
connect_and_sign_in,
connect_client,
fixed_time,
perform_login,
read_until_command
)
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 6ddd09f

Please sign in to comment.