diff --git a/.github/workflows/build_image.yml b/.github/workflows/build_image.yml index ccaa35f..94714b2 100644 --- a/.github/workflows/build_image.yml +++ b/.github/workflows/build_image.yml @@ -10,7 +10,6 @@ on: jobs: build_and_publish_image: - permissions: contents: read packages: write diff --git a/Readme.md b/Readme.md index 1abf98a..9e6ac75 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,4 @@ -[![Github Container Pulls](https://img.shields.io/badge/Github%20Container%20Pulls-780-blue +[![Github Container Pulls](https://img.shields.io/badge/Github%20Container%20Pulls-960-blue )](https://github.com/tbmc/sporteasy-calendar-connector/pkgs/container/sporteasy-calendar-connector) [![Docker Pulls](https://img.shields.io/docker/pulls/tbmc/sporteasy-calendar-connector)](https://hub.docker.com/r/tbmc/sporteasy-calendar-connector) diff --git a/test/test_get_calendar_text.py b/test/test_get_calendar_text.py index 8c085fc..c688ce9 100644 --- a/test/test_get_calendar_text.py +++ b/test/test_get_calendar_text.py @@ -1,15 +1,28 @@ +import datetime +import importlib +from unittest.mock import patch, MagicMock + import requests_mock +import utils.calendar_converter +from utils.consts import url_list_events, url_list_teams, url_authenticate from .test_utils import ( read_text_by_name, replace_unwanted_lines, - replace_last_sync_and_sequence, ) -from utils.calendar_converter import CalendarConverter -from utils.consts import url_list_events, url_list_teams, url_authenticate -def test_get_calendar_text() -> None: +@patch("utils.datetime_utils.get_current_timestamp", return_value=173512350) +@patch( + "utils.datetime_utils.get_current_datetime", + return_value=datetime.datetime(2024, 12, 25, 10, 45, 0), +) +def test_get_calendar_text( + timestamp_mock: MagicMock, + datetime_mock: MagicMock, +) -> None: + importlib.reload(utils.calendar_converter) + mocked_response_teams = read_text_by_name("list_teams.json") mocked_response_events = read_text_by_name("list_events.json") @@ -17,17 +30,19 @@ def test_get_calendar_text() -> None: read_text_by_name("expected_calendar.ics") ) - with requests_mock.Mocker() as m: - m.post( + with requests_mock.Mocker() as request_mocker: + request_mocker.post( url_authenticate, status_code=200, cookies={"sporteasy": "token test calendar"}, ) - m.get(url_list_teams, text=mocked_response_teams) - m.get(url_list_events.format(team_id=1), text=mocked_response_events) + request_mocker.get(url_list_teams, text=mocked_response_teams) + request_mocker.get( + url_list_events.format(team_id=1), text=mocked_response_events + ) - converter = CalendarConverter() + converter = utils.calendar_converter.CalendarConverter() calendar_text = converter.get_calendar_text("username", "password", "1") - result = replace_last_sync_and_sequence(replace_unwanted_lines(calendar_text)) + result = replace_unwanted_lines(calendar_text) assert result == expected_calendar diff --git a/test/test_utils.py b/test/test_utils.py index 46221e4..7321e55 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -1,4 +1,3 @@ -import re import json from pathlib import Path from typing import Any @@ -41,13 +40,3 @@ def read_json(path: Path) -> Any: def replace_unwanted_lines(text: str) -> str: return text.replace("\r\n", "\n").replace("\n\n", "\n").strip() - - -_SEQUENCE_REGEX = r"(?<=SEQUENCE:)\d+" -_LAST_SYNC_REGEX = r"(?<=\| Last sync: )\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}" - - -def replace_last_sync_and_sequence(text: str) -> str: - text_sequence = re.sub(_SEQUENCE_REGEX, "173512350", text) - text_date = re.sub(_LAST_SYNC_REGEX, "2024-12-25 10:45:00", text_sequence) - return text_date diff --git a/utils/calendar_converter.py b/utils/calendar_converter.py index 0dfcaaa..b69ec9a 100644 --- a/utils/calendar_converter.py +++ b/utils/calendar_converter.py @@ -1,4 +1,3 @@ -import time from datetime import datetime, timedelta from typing import Any, cast, Literal @@ -14,6 +13,8 @@ url_list_teams, url_list_events, ) + +from utils.datetime_utils import get_current_timestamp, get_current_datetime from utils.env import load_env_data from utils.normalize import normalize @@ -165,7 +166,7 @@ def event_to_calendar_event(team_name: str, event_data: EVENT_TYPE) -> Event: _extract_event_description(event_data, event) event.add("class", "PUBLIC") - current_timestamp = int(time.time() / 10) + current_timestamp = get_current_timestamp() event.add("sequence", current_timestamp) event.add("transp", "OPAQUE") @@ -227,7 +228,7 @@ def get_calendar_text( cal.add("x-wr-calname", "SportEasy Calendar") cal.add("x-wr-timezone", "Europe/Paris") - formatted_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + formatted_time = get_current_datetime().strftime("%Y-%m-%d %H:%M:%S") cal.add("x-wr-caldesc", f"SportEasy Calendar | Last sync: {formatted_time}") cal.add("REFRESH-INTERVAL;VALUE=DURATION", "PT8H") cal.add("X-PUBLISHED-TTL", "PT8H") diff --git a/utils/datetime_utils.py b/utils/datetime_utils.py new file mode 100644 index 0000000..f45ecff --- /dev/null +++ b/utils/datetime_utils.py @@ -0,0 +1,10 @@ +import time +import datetime + + +def get_current_timestamp() -> int: + return int(time.time() / 10) + + +def get_current_datetime() -> datetime.datetime: + return datetime.datetime.now()