From 542eb721fcc7603015cf846413cc75c42f6f8bdd Mon Sep 17 00:00:00 2001 From: Askaholic Date: Sat, 29 Aug 2020 19:17:14 +0000 Subject: [PATCH] Use UTC instead of local timezone (#650) --- server/lobbyconnection.py | 12 ++++++----- tests/unit_tests/test_lobbyconnection.py | 26 ++++++++++++++++-------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/server/lobbyconnection.py b/server/lobbyconnection.py index 51d5faf60..493a93b06 100644 --- a/server/lobbyconnection.py +++ b/server/lobbyconnection.py @@ -68,14 +68,16 @@ def message(self): f"{self.ban_reason}") def _ban_duration_text(self): - ban_duration = self.ban_expiry - datetime.now() + ban_duration = self.ban_expiry - datetime.utcnow() if ban_duration.days > 365 * 100: return "forever" - humanized_ban_duration = humanize.precisedelta(ban_duration, minimum_unit="hours") + humanized_ban_duration = humanize.precisedelta( + ban_duration, + minimum_unit="hours" + ) return f"for {humanized_ban_duration}" - class AuthenticationError(Exception): def __init__(self, message, *args, **kwargs): super().__init__(*args, **kwargs) @@ -413,7 +415,7 @@ async def check_user_login(self, conn, username, password): metrics.user_logins.labels("failure").inc() raise AuthenticationError(auth_error_message) - now = datetime.now() + now = datetime.utcnow() if ban_reason is not None and now < ban_expiry: self._logger.debug('Rejected login from banned user: %s, %s, %s', player_id, username, self.session) @@ -1093,7 +1095,7 @@ async def nop(*args, **kwargs): async def abort_connection_if_banned(self): async with self._db.acquire() as conn: - now = datetime.now() + now = datetime.utcnow() result = await conn.execute( select([lobby_ban.c.reason, lobby_ban.c.expires_at]) .where(lobby_ban.c.idUser == self.player.id) diff --git a/tests/unit_tests/test_lobbyconnection.py b/tests/unit_tests/test_lobbyconnection.py index 8a79e1080..3a502a8e4 100644 --- a/tests/unit_tests/test_lobbyconnection.py +++ b/tests/unit_tests/test_lobbyconnection.py @@ -1,10 +1,10 @@ +import re from hashlib import sha256 from unittest import mock from unittest.mock import Mock import asynctest import pytest -import re from aiohttp import web from asynctest import CoroutineMock from sqlalchemy import and_, select @@ -956,21 +956,31 @@ async def test_abort_connection_if_banned( lobbyconnection: LobbyConnection, mock_nts_client ): - lobbyconnection.player.id = 1 # test user that has never been banned + # test user that has never been banned + lobbyconnection.player.id = 1 await lobbyconnection.abort_connection_if_banned() - lobbyconnection.player.id = 201 # test user whose ban has been revoked + # test user whose ban has been revoked + lobbyconnection.player.id = 201 await lobbyconnection.abort_connection_if_banned() - lobbyconnection.player.id = 202 # test user whose ban has expired + # test user whose ban has expired + lobbyconnection.player.id = 202 await lobbyconnection.abort_connection_if_banned() - lobbyconnection.player.id = 203 # test user who is permabanned + # test user who is permabanned + lobbyconnection.player.id = 203 with pytest.raises(BanError) as banned_error: await lobbyconnection.abort_connection_if_banned() - assert banned_error.value.message() == "You are banned from FAF forever.
Reason :
Test permanent ban" + assert banned_error.value.message() == \ + "You are banned from FAF forever.
Reason :
Test permanent ban" - lobbyconnection.player.id = 204 # test user who is banned for another 46 hours + # test user who is banned for another 46 hours + lobbyconnection.player.id = 204 with pytest.raises(BanError) as banned_error: await lobbyconnection.abort_connection_if_banned() - assert re.match(r"You are banned from FAF for 1 day and 2[0-3]\.[0-9]+ hours.
Reason :
Test ongoing ban with 46 hours left", banned_error.value.message()) + assert re.match( + r"You are banned from FAF for 1 day and 2[12]\.[0-9]+ hours.
" + "Reason :
Test ongoing ban with 46 hours left", + banned_error.value.message() + )