Skip to content

Commit

Permalink
Use UTC instead of local timezone (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic authored Aug 29, 2020
1 parent eb36ea7 commit 542eb72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
12 changes: 7 additions & 5 deletions server/lobbyconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 18 additions & 8 deletions tests/unit_tests/test_lobbyconnection.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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. <br>Reason : <br>Test permanent ban"
assert banned_error.value.message() == \
"You are banned from FAF forever. <br>Reason : <br>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. <br>Reason : <br>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. <br>"
"Reason : <br>Test ongoing ban with 46 hours left",
banned_error.value.message()
)

0 comments on commit 542eb72

Please sign in to comment.