Added a lot of unit tests

This commit is contained in:
Stanislas Jouffroy 2024-03-18 23:46:01 +01:00
parent 0938fb98b7
commit 16d4a0724c
32 changed files with 4268 additions and 497 deletions

View file

@ -1,59 +1,8 @@
from urllib.parse import urljoin
import pendulum
from models import BookingFilter, Club
from pendulum import DateTime
from tests.fixtures import (
a_booking_failure_response,
a_booking_filter,
a_booking_success_response,
mes_resas_html,
)
def mock_successful_connection(aio_mock, url):
"""
Mock a call to the connection endpoint
:param aio_mock: the aioresponses mock object
:param url: the URL of the connection endpoint
"""
aio_mock.get(
url,
status=200,
headers={"Set-Cookie": f"connection_called=True; Domain={url}"},
)
def mock_successful_login(aio_mock, url):
"""
Mock a call to the login endpoint
:param aio_mock: the aioresponses mock object
:param url: the URL of the login endpoint
"""
aio_mock.post(
url,
status=200,
headers={"Set-Cookie": f"login_called=True; Domain={url}"},
)
def mock_booking(aio_mock, url, response):
"""
Mock a call to the booking endpoint
:param aio_mock: the aioresponses mock object
:param url: the URL of the booking endpoint
:param response: the response from the booking endpoint
"""
aio_mock.post(
url,
status=200,
headers={"Set-Cookie": f"booking_called=True; Domain={url}"},
body=response,
)
from tests.fixtures import a_booking_filter
def retrieve_booking_datetime(
@ -74,48 +23,3 @@ def retrieve_booking_datetime(
return date_to_book.subtract(days=booking_opening.days_before).at(
booking_hour, booking_minute
)
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,
mes_resas_html: str,
):
"""
Mock a REST API from a club.
It mocks the calls to the connexion to the website, a call to log in the user
and 2 calls to the booking endpoint
:param aio_mock: the pendulum.now() mock
: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)
mock_booking(aio_mock, booking_url, a_booking_success_response)
mock_booking(aio_mock, booking_url, a_booking_failure_response)