mirror of
https://github.com/Significant-Gravitas/AutoGPT.git
synced 2026-04-08 03:00:28 -04:00
change to using settings config rather than dotenv
This commit is contained in:
@@ -2,13 +2,11 @@ import logging
|
||||
import os
|
||||
import uuid
|
||||
|
||||
import dotenv
|
||||
import fastapi
|
||||
from google.cloud import storage
|
||||
|
||||
import backend.server.v2.store.exceptions
|
||||
|
||||
dotenv.load_dotenv()
|
||||
from backend.util.settings import Settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -18,11 +16,14 @@ MAX_FILE_SIZE = 50 * 1024 * 1024 # 50MB
|
||||
|
||||
|
||||
async def upload_media(user_id: str, file: fastapi.UploadFile) -> str:
|
||||
# Check required environment variables first before doing any file processing
|
||||
if not os.environ.get("MEDIA_GCS_BUCKET_NAME") or not os.environ.get(
|
||||
"GOOGLE_APPLICATION_CREDENTIALS"
|
||||
settings = Settings()
|
||||
|
||||
# Check required settings first before doing any file processing
|
||||
if (
|
||||
not settings.config.media_gcs_bucket_name
|
||||
or not settings.config.google_application_credentials
|
||||
):
|
||||
logger.error("Missing required GCS environment variables")
|
||||
logger.error("Missing required GCS settings")
|
||||
raise backend.server.v2.store.exceptions.StorageConfigError(
|
||||
"Missing storage configuration"
|
||||
)
|
||||
@@ -73,7 +74,7 @@ async def upload_media(user_id: str, file: fastapi.UploadFile) -> str:
|
||||
|
||||
try:
|
||||
storage_client = storage.Client()
|
||||
bucket = storage_client.bucket(os.environ["MEDIA_GCS_BUCKET_NAME"])
|
||||
bucket = storage_client.bucket(settings.config.media_gcs_bucket_name)
|
||||
blob = bucket.blob(storage_path)
|
||||
blob.content_type = content_type
|
||||
|
||||
|
||||
@@ -7,12 +7,16 @@ import starlette.datastructures
|
||||
|
||||
import backend.server.v2.store.exceptions
|
||||
import backend.server.v2.store.media
|
||||
from backend.util.settings import Settings
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_env_vars(monkeypatch):
|
||||
monkeypatch.setenv("GCS_BUCKET_NAME", "test-bucket")
|
||||
monkeypatch.setenv("GOOGLE_APPLICATION_CREDENTIALS", "test-credentials")
|
||||
def mock_settings(monkeypatch):
|
||||
settings = Settings()
|
||||
settings.config.media_gcs_bucket_name = "test-bucket"
|
||||
settings.config.google_application_credentials = "test-credentials"
|
||||
monkeypatch.setattr("backend.server.v2.store.media.Settings", lambda: settings)
|
||||
return settings
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -30,7 +34,7 @@ def mock_storage_client(mocker):
|
||||
return mock_client
|
||||
|
||||
|
||||
async def test_upload_media_success(mock_env_vars, mock_storage_client):
|
||||
async def test_upload_media_success(mock_settings, mock_storage_client):
|
||||
test_file = fastapi.UploadFile(
|
||||
filename="test.jpeg",
|
||||
file=io.BytesIO(b"test data"),
|
||||
@@ -45,7 +49,7 @@ async def test_upload_media_success(mock_env_vars, mock_storage_client):
|
||||
mock_blob.upload_from_string.assert_called_once()
|
||||
|
||||
|
||||
async def test_upload_media_invalid_type(mock_env_vars, mock_storage_client):
|
||||
async def test_upload_media_invalid_type(mock_settings, mock_storage_client):
|
||||
test_file = fastapi.UploadFile(
|
||||
filename="test.txt",
|
||||
file=io.BytesIO(b"test data"),
|
||||
@@ -61,7 +65,10 @@ async def test_upload_media_invalid_type(mock_env_vars, mock_storage_client):
|
||||
|
||||
|
||||
async def test_upload_media_missing_credentials(monkeypatch):
|
||||
monkeypatch.delenv("GCS_BUCKET_NAME", raising=False)
|
||||
settings = Settings()
|
||||
settings.config.media_gcs_bucket_name = ""
|
||||
settings.config.google_application_credentials = ""
|
||||
monkeypatch.setattr("backend.server.v2.store.media.Settings", lambda: settings)
|
||||
|
||||
test_file = fastapi.UploadFile(
|
||||
filename="test.jpeg",
|
||||
@@ -73,7 +80,7 @@ async def test_upload_media_missing_credentials(monkeypatch):
|
||||
await backend.server.v2.store.media.upload_media("test-user", test_file)
|
||||
|
||||
|
||||
async def test_upload_media_video_type(mock_env_vars, mock_storage_client):
|
||||
async def test_upload_media_video_type(mock_settings, mock_storage_client):
|
||||
test_file = fastapi.UploadFile(
|
||||
filename="test.mp4",
|
||||
file=io.BytesIO(b"test video data"),
|
||||
@@ -88,7 +95,7 @@ async def test_upload_media_video_type(mock_env_vars, mock_storage_client):
|
||||
mock_blob.upload_from_string.assert_called_once()
|
||||
|
||||
|
||||
async def test_upload_media_file_too_large(mock_env_vars, mock_storage_client):
|
||||
async def test_upload_media_file_too_large(mock_settings, mock_storage_client):
|
||||
large_data = b"x" * (50 * 1024 * 1024 + 1) # 50MB + 1 byte
|
||||
test_file = fastapi.UploadFile(
|
||||
filename="test.jpeg",
|
||||
|
||||
@@ -148,6 +148,16 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):
|
||||
"This value is then used to generate redirect URLs for OAuth flows.",
|
||||
)
|
||||
|
||||
media_gcs_bucket_name: str = Field(
|
||||
default="",
|
||||
description="The name of the Google Cloud Storage bucket for media files",
|
||||
)
|
||||
|
||||
google_application_credentials: str = Field(
|
||||
default="",
|
||||
description="The path to the Google Cloud credentials JSON file",
|
||||
)
|
||||
|
||||
@field_validator("platform_base_url", "frontend_base_url")
|
||||
@classmethod
|
||||
def validate_platform_base_url(cls, v: str, info: ValidationInfo) -> str:
|
||||
|
||||
@@ -391,13 +391,13 @@ async def main():
|
||||
# Create a copy of users list and shuffle it to avoid duplicates
|
||||
available_reviewers = users.copy()
|
||||
random.shuffle(available_reviewers)
|
||||
|
||||
|
||||
# Limit number of reviews to available unique reviewers
|
||||
num_reviews = min(
|
||||
random.randint(MIN_REVIEWS_PER_VERSION, MAX_REVIEWS_PER_VERSION),
|
||||
len(available_reviewers)
|
||||
len(available_reviewers),
|
||||
)
|
||||
|
||||
|
||||
# Take only the first num_reviews reviewers
|
||||
for reviewer in available_reviewers[:num_reviews]:
|
||||
await db.storelistingreview.create(
|
||||
|
||||
Reference in New Issue
Block a user