""" Test migration endpoints """ import pytest from fastapi.testclient import TestClient def test_migrate_data_without_api_key(client: TestClient): """Test migration endpoint without API key""" migration_request = { "source_url": "http://test-source.com", "api_key": "test-key", "tickers": ["AAPL", "MSFT"] } response = client.post("/api/v1/admin/migrate", json=migration_request) assert response.status_code == 401 data = response.json() assert "detail" in data assert "error_type" in data["detail"] assert data["detail"]["error_type"] == "AUTHENTICATION_ERROR" def test_migrate_data_with_invalid_api_key(client: TestClient): """Test migration endpoint with invalid API key""" migration_request = { "source_url": "http://test-source.com", "api_key": "test-key", "tickers": ["AAPL", "MSFT"] } headers = {"X-API-Key": "invalid-key"} response = client.post("/api/v1/admin/migrate", json=migration_request, headers=headers) assert response.status_code == 401 data = response.json() assert "detail" in data assert "error_type" in data["detail"] assert data["detail"]["error_type"] == "AUTHENTICATION_ERROR" def test_migrate_data_with_valid_api_key(client: TestClient, valid_migration_key): """Test migration endpoint with valid API key""" migration_request = { "source_url": "http://test-source.com", "api_key": "test-key", "tickers": ["AAPL"] } headers = {"X-API-Key": valid_migration_key} response = client.post("/api/v1/admin/migrate", json=migration_request, headers=headers) # Should proceed with migration attempt (will likely fail due to invalid source URL) assert response.status_code in [200, 500] if response.status_code == 200: data = response.json() # Check response structure assert "status" in data assert "total_records" in data assert "migrated_records" in data assert "failed_records" in data assert "errors" in data assert "duration_seconds" in data def test_migration_request_validation(client: TestClient, valid_migration_key): """Test migration request validation""" headers = {"X-API-Key": valid_migration_key} # Missing required fields invalid_requests = [ { # Missing source_url "api_key": "test-key" }, { # Missing api_key "source_url": "http://test.com" }, { # Invalid URL format "source_url": "not-a-url", "api_key": "test-key" } ] for invalid_request in invalid_requests: response = client.post("/api/v1/admin/migrate", json=invalid_request, headers=headers) assert response.status_code == 422 def test_migration_with_optional_parameters(client: TestClient, valid_migration_key): """Test migration with optional parameters""" migration_request = { "source_url": "http://test-source.com", "api_key": "test-key", "tickers": ["AAPL", "MSFT"], "start_date": "2023-01-01T00:00:00", "end_date": "2023-12-31T23:59:59" } headers = {"X-API-Key": valid_migration_key} response = client.post("/api/v1/admin/migrate", json=migration_request, headers=headers) # Should not fail on validation assert response.status_code != 422 def test_export_data_without_api_key(client: TestClient): """Test export endpoint without API key""" response = client.get("/api/v1/admin/migration/export/AAPL") assert response.status_code == 401 data = response.json() assert "detail" in data assert "error_type" in data["detail"] assert data["detail"]["error_type"] == "AUTHENTICATION_ERROR" def test_export_data_with_valid_api_key(client: TestClient, valid_migration_key): """Test export endpoint with valid API key""" headers = {"X-API-Key": valid_migration_key} response = client.get("/api/v1/admin/migration/export/AAPL", headers=headers) assert response.status_code == 200 data = response.json() # Check response structure assert "ticker" in data assert data["ticker"] == "AAPL" assert "message" in data def test_export_data_with_parameters(client: TestClient, valid_migration_key): """Test export endpoint with query parameters""" params = { "start_date": "2023-01-01T00:00:00", "end_date": "2023-12-31T23:59:59" } headers = {"X-API-Key": valid_migration_key} response = client.get("/api/v1/admin/migration/export/AAPL", params=params, headers=headers) assert response.status_code == 200 def test_migration_disabled_setting(client: TestClient, monkeypatch): """Test migration endpoints when migration is disabled""" # Mock settings to disable migration from app.core.config import settings monkeypatch.setattr(settings, "ALLOW_MIGRATION", False) migration_request = { "source_url": "http://test-source.com", "api_key": "test-key" } headers = {"X-API-Key": "any-key"} response = client.post("/api/v1/admin/migrate", json=migration_request, headers=headers) assert response.status_code == 403 data = response.json() assert "detail" in data assert "error_type" in data["detail"] assert data["detail"]["error_type"] == "AUTHENTICATION_ERROR" assert "disabled" in data["detail"]["message"]