feat: implement Fitbit OAuth, Garmin MFA, and optimize segment discovery

- Add Fitbit authentication flow (save credentials, OAuth callback handling)
- Implement Garmin MFA support with successful session/cookie handling
- Optimize segment discovery with new sampling and activity query services
- Refactor database session management in discovery API for better testability
- Enhance activity data parsing for charts and analysis
- Update tests to use testcontainers and proper dependency injection
- Clean up repository by ignoring and removing tracked transient files (.pyc, .db)
This commit is contained in:
2026-01-16 15:35:26 -08:00
parent 45dbc32295
commit d1cfd0fd8e
217 changed files with 1795 additions and 922 deletions

View File

@@ -2,13 +2,16 @@ import sys
import os
import pytest
from starlette.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy import create_engine, text
from sqlalchemy.orm import sessionmaker
from testcontainers.postgres import PostgresContainer
import time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Add backend root
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Add backend root to START of path
os.environ["TESTING"] = "1"
from main import app
from src.models.base import Base # Explicitly import Base from its definition
from src.models.base import Base
# Import all models to ensure Base.metadata.create_all is aware of them
from src.models.api_token import APIToken
from src.models.activity import Activity
@@ -16,40 +19,62 @@ from src.models.auth_status import AuthStatus
from src.models.config import Configuration
from src.models.health_metric import HealthMetric
from src.models.sync_log import SyncLog
from src.models.weight_record import WeightRecord # Ensure all models are imported
from src.api.status import get_db # Import get_db from an API file
import os
# Use an in-memory SQLite database for testing
SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
from src.models.weight_record import WeightRecord
from src.models.stream import ActivityStream
from src.api.status import get_db
@pytest.fixture(scope="session")
def db_engine():
"""Create a test database engine."""
Base.metadata.create_all(bind=engine) # Create tables
def postgres_container():
"""Spin up a Postgres container with PostGIS."""
print("DEBUG: Starting Postgres Container...")
# Use postgis/postgis image
postgres = PostgresContainer("postgis/postgis:15-3.4")
postgres.start()
print("DEBUG: Postgres Container Started.")
try:
yield postgres
finally:
postgres.stop()
@pytest.fixture(scope="session")
def db_engine(postgres_container):
"""Create a test database engine connected to the container."""
print("DEBUG: Configuring DB Engine...")
# Ensure usage of psycopg2 driver
db_url = postgres_container.get_connection_url().replace("postgresql://", "postgresql+psycopg2://")
engine = create_engine(db_url)
# Enable PostGIS extension
with engine.connect() as conn:
conn.execute(text("CREATE EXTENSION IF NOT EXISTS postgis"))
conn.commit()
# Create tables
Base.metadata.create_all(bind=engine)
yield engine
Base.metadata.drop_all(bind=engine) # Drop tables after tests
# Teardown logic
Base.metadata.drop_all(bind=engine)
@pytest.fixture(scope="module")
@pytest.fixture(scope="function")
def db_session(db_engine):
"""Create a test database session."""
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=db_engine)
connection = db_engine.connect()
transaction = connection.begin()
session = TestingSessionLocal(bind=connection)
yield session
session.close()
transaction.rollback()
connection.close()
@pytest.fixture(scope="module")
@pytest.fixture(scope="function")
def client(db_session):
"""Create a FastAPI test client."""
@@ -57,7 +82,7 @@ def client(db_session):
try:
yield db_session
finally:
db_session.close()
pass
app.dependency_overrides[get_db] = override_get_db
with TestClient(app) as c: