added cargo files

This commit is contained in:
2026-03-03 10:57:43 -05:00
parent 478a90e01b
commit 169df46bc2
813 changed files with 227273 additions and 9 deletions

View File

@@ -0,0 +1,87 @@
import pytest
import pytest_asyncio
from httpx import AsyncClient
import os
from typing import Generator
import mysql.connector
import psycopg
from psycopg_pool import ConnectionPool
from mysql.connector import pooling
# Import and run environment setup before any other imports
from test_environment import setup_test_environment
setup_test_environment()
# Set test environment variables BEFORE any app imports
os.environ['TEST_MODE'] = 'True'
os.environ['DB_TYPE'] = os.getenv('TEST_DB_TYPE', 'postgresql')
# Debug print statements
print(f"Current directory: {os.getcwd()}")
print(f"Setting up test environment...")
print(f"DB_TYPE set to: {os.getenv('DB_TYPE')}")
print(f"TEST_DB_TYPE set to: {os.getenv('TEST_DB_TYPE')}")
# Test database configurations
MYSQL_CONFIG = {
'user': 'test_user',
'password': 'test_password',
'host': '127.0.0.1',
'port': 3306,
'database': 'test_db'
}
POSTGRES_CONFIG = {
'user': 'test_user',
'password': 'test_password',
'host': '127.0.0.1',
'port': 5432,
'dbname': 'test_db'
}
# Only import app after environment variables are set
from clients.clientapi import app
@pytest.fixture(scope="session", autouse=True)
def setup_test_env():
"""Set up test environment variables"""
# Environment variables already set at module level
# Set up test database configuration
if os.getenv('DB_TYPE') == 'postgresql':
os.environ['DATABASE_URL'] = 'postgresql://test_user:test_password@localhost:5432/test_db'
else:
os.environ['DATABASE_URL'] = 'mysql://test_user:test_password@localhost:3306/test_db'
yield
# Cleanup
if 'TEST_MODE' in os.environ:
del os.environ['TEST_MODE']
@pytest.fixture(scope="session")
def db_connection():
"""Create database connection based on configured database type"""
db_type = os.getenv('DB_TYPE', 'postgresql')
if db_type == 'postgresql':
conn = psycopg.connect(**POSTGRES_CONFIG)
yield conn
conn.close()
else:
pool = pooling.MySQLConnectionPool(
pool_name="test_pool",
pool_size=5,
**MYSQL_CONFIG
)
conn = pool.get_connection()
yield conn
conn.close()
@pytest_asyncio.fixture
async def async_client():
"""Create async client for testing FastAPI endpoints"""
async with AsyncClient(app=app, base_url="http://test") as client:
yield client

View File

@@ -0,0 +1,10 @@
import pytest
import pytest_asyncio
@pytest.mark.asyncio
async def test_health_check(async_client):
"""Test the health check endpoint"""
response = await async_client.get("/api/pinepods_check")
assert response.status_code == 200
# Check for the expected response data
assert response.json() == {"status_code": 200, "pinepods_instance": True}

View File

@@ -0,0 +1,13 @@
import os
def setup_test_environment():
"""Set up test environment variables for database configuration"""
os.environ['DB_TYPE'] = os.getenv('TEST_DB_TYPE', 'postgresql')
os.environ['DB_HOST'] = '127.0.0.1'
os.environ['DB_PORT'] = '5432' if os.getenv('DB_TYPE') == 'postgresql' else '3306'
os.environ['DB_USER'] = 'test_user'
os.environ['DB_PASSWORD'] = 'test_password'
os.environ['DB_NAME'] = 'test_db'
os.environ['TEST_MODE'] = 'True'
os.environ['SEARCH_API_URL'] = 'https://search.pinepods.online/api/search'
os.environ['PEOPLE_API_URL'] = 'https://people.pinepods.online/api/hosts'

View File

@@ -0,0 +1,124 @@
import pytest
import pytest_asyncio
import os
# Use consistent environment variables
DB_USER = os.environ.get("DB_USER", "test_user")
DB_PASSWORD = os.environ.get("DB_PASSWORD", "test_password")
DB_HOST = os.environ.get("DB_HOST", "127.0.0.1")
DB_PORT = os.environ.get("DB_PORT", "5432")
DB_NAME = os.environ.get("DB_NAME", "test_db")
# Read the API key from the file
def get_admin_api_key():
try:
with open("/tmp/web_api_key.txt", "r") as f:
return f.read().strip()
except FileNotFoundError:
raise RuntimeError("API key file not found. Ensure database setup has completed.")
# Get the API key once at module level
ADMIN_API_KEY = get_admin_api_key()
@pytest.mark.asyncio
async def test_pinepods_check(async_client):
"""Test the basic health check endpoint"""
response = await async_client.get("/api/pinepods_check")
assert response.status_code == 200
assert response.json() == {"status_code": 200, "pinepods_instance": True}
@pytest.mark.asyncio
async def test_verify_api_key(async_client):
"""Test API key verification with admin web key"""
response = await async_client.get(
"/api/data/verify_key",
headers={"Api-Key": ADMIN_API_KEY}
)
assert response.status_code == 200
assert response.json() == {"status": "success"}
@pytest.mark.asyncio
async def test_get_podcast_details_dynamic(async_client):
"""Test fetching dynamic podcast details from the feed"""
params = {
"user_id": 1, # Admin user ID is typically 1
"podcast_title": "PinePods News",
"podcast_url": "https://news.pinepods.online/feed.xml",
"added": False,
"display_only": False
}
response = await async_client.get(
"/api/data/get_podcast_details_dynamic",
params=params,
headers={"Api-Key": ADMIN_API_KEY}
)
assert response.status_code == 200
data = response.json()
assert data["podcast_title"] == "Pinepods News Feed"
assert data["podcast_url"] == "https://news.pinepods.online/feed.xml"
@pytest.mark.asyncio
async def test_add_podcast(async_client):
"""Test adding a podcast to the database"""
# Mock the database functions
import database_functions.functions
# Store original function
original_add_podcast = database_functions.functions.add_podcast
# Mock the add_podcast function to return expected values
def mock_add_podcast(*args, **kwargs):
return (1, 1) # Return a tuple of (podcast_id, first_episode_id)
# Patch the function
database_functions.functions.add_podcast = mock_add_podcast
try:
# First get the podcast details
params = {
"user_id": 1,
"podcast_title": "PinePods News",
"podcast_url": "https://news.pinepods.online/feed.xml",
"added": False,
"display_only": False
}
details_response = await async_client.get(
"/api/data/get_podcast_details_dynamic",
params=params,
headers={"Api-Key": ADMIN_API_KEY}
)
podcast_details = details_response.json()
# Then add the podcast
add_request = {
"podcast_values": {
"pod_title": podcast_details["podcast_title"],
"pod_artwork": podcast_details["podcast_artwork"],
"pod_author": podcast_details["podcast_author"],
"categories": podcast_details["podcast_categories"],
"pod_description": podcast_details["podcast_description"],
"pod_episode_count": podcast_details["podcast_episode_count"],
"pod_feed_url": podcast_details["podcast_url"],
"pod_website": podcast_details["podcast_link"],
"pod_explicit": podcast_details["podcast_explicit"],
"user_id": 1
},
"podcast_index_id": 0
}
response = await async_client.post(
"/api/data/add_podcast",
json=add_request,
headers={"Api-Key": ADMIN_API_KEY}
)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert "podcast_id" in data
assert "first_episode_id" in data
finally:
# Restore original function
database_functions.functions.add_podcast = original_add_podcast