Big refactoring.

- clubs, booking platforms and user are now defined in customization files -> there are less environment variables
- the responsibility of the session moved
- booking cancellation is available
This commit is contained in:
Stanislas Jouffroy 2024-03-17 23:57:50 +01:00 committed by stanislas
parent dbda5a158e
commit 0938fb98b7
27 changed files with 3050 additions and 696 deletions

View file

@ -1,77 +1,70 @@
import asyncio
import os
from unittest.mock import patch
import config
import pendulum
from aioresponses import aioresponses
from models import BookingFilter, Club
from pendulum import Time
from models import BookingFilter, User
from resa_padel import booking
from tests import fixtures, utils
from tests.fixtures import (
a_booking_failure_response,
a_booking_success_response,
mes_resas_html,
@patch.dict(
os.environ,
{"CLUB_ID": "tpc"},
clear=True,
)
def test_real_booking():
club = config.get_club()
user = User(login="padel.testing@jouf.fr", password="ridicule")
booking_filter = BookingFilter(
sport_name="Padel", date=pendulum.parse("2024-03-21T13:30:00+01:00")
)
booked_court, user_that_booked = asyncio.run(
booking.book_court(club, [user], booking_filter)
)
assert booked_court is not None
assert user_that_booked == user
login = "user"
password = "password"
available_credentials = login + ":" + password + ",some_user:some_password"
club_id = "88"
court_id = "11"
paris_tz = "Europe/Paris"
datetime_to_book = (
pendulum.now().add(days=6).set(hour=18, minute=0, second=0, tz=paris_tz)
@patch.dict(
os.environ,
{"CLUB_ID": "tpc"},
clear=True,
)
def test_real_cancellation():
club = config.get_club()
user = User(login="padel.testing@jouf.fr", password="ridicule")
asyncio.run(booking.cancel_booking_id(club, user, 3605033))
@patch("pendulum.now")
@patch.dict(
os.environ,
{
"LOGIN": login,
"PASSWORD": password,
"CLUB_ID": club_id,
"CLUB_URL": fixtures.url,
"COURT_IDS": "7,8,10",
"SPORT_ID": "217",
"DATE_TIME": datetime_to_book.isoformat(),
"AVAILABLE_USERS_CREDENTIALS": available_credentials,
"CLUB_ID": "tpc",
"ACTION": "book",
"SPORT_NAME": "Padel",
"DATE_TIME": "2024-03-21T13:30:00+01:00",
},
clear=True,
)
def test_main(
mock_now,
a_booking_success_response: str,
a_booking_failure_response: str,
mes_resas_html: str,
):
"""
Test the main function to book a court
def test_main_booking():
court, user = booking.main()
assert court is not None
assert user.username == "padel.testing@jouf"
:param mock_now: the pendulum.now() mock
:param a_booking_success_response: the success json response
:param a_booking_failure_response: the failure json response
"""
booking_filter = BookingFilter(sport_id=666, date=datetime_to_book)
club = Club(
id="club",
url="some.url",
courts_ids=[7, 8, 10],
booking_open_days_before=7,
booking_opening_time=Time(hour=0, minute=0),
)
booking_datetime = utils.retrieve_booking_datetime(booking_filter, club)
mock_now.side_effect = [booking_datetime]
with aioresponses() as aio_mock:
utils.mock_rest_api_from_connection_to_booking(
aio_mock,
fixtures.url,
a_booking_failure_response,
a_booking_success_response,
mes_resas_html,
)
court_booked = booking.main()
assert court_booked == 8
@patch.dict(
os.environ,
{
"CLUB_ID": "tpc",
"ACTION": "cancel",
"SPORT_NAME": "Padel",
"DATE_TIME": "2024-03-21T13:30:00+01:00",
"LOGIN": "padel.testing@jouf.fr",
"PASSWORD": "ridicule",
},
clear=True,
)
def test_main_cancellation():
booking.main()