Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RealOrangeOne committed Mar 26, 2024
1 parent 125865f commit 67a9ef7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
3 changes: 2 additions & 1 deletion calmerge/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from aiohttp.web import run_app
from pydantic import ValidationError
from tomllib import TOMLDecodeError

from . import get_aiohttp_app
from .config import Config
Expand All @@ -28,7 +29,7 @@ def serve(args: argparse.Namespace) -> None:
def validate_config(args: argparse.Namespace) -> None:
try:
Config.from_file(args.config)
except ValidationError as e:
except (ValidationError, TOMLDecodeError) as e:
print(e)
exit(1)
else:
Expand Down
11 changes: 7 additions & 4 deletions calmerge/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ async def calendar(request: web.Request) -> web.Response:

calendar = await fetch_merged_calendar(calendar_config)

if calendar_config.allow_custom_offset and (
offset_days := try_parse_int(request.query.get("offset_days", ""))
):
if abs(offset_days) > MAX_OFFSET:
if calendar_config.allow_custom_offset:
offset_days = try_parse_int(request.query.get("offset_days", ""))

if offset_days is None:
raise web.HTTPBadRequest(reason="offset_days is invalid")
elif abs(offset_days) > MAX_OFFSET:
raise web.HTTPBadRequest(
reason=f"offset_days is too large (must be between -{MAX_OFFSET} and {MAX_OFFSET})"
)

offset_calendar(calendar, offset_days)

elif offset_days := calendar_config.offset_days:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_calendar_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ async def test_retrieves_calendars(client: TestClient) -> None:
assert not calendar.is_broken


async def test_unknown_calendar(client: TestClient) -> None:
response = await client.get("/unknown.ics")
assert response.status == 404


async def test_404_without_auth(client: TestClient) -> None:
response = await client.get("/python-authed.ics")
assert response.status == 404
Expand Down Expand Up @@ -119,3 +124,14 @@ async def test_out_of_bounds_custom_offset(client: TestClient, offset: int) -> N
await response.text()
== f"400: offset_days is too large (must be between -{MAX_OFFSET} and {MAX_OFFSET})"
)


@pytest.mark.parametrize("offset", ["invalid", "", "\0"])
async def test_invalid_offset(client: TestClient, offset: str) -> None:
response = await client.get(
"/python-custom-offset.ics",
params={"offset_days": offset},
)

assert response.status == 400
assert await response.text() == "400: offset_days is invalid"
44 changes: 44 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import subprocess
import sys
from pathlib import Path

import pytest
Expand Down Expand Up @@ -52,3 +54,45 @@ def test_empty_config(tmp_path: Path) -> None:
def test_invalid_file() -> None:
with pytest.raises(TOMLDecodeError):
Config.from_file(Path(__file__).resolve())


def test_validate_config_command(tmp_path: Path, config_path: Path) -> None:
result = subprocess.run(
[sys.executable, "-m", "calmerge", "--config", str(config_path), "validate"],
stdout=subprocess.PIPE,
)
assert result.returncode == 0
assert result.stdout == b"Config is valid!\n"


def test_validate_config_command_invalid_config() -> None:
result = subprocess.run(
[
sys.executable,
"-m",
"calmerge",
"--config",
str(Path(__file__).resolve()),
"validate",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 1


def test_validate_config_command_missing_file(tmp_path: Path) -> None:
test_config_file = tmp_path / "test.toml"
result = subprocess.run(
[
sys.executable,
"-m",
"calmerge",
"--config",
str(test_config_file),
"validate",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
assert result.returncode == 2

0 comments on commit 67a9ef7

Please sign in to comment.