Choose a user with booking availability among many

This commit is contained in:
Stanislas Jouffroy 2024-03-05 00:24:28 +01:00
parent a8322d6be0
commit 559c3b6d69
18 changed files with 1810 additions and 147 deletions

1363
tests/data/mes_resas.html Normal file

File diff suppressed because one or more lines are too long

View file

@ -1,4 +1,5 @@
import json
from pathlib import Path
import pendulum
import pytest
@ -42,6 +43,9 @@ booking_payload = (
.build()
)
html_file = Path(__file__).parent / "data" / "mes_resas.html"
_mes_resas_html = html_file.read_text(encoding="utf-8")
@pytest.fixture
def a_user() -> User:
@ -71,3 +75,8 @@ def a_booking_failure_response() -> str:
@pytest.fixture
def a_booking_payload() -> str:
return booking_payload
@pytest.fixture
def mes_resas_html() -> str:
return _mes_resas_html

View file

@ -13,7 +13,25 @@ from tests.fixtures import (
)
tpc_url = "https://toulousepadelclub.gestion-sports.com"
o = "https://toulousepadelclub.gestion-sports.fr"
TPC_COURTS = [
None,
596,
597,
598,
599,
600,
601,
602,
603,
604,
605,
606,
607,
608,
609,
610,
611,
]
@pytest.mark.asyncio
@ -82,27 +100,7 @@ async def test_booking_url_should_be_reachable(
court_booked = await gs_connector.book(a_booking_filter, a_club)
# At 18:00 no chance to get a booking, any day of the week
assert court_booked in [
None,
597,
598,
599,
600,
601,
602,
603,
604,
605,
606,
607,
608,
609,
610,
611,
612,
613,
614,
]
assert court_booked in TPC_COURTS
@pytest.mark.asyncio
@ -147,7 +145,9 @@ def test_response_status_should_be_ok(a_booking_success_response: str) -> None:
:param a_booking_success_response: the success response mock
"""
is_booked = GestionSportsConnector.is_response_status_ok(a_booking_success_response)
is_booked = GestionSportsConnector.is_booking_response_status_ok(
a_booking_success_response
)
assert is_booked
@ -158,5 +158,26 @@ def test_response_status_should_be_not_ok(a_booking_failure_response: str) -> No
:param a_booking_failure_response: the failure response mock
"""
is_booked = GestionSportsConnector.is_response_status_ok(a_booking_failure_response)
is_booked = GestionSportsConnector.is_booking_response_status_ok(
a_booking_failure_response
)
assert not is_booked
@pytest.mark.asyncio
async def test_get_user_ongoing_bookings(a_user: User, a_club: Club) -> None:
"""
Test that the user has 2 ongoing bookings
:param a_user:
:param a_club:
:return:
"""
async with ClientSession() as session:
gs_connector = GestionSportsConnector(session, tpc_url)
await gs_connector.land()
await gs_connector.login(a_user, a_club)
bookings = await gs_connector.get_ongoing_bookings()
assert len(bookings) == 0

View file

@ -0,0 +1,8 @@
from gestion_sports import gestion_sports_html_parser as parser
from tests.fixtures import mes_resas_html
def test_html_parser(mes_resas_html):
hash_value = parser.get_hash_input(mes_resas_html)
assert hash_value == "ef4403f4c44fa91060a92476aae011a2184323ec"

View file

@ -1,6 +1,7 @@
from resa_padel.gestion_sports.payload_builders import (
GestionSportsBookingPayloadBuilder,
GestionSportsLoginPayloadBuilder,
GestionSportsUsersBookingsPayloadBuilder,
)
from tests.fixtures import a_booking_filter, a_club, a_user
@ -48,3 +49,13 @@ def test_booking_payload_should_be_built(a_booking_filter):
)
assert booking_payload == expected_payload
def test_users_bookings_payload_should_be_built():
builder = GestionSportsUsersBookingsPayloadBuilder()
builder.hash("super_hash")
expected_payload = "ajax=loadResa&hash=super_hash"
actual_payload = builder.build()
assert actual_payload == expected_payload

View file

@ -8,10 +8,15 @@ 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
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"
@ -31,11 +36,15 @@ datetime_to_book = (
"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
mock_now,
a_booking_success_response: str,
a_booking_failure_response: str,
mes_resas_html: str,
):
"""
Test the main function to book a court
@ -61,6 +70,7 @@ def test_main(
fixtures.url,
a_booking_failure_response,
a_booking_success_response,
mes_resas_html,
)
court_booked = booking.main()

View file

@ -3,6 +3,14 @@ from urllib.parse import urljoin
from models import BookingFilter, Club
from pendulum import DateTime
from tests.fixtures import (
a_booking_failure_response,
a_booking_filter,
a_booking_success_response,
a_club,
mes_resas_html,
)
def mock_successful_connection(aio_mock, url):
"""
@ -66,8 +74,20 @@ def retrieve_booking_datetime(
)
def mock_get_users_booking(aio_mock, url: str, booking_response: str):
return aio_mock.get(url, body=booking_response)
def mock_post_users_booking(aio_mock, url: str):
return aio_mock.post(url, payload=[])
def mock_rest_api_from_connection_to_booking(
aio_mock, url: str, a_booking_failure_response: str, a_booking_success_response: str
aio_mock,
url: str,
a_booking_failure_response: str,
a_booking_success_response: str,
mes_resas_html: str,
):
"""
Mock a REST API from a club.
@ -78,13 +98,20 @@ def mock_rest_api_from_connection_to_booking(
:param url: the API root URL
:param a_booking_success_response: the success json response
:param a_booking_failure_response: the failure json response
:param mes_resas_html: the html response for getting the bookings
:return:
"""
connexion_url = urljoin(url, "/connexion.php?")
mock_successful_connection(aio_mock, connexion_url)
mock_successful_connection(aio_mock, connexion_url)
login_url = urljoin(url, "/connexion.php?")
mock_successful_login(aio_mock, login_url)
mock_successful_login(aio_mock, login_url)
users_bookings_url = urljoin(url, "/membre/mesresas.html")
mock_get_users_booking(aio_mock, users_bookings_url, mes_resas_html)
mock_post_users_booking(aio_mock, users_bookings_url)
booking_url = urljoin(url, "/membre/reservation.html?")
mock_booking(aio_mock, booking_url, a_booking_failure_response)