From d0a0072b6d4e63000c91122902c0708e2f70b312 Mon Sep 17 00:00:00 2001 From: stanislas Date: Tue, 20 Feb 2024 18:47:11 +0100 Subject: [PATCH] added available users in config --- resa_padel/config.py | 19 ++++++++++++ tests/test_config.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tests/test_config.py diff --git a/resa_padel/config.py b/resa_padel/config.py index ad15c18..749ea5e 100644 --- a/resa_padel/config.py +++ b/resa_padel/config.py @@ -64,6 +64,25 @@ def get_user() -> User: return User(login=login, password=password) +def get_available_users() -> list[User]: + """ + Read the environment variables to get all the available users in order + to increase the chance of having a user with a free slot for a booking + + :return: the list of all users that can book a court + """ + available_users_credentials = os.environ.get("AVAILABLE_USERS_CREDENTIALS") + available_users = [ + credential for credential in available_users_credentials.split(",") + ] + users = [] + for user in available_users: + login, password = user.split(":") + users.append(User(login=login, password=password)) + + return users + + def get_post_headers(platform_id: str) -> dict: """ Get the headers for the POST endpoint related to a specific booking platform diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..0c1e25b --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,74 @@ +import os +from unittest.mock import patch + +import config +from pendulum import DateTime, Time, Timezone + + +@patch.dict( + os.environ, + { + "CLUB_URL": "club.url", + "COURT_IDS": "7,8,10", + "CLUB_ID": "666", + "BOOKING_OPEN_DAYS_BEFORE": "5", + "BOOKING_OPENING_TIME": "18:37", + }, + clear=True, +) +def test_get_club(): + club = config.get_club() + assert club.url == "club.url" + assert club.courts_ids == [7, 8, 10] + assert club.id == "666" + assert club.booking_open_days_before == 5 + assert club.booking_opening_time == Time(hour=18, minute=37) + + +@patch.dict( + os.environ, + { + "SPORT_ID": "666", + "DATE_TIME": "2024-02-03T22:38:45Z", + }, + clear=True, +) +def test_get_booking_filter(): + booking_filter = config.get_booking_filter() + assert booking_filter.sport_id == 666 + assert booking_filter.date == DateTime( + year=2024, + month=2, + day=3, + hour=23, + minute=38, + second=45, + tzinfo=Timezone("Europe/Paris"), + ) + + +@patch.dict( + os.environ, + { + "LOGIN": "login@user.tld", + "PASSWORD": "gloups", + }, + clear=True, +) +def test_get_available_user(): + user = config.get_user() + assert user.login == "login@user.tld" + assert user.password == "gloups" + + +@patch.dict( + os.environ, + {"AVAILABLE_USERS_CREDENTIALS": "login@user.tld:gloups,other@user.tld:patatras"}, + clear=True, +) +def test_user(): + users = config.get_available_users() + assert users[0].login == "login@user.tld" + assert users[0].password == "gloups" + assert users[1].login == "other@user.tld" + assert users[1].password == "patatras"