add ip change notification and refine sender display
This commit is contained in:
+87
-3
@@ -6,7 +6,9 @@ from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import Session, sessionmaker
|
||||
|
||||
from app.services.public_ip import PublicIPCheckResult, check_public_ipv4
|
||||
from app.config import Settings
|
||||
from app.services.email import EmailDeliveryError
|
||||
from app.services.public_ip import PublicIPCheckResult, check_public_ipv4, check_public_ipv4_and_notify
|
||||
|
||||
|
||||
def _make_session(database_url: str) -> Session:
|
||||
@@ -152,8 +154,8 @@ def test_public_ip_check_endpoint_hides_ip_values(client: TestClient, monkeypatc
|
||||
|
||||
monkeypatch.setattr(
|
||||
public_ip_route,
|
||||
"check_public_ipv4",
|
||||
lambda session: PublicIPCheckResult(
|
||||
"check_public_ipv4_and_notify",
|
||||
lambda session, bootstrap_settings: PublicIPCheckResult(
|
||||
status="changed",
|
||||
checked_at=fixed_checked_at,
|
||||
changed=True,
|
||||
@@ -172,3 +174,85 @@ def test_public_ip_check_endpoint_hides_ip_values(client: TestClient, monkeypatc
|
||||
assert "current_ipv4" not in response.text
|
||||
assert "previous_ipv4" not in response.text
|
||||
assert "203.0.113.10" not in response.text
|
||||
|
||||
|
||||
def _notification_settings() -> Settings:
|
||||
return Settings(
|
||||
_env_file=None,
|
||||
app_env="development",
|
||||
app_hostname="localhost:8000",
|
||||
app_database_url="sqlite:///./data/app.db",
|
||||
location_database_url="sqlite:///./data/locationRecorder.db",
|
||||
poo_database_url="sqlite:///./data/pooRecorder.db",
|
||||
auth_bootstrap_username="admin",
|
||||
auth_bootstrap_password="secret-password",
|
||||
smtp_enabled=True,
|
||||
smtp_host="smtp.example.com",
|
||||
smtp_port=587,
|
||||
smtp_username="smtp-user",
|
||||
smtp_password="super-secret-password",
|
||||
smtp_from_address="sender@example.com",
|
||||
smtp_to_address="recipient@example.com",
|
||||
smtp_use_starttls=True,
|
||||
)
|
||||
|
||||
|
||||
def test_public_ip_notification_sends_only_when_changed(auth_database, monkeypatch) -> None:
|
||||
session = _make_session(auth_database["app_url"])
|
||||
sent = []
|
||||
monkeypatch.setattr(
|
||||
"app.services.public_ip.send_public_ip_changed_email",
|
||||
lambda settings, *, previous_ipv4, current_ipv4, detected_at: sent.append(
|
||||
(previous_ipv4, current_ipv4, detected_at)
|
||||
),
|
||||
)
|
||||
try:
|
||||
first_seen = check_public_ipv4_and_notify(
|
||||
session,
|
||||
bootstrap_settings=_notification_settings(),
|
||||
fetch_public_ipv4=lambda: "203.0.113.10",
|
||||
)
|
||||
unchanged = check_public_ipv4_and_notify(
|
||||
session,
|
||||
bootstrap_settings=_notification_settings(),
|
||||
fetch_public_ipv4=lambda: "203.0.113.10",
|
||||
)
|
||||
changed = check_public_ipv4_and_notify(
|
||||
session,
|
||||
bootstrap_settings=_notification_settings(),
|
||||
fetch_public_ipv4=lambda: "198.51.100.25",
|
||||
)
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
assert first_seen.status == "first_seen"
|
||||
assert unchanged.status == "unchanged"
|
||||
assert changed.status == "changed"
|
||||
assert len(sent) == 1
|
||||
assert sent[0][0] == "203.0.113.10"
|
||||
assert sent[0][1] == "198.51.100.25"
|
||||
assert sent[0][2] == changed.checked_at
|
||||
|
||||
|
||||
def test_public_ip_notification_failure_does_not_break_changed_result(auth_database, monkeypatch) -> None:
|
||||
session = _make_session(auth_database["app_url"])
|
||||
monkeypatch.setattr(
|
||||
"app.services.public_ip.send_public_ip_changed_email",
|
||||
lambda settings, *, previous_ipv4, current_ipv4, detected_at: (_ for _ in ()).throw(
|
||||
EmailDeliveryError("smtp down")
|
||||
),
|
||||
)
|
||||
try:
|
||||
check_public_ipv4(session, fetch_public_ipv4=lambda: "203.0.113.10")
|
||||
result = check_public_ipv4_and_notify(
|
||||
session,
|
||||
bootstrap_settings=_notification_settings(),
|
||||
fetch_public_ipv4=lambda: "198.51.100.25",
|
||||
)
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
assert result.status == "changed"
|
||||
assert result.changed is True
|
||||
assert result.previous_ipv4 == "203.0.113.10"
|
||||
assert result.current_ipv4 == "198.51.100.25"
|
||||
|
||||
Reference in New Issue
Block a user