ABle to send the booking request to several courts at the same time
This commit is contained in:
parent
fcc08f03f1
commit
51af600d28
11 changed files with 288 additions and 99 deletions
80
tests/fixtures.py
Normal file
80
tests/fixtures.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
import json
|
||||
|
||||
import pendulum
|
||||
import pytest
|
||||
from aiohttp import ClientSession
|
||||
|
||||
from resa_padel.gestion_sports.gestion_sports_connector import \
|
||||
GestionSportsConnector
|
||||
from resa_padel.gestion_sports.gestion_sports_payload_builder import \
|
||||
GestionSportsPayloadBuilder
|
||||
from resa_padel.models import BookingFilter, User
|
||||
|
||||
user = User(login="padel.testing@jouf.fr", password="ridicule", club_id="123")
|
||||
|
||||
courts = [606, 607, 608]
|
||||
sport_id = 217
|
||||
booking_date = pendulum.now().add(days=6).set(hour=18, minute=0, second=0)
|
||||
booking_filter = BookingFilter(court_ids=courts, sport_id=sport_id, date=booking_date)
|
||||
|
||||
booking_failure_response = json.dumps(
|
||||
{
|
||||
"status": "error",
|
||||
"message": "Désolé mais vous avez 1 réservation en cours au Padel en heures pleines et le réglement"
|
||||
" n'autorise qu'une réservation en heures pleines à la fois au Padel!",
|
||||
}
|
||||
)
|
||||
|
||||
booking_success_response = json.dumps(
|
||||
{
|
||||
"status": "ok",
|
||||
"message": "Merci, votre réservation s'est bien effectuée, vous allez recevoir un email"
|
||||
" avec le récapitulatif de votre réservation, pensez à le conserver.",
|
||||
"id_resa": 3503741,
|
||||
}
|
||||
)
|
||||
|
||||
date_format = "%d/%m/%Y"
|
||||
time_format = "%H:%M"
|
||||
booking_payload = (
|
||||
GestionSportsPayloadBuilder()
|
||||
.date(booking_date.date().strftime(date_format))
|
||||
.time(booking_date.time().strftime(time_format))
|
||||
.sport_id(sport_id)
|
||||
.court_id(courts[0])
|
||||
.build_booking_payload()
|
||||
)
|
||||
|
||||
session = ClientSession()
|
||||
gestion_sports_url = "https://toulousepadelclub.gestion-sports.com"
|
||||
gs_connector = GestionSportsConnector(session, gestion_sports_url)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_user() -> User:
|
||||
return user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_booking_filter() -> BookingFilter:
|
||||
return booking_filter
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_booking_success_response() -> str:
|
||||
return booking_success_response
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_booking_failure_response() -> str:
|
||||
return booking_failure_response
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_booking_payload() -> str:
|
||||
return booking_payload
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def a_connector():
|
||||
return gs_connector
|
|
@ -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
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
import asyncio
|
||||
|
||||
import pendulum
|
||||
from aioresponses import aioresponses
|
||||
|
||||
from resa_padel import booking
|
||||
from resa_padel.models import BookingFilter, User
|
||||
from tests.fixtures import (a_booking_failure_response,
|
||||
a_booking_success_response)
|
||||
|
||||
user = "user"
|
||||
login = "user"
|
||||
password = "password"
|
||||
club_id = "98"
|
||||
court_id = "11"
|
||||
|
@ -14,11 +18,14 @@ court_id = "11"
|
|||
# check that called are passed to the given urls
|
||||
# check made with cookies, but at the current time, cookies from the response are
|
||||
# not set in the session. Why ? I don't know....
|
||||
def test_booking_does_the_rights_calls():
|
||||
def test_booking_does_the_rights_calls(
|
||||
a_booking_success_response, a_booking_failure_response
|
||||
):
|
||||
# mock connection to the booking platform
|
||||
booking_url = "https://some.url"
|
||||
connection_url = booking_url + "/connexion.php"
|
||||
platform_url = "https://some.url"
|
||||
connection_url = platform_url + "/connexion.php"
|
||||
login_url = connection_url
|
||||
booking_url = platform_url + "/membre/reservation.html"
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
|
@ -26,19 +33,34 @@ def test_booking_does_the_rights_calls():
|
|||
aio_mock.get(
|
||||
connection_url,
|
||||
status=200,
|
||||
headers={"Set-Cookie": f"connection_called=True; Domain={booking_url}"},
|
||||
headers={"Set-Cookie": f"connection_called=True; Domain={platform_url}"},
|
||||
)
|
||||
aio_mock.post(
|
||||
login_url,
|
||||
status=200,
|
||||
headers={"Set-Cookie": f"login_called=True; Domain={booking_url}"},
|
||||
headers={"Set-Cookie": f"login_called=True; Domain={platform_url}"},
|
||||
)
|
||||
aio_mock.post(
|
||||
booking_url,
|
||||
status=200,
|
||||
headers={"Set-Cookie": f"booking_called=True; Domain={platform_url}"},
|
||||
body=a_booking_failure_response,
|
||||
)
|
||||
aio_mock.post(
|
||||
booking_url,
|
||||
status=200,
|
||||
headers={"Set-Cookie": f"booking_called=True; Domain={platform_url}"},
|
||||
body=a_booking_success_response,
|
||||
)
|
||||
|
||||
session = loop.run_until_complete(
|
||||
booking.book(booking_url, user, password, club_id, court_id)
|
||||
user = User(login=login, password=password, club_id=club_id)
|
||||
booking_filter = BookingFilter(
|
||||
court_ids=[607, 606], sport_id=444, date=pendulum.now().add(days=6)
|
||||
)
|
||||
|
||||
cookies = session.cookie_jar.filter_cookies(booking_url)
|
||||
loop.run_until_complete(booking.book(platform_url, user, booking_filter))
|
||||
|
||||
# cookies = session.cookie_jar.filter_cookies(platform_url)
|
||||
# assert cookies.get("connection_called") == "True"
|
||||
# assert cookies.get("login_called") == "True"
|
||||
# assert cookies.get("booking_called") == "True"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue