Files
sigcal/tests/test_feed.py
T
kawaiipunk 2b32c1a7f2 Rename project to signal-cal-bot
- package src/signal-cal-bot -> src/signal_cal_bot
- distribution and CLI entry point: signal-cal-bot
- env vars CALBOT_* -> SIGNAL_CAL_BOT_*
- database file, container names, user agents and logs renamed
- docs and compose updated
2026-11-18 16:45:54 +00:00

57 lines
2.0 KiB
Python

from __future__ import annotations
from datetime import datetime, timezone
from pathlib import Path
import httpx
from signal_cal_bot.feed import Feed, FeedResponse, normalize_feed_url
from signal_cal_bot.store import Store
NOW = datetime(2026, 11, 18, 12, 0, tzinfo=timezone.utc)
EMPTY_FEED = b"BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//test//EN\r\nEND:VCALENDAR\r\n"
def test_webcal_is_fetched_over_https():
assert normalize_feed_url("webcal://example.test/x.ics") == "https://example.test/x.ics"
assert normalize_feed_url("https://example.test/x.ics") == "https://example.test/x.ics"
def test_validators_are_only_stored_after_commit(tmp_path):
def handler(request: httpx.Request) -> httpx.Response:
if request.headers.get("if-none-match") == '"abc"':
return httpx.Response(304)
return httpx.Response(200, content=EMPTY_FEED, headers={"ETag": '"abc"'})
store = Store(Path(tmp_path) / "db.sqlite3")
client = httpx.Client(transport=httpx.MockTransport(handler))
feed = Feed("webcal://example.test/feed.ics", store, 15, client)
response = feed.fetch(NOW)
assert isinstance(response, FeedResponse)
assert response.content == EMPTY_FEED
# A parse failure would skip commit(), so nothing may be persisted yet.
assert store.get_meta("feed_etag") is None
feed.commit(response)
assert store.get_meta("feed_etag") == '"abc"'
second = feed.fetch(NOW)
assert second.not_modified
assert second.content is None
def test_fetch_raises_on_http_error(tmp_path):
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(500, text="boom")
store = Store(Path(tmp_path) / "db.sqlite3")
client = httpx.Client(transport=httpx.MockTransport(handler))
feed = Feed("https://example.test/feed.ics", store, 15, client)
try:
feed.fetch(NOW)
except httpx.HTTPStatusError:
pass
else: # pragma: no cover
raise AssertionError("expected HTTPStatusError")