Added a service that can get all current tournaments list

This commit is contained in:
Stanislas Jouffroy 2024-03-23 11:56:31 +01:00
parent e6023e0687
commit 3d0bd47079
26 changed files with 4305 additions and 204 deletions

View file

@ -4,7 +4,7 @@ from pathlib import Path
import config
import pendulum
import pytest
from connectors import GestionSportsConnector
from gestion_sport_connector import GestionSportsConnector
from models import BookingFilter, Club, User
TEST_FOLDER = Path(__file__).parent.parent
@ -36,11 +36,17 @@ def booking_filter() -> BookingFilter:
@pytest.fixture
def booking_success_response() -> dict:
booking_success_file = RESPONSES_FOLDER / "booking_success.json"
booking_success_file = RESPONSES_FOLDER / "booking-success.json"
return json.loads(booking_success_file.read_text(encoding="utf-8"))
@pytest.fixture
def booking_failure_response() -> dict:
booking_failure_file = RESPONSES_FOLDER / "booking_failure.json"
return json.loads(booking_failure_file.read_text(encoding="utf-8"))
file = RESPONSES_FOLDER / "booking-failure.json"
return json.loads(file.read_text(encoding="utf-8"))
@pytest.fixture
def tournament_sessions_json() -> str:
file = RESPONSES_FOLDER / "tournament-sessions.json"
return file.read_text(encoding="utf-8")

View file

@ -1,36 +1,15 @@
import json
import os
from pathlib import Path
from unittest.mock import patch
import aiohttp
import pendulum
import pytest
from connectors import GestionSportsConnector
from models import BookingFilter, Club
from pendulum import DateTime
from aiohttp import ClientSession
from gestion_sport_connector import GestionSportsConnector
from yarl import URL
def retrieve_booking_datetime(
a_booking_filter: BookingFilter, a_club: Club
) -> DateTime:
"""
Utility to retrieve the booking datetime from the booking filter and the club
:param a_booking_filter: the booking filter that contains the date to book
:param a_club: the club which has the number of days before the date and the booking time
"""
booking_opening = a_club.booking_platform.booking_opening
opening_time = pendulum.parse(booking_opening.opening_time)
booking_hour = opening_time.hour
booking_minute = opening_time.minute
date_to_book = a_booking_filter.date
return date_to_book.subtract(days=booking_opening.days_before).at(
booking_hour, booking_minute
)
@patch.dict(
os.environ,
{"CLUB_ID": "tpc"},
@ -57,6 +36,10 @@ def test_urls(connector):
connector.booking_cancellation_url
== "https://toulousepadelclub.gestion-sports.com/membre/mesresas.html"
)
assert (
connector.tournaments_sessions_url
== "https://toulousepadelclub.gestion-sports.com/membre/index.php"
)
@patch.dict(
@ -76,11 +59,15 @@ def test_urls_payload_templates(connector):
connector.booking_cancel_template
== resources_folder / "booking-cancellation-payload.txt"
)
assert (
connector.tournaments_sessions_template
== resources_folder / "tournament-sessions-payload.txt"
)
@pytest.mark.asyncio
async def test_landing_page(connector):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
response = await connector.land(session)
assert response.status == 200
@ -93,7 +80,7 @@ async def test_landing_page(connector):
@pytest.mark.asyncio
async def test_login(connector, user):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
response = await connector.login(session, user)
@ -128,7 +115,7 @@ def test_get_booked_court(
@pytest.mark.asyncio
async def test_book_one_court(connector, user, booking_filter):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
@ -156,28 +143,9 @@ def test_build_booking_datetime(connector, booking_filter):
assert opening_datetime.minute == 0
@patch("pendulum.now")
def test_wait_until_booking_time(mock_now, connector, booking_filter, club):
booking_datetime = retrieve_booking_datetime(booking_filter, club)
seconds = [
booking_datetime.subtract(seconds=3),
booking_datetime.subtract(seconds=2),
booking_datetime.subtract(seconds=1),
booking_datetime,
booking_datetime.add(microseconds=1),
booking_datetime.add(microseconds=2),
]
mock_now.side_effect = seconds
connector.wait_until_booking_time(booking_filter)
assert pendulum.now() == booking_datetime.add(microseconds=1)
@pytest.mark.asyncio
async def test_get_hash(connector, user):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
@ -197,7 +165,7 @@ def test_get_hash_input():
@pytest.mark.asyncio
async def test_get_bookings(connector, user):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
@ -210,7 +178,7 @@ async def test_get_bookings(connector, user):
@pytest.mark.asyncio
async def test_get_ongoing_bookings(connector, user):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
@ -218,20 +186,15 @@ async def test_get_ongoing_bookings(connector, user):
print(bookings)
@pytest.mark.asyncio
async def test_has_user_ongoing_bookings(connector, user):
assert await connector.has_user_ongoing_booking(user)
@pytest.mark.asyncio
async def test_cancel_booking_id(connector, user):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
ongoing_bookings = await connector.get_ongoing_bookings(session)
booking_id = ongoing_bookings[0].id
response = await connector.cancel_booking_id(user, booking_id)
response = await connector.cancel_booking_id(session, 666)
assert len(await connector.get_ongoing_bookings(session)) == 0
@ -245,7 +208,33 @@ def test_find_court(connector):
@pytest.mark.asyncio
async def test_cancel_booking(connector, user, booking_filter):
async with aiohttp.ClientSession() as session:
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
await connector.cancel_booking(session, booking_filter)
@pytest.mark.asyncio
async def test_tournament_sessions(connector, user):
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
response = await connector.send_tournaments_sessions_request(session)
assert response.status == 200
all_sessions = json.loads(await response.text())
sessions = all_sessions.get("Inscription tournois:school-outline")
assert len(sessions) == 1
@pytest.mark.asyncio
async def test_send_tournaments_request(connector, user):
async with ClientSession() as session:
await connector.land(session)
await connector.login(session, user)
tournament_session_id = "1174"
response = await connector.send_tournaments_request(
session, tournament_session_id
)
assert "<span class='nb_place_libre'>Complet</span>" in await response.text()

View file

@ -18,3 +18,9 @@ async def test_user_has_available_slots(club, user):
@pytest.mark.asyncio
async def test_cancel_booking(club, user, booking_filter):
await GestionSportsServices.cancel_booking(user, club, booking_filter)
@pytest.mark.asyncio
async def test_get_all_tournaments(user, club):
tournaments = await GestionSportsServices.get_all_tournaments(user, club)
assert len(tournaments) == 14