ABle to send the booking request to several courts at the same time

This commit is contained in:
Stanislas Jouffroy 2024-02-15 19:55:07 +01:00
parent fcc08f03f1
commit 51af600d28
11 changed files with 288 additions and 99 deletions

View file

@ -1,26 +1,14 @@
import json
import pendulum
import pytest
from aiohttp import ClientSession
from yarl import URL
from resa_padel.gestion_sports.gestion_sports_connector import GestionSportsConnector
from resa_padel.models import User, BookingFilter
from resa_padel.gestion_sports.gestion_sports_connector import \
GestionSportsConnector
from tests.fixtures import (a_booking_failure_response, a_booking_filter,
a_booking_payload, a_booking_success_response,
a_connector, a_user)
gestion_sports_url = "https://toulousepadelclub.gestion-sports.com"
test_user = "padel.testing@jouf.fr"
test_user_id = "232382"
test_password = "ridicule"
test_club_id = "88"
user = User(login=test_user, password=test_password, club_id=test_club_id)
test_court_id = "607"
now = pendulum.now()
date_to_book = now.add(days=6)
datetime_to_book = date_to_book.set(hour=18, minute=0, second=0)
padel_id = 217
court_id = 607
@pytest.mark.asyncio
@ -43,43 +31,86 @@ async def test_should_connect_to_gestion_sports_website():
@pytest.mark.asyncio
async def test_should_login_to_gestion_sports_website():
async def test_should_login_to_gestion_sports_website(a_user):
async with ClientSession() as session:
gs_connector = GestionSportsConnector(session, gestion_sports_url)
await gs_connector.connect()
response = await gs_connector.login(user)
response = await gs_connector.login(a_user)
assert response.status == 200
assert response.request_info.url == URL(gestion_sports_url + "/connexion.php")
assert response.request_info.method == "POST"
cookies = session.cookie_jar.filter_cookies(URL(gestion_sports_url))
assert cookies.get("COOK_ID_CLUB").value == test_club_id
assert cookies.get("COOK_ID_USER").value == test_user_id
assert cookies.get("COOK_ID_CLUB").value is not None
assert cookies.get("COOK_ID_USER").value is not None
assert cookies.get("PHPSESSID") is not None
@pytest.mark.asyncio
async def test_should_book_a_court_from_gestion_sports():
async def test_booking_url_should_be_reachable(a_user, a_booking_filter):
async with ClientSession() as session:
gs_connector = GestionSportsConnector(session, gestion_sports_url)
await gs_connector.connect()
await gs_connector.login(user)
await gs_connector.login(a_user)
booking_filter = BookingFilter(
court_id=court_id, sport_id=padel_id, date=datetime_to_book
)
response = await gs_connector.book(booking_filter)
court_booked = await gs_connector.book(a_booking_filter)
# At 18:00 no chance to get a booking, any day of the week
assert court_booked is None
assert response.status == 200
assert response.request_info.url == URL(
gestion_sports_url + "/membre/reservation.html?"
)
assert response.request_info.method == "POST"
result = await response.text()
formatted_result = result.removeprefix('"').removesuffix('"')
result_json = json.loads(formatted_result)
# booking any day at 18:00 should always be an error if everything goes well ;)
assert result_json["status"] == "error"
@pytest.mark.asyncio
async def test_should_book_a_court_from_gestion_sports(
aioresponses,
a_booking_filter,
a_booking_success_response,
a_booking_failure_response,
):
booking_url = URL(gestion_sports_url + "/membre/reservation.html?")
# first booking request will fail
aioresponses.post(URL(booking_url), status=200, body=a_booking_failure_response)
# first booking request will succeed
aioresponses.post(URL(booking_url), status=200, body=a_booking_success_response)
# first booking request will fail
aioresponses.post(URL(booking_url), status=200, body=a_booking_failure_response)
async with ClientSession() as session:
gs_connector = GestionSportsConnector(session, gestion_sports_url)
court_booked = await gs_connector.book(a_booking_filter)
# the second element of the list is the booked court
assert court_booked == a_booking_filter.court_ids[1]
def test_response_status_should_be_ok(a_booking_success_response):
is_booked = GestionSportsConnector.is_response_status_ok(a_booking_success_response)
assert is_booked
def test_response_status_should_be_not_ok(aioresponses, a_booking_failure_response):
is_booked = GestionSportsConnector.is_response_status_ok(a_booking_failure_response)
assert not is_booked
@pytest.mark.asyncio
async def test_court_should_not_be_booked(
aioresponses, a_connector, a_booking_payload, a_booking_failure_response
):
aioresponses.post(
URL(a_connector.booking_url), status=200, body=a_booking_failure_response
)
is_booked = await a_connector.is_court_booked(a_booking_payload)
assert not is_booked
@pytest.mark.asyncio
async def test_court_should_be_booked(
aioresponses, a_connector, a_booking_payload, a_booking_success_response
):
aioresponses.post(
URL(a_connector.booking_url), status=200, body=a_booking_success_response
)
is_booked = await a_connector.is_court_booked(a_booking_payload)
assert is_booked

View file

@ -1,8 +1,7 @@
import pendulum
from resa_padel.gestion_sports.gestion_sports_payload_builder import (
GestionSportsPayloadBuilder,
)
from resa_padel.gestion_sports.gestion_sports_payload_builder import \
GestionSportsPayloadBuilder
def test_login_payload_should_be_built():