Add Home Assistant inbound gateway
This commit is contained in:
@@ -4,7 +4,10 @@ import pytest
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
import app.db as app_db
|
||||
from app.config import get_settings
|
||||
from app.main import create_app
|
||||
|
||||
@@ -52,3 +55,20 @@ def app(ready_location_database):
|
||||
def client(app):
|
||||
with TestClient(app) as test_client:
|
||||
yield test_client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def location_client(ready_location_database, monkeypatch: pytest.MonkeyPatch):
|
||||
database_url = ready_location_database["location_url"]
|
||||
|
||||
engine = create_engine(database_url, connect_args={"check_same_thread": False})
|
||||
session_local = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
monkeypatch.setattr(app_db, "engine", engine)
|
||||
monkeypatch.setattr(app_db, "SessionLocal", session_local)
|
||||
|
||||
fastapi_app = create_app()
|
||||
with TestClient(fastapi_app) as client:
|
||||
yield client, engine
|
||||
|
||||
engine.dispose()
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
from sqlalchemy import text
|
||||
|
||||
|
||||
def test_homeassistant_publish_records_location(location_client) -> None:
|
||||
client, engine = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "record",
|
||||
"content": "{'person': 'tianyu', 'latitude': '1.23', 'longitude': '4.56'}",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.text == ""
|
||||
|
||||
with engine.connect() as conn:
|
||||
row = conn.execute(
|
||||
text(
|
||||
"SELECT person, latitude, longitude, altitude "
|
||||
"FROM location ORDER BY datetime DESC LIMIT 1"
|
||||
)
|
||||
).one()
|
||||
|
||||
assert row.person == "tianyu"
|
||||
assert row.latitude == 1.23
|
||||
assert row.longitude == 4.56
|
||||
assert row.altitude == 0.0
|
||||
|
||||
|
||||
def test_homeassistant_publish_records_location_with_altitude(location_client) -> None:
|
||||
client, engine = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "record",
|
||||
"content": (
|
||||
"{'person': 'tianyu-alt', 'latitude': '1.23', "
|
||||
"'longitude': '4.56', 'altitude': '7.89'}"
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.text == ""
|
||||
|
||||
with engine.connect() as conn:
|
||||
row = conn.execute(
|
||||
text(
|
||||
"SELECT person, latitude, longitude, altitude "
|
||||
"FROM location ORDER BY datetime DESC LIMIT 1"
|
||||
)
|
||||
).one()
|
||||
|
||||
assert row.person == "tianyu-alt"
|
||||
assert row.latitude == 1.23
|
||||
assert row.longitude == 4.56
|
||||
assert row.altitude == 7.89
|
||||
|
||||
|
||||
def test_homeassistant_publish_rejects_invalid_envelope(location_client) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "record",
|
||||
"content": "{}",
|
||||
"extra": "not-allowed",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.text == "bad request"
|
||||
assert "extra" not in response.text
|
||||
|
||||
|
||||
def test_homeassistant_publish_rejects_invalid_json_body(location_client) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
content='{"target": "location_recorder", "action": "record", "content": ',
|
||||
headers={"Content-Type": "application/json"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.text == "bad request"
|
||||
|
||||
|
||||
def test_homeassistant_publish_rejects_missing_content(location_client) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "record",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.text == "bad request"
|
||||
assert "content" not in response.text
|
||||
|
||||
|
||||
def test_homeassistant_publish_returns_not_implemented_for_unknown_target(location_client) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "ticktick",
|
||||
"action": "create_action_task",
|
||||
"content": "{}",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 500
|
||||
assert response.text == "internal server error"
|
||||
|
||||
|
||||
def test_homeassistant_publish_returns_not_implemented_for_unknown_location_action(
|
||||
location_client,
|
||||
) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "unknown_action",
|
||||
"content": "{}",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 500
|
||||
assert response.text == "internal server error"
|
||||
|
||||
|
||||
def test_homeassistant_publish_rejects_invalid_location_content(location_client) -> None:
|
||||
client, _ = location_client
|
||||
|
||||
response = client.post(
|
||||
"/homeassistant/publish",
|
||||
json={
|
||||
"target": "location_recorder",
|
||||
"action": "record",
|
||||
"content": "{'person': 'tianyu', 'latitude': 'bad-lat', 'longitude': '4.56'}",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.text == "bad request"
|
||||
assert "bad-lat" not in response.text
|
||||
+3
-23
@@ -8,7 +8,7 @@ from alembic.config import Config
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
import app.db
|
||||
import app.db as app_db
|
||||
from app.main import create_app
|
||||
from scripts.location_db_adopt import (
|
||||
EXPECTED_USER_VERSION,
|
||||
@@ -23,26 +23,6 @@ def _make_alembic_config(database_url: str) -> Config:
|
||||
config.set_main_option("sqlalchemy.url", database_url)
|
||||
return config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def location_client(ready_location_database, monkeypatch: pytest.MonkeyPatch):
|
||||
database_url = ready_location_database["location_url"]
|
||||
|
||||
engine = create_engine(database_url, connect_args={"check_same_thread": False})
|
||||
session_local = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
monkeypatch.setattr(app.db, "engine", engine)
|
||||
monkeypatch.setattr(app.db, "SessionLocal", session_local)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
fastapi_app = create_app()
|
||||
with TestClient(fastapi_app) as client:
|
||||
yield client, engine
|
||||
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def test_location_record_endpoint_writes_row(location_client) -> None:
|
||||
client, engine = location_client
|
||||
|
||||
@@ -243,8 +223,8 @@ def test_legacy_style_location_db_can_be_stamped_and_adopted(
|
||||
|
||||
engine = create_engine(database_url, connect_args={"check_same_thread": False})
|
||||
session_local = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
monkeypatch.setattr(app.db, "engine", engine)
|
||||
monkeypatch.setattr(app.db, "SessionLocal", session_local)
|
||||
monkeypatch.setattr(app_db, "engine", engine)
|
||||
monkeypatch.setattr(app_db, "SessionLocal", session_local)
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
Reference in New Issue
Block a user