Refactoring for reading the config

This commit is contained in:
Stanislas Jouffroy 2024-02-17 09:35:49 +01:00
parent 963ee6b86f
commit fc11a1e1eb
9 changed files with 264 additions and 166 deletions

View file

@ -1,66 +1,109 @@
import asyncio
import os
from unittest.mock import patch
from urllib.parse import urljoin
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)
from tests import fixtures
from tests.fixtures import (
a_booking_failure_response,
a_booking_filter,
a_booking_success_response,
a_club,
a_user,
)
login = "user"
password = "password"
club_id = "98"
club_id = "88"
court_id = "11"
# FIXME
# 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 mock_successful_connection(aio_mock, url):
aio_mock.get(
url,
status=200,
headers={"Set-Cookie": f"connection_called=True; Domain={url}"},
)
def mock_successful_login(aio_mock, url):
aio_mock.post(
url,
status=200,
headers={"Set-Cookie": f"login_called=True; Domain={url}"},
)
def mock_booking(aio_mock, url, response):
aio_mock.post(
url,
status=200,
headers={"Set-Cookie": f"booking_called=True; Domain={url}"},
body=response,
)
def mock_rest_api_from_connection_to_booking(
aio_mock, url, a_booking_failure_response, a_booking_success_response
):
connexion_url = urljoin(url, "/connexion.php?")
mock_successful_connection(aio_mock, connexion_url)
login_url = urljoin(url, "/connexion.php?")
mock_successful_login(aio_mock, login_url)
booking_url = urljoin(url, "/membre/reservation.html?")
mock_booking(aio_mock, booking_url, a_booking_failure_response)
mock_booking(aio_mock, booking_url, a_booking_success_response)
def test_booking_does_the_rights_calls(
a_booking_success_response, a_booking_failure_response
a_booking_success_response,
a_booking_failure_response,
a_user,
a_club,
a_booking_filter,
):
# mock connection to the booking platform
platform_url = "https://some.url"
connection_url = platform_url + "/connexion.php"
login_url = connection_url
booking_url = platform_url + "/membre/reservation.html"
with aioresponses() as aio_mock:
mock_rest_api_from_connection_to_booking(
aio_mock,
fixtures.url,
a_booking_failure_response,
a_booking_success_response,
)
loop = asyncio.get_event_loop()
loop = asyncio.get_event_loop()
court_booked = loop.run_until_complete(
booking.book(a_club, a_user, a_booking_filter)
)
assert court_booked == a_club.courts_ids[1]
@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": "2024-04-23T15:00:00Z",
},
clear=True,
)
def test_main(a_booking_success_response, a_booking_failure_response):
with aioresponses() as aio_mock:
aio_mock.get(
connection_url,
status=200,
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={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,
mock_rest_api_from_connection_to_booking(
aio_mock,
fixtures.url,
a_booking_failure_response,
a_booking_success_response,
)
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)
)
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"
court_booked = booking.main()
assert court_booked == 8