mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Use requests for external requests
L'API de requests est beaucoup plus claire que celle d'urllib et urllib3.
This commit is contained in:
@ -2,7 +2,7 @@ from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import final
|
||||
|
||||
import urllib3
|
||||
import requests
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.conf import settings
|
||||
from django.urls import reverse
|
||||
@ -35,16 +35,15 @@ class IcsCalendar:
|
||||
|
||||
@classmethod
|
||||
def make_external(cls) -> Path | None:
|
||||
calendar = urllib3.request(
|
||||
"GET",
|
||||
"https://calendar.google.com/calendar/ical/ae.utbm%40gmail.com/public/basic.ics",
|
||||
calendar = requests.get(
|
||||
"https://calendar.google.com/calendar/ical/ae.utbm%40gmail.com/public/basic.ics"
|
||||
)
|
||||
if calendar.status != 200:
|
||||
if not calendar.ok:
|
||||
return None
|
||||
|
||||
cls._CACHE_FOLDER.mkdir(parents=True, exist_ok=True)
|
||||
with open(cls._EXTERNAL_CALENDAR, "wb") as f:
|
||||
_ = f.write(calendar.data)
|
||||
_ = f.write(calendar.content)
|
||||
return cls._EXTERNAL_CALENDAR
|
||||
|
||||
@classmethod
|
||||
|
@ -16,11 +16,11 @@ from com.calendar import IcsCalendar
|
||||
|
||||
@dataclass
|
||||
class MockResponse:
|
||||
status: int
|
||||
ok: bool
|
||||
value: str
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
def content(self):
|
||||
return self.value.encode("utf8")
|
||||
|
||||
|
||||
@ -38,7 +38,7 @@ class TestExternalCalendar:
|
||||
@pytest.fixture
|
||||
def mock_request(self):
|
||||
mock = MagicMock()
|
||||
with patch("urllib3.request", mock):
|
||||
with patch("requests.get", mock):
|
||||
yield mock
|
||||
|
||||
@pytest.fixture
|
||||
@ -52,15 +52,12 @@ class TestExternalCalendar:
|
||||
def clear_cache(self):
|
||||
IcsCalendar._EXTERNAL_CALENDAR.unlink(missing_ok=True)
|
||||
|
||||
@pytest.mark.parametrize("error_code", [403, 404, 500])
|
||||
def test_fetch_error(
|
||||
self, client: Client, mock_request: MagicMock, error_code: int
|
||||
):
|
||||
mock_request.return_value = MockResponse(error_code, "not allowed")
|
||||
def test_fetch_error(self, client: Client, mock_request: MagicMock):
|
||||
mock_request.return_value = MockResponse(ok=False, value="not allowed")
|
||||
assert client.get(reverse("api:calendar_external")).status_code == 404
|
||||
|
||||
def test_fetch_success(self, client: Client, mock_request: MagicMock):
|
||||
external_response = MockResponse(200, "Definitely an ICS")
|
||||
external_response = MockResponse(ok=True, value="Definitely an ICS")
|
||||
mock_request.return_value = external_response
|
||||
response = client.get(reverse("api:calendar_external"))
|
||||
assert response.status_code == 200
|
||||
|
Reference in New Issue
Block a user