- add verbatim GPLv3 text as LICENSE - declare license and license-files in pyproject - add SPDX headers to Python sources - document licensing in README
60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
# Copyright (C) 2026 kawaiipunk
|
|
|
|
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")
|