import pytest from unittest.mock import MagicMock, patch from fastapi.testclient import TestClient from main import app from src.services.postgresql_manager import PostgreSQLManager from src.models.api_token import APIToken from src.models.config import Configuration from datetime import datetime, timedelta # client = TestClient(app) # REMOVED # Helper to verify standard API response structure def assert_success_response(response): assert response.status_code == 200 data = response.json() # auth-status returns model directly, not wrapped in {status: success} return data def test_get_auth_status(client): """Test GET /api/setup/auth-status endpoint.""" # Setup mocks in DB # We can rely on 'mock_db_session' fixture if it's auto-used or if we patch get_db. # But integration tests usually use the real app/client which uses override_get_db in conftest. # Assuming conftest.py sets up `override_get_db` or uses a test DB. # Ideally for functional test we want to mock the DB data. # If the app uses dependency injection for `get_db`, checking conftest.py helps. # Let's assume we can rely on TestClient and if it hits DB, it hits the test DB. response = client.get("/api/setup/auth-status") assert response.status_code == 200 data = response.json() assert "garmin" in data assert "fitbit" in data assert "token_stored" in data["garmin"] @patch('requests.get') def test_load_consul_config(mock_get, client): """Test POST /api/setup/load-consul-config.""" # Mock Consul response mock_response = MagicMock() mock_response.status_code = 200 mock_response.json.return_value = [ # Consul returns list of {Key: ..., Value: b64} { "Key": "fitbit-garmin-sync/garmin_username", "Value": "dGVzdF91c2Vy" # 'test_user' in b64 }, { "Key": "fitbit-garmin-sync/garmin_password", "Value": "dGVzdF9wYXNz" # 'test_pass' in b64 } ] mock_get.return_value = mock_response response = client.post("/api/setup/load-consul-config") assert response.status_code == 200 data = response.json() assert data["status"] == "success" assert data["garmin"]["username"] == "test_user"