Skip to content

Commit

Permalink
Migrate from real-time login tests to fixed_time(...) context mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
baterflyrity committed Feb 8, 2024
1 parent 3fca2e9 commit cecc26e
Showing 1 changed file with 130 additions and 135 deletions.
265 changes: 130 additions & 135 deletions tests/integration_tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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():

Check failure on line 99 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid[qstream] AttributeError: module 'datetime' has no attribute 'UTC'

Check failure on line 99 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid[json] AttributeError: module 'datetime' has no attribute 'UTC'
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',

Check warning on line 124 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / flake8

Single quotes found but double quotes preferred
"id": 3,
"login": "Rhiza"
}
msg = await proto.read_message()
assert msg == {
"command": "player_info",
Expand All @@ -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():

Check failure on line 146 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid_admin[qstream] AttributeError: module 'datetime' has no attribute 'UTC'

Check failure on line 146 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid_admin[json] AttributeError: module 'datetime' has no attribute 'UTC'
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',

Check warning on line 171 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / flake8

Single quotes found but double quotes preferred
"id": 1,
"login": "test"
}
msg = await proto.read_message()
assert msg == {
"command": "player_info",
Expand All @@ -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():

Check failure on line 193 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid_moderator[qstream] AttributeError: module 'datetime' has no attribute 'UTC'

Check failure on line 193 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_valid_moderator[json] AttributeError: module 'datetime' has no attribute 'UTC'
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',

Check warning on line 217 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / flake8

Single quotes found but double quotes preferred
"id": 20,
"login": "moderator"
}
msg = await proto.read_message()
assert msg == {
"command": "player_info",
Expand Down Expand Up @@ -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:

Check failure on line 274 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_token_valid[qstream] AttributeError: __enter__

Check failure on line 274 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / unit-test

test_server_login_token_valid[json] AttributeError: __enter__
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',

Check warning on line 317 in tests/integration_tests/test_login.py

View workflow job for this annotation

GitHub Actions / flake8

Single quotes found but double quotes preferred
"id": 3,
"login": "Rhiza"
}
msg = await proto.read_message()
assert msg == {
"command": "player_info",
Expand Down

0 comments on commit cecc26e

Please sign in to comment.