resa-padel/tests/test_booking.py

77 lines
2 KiB
Python

import os
from unittest.mock import patch
import pendulum
from aioresponses import aioresponses
from models import BookingFilter, Club
from pendulum import Time
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,
)
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("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,
},
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
: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