diff --git a/backend/api/routes/admin.py b/backend/api/routes/admin.py index 84406b2e..d33bb73f 100644 --- a/backend/api/routes/admin.py +++ b/backend/api/routes/admin.py @@ -55,7 +55,6 @@ class AdminStatsOverview(BaseModel): jobs_last_30d: int jobs_by_status: JobsByStatusResponse total_credits_issued_30d: int - total_beta_testers: int class FileInfo(BaseModel): @@ -203,8 +202,6 @@ async def get_admin_stats_overview( 1 for u in all_users if u.get("last_login_at") and _normalize_datetime(u["last_login_at"]) >= thirty_days_ago ) - total_beta_testers = sum(1 for u in all_users if u.get("is_beta_tester")) - # Calculate credits from filtered users total_credits_issued_30d = 0 for user_data in all_users: @@ -270,10 +267,6 @@ async def get_admin_stats_overview( active_users_30d = get_count( users_collection.where(filter=FieldFilter("last_login_at", ">=", thirty_days_ago)) ) - total_beta_testers = get_count( - users_collection.where(filter=FieldFilter("is_beta_tester", "==", True)) - ) - # Job statistics total_jobs = get_count(jobs_collection) jobs_last_7d = get_count( @@ -341,7 +334,6 @@ async def get_admin_stats_overview( jobs_last_30d=jobs_last_30d, jobs_by_status=jobs_by_status, total_credits_issued_30d=total_credits_issued_30d, - total_beta_testers=total_beta_testers, ) diff --git a/backend/api/routes/rate_limits.py b/backend/api/routes/rate_limits.py index 34dd657d..a51535a3 100644 --- a/backend/api/routes/rate_limits.py +++ b/backend/api/routes/rate_limits.py @@ -32,13 +32,10 @@ class RateLimitStatsResponse(BaseModel): """Current rate limit statistics.""" # Configuration jobs_per_day_limit: int - youtube_uploads_per_day_limit: int - beta_ip_per_day_limit: int rate_limiting_enabled: bool - # Current usage (legacy count-based) + # YouTube uploads today (from quota service, PT-based) youtube_uploads_today: int - youtube_uploads_remaining: int # YouTube quota (unit-based tracking) youtube_quota_units_consumed: int @@ -61,6 +58,14 @@ class RateLimitStatsResponse(BaseModel): # User override stats total_overrides: int + # GCP quota monitoring (Phase 3) + gcp_quota_available: bool = False + gcp_quota_units_consumed: Optional[int] = None + gcp_quota_last_datapoint: Optional[str] = None + gcp_quota_data_delay_minutes: Optional[int] = None + quota_drift: Optional[int] = None + quota_drift_alert: bool = False + class YouTubeQueueEntry(BaseModel): """A queued YouTube upload entry.""" @@ -166,12 +171,7 @@ async def get_rate_limit_stats( rate_limit_service = get_rate_limit_service() email_validation = get_email_validation_service() - # Get YouTube uploads today (legacy count) - youtube_today = rate_limit_service.get_youtube_uploads_today() - youtube_limit = settings.rate_limit_youtube_uploads_per_day - youtube_remaining = max(0, youtube_limit - youtube_today) if youtube_limit > 0 else -1 - - # Get YouTube quota stats (unit-based) + # Get YouTube quota stats (unit-based, PT timezone) from backend.services.youtube_quota_service import get_youtube_quota_service quota_service = get_youtube_quota_service() quota_stats = quota_service.get_quota_stats() @@ -187,13 +187,26 @@ async def get_rate_limit_stats( # Get override count overrides = rate_limit_service.get_all_overrides() + # Get GCP quota data if available + gcp_data = {} + try: + gcp_result = quota_service.get_gcp_quota_usage() + if gcp_result.get("available"): + gcp_data = { + "gcp_quota_available": True, + "gcp_quota_units_consumed": gcp_result.get("gcp_units_consumed"), + "gcp_quota_last_datapoint": gcp_result.get("gcp_last_datapoint_time"), + "gcp_quota_data_delay_minutes": gcp_result.get("gcp_data_delay_minutes"), + "quota_drift": gcp_result.get("drift"), + "quota_drift_alert": gcp_result.get("drift_alert", False), + } + except Exception: + pass # GCP quota is optional, graceful fallback + return RateLimitStatsResponse( jobs_per_day_limit=settings.rate_limit_jobs_per_day, - youtube_uploads_per_day_limit=settings.rate_limit_youtube_uploads_per_day, - beta_ip_per_day_limit=settings.rate_limit_beta_ip_per_day, rate_limiting_enabled=settings.enable_rate_limiting, - youtube_uploads_today=youtube_today, - youtube_uploads_remaining=youtube_remaining, + youtube_uploads_today=quota_stats["upload_count"], youtube_quota_units_consumed=quota_stats["units_consumed"], youtube_quota_units_remaining=quota_stats["units_remaining"], youtube_quota_daily_limit=quota_stats["units_limit"], @@ -207,6 +220,7 @@ async def get_rate_limit_stats( blocked_emails_count=blocklist_stats["blocked_emails_count"], blocked_ips_count=blocklist_stats["blocked_ips_count"], total_overrides=len(overrides), + **gcp_data, ) diff --git a/backend/api/routes/users.py b/backend/api/routes/users.py index cf8b5636..43941493 100644 --- a/backend/api/routes/users.py +++ b/backend/api/routes/users.py @@ -43,12 +43,6 @@ from backend.models.user import ( AddCreditsRequest, AddCreditsResponse, UserListResponse, - BetaTesterStatus, - BetaTesterEnrollRequest, - BetaTesterEnrollResponse, - BetaFeedbackRequest, - BetaFeedbackResponse, - BetaTesterFeedback, UserFeedback, UserFeedbackRequest, UserFeedbackResponse, @@ -1014,294 +1008,6 @@ async def stripe_webhook( return {"status": "received", "type": event_type} -# ============================================================================= -# Beta Tester Program -# ============================================================================= - -BETA_TESTER_FREE_CREDITS = 1 # Number of free credits for beta testers - - -@router.post("/beta/enroll", response_model=BetaTesterEnrollResponse) -async def enroll_beta_tester( - request: BetaTesterEnrollRequest, - http_request: Request, - user_service: UserService = Depends(get_user_service), - email_service: EmailService = Depends(get_email_service), -): - """ - Enroll as a beta tester to receive free karaoke credits. - - Requirements: - - Accept that there may be work to review/correct lyrics - - Promise to provide feedback after using the tool - - Returns free credits and optionally a session token for new users. - """ - from backend.services.email_validation_service import get_email_validation_service - from backend.services.rate_limit_service import get_rate_limit_service - - # Check if email service is configured - if not email_service.is_configured(): - logger.error("Email service not configured - cannot send beta welcome emails") - raise HTTPException( - status_code=503, - detail="Email service is not available. Please contact support." - ) - - email = request.email.lower() - email_validation = get_email_validation_service() - rate_limit_service = get_rate_limit_service() - - # ----- ABUSE PREVENTION CHECKS ----- - - # 1. Validate email (disposable domain, blocked email checks) - is_valid, error_message = email_validation.validate_email_for_beta(email) - if not is_valid: - logger.warning(f"Beta enrollment rejected - email validation failed: {email} - {error_message}") - raise HTTPException( - status_code=400, - detail=error_message - ) - - # 2. Check IP blocking - ip_address = http_request.client.host if http_request.client else None - if ip_address and email_validation.is_ip_blocked(ip_address): - logger.warning(f"Beta enrollment rejected - IP blocked: {ip_address}") - raise HTTPException( - status_code=403, - detail="Access denied from this location" - ) - - # 3. Check for E2E test bypass (allows automated testing to skip IP rate limit) - from backend.config import settings - e2e_bypass_key = http_request.headers.get("X-E2E-Bypass-Key") - skip_ip_rate_limit = False - if e2e_bypass_key and settings.e2e_bypass_key: - if e2e_bypass_key == settings.e2e_bypass_key: - logger.info(f"Beta enrollment: E2E bypass key validated for {_mask_email(email)}") - skip_ip_rate_limit = True - else: - logger.warning(f"Beta enrollment: Invalid E2E bypass key attempted for {_mask_email(email)}") - - # 4. Check IP-based enrollment rate limit (1 per 24h per IP) - if ip_address and not skip_ip_rate_limit: - allowed, remaining, message = rate_limit_service.check_beta_ip_limit(ip_address) - if not allowed: - logger.warning(f"Beta enrollment rejected - IP rate limit: {ip_address} - {message}") - raise HTTPException( - status_code=429, - detail="Too many beta enrollments from your location. Please try again tomorrow." - ) - - # 5. Check for duplicate enrollment via normalized email - normalized_email = email_validation.normalize_email(email) - if normalized_email != email: - # Check if normalized version is already enrolled - normalized_user = user_service.get_user(normalized_email) - if normalized_user and normalized_user.is_beta_tester: - logger.warning(f"Beta enrollment rejected - normalized email already enrolled: {email} -> {normalized_email}") - raise HTTPException( - status_code=400, - detail="An account with this email address is already enrolled in the beta program" - ) - - # ----- END ABUSE PREVENTION CHECKS ----- - - # Validate acceptance - if not request.accept_corrections_work: - raise HTTPException( - status_code=400, - detail="You must accept that you may need to review/correct lyrics" - ) - - if len(request.promise_text.strip()) < 10: - raise HTTPException( - status_code=400, - detail="Please write a sentence confirming your promise to provide feedback" - ) - - # Get or create user - user = user_service.get_or_create_user(email) - - # Check if already enrolled as beta tester - if user.is_beta_tester: - raise HTTPException( - status_code=400, - detail="You are already enrolled in the beta program" - ) - - # Get client info (ip_address already set above in abuse prevention) - user_agent = http_request.headers.get("user-agent") - - # Enroll as beta tester - from datetime import datetime - user_service.update_user( - email, - is_beta_tester=True, - beta_tester_status=BetaTesterStatus.ACTIVE.value, - beta_enrolled_at=datetime.utcnow(), - beta_promise_text=request.promise_text.strip(), - ) - - # Add free credits - _, new_balance, _ = user_service.add_credits( - email=email, - amount=BETA_TESTER_FREE_CREDITS, - reason="beta_tester_enrollment", - ) - - # Record IP enrollment for rate limiting - if ip_address: - try: - rate_limit_service.record_beta_enrollment(ip_address, email) - except Exception as e: - logger.warning(f"Failed to record beta IP enrollment: {e}") - - # Create session for the user (so they can start using the service immediately) - session = user_service.create_session(email, ip_address=ip_address, user_agent=user_agent) - - # Send welcome email - email_service.send_beta_welcome_email(email, BETA_TESTER_FREE_CREDITS) - - logger.info(f"Beta tester enrolled: {email}, granted {BETA_TESTER_FREE_CREDITS} credits") - - return BetaTesterEnrollResponse( - status="success", - message=f"Welcome to the beta program! You have {new_balance} free credits.", - credits_granted=BETA_TESTER_FREE_CREDITS, - session_token=session.token, - ) - - -@router.post("/beta/feedback", response_model=BetaFeedbackResponse) -async def submit_beta_feedback( - request: BetaFeedbackRequest, - authorization: Optional[str] = Header(None), - user_service: UserService = Depends(get_user_service), -): - """ - Submit feedback as a beta tester. - - Requires authentication. Updates beta tester status to completed. - May grant bonus credits for detailed feedback. - """ - if not authorization: - raise HTTPException(status_code=401, detail="Authentication required") - - # Extract token - token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization - - # Validate session - valid, user, message = user_service.validate_session(token) - if not valid or not user: - raise HTTPException(status_code=401, detail=message) - - # Check if user is a beta tester - if not user.is_beta_tester: - raise HTTPException( - status_code=400, - detail="Only beta testers can submit feedback through this endpoint" - ) - - # Check if already completed feedback - if user.beta_tester_status == BetaTesterStatus.COMPLETED: - raise HTTPException( - status_code=400, - detail="You have already submitted feedback. Thank you!" - ) - - # Save feedback to Firestore - import uuid - - feedback = BetaTesterFeedback( - id=str(uuid.uuid4()), - user_email=user.email, - job_id=request.job_id, - overall_rating=request.overall_rating, - ease_of_use_rating=request.ease_of_use_rating, - lyrics_accuracy_rating=request.lyrics_accuracy_rating, - correction_experience_rating=request.correction_experience_rating, - what_went_well=request.what_went_well, - what_could_improve=request.what_could_improve, - additional_comments=request.additional_comments, - would_recommend=request.would_recommend, - would_use_again=request.would_use_again, - submitted_via="web", - ) - - # Save to Firestore - user_service.db.collection("beta_feedback").document(feedback.id).set( - feedback.model_dump(mode='json') - ) - - # Update user status - user_service.update_user( - user.email, - beta_tester_status=BetaTesterStatus.COMPLETED.value, - ) - - # Calculate bonus credits for detailed feedback - bonus_credits = 0 - has_detailed_feedback = ( - (request.what_went_well and len(request.what_went_well) > 50) or - (request.what_could_improve and len(request.what_could_improve) > 50) or - (request.additional_comments and len(request.additional_comments) > 50) - ) - - if has_detailed_feedback: - # Grant bonus credit for detailed feedback - bonus_credits = 1 - user_service.add_credits( - email=user.email, - amount=bonus_credits, - reason="beta_feedback_bonus", - ) - - logger.info(f"Beta feedback received from {user.email}, bonus: {bonus_credits}") - - return BetaFeedbackResponse( - status="success", - message="Thank you for your feedback!" + ( - f" You earned {bonus_credits} bonus credit for your detailed response!" - if bonus_credits > 0 else "" - ), - bonus_credits=bonus_credits, - ) - - -@router.get("/beta/feedback-form") -async def get_feedback_form_data( - authorization: Optional[str] = Header(None), - user_service: UserService = Depends(get_user_service), -): - """ - Get data needed to show the feedback form. - - Returns whether the user needs to submit feedback and any job context. - """ - if not authorization: - raise HTTPException(status_code=401, detail="Authentication required") - - token = authorization.replace("Bearer ", "") if authorization.startswith("Bearer ") else authorization - valid, user, message = user_service.validate_session(token) - - if not valid or not user: - raise HTTPException(status_code=401, detail=message) - - return { - "is_beta_tester": user.is_beta_tester, - "beta_status": user.beta_tester_status, - "needs_feedback": ( - user.is_beta_tester and - user.beta_tester_status == BetaTesterStatus.PENDING_FEEDBACK.value - ), - "can_submit_feedback": ( - user.is_beta_tester and - user.beta_tester_status != BetaTesterStatus.COMPLETED.value - ), - } - - # ============================================================================= # User Feedback-for-Credits Endpoints # ============================================================================= @@ -1475,8 +1181,6 @@ class UserDetailResponse(BaseModel): last_login_at: Optional[str] = None total_jobs_created: int = 0 total_jobs_completed: int = 0 - is_beta_tester: bool = False - beta_tester_status: Optional[str] = None credit_transactions: list[dict] = [] recent_jobs: list[dict] = [] active_sessions_count: int = 0 @@ -1639,8 +1343,6 @@ async def get_user_detail( last_login_at=user.last_login_at.isoformat() if user.last_login_at else None, total_jobs_created=user.total_jobs_created, total_jobs_completed=user.total_jobs_completed, - is_beta_tester=user.is_beta_tester, - beta_tester_status=user.beta_tester_status, credit_transactions=credit_transactions, recent_jobs=recent_jobs, active_sessions_count=active_sessions_count, @@ -1775,118 +1477,3 @@ async def set_user_role( return {"status": "success", "message": f"User {email} role set to {role.value}"} -@router.get("/admin/beta/feedback") -async def list_beta_feedback( - limit: int = 50, - auth_data: Tuple[str, UserType, int] = Depends(require_admin), - user_service: UserService = Depends(get_user_service), -): - """ - List all beta tester feedback (admin only). - """ - from google.cloud import firestore - - query = user_service.db.collection("beta_feedback") - query = query.order_by("created_at", direction=firestore.Query.DESCENDING) - query = query.limit(limit) - - docs = query.stream() - feedback_list = [doc.to_dict() for doc in docs] - - return { - "feedback": feedback_list, - "total": len(feedback_list), - } - - -@router.get("/admin/beta/stats") -async def get_beta_stats( - exclude_test: bool = True, - auth_data: Tuple[str, UserType, int] = Depends(require_admin), - user_service: UserService = Depends(get_user_service), -): - """ - Get beta tester program statistics (admin only). - - Args: - exclude_test: If True (default), exclude test users from beta stats - """ - from google.cloud.firestore_v1 import FieldFilter - from google.cloud.firestore_v1 import aggregation - - users_collection = user_service.db.collection(USERS_COLLECTION) - - if exclude_test: - # Stream and filter in Python since Firestore doesn't support "not ends with" - all_beta_users = [] - for doc in users_collection.where(filter=FieldFilter("is_beta_tester", "==", True)).stream(): - data = doc.to_dict() - if not is_test_email(data.get("email", "")): - all_beta_users.append(data) - - total_beta_testers = len(all_beta_users) - active_testers = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "active") - pending_feedback = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "pending_feedback") - completed_feedback = sum(1 for u in all_beta_users if u.get("beta_tester_status") == "completed") - - # Filter feedback by non-test users - all_feedback = [] - for doc in user_service.db.collection("beta_feedback").stream(): - data = doc.to_dict() - if not is_test_email(data.get("user_email", "")): - all_feedback.append(data) - feedback_docs = all_feedback - else: - # Use efficient aggregation queries when including test data - def get_count(query) -> int: - agg_query = aggregation.AggregationQuery(query) - agg_query.count(alias="count") - results = agg_query.get() - return results[0][0].value if results else 0 - - total_beta_testers = get_count( - users_collection.where(filter=FieldFilter("is_beta_tester", "==", True)) - ) - active_testers = get_count( - users_collection.where(filter=FieldFilter("beta_tester_status", "==", "active")) - ) - pending_feedback = get_count( - users_collection.where(filter=FieldFilter("beta_tester_status", "==", "pending_feedback")) - ) - completed_feedback = get_count( - users_collection.where(filter=FieldFilter("beta_tester_status", "==", "completed")) - ) - feedback_docs = [doc.to_dict() for doc in user_service.db.collection("beta_feedback").stream()] - - # Calculate average ratings from feedback - avg_overall = 0 - avg_ease = 0 - avg_accuracy = 0 - avg_correction = 0 - - if feedback_docs: - total = len(feedback_docs) - for data in feedback_docs: - avg_overall += data.get("overall_rating", 0) - avg_ease += data.get("ease_of_use_rating", 0) - avg_accuracy += data.get("lyrics_accuracy_rating", 0) - avg_correction += data.get("correction_experience_rating", 0) - - avg_overall = round(avg_overall / total, 2) - avg_ease = round(avg_ease / total, 2) - avg_accuracy = round(avg_accuracy / total, 2) - avg_correction = round(avg_correction / total, 2) - - return { - "total_beta_testers": total_beta_testers, - "active_testers": active_testers, - "pending_feedback": pending_feedback, - "completed_feedback": completed_feedback, - "total_feedback_submissions": len(feedback_docs), - "average_ratings": { - "overall": avg_overall, - "ease_of_use": avg_ease, - "lyrics_accuracy": avg_accuracy, - "correction_experience": avg_correction, - }, - } diff --git a/backend/config.py b/backend/config.py index 3cc881fa..caa5c906 100644 --- a/backend/config.py +++ b/backend/config.py @@ -128,10 +128,6 @@ class Settings(BaseSettings): enable_rate_limiting: bool = os.getenv("ENABLE_RATE_LIMITING", "true").lower() in ("true", "1", "yes") # Maximum jobs a user can create per day (0 = unlimited) rate_limit_jobs_per_day: int = int(os.getenv("RATE_LIMIT_JOBS_PER_DAY", "5")) - # Maximum YouTube uploads system-wide per day (0 = unlimited) - # DEPRECATED: Use youtube_quota_daily_limit instead. Kept for backward compatibility. - rate_limit_youtube_uploads_per_day: int = int(os.getenv("RATE_LIMIT_YOUTUBE_UPLOADS_PER_DAY", "10")) - # YouTube Data API v3 Quota Configuration # Daily quota limit (YouTube grants 10,000 units/day by default) youtube_quota_daily_limit: int = int(os.getenv("YOUTUBE_QUOTA_DAILY_LIMIT", "10000")) @@ -139,9 +135,6 @@ class Settings(BaseSettings): youtube_quota_upload_cost: int = int(os.getenv("YOUTUBE_QUOTA_UPLOAD_COST", "300")) # Safety margin to reserve (avoid hitting exact limit) youtube_quota_safety_margin: int = int(os.getenv("YOUTUBE_QUOTA_SAFETY_MARGIN", "500")) - # Maximum beta enrollments from same IP per day (0 = unlimited) - rate_limit_beta_ip_per_day: int = int(os.getenv("RATE_LIMIT_BETA_IP_PER_DAY", "1")) - # E2E test bypass key for rate limiting (set via secret in production) e2e_bypass_key: str = os.getenv("E2E_BYPASS_KEY", "") default_youtube_description: str = os.getenv( diff --git a/backend/models/user.py b/backend/models/user.py index bcac0c28..a55d503e 100644 --- a/backend/models/user.py +++ b/backend/models/user.py @@ -6,7 +6,6 @@ Supports: - Credit system for karaoke generation - Role-based access control (user/admin) - Stripe integration for payments -- Beta tester program with feedback collection """ from datetime import datetime, timezone from enum import Enum @@ -20,14 +19,6 @@ class UserRole(str, Enum): ADMIN = "admin" -class BetaTesterStatus(str, Enum): - """Status of beta tester participation.""" - ACTIVE = "active" # Enrolled, free credits available - PENDING_FEEDBACK = "pending_feedback" # Job completed, awaiting feedback - COMPLETED = "completed" # Feedback submitted - EXPIRED = "expired" # 24hr deadline passed without feedback - - class CreditTransaction(BaseModel): """Record of a credit transaction.""" id: str @@ -92,14 +83,6 @@ class User(BaseModel): # Optional profile fields for future use display_name: Optional[str] = None - # Beta tester program - is_beta_tester: bool = False - beta_tester_status: Optional[BetaTesterStatus] = None - beta_enrolled_at: Optional[datetime] = None - beta_promise_text: Optional[str] = None # User's promise statement - beta_feedback_due_at: Optional[datetime] = None # 24hr after job completion - beta_feedback_email_sent: bool = False - # Push notification subscriptions (Web Push API) # Users can subscribe from multiple devices/browsers push_subscriptions: List[PushSubscription] = Field(default_factory=list) @@ -214,87 +197,6 @@ class UserListResponse(BaseModel): total: int -# ============================================================================ -# Beta Tester Program Models -# ============================================================================ - -class BetaTesterFeedback(BaseModel): - """ - Feedback from a beta tester after using the service. - - Stored in Firestore 'beta_feedback' collection. - """ - id: str # Unique feedback ID - user_email: str - job_id: Optional[str] = None # The job they're providing feedback on - - # Ratings (1-5 scale) - overall_rating: int = Field(ge=1, le=5) - ease_of_use_rating: int = Field(ge=1, le=5) - lyrics_accuracy_rating: int = Field(ge=1, le=5) - correction_experience_rating: int = Field(ge=1, le=5) - - # Open-ended feedback - what_went_well: Optional[str] = None - what_could_improve: Optional[str] = None - additional_comments: Optional[str] = None - - # Would they recommend / use again? - would_recommend: bool = True - would_use_again: bool = True - - # Metadata - created_at: datetime = Field(default_factory=datetime.utcnow) - submitted_via: str = "web" # 'web' or 'email' - - -class BetaTesterEnrollRequest(BaseModel): - """Request to enroll as a beta tester.""" - email: str - promise_text: str = Field( - min_length=10, - description="User's promise to provide feedback (min 10 chars)" - ) - accept_corrections_work: bool = Field( - description="User accepts they may need to review/correct lyrics" - ) - - -class BetaTesterEnrollResponse(BaseModel): - """Response after enrolling as beta tester.""" - status: str - message: str - credits_granted: int - session_token: Optional[str] = None # If new user, provide session - - -class BetaFeedbackRequest(BaseModel): - """Request to submit beta tester feedback.""" - job_id: Optional[str] = None - - # Ratings (1-5 scale) - overall_rating: int = Field(ge=1, le=5) - ease_of_use_rating: int = Field(ge=1, le=5) - lyrics_accuracy_rating: int = Field(ge=1, le=5) - correction_experience_rating: int = Field(ge=1, le=5) - - # Open-ended feedback - what_went_well: Optional[str] = None - what_could_improve: Optional[str] = None - additional_comments: Optional[str] = None - - # Would they recommend / use again? - would_recommend: bool = True - would_use_again: bool = True - - -class BetaFeedbackResponse(BaseModel): - """Response after submitting feedback.""" - status: str - message: str - bonus_credits: int = 0 # Bonus credits for great feedback - - # ============================================================================ # User Feedback-for-Credits Models # ============================================================================ diff --git a/backend/services/email_service.py b/backend/services/email_service.py index 76478f80..a4c80696 100644 --- a/backend/services/email_service.py +++ b/backend/services/email_service.py @@ -529,204 +529,6 @@ Complete 2 videos and earn 2 more free credits by sharing your feedback! Get started: {self.frontend_url} ---- -© {self._get_year()} Nomad Karaoke -""" - - return self.provider.send_email(email, subject, html_content, text_content) - - def send_beta_welcome_email(self, email: str, credits: int = 1) -> bool: - """ - Send welcome email to new beta testers. - - Args: - email: User's email address - credits: Initial free credits granted - - Returns: - True if email was sent successfully - """ - subject = "Welcome Beta Tester! Free Karaoke Credits Inside 🎤" - - extra_styles = f""" - .beta-badge {{ - display: inline-block; - background: linear-gradient(135deg, {self.BRAND_PRIMARY}, {self.BRAND_SECONDARY}); - color: #333; - padding: 4px 12px; - border-radius: 20px; - font-size: 12px; - font-weight: bold; - margin-top: 10px; - }} - .credits-box {{ - background-color: #ecfdf5; - border: 2px solid {self.BRAND_SUCCESS}; - border-radius: 12px; - padding: 24px; - text-align: center; - margin: 20px 0; - }} - .credits-number {{ - font-size: 48px; - font-weight: bold; - color: {self.BRAND_SUCCESS}; - }} - .reminder {{ - background-color: #fef3c7; - border: 1px solid #fcd34d; - border-radius: 8px; - padding: 16px; - margin: 20px 0; - }} -""" - - content = f""" -
Thank you for joining our beta program!
- -As promised, here's your free credit to create a karaoke video:
- -Thanks for helping us make Nomad Karaoke better!
-""" - - extra_header = 'Hi there!
- -Hope you enjoyed creating your karaoke video{job_context}! As a beta tester, your feedback is super valuable to us.
- -How was your experience?
-Quick 2-minute survey - we'd love to hear your thoughts!
-Specifically, we'd love to know:
-Thanks for being part of making Nomad Karaoke better!
-""" - - html_content = self._build_email_html(content, extra_styles) - - text_content = f""" -Quick feedback on your karaoke experience? - -Hi there! - -Hope you enjoyed creating your karaoke video{' for ' + safe_job_title if safe_job_title else ''}! - -As a beta tester, your feedback is super valuable to us. - -Share your feedback (2-minute survey): {feedback_url} - -We'd love to know: -- How easy was it to use? -- Were the lyrics accurate? -- How was the correction experience? -- What could we improve? - -Thanks for being part of making Nomad Karaoke better! - --- © {self._get_year()} Nomad Karaoke """ diff --git a/backend/services/email_validation_service.py b/backend/services/email_validation_service.py index 30f7d03a..3fb56a58 100644 --- a/backend/services/email_validation_service.py +++ b/backend/services/email_validation_service.py @@ -2,7 +2,7 @@ Email Validation Service. Provides email validation, normalization, and blocklist checking -to prevent abuse during beta enrollment and other flows. +to prevent abuse during enrollment and other flows. """ import hashlib @@ -369,36 +369,6 @@ class EmailValidationService: config = self.get_blocklist_config() return ip_address in config["blocked_ips"] - def validate_email_for_beta(self, email: str) -> Tuple[bool, str]: - """ - Validate an email for beta enrollment. - - Performs all validation checks: - 1. Basic format validation - 2. Disposable domain check - 3. Blocked email check - - Args: - email: Email address to validate - - Returns: - Tuple of (is_valid, error_message) - """ - if not email or "@" not in email: - return False, "Invalid email format" - - # Check disposable domain - if self.is_disposable_domain(email): - logger.warning(f"Beta enrollment blocked - disposable domain: {_mask_email(email)}") - return False, "Disposable email addresses are not allowed" - - # Check blocked email - if self.is_email_blocked(email): - logger.warning(f"Beta enrollment blocked - email blocked: {_mask_email(email)}") - return False, "This email address is not allowed" - - return True, "" - def hash_ip(self, ip_address: str) -> str: """ Hash an IP address for privacy-preserving storage. diff --git a/backend/services/rate_limit_service.py b/backend/services/rate_limit_service.py index 6a46ecad..d4f5a316 100644 --- a/backend/services/rate_limit_service.py +++ b/backend/services/rate_limit_service.py @@ -1,10 +1,9 @@ """ -Rate limiting service for job creation, YouTube uploads, and beta enrollment. +Rate limiting service for job creation. Uses Firestore for distributed rate limit tracking with date-based document IDs that automatically reset at UTC midnight. """ -import hashlib import logging from datetime import datetime, timezone from typing import Tuple, Optional, List, Dict, Any @@ -18,7 +17,6 @@ logger = logging.getLogger(__name__) # Firestore collection names RATE_LIMITS_COLLECTION = "rate_limits" -BLOCKLISTS_COLLECTION = "blocklists" OVERRIDES_COLLECTION = "overrides" @@ -37,11 +35,6 @@ def _seconds_until_midnight_utc() -> int: return int((next_midnight - now).total_seconds()) -def _hash_ip(ip_address: str) -> str: - """Hash an IP address for storage (privacy-preserving).""" - return hashlib.sha256(ip_address.encode()).hexdigest()[:16] - - class RateLimitService: """ Service for managing rate limits across the platform. @@ -56,9 +49,6 @@ class RateLimitService: self.db = firestore.Client(project=settings.google_cloud_project) else: self.db = db - self._blocklist_cache: Optional[Dict[str, Any]] = None - self._blocklist_cache_time: Optional[datetime] = None - self._cache_ttl_seconds = 300 # 5 minute cache # ------------------------------------------------------------------------- # Job Rate Limiting @@ -163,195 +153,6 @@ class RateLimitService: update_in_transaction(transaction, doc_ref) logger.info(f"Recorded job {job_id} for user {user_email} rate limiting") - # ------------------------------------------------------------------------- - # YouTube Upload Rate Limiting (DEPRECATED - use YouTubeQuotaService) - # ------------------------------------------------------------------------- - - def check_youtube_upload_limit(self) -> Tuple[bool, int, str]: - """ - Check if the system can perform a YouTube upload. - - DEPRECATED: Use YouTubeQuotaService.check_quota_available() instead. - This method now delegates to the quota service for backward compatibility. - - Returns: - Tuple of (allowed, remaining, message) - """ - try: - from backend.services.youtube_quota_service import get_youtube_quota_service - quota_service = get_youtube_quota_service() - except (ImportError, Exception) as e: - logger.warning(f"Quota service unavailable, falling back to legacy check: {e}") - # Fallback to legacy behavior below - quota_service = None - - if quota_service is not None: - try: - return quota_service.check_quota_available() - except Exception as e: - logger.error(f"Quota service check failed: {e}") - return False, 0, f"Quota service error: {e}" - - # Fallback to legacy behavior - if not settings.enable_rate_limiting: - return True, -1, "Rate limiting disabled" - limit = settings.rate_limit_youtube_uploads_per_day - if limit == 0: - return True, -1, "No YouTube upload limit configured" - current_count = self.get_youtube_uploads_today() - remaining = max(0, limit - current_count) - if current_count >= limit: - return False, 0, f"Daily YouTube upload limit reached ({limit} per day)" - return True, remaining, f"{remaining} YouTube uploads remaining today" - - def get_youtube_uploads_today(self) -> int: - """Get the number of YouTube uploads performed today (system-wide).""" - date_str = _get_today_date_str() - doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document( - f"youtube_uploads_{date_str}" - ) - doc = doc_ref.get() - - if not doc.exists: - return 0 - - return doc.to_dict().get("count", 0) - - def record_youtube_upload(self, job_id: str, user_email: str) -> None: - """ - Record a YouTube upload for rate limiting. - - DEPRECATED: Use YouTubeQuotaService.record_operation() instead. - This method still records to the legacy collection for backward compatibility. - """ - if not settings.enable_rate_limiting: - return - - date_str = _get_today_date_str() - doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document( - f"youtube_uploads_{date_str}" - ) - - @firestore.transactional - def update_in_transaction(transaction, doc_ref): - doc = doc_ref.get(transaction=transaction) - if doc.exists: - data = doc.to_dict() - count = data.get("count", 0) + 1 - uploads = data.get("uploads", []) - else: - count = 1 - uploads = [] - - uploads.append({ - "job_id": job_id, - "user_email": user_email, - "timestamp": datetime.now(timezone.utc), - }) - - transaction.set(doc_ref, { - "date": date_str, - "count": count, - "uploads": uploads, - "updated_at": datetime.now(timezone.utc), - }) - - transaction = self.db.transaction() - update_in_transaction(transaction, doc_ref) - logger.info(f"Recorded YouTube upload for job {job_id}") - - # ------------------------------------------------------------------------- - # Beta Enrollment IP Rate Limiting - # ------------------------------------------------------------------------- - - def check_beta_ip_limit(self, ip_address: str) -> Tuple[bool, int, str]: - """ - Check if an IP address can enroll in the beta program. - - Args: - ip_address: Client IP address - - Returns: - Tuple of (allowed, remaining, message) - """ - if not settings.enable_rate_limiting: - return True, -1, "Rate limiting disabled" - - limit = settings.rate_limit_beta_ip_per_day - if limit == 0: - return True, -1, "No beta IP limit configured" - - # Check today's enrollment count for this IP - ip_hash = _hash_ip(ip_address) - date_str = _get_today_date_str() - - doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document( - f"beta_ip_{ip_hash}_{date_str}" - ) - doc = doc_ref.get() - - current_count = 0 - if doc.exists: - current_count = doc.to_dict().get("count", 0) - - remaining = max(0, limit - current_count) - - if current_count >= limit: - seconds = _seconds_until_midnight_utc() - logger.warning( - f"Beta enrollment IP limit exceeded for {ip_hash}: {current_count}/{limit} enrollments today" - ) - return ( - False, - 0, - f"Too many beta enrollments from this network today. Please try again tomorrow." - ) - - return True, remaining, f"{remaining} beta enrollments remaining from this IP today" - - def record_beta_enrollment(self, ip_address: str, email: str) -> None: - """ - Record a beta enrollment for IP rate limiting. - - Uses Firestore transactions for atomic increment. - """ - if not settings.enable_rate_limiting: - return - - ip_hash = _hash_ip(ip_address) - date_str = _get_today_date_str() - doc_ref = self.db.collection(RATE_LIMITS_COLLECTION).document( - f"beta_ip_{ip_hash}_{date_str}" - ) - - @firestore.transactional - def update_in_transaction(transaction, doc_ref): - doc = doc_ref.get(transaction=transaction) - if doc.exists: - data = doc.to_dict() - count = data.get("count", 0) + 1 - enrollments = data.get("enrollments", []) - else: - count = 1 - enrollments = [] - - enrollments.append({ - "email": email, - "timestamp": datetime.now(timezone.utc), - }) - - transaction.set(doc_ref, { - "ip_hash": ip_hash, - "date": date_str, - "count": count, - "enrollments": enrollments, - "updated_at": datetime.now(timezone.utc), - }) - - transaction = self.db.transaction() - update_in_transaction(transaction, doc_ref) - logger.info(f"Recorded beta enrollment from IP {ip_hash} for {email}") - # ------------------------------------------------------------------------- # User Overrides (Whitelist) # ------------------------------------------------------------------------- @@ -422,175 +223,6 @@ class RateLimitService: docs = self.db.collection(OVERRIDES_COLLECTION).stream() return {doc.id: doc.to_dict() for doc in docs} - # ------------------------------------------------------------------------- - # Blocklist Management - # ------------------------------------------------------------------------- - - def _load_blocklist(self, force_refresh: bool = False) -> Dict[str, Any]: - """ - Load blocklist from Firestore with caching. - - Returns: - Dict with keys: disposable_domains, blocked_emails, blocked_ips - """ - now = datetime.now(timezone.utc) - - # Check cache - if not force_refresh and self._blocklist_cache and self._blocklist_cache_time: - cache_age = (now - self._blocklist_cache_time).total_seconds() - if cache_age < self._cache_ttl_seconds: - return self._blocklist_cache - - # Load from Firestore - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc = doc_ref.get() - - if not doc.exists: - # Initialize with empty blocklist - self._blocklist_cache = { - "disposable_domains": [], - "blocked_emails": [], - "blocked_ips": [], - } - else: - self._blocklist_cache = doc.to_dict() - - self._blocklist_cache_time = now - return self._blocklist_cache - - def is_disposable_domain(self, domain: str) -> bool: - """Check if a domain is in the disposable domains blocklist.""" - blocklist = self._load_blocklist() - return domain.lower() in [d.lower() for d in blocklist.get("disposable_domains", [])] - - def is_blocked_email(self, email: str) -> bool: - """Check if an email is explicitly blocked.""" - blocklist = self._load_blocklist() - return email.lower() in [e.lower() for e in blocklist.get("blocked_emails", [])] - - def is_blocked_ip(self, ip_address: str) -> bool: - """Check if an IP address is blocked.""" - blocklist = self._load_blocklist() - return ip_address in blocklist.get("blocked_ips", []) - - def get_blocklist(self) -> Dict[str, Any]: - """Get all blocklist data.""" - return self._load_blocklist(force_refresh=True) - - def add_disposable_domain(self, domain: str, admin_email: str) -> None: - """Add a domain to the disposable domains blocklist.""" - blocklist = self._load_blocklist(force_refresh=True) - domains = set(blocklist.get("disposable_domains", [])) - domains.add(domain.lower()) - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "disposable_domains": list(domains), - "updated_at": datetime.now(timezone.utc), - "updated_by": admin_email, - }, merge=True) - - self._blocklist_cache = None # Invalidate cache - logger.info(f"Added disposable domain {domain} by {admin_email}") - - def remove_disposable_domain(self, domain: str) -> bool: - """Remove a domain from the disposable domains blocklist.""" - blocklist = self._load_blocklist(force_refresh=True) - domains = set(blocklist.get("disposable_domains", [])) - - if domain.lower() not in [d.lower() for d in domains]: - return False - - domains = {d for d in domains if d.lower() != domain.lower()} - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "disposable_domains": list(domains), - "updated_at": datetime.now(timezone.utc), - }, merge=True) - - self._blocklist_cache = None - logger.info(f"Removed disposable domain {domain}") - return True - - def add_blocked_email(self, email: str, admin_email: str) -> None: - """Add an email to the blocked emails list.""" - blocklist = self._load_blocklist(force_refresh=True) - emails = set(blocklist.get("blocked_emails", [])) - emails.add(email.lower()) - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "blocked_emails": list(emails), - "updated_at": datetime.now(timezone.utc), - "updated_by": admin_email, - }, merge=True) - - self._blocklist_cache = None - logger.info(f"Added blocked email {email} by {admin_email}") - - def remove_blocked_email(self, email: str) -> bool: - """Remove an email from the blocked emails list.""" - blocklist = self._load_blocklist(force_refresh=True) - emails = set(blocklist.get("blocked_emails", [])) - - if email.lower() not in [e.lower() for e in emails]: - return False - - emails = {e for e in emails if e.lower() != email.lower()} - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "blocked_emails": list(emails), - "updated_at": datetime.now(timezone.utc), - }, merge=True) - - self._blocklist_cache = None - logger.info(f"Removed blocked email {email}") - return True - - def add_blocked_ip(self, ip_address: str, admin_email: str) -> None: - """Add an IP address to the blocked IPs list.""" - blocklist = self._load_blocklist(force_refresh=True) - ips = set(blocklist.get("blocked_ips", [])) - ips.add(ip_address) - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "blocked_ips": list(ips), - "updated_at": datetime.now(timezone.utc), - "updated_by": admin_email, - }, merge=True) - - self._blocklist_cache = None - logger.info(f"Added blocked IP {ip_address} by {admin_email}") - - def remove_blocked_ip(self, ip_address: str) -> bool: - """Remove an IP address from the blocked IPs list.""" - blocklist = self._load_blocklist(force_refresh=True) - ips = set(blocklist.get("blocked_ips", [])) - - if ip_address not in ips: - return False - - ips.discard(ip_address) - - doc_ref = self.db.collection(BLOCKLISTS_COLLECTION).document("config") - doc_ref.set({ - **blocklist, - "blocked_ips": list(ips), - "updated_at": datetime.now(timezone.utc), - }, merge=True) - - self._blocklist_cache = None - logger.info(f"Removed blocked IP {ip_address}") - return True - # ------------------------------------------------------------------------- # Stats # ------------------------------------------------------------------------- @@ -604,9 +236,6 @@ class RateLimitService: """ date_str = _get_today_date_str() - # Get YouTube upload count - youtube_count = self.get_youtube_uploads_today() - # Count unique users with jobs today users_with_jobs = set() total_jobs_today = 0 @@ -624,12 +253,9 @@ class RateLimitService: return { "date": date_str, - "youtube_uploads_today": youtube_count, - "youtube_uploads_limit": settings.rate_limit_youtube_uploads_per_day, "total_jobs_today": total_jobs_today, "users_with_jobs_today": len(users_with_jobs), "job_limit_per_user": settings.rate_limit_jobs_per_day, - "beta_ip_limit_per_day": settings.rate_limit_beta_ip_per_day, "rate_limiting_enabled": settings.enable_rate_limiting, "seconds_until_reset": _seconds_until_midnight_utc(), } diff --git a/backend/services/youtube_quota_service.py b/backend/services/youtube_quota_service.py index 01c0a20c..fd7e4563 100644 --- a/backend/services/youtube_quota_service.py +++ b/backend/services/youtube_quota_service.py @@ -12,7 +12,8 @@ that reflects actual API quota costs: - videos.delete: 50 units """ import logging -from datetime import datetime +import time +from datetime import datetime, timedelta from typing import Tuple, Optional, Dict, Any from zoneinfo import ZoneInfo @@ -23,6 +24,11 @@ from backend.config import settings logger = logging.getLogger(__name__) +# GCP quota cache (module-level for singleton behavior) +_gcp_quota_cache: Optional[Dict[str, Any]] = None +_gcp_quota_cache_time: float = 0 +_GCP_CACHE_TTL_SECONDS = 300 # 5 minutes + YOUTUBE_QUOTA_COLLECTION = "youtube_quota" PACIFIC_TZ = ZoneInfo("America/Los_Angeles") @@ -190,12 +196,17 @@ class YouTubeQuotaService: "estimated_uploads_remaining": effective_limit // settings.youtube_quota_upload_cost if settings.youtube_quota_upload_cost > 0 else -1, "seconds_until_reset": _seconds_until_midnight_pt(), "operations_count": 0, + "upload_count": 0, } data = doc.to_dict() consumed = data.get("units_consumed", 0) remaining = max(0, effective_limit - consumed) + # Count videos.insert operations specifically (actual uploads) + operations = data.get("operations", []) + upload_count = sum(1 for op in operations if op.get("operation") == "videos.insert") + return { "date_pt": date_str, "units_consumed": consumed, @@ -205,9 +216,110 @@ class YouTubeQuotaService: "upload_cost": settings.youtube_quota_upload_cost, "estimated_uploads_remaining": remaining // settings.youtube_quota_upload_cost if settings.youtube_quota_upload_cost > 0 else -1, "seconds_until_reset": _seconds_until_midnight_pt(), - "operations_count": len(data.get("operations", [])), + "operations_count": len(operations), + "upload_count": upload_count, } + def get_gcp_quota_usage(self) -> Dict[str, Any]: + """ + Query GCP Cloud Monitoring for actual YouTube API quota usage. + + Uses the metric serviceruntime.googleapis.com/quota/rate/net_usage + for youtube.googleapis.com. Results are cached for 5 minutes. + + Important: GCP metrics have ~1-2 hour delay, so they cannot replace + Firestore tracking for real-time gating. They serve as source-of-truth + for dashboard display and drift detection. + + Returns: + Dict with gcp_units_consumed, gcp_last_datapoint_time, + gcp_data_delay_minutes, available flag, and drift info. + """ + global _gcp_quota_cache, _gcp_quota_cache_time + + # Check cache + now = time.time() + if _gcp_quota_cache is not None and (now - _gcp_quota_cache_time) < _GCP_CACHE_TTL_SECONDS: + return _gcp_quota_cache + + try: + from google.cloud import monitoring_v3 + + client = monitoring_v3.MetricServiceClient() + project_name = f"projects/{settings.google_cloud_project}" + + # Query for today's YouTube quota usage (PT date range) + now_dt = datetime.now(PACIFIC_TZ) + start_of_day = now_dt.replace(hour=0, minute=0, second=0, microsecond=0) + + interval = monitoring_v3.TimeInterval( + start_time=start_of_day, + end_time=now_dt, + ) + + # Query the quota usage metric for YouTube Data API + results = client.list_time_series( + request={ + "name": project_name, + "filter": ( + 'metric.type = "serviceruntime.googleapis.com/quota/rate/net_usage" ' + 'AND resource.labels.service = "youtube.googleapis.com"' + ), + "interval": interval, + "view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL, + "aggregation": monitoring_v3.Aggregation( + alignment_period={"seconds": 86400}, # Aggregate full day + per_series_aligner=monitoring_v3.Aggregation.Aligner.ALIGN_SUM, + ), + } + ) + + total_units = 0 + last_datapoint_time = None + + for ts in results: + for point in ts.points: + total_units += int(point.value.int64_value) + point_time = point.interval.end_time + if last_datapoint_time is None or point_time > last_datapoint_time: + last_datapoint_time = point_time + + # Calculate data delay + delay_minutes = None + last_datapoint_str = None + if last_datapoint_time: + delay = now_dt - last_datapoint_time.astimezone(PACIFIC_TZ) + delay_minutes = int(delay.total_seconds() / 60) + last_datapoint_str = last_datapoint_time.isoformat() + + # Calculate drift against Firestore tracking + firestore_consumed = self._get_units_consumed_today() + drift = abs(total_units - firestore_consumed) if total_units > 0 else None + drift_alert = False + if drift is not None and firestore_consumed > 0: + drift_pct = drift / firestore_consumed + drift_alert = drift_pct > 0.10 # Alert if >10% drift + + result = { + "available": True, + "gcp_units_consumed": total_units, + "gcp_last_datapoint_time": last_datapoint_str, + "gcp_data_delay_minutes": delay_minutes, + "drift": drift, + "drift_alert": drift_alert, + } + + _gcp_quota_cache = result + _gcp_quota_cache_time = now + return result + + except ImportError: + logger.debug("google-cloud-monitoring not installed, GCP quota unavailable") + return {"available": False} + except Exception as e: + logger.warning(f"GCP quota query failed: {e}") + return {"available": False} + def _get_units_consumed_today(self) -> int: """Get total quota units consumed today (Pacific Time).""" date_str = _get_today_date_pt() diff --git a/backend/tests/test_email_validation_service.py b/backend/tests/test_email_validation_service.py index 9d6f1a7f..4ff2f7d7 100644 --- a/backend/tests/test_email_validation_service.py +++ b/backend/tests/test_email_validation_service.py @@ -209,62 +209,6 @@ class TestBlocklistManagement: assert mock_db.collection.return_value.document.return_value.get.call_count == 1 -class TestBetaEnrollmentValidation: - """Test beta enrollment validation.""" - - @pytest.fixture - def mock_db(self): - """Create a mock Firestore client.""" - mock = MagicMock() - mock_doc = Mock() - mock_doc.exists = True - mock_doc.to_dict.return_value = { - "disposable_domains": [], - "blocked_emails": ["blocked@example.com"], - "blocked_ips": [], - } - mock.collection.return_value.document.return_value.get.return_value = mock_doc - return mock - - @pytest.fixture - def email_service(self, mock_db): - """Create EmailValidationService instance with mocks.""" - from backend.services.email_validation_service import EmailValidationService - EmailValidationService._blocklist_cache = None - EmailValidationService._blocklist_cache_time = None - service = EmailValidationService(db=mock_db) - return service - - def test_validate_legitimate_email(self, email_service): - """Test legitimate email passes validation.""" - is_valid, error = email_service.validate_email_for_beta("user@gmail.com") - assert is_valid is True - assert error == "" - - def test_validate_disposable_email_rejected(self, email_service): - """Test disposable email is rejected.""" - is_valid, error = email_service.validate_email_for_beta("user@tempmail.com") - assert is_valid is False - assert "Disposable email" in error - - def test_validate_blocked_email_rejected(self, email_service): - """Test blocked email is rejected.""" - is_valid, error = email_service.validate_email_for_beta("blocked@example.com") - assert is_valid is False - assert "not allowed" in error - - def test_validate_invalid_format_rejected(self, email_service): - """Test invalid email format is rejected.""" - is_valid, error = email_service.validate_email_for_beta("notanemail") - assert is_valid is False - assert "Invalid email format" in error - - def test_validate_empty_email_rejected(self, email_service): - """Test empty email is rejected.""" - is_valid, error = email_service.validate_email_for_beta("") - assert is_valid is False - - class TestIPHashing: """Test IP address hashing.""" diff --git a/backend/tests/test_gcp_quota_monitor.py b/backend/tests/test_gcp_quota_monitor.py new file mode 100644 index 00000000..962eaaf1 --- /dev/null +++ b/backend/tests/test_gcp_quota_monitor.py @@ -0,0 +1,365 @@ +""" +Unit tests for YouTubeQuotaService.get_gcp_quota_usage(). + +Tests GCP Cloud Monitoring integration for quota drift detection, +caching behavior, and graceful fallbacks. +""" + +import pytest +import time +from unittest.mock import Mock, MagicMock, patch +from datetime import datetime +from zoneinfo import ZoneInfo + +# Mock Google Cloud before imports +import sys +sys.modules.setdefault('google.cloud.firestore', MagicMock()) +sys.modules.setdefault('google.cloud.storage', MagicMock()) + + +PACIFIC_TZ = ZoneInfo("America/Los_Angeles") + + +class TestGCPQuotaMonitor: + """Test get_gcp_quota_usage() on YouTubeQuotaService.""" + + @pytest.fixture + def mock_db(self): + """Create a mock Firestore client.""" + mock = MagicMock() + return mock + + @pytest.fixture + def mock_settings(self): + """Create mock settings.""" + settings = Mock() + settings.google_cloud_project = "test-project" + settings.youtube_quota_daily_limit = 10000 + settings.youtube_quota_upload_cost = 300 + settings.youtube_quota_safety_margin = 500 + return settings + + @pytest.fixture(autouse=True) + def clear_gcp_cache(self): + """Clear the module-level GCP quota cache before each test.""" + import backend.services.youtube_quota_service as mod + mod._gcp_quota_cache = None + mod._gcp_quota_cache_time = 0 + yield + # Also clear after test to avoid leaking state + mod._gcp_quota_cache = None + mod._gcp_quota_cache_time = 0 + + @pytest.fixture + def quota_service(self, mock_db, mock_settings): + """Create YouTubeQuotaService instance with mocks.""" + with patch('backend.services.youtube_quota_service.settings', mock_settings): + from backend.services.youtube_quota_service import YouTubeQuotaService + service = YouTubeQuotaService(db=mock_db) + return service + + def _make_mock_point(self, value, end_time): + """Create a mock Cloud Monitoring data point.""" + point = Mock() + point.value.int64_value = value + point.interval.end_time = end_time + return point + + def _make_mock_time_series(self, points): + """Create a mock time series with given points.""" + ts = Mock() + ts.points = points + return ts + + # ========================================================================= + # Successful Query Tests + # ========================================================================= + + def test_successful_query_returns_units_consumed(self, quota_service, mock_db, mock_settings): + """Test successful GCP monitoring query returns units and drift info.""" + # Mock Firestore for _get_units_consumed_today (for drift calc) + mock_doc = Mock() + mock_doc.exists = True + mock_doc.to_dict.return_value = {"units_consumed": 800} + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + # Mock the Cloud Monitoring client + now_pt = datetime.now(PACIFIC_TZ) + mock_point = self._make_mock_point(850, now_pt) + mock_ts = self._make_mock_time_series([mock_point]) + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [mock_ts] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + # The method does `from google.cloud import monitoring_v3` locally, + # so we inject the mock into sys.modules for that import to resolve. + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["gcp_units_consumed"] == 850 + assert result["gcp_last_datapoint_time"] is not None + # Drift = abs(850 - 800) = 50 + assert result["drift"] == 50 + assert result["drift_alert"] is False # 50/800 = 6.25% < 10% + + def test_successful_query_multiple_time_series(self, quota_service, mock_db, mock_settings): + """Test query with multiple time series sums all values.""" + mock_doc = Mock() + mock_doc.exists = True + mock_doc.to_dict.return_value = {"units_consumed": 500} + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + now_pt = datetime.now(PACIFIC_TZ) + mock_point1 = self._make_mock_point(200, now_pt) + mock_point2 = self._make_mock_point(300, now_pt) + mock_ts1 = self._make_mock_time_series([mock_point1]) + mock_ts2 = self._make_mock_time_series([mock_point2]) + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [mock_ts1, mock_ts2] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["gcp_units_consumed"] == 500 # 200 + 300 + + def test_successful_query_no_data_points(self, quota_service, mock_db, mock_settings): + """Test query that returns no data points (e.g., start of day).""" + mock_doc = Mock() + mock_doc.exists = False + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["gcp_units_consumed"] == 0 + assert result["gcp_last_datapoint_time"] is None + assert result["drift"] is None # No GCP data to compare + + # ========================================================================= + # Caching Tests + # ========================================================================= + + def test_caching_returns_cached_result(self, quota_service, mock_db, mock_settings): + """Test that second call within 5 min returns cached result.""" + mock_doc = Mock() + mock_doc.exists = False + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + # First call - should hit the monitoring API + result1 = quota_service.get_gcp_quota_usage() + assert mock_monitoring_client.list_time_series.call_count == 1 + + # Second call - should use cache + result2 = quota_service.get_gcp_quota_usage() + assert mock_monitoring_client.list_time_series.call_count == 1 # Not called again + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result1 == result2 + + def test_cache_expires_after_ttl(self, quota_service, mock_db, mock_settings): + """Test that cache expires and re-queries after TTL.""" + import backend.services.youtube_quota_service as mod + + mock_doc = Mock() + mock_doc.exists = False + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + # First call + quota_service.get_gcp_quota_usage() + assert mock_monitoring_client.list_time_series.call_count == 1 + + # Simulate cache expiry by backdating the cache time + mod._gcp_quota_cache_time = time.time() - 301 # 301 seconds ago (TTL is 300) + + # Second call - should re-query + quota_service.get_gcp_quota_usage() + assert mock_monitoring_client.list_time_series.call_count == 2 + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + # ========================================================================= + # ImportError Fallback Tests + # ========================================================================= + + def test_import_error_fallback(self, quota_service, mock_settings): + """Test graceful fallback when google-cloud-monitoring is not installed.""" + # Remove the module from sys.modules so the import fails + saved = sys.modules.pop('google.cloud.monitoring_v3', None) + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + # Patch the import to raise ImportError + original_import = __builtins__.__import__ if hasattr(__builtins__, '__import__') else __import__ + + def mock_import(name, *args, **kwargs): + if 'monitoring_v3' in name: + raise ImportError("No module named 'google.cloud.monitoring_v3'") + return original_import(name, *args, **kwargs) + + with patch('builtins.__import__', side_effect=mock_import): + result = quota_service.get_gcp_quota_usage() + + assert result == {"available": False} + + # Restore if it was there + if saved is not None: + sys.modules['google.cloud.monitoring_v3'] = saved + + # ========================================================================= + # General Exception Fallback Tests + # ========================================================================= + + def test_general_exception_fallback(self, quota_service, mock_settings): + """Test graceful fallback when Cloud Monitoring query raises an error.""" + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.side_effect = Exception("API quota exceeded") + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result == {"available": False} + + # ========================================================================= + # Drift Alert Tests + # ========================================================================= + + def test_drift_alert_when_drift_exceeds_threshold(self, quota_service, mock_db, mock_settings): + """Test drift alert is triggered when drift > 10% of Firestore value.""" + # Firestore says 1000 units consumed + mock_doc = Mock() + mock_doc.exists = True + mock_doc.to_dict.return_value = {"units_consumed": 1000} + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + # GCP says 1200 units consumed (200 drift = 20% > 10%) + now_pt = datetime.now(PACIFIC_TZ) + mock_point = self._make_mock_point(1200, now_pt) + mock_ts = self._make_mock_time_series([mock_point]) + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [mock_ts] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["drift"] == 200 # abs(1200 - 1000) + assert result["drift_alert"] is True # 200/1000 = 20% > 10% + + def test_no_drift_alert_within_threshold(self, quota_service, mock_db, mock_settings): + """Test no drift alert when drift <= 10% of Firestore value.""" + # Firestore says 1000 units consumed + mock_doc = Mock() + mock_doc.exists = True + mock_doc.to_dict.return_value = {"units_consumed": 1000} + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + # GCP says 1050 units consumed (50 drift = 5% <= 10%) + now_pt = datetime.now(PACIFIC_TZ) + mock_point = self._make_mock_point(1050, now_pt) + mock_ts = self._make_mock_time_series([mock_point]) + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [mock_ts] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["drift"] == 50 + assert result["drift_alert"] is False + + def test_no_drift_when_firestore_zero(self, quota_service, mock_db, mock_settings): + """Test drift_alert is False when Firestore has zero usage (avoid division by zero).""" + mock_doc = Mock() + mock_doc.exists = False + mock_db.collection.return_value.document.return_value.get.return_value = mock_doc + + now_pt = datetime.now(PACIFIC_TZ) + mock_point = self._make_mock_point(100, now_pt) + mock_ts = self._make_mock_time_series([mock_point]) + + mock_monitoring_client = Mock() + mock_monitoring_client.list_time_series.return_value = [mock_ts] + + mock_monitoring_v3 = MagicMock() + mock_monitoring_v3.MetricServiceClient.return_value = mock_monitoring_client + + with patch('backend.services.youtube_quota_service.settings', mock_settings): + sys.modules['google.cloud.monitoring_v3'] = mock_monitoring_v3 + try: + result = quota_service.get_gcp_quota_usage() + finally: + sys.modules.pop('google.cloud.monitoring_v3', None) + + assert result["available"] is True + assert result["gcp_units_consumed"] == 100 + # Firestore consumed is 0, so drift_alert should be False (no division by zero) + assert result["drift_alert"] is False diff --git a/backend/tests/test_rate_limit_service.py b/backend/tests/test_rate_limit_service.py index 6bb7e7c8..cf0b6420 100644 --- a/backend/tests/test_rate_limit_service.py +++ b/backend/tests/test_rate_limit_service.py @@ -1,7 +1,7 @@ """ Unit tests for RateLimitService. -Tests rate limiting logic for jobs, YouTube uploads, and beta enrollments. +Tests rate limiting logic for job creation, user overrides, and stats. """ import pytest @@ -29,8 +29,6 @@ class TestRateLimitService: settings = Mock() settings.enable_rate_limiting = True settings.rate_limit_jobs_per_day = 5 - settings.rate_limit_youtube_uploads_per_day = 10 - settings.rate_limit_beta_ip_per_day = 1 return settings @pytest.fixture @@ -130,10 +128,6 @@ class TestRateLimitService: mock_override_doc.exists = True mock_override_doc.to_dict.return_value = {"bypass_job_limit": True} - def mock_get(*args, **kwargs): - # Return different docs based on collection - return mock_override_doc if "overrides" in str(mock_db.collection.call_args) else mock_job_doc - mock_db.collection.return_value.document.return_value.get.side_effect = [mock_override_doc, mock_job_doc] with patch('backend.services.rate_limit_service.settings', mock_settings): @@ -172,105 +166,6 @@ class TestRateLimitService: assert remaining == -1 assert "Rate limiting disabled" in message - # ========================================================================= - # YouTube Upload Rate Limiting Tests - # ========================================================================= - - def test_check_youtube_limit_delegates_to_quota_service(self, rate_limit_service, mock_db, mock_settings): - """Test YouTube upload check delegates to YouTubeQuotaService.""" - mock_quota_service = Mock() - mock_quota_service.check_quota_available.return_value = (True, 9500, "9500 quota units remaining") - - with patch('backend.services.rate_limit_service.settings', mock_settings), \ - patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service): - allowed, remaining, message = rate_limit_service.check_youtube_upload_limit() - - assert allowed is True - assert remaining == 9500 - mock_quota_service.check_quota_available.assert_called_once() - - def test_check_youtube_limit_quota_exceeded(self, rate_limit_service, mock_db, mock_settings): - """Test YouTube upload check when quota is exceeded.""" - mock_quota_service = Mock() - mock_quota_service.check_quota_available.return_value = (False, 0, "YouTube API quota exhausted") - - with patch('backend.services.rate_limit_service.settings', mock_settings), \ - patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service): - allowed, remaining, message = rate_limit_service.check_youtube_upload_limit() - - assert allowed is False - assert remaining == 0 - assert "quota" in message.lower() - - def test_check_youtube_limit_fallback_on_quota_service_error(self, rate_limit_service, mock_db, mock_settings): - """Test YouTube upload falls back to legacy when quota service unavailable.""" - mock_doc = Mock() - mock_doc.exists = True - mock_doc.to_dict.return_value = {"count": 3} - mock_db.collection.return_value.document.return_value.get.return_value = mock_doc - - with patch('backend.services.rate_limit_service.settings', mock_settings), \ - patch('backend.services.youtube_quota_service.get_youtube_quota_service', side_effect=Exception("Service unavailable")): - allowed, remaining, message = rate_limit_service.check_youtube_upload_limit() - - assert allowed is True - assert remaining == 7 # 10 - 3 - - def test_check_youtube_limit_fallback_zero_configured(self, rate_limit_service, mock_db, mock_settings): - """Test that zero limit means unlimited when falling back to legacy.""" - mock_settings.rate_limit_youtube_uploads_per_day = 0 - - with patch('backend.services.rate_limit_service.settings', mock_settings), \ - patch('backend.services.youtube_quota_service.get_youtube_quota_service', side_effect=Exception("Service unavailable")): - allowed, remaining, message = rate_limit_service.check_youtube_upload_limit() - - assert allowed is True - assert remaining == -1 - assert "No YouTube upload limit" in message - - def test_check_youtube_limit_denies_on_runtime_error(self, rate_limit_service, mock_db, mock_settings): - """Test YouTube upload denied when quota service check_quota_available raises.""" - mock_quota_service = Mock() - mock_quota_service.check_quota_available.side_effect = Exception("Firestore timeout") - - with patch('backend.services.rate_limit_service.settings', mock_settings), \ - patch('backend.services.youtube_quota_service.get_youtube_quota_service', return_value=mock_quota_service): - allowed, remaining, message = rate_limit_service.check_youtube_upload_limit() - - assert allowed is False - assert remaining == 0 - assert "Quota service error" in message - - # ========================================================================= - # Beta Enrollment IP Rate Limiting Tests - # ========================================================================= - - def test_check_beta_ip_limit_first_enrollment(self, rate_limit_service, mock_db, mock_settings): - """Test first enrollment from IP is allowed.""" - mock_doc = Mock() - mock_doc.exists = False - mock_db.collection.return_value.document.return_value.get.return_value = mock_doc - - with patch('backend.services.rate_limit_service.settings', mock_settings): - allowed, remaining, message = rate_limit_service.check_beta_ip_limit("192.168.1.1") - - assert allowed is True - assert remaining == 1 - - def test_check_beta_ip_limit_already_enrolled(self, rate_limit_service, mock_db, mock_settings): - """Test second enrollment from same IP is blocked.""" - mock_doc = Mock() - mock_doc.exists = True - mock_doc.to_dict.return_value = {"count": 1} - mock_db.collection.return_value.document.return_value.get.return_value = mock_doc - - with patch('backend.services.rate_limit_service.settings', mock_settings): - allowed, remaining, message = rate_limit_service.check_beta_ip_limit("192.168.1.1") - - assert allowed is False - assert remaining == 0 - assert "Too many beta enrollments" in message - # ========================================================================= # Recording Tests # ========================================================================= @@ -293,22 +188,6 @@ class TestRateLimitService: # Verify transaction was used mock_db.transaction.assert_called() - def test_record_youtube_upload(self, rate_limit_service, mock_db, mock_settings): - """Test recording a YouTube upload.""" - mock_transaction = Mock() - mock_db.transaction.return_value = mock_transaction - - mock_doc = Mock() - mock_doc.exists = False - mock_db.collection.return_value.document.return_value.get.return_value = mock_doc - - with patch('backend.services.rate_limit_service.settings', mock_settings): - with patch('backend.services.rate_limit_service.firestore') as mock_firestore: - mock_firestore.transactional = lambda f: f - rate_limit_service.record_youtube_upload("job123", "user@example.com") - - mock_db.transaction.assert_called() - def test_record_skipped_when_disabled(self, rate_limit_service, mock_db, mock_settings): """Test that recording is skipped when rate limiting is disabled.""" mock_settings.enable_rate_limiting = False @@ -381,6 +260,29 @@ class TestRateLimitService: assert result is False + # ========================================================================= + # Stats Tests + # ========================================================================= + + def test_get_rate_limit_stats(self, rate_limit_service, mock_db, mock_settings): + """Test getting rate limit statistics.""" + # Mock Firestore stream returning one user's job doc + mock_doc = Mock() + mock_doc.to_dict.return_value = { + "user_email": "user@example.com", + "count": 3, + } + mock_db.collection.return_value.where.return_value.stream.return_value = [mock_doc] + + with patch('backend.services.rate_limit_service.settings', mock_settings): + stats = rate_limit_service.get_rate_limit_stats() + + assert stats["rate_limiting_enabled"] is True + assert stats["job_limit_per_user"] == 5 + assert stats["total_jobs_today"] == 3 + assert stats["users_with_jobs_today"] == 1 + assert "seconds_until_reset" in stats + class TestRateLimitExceededError: """Test RateLimitExceededError exception.""" diff --git a/backend/tests/test_rate_limits_api.py b/backend/tests/test_rate_limits_api.py index e2ef9439..0b34adcd 100644 --- a/backend/tests/test_rate_limits_api.py +++ b/backend/tests/test_rate_limits_api.py @@ -47,8 +47,6 @@ def mock_settings(): settings = Mock() settings.enable_rate_limiting = True settings.rate_limit_jobs_per_day = 5 - settings.rate_limit_youtube_uploads_per_day = 10 - settings.rate_limit_beta_ip_per_day = 1 return settings @@ -65,7 +63,6 @@ class TestGetRateLimitStats: # Setup rate limit service mock mock_rls = Mock() - mock_rls.get_youtube_uploads_today.return_value = 3 mock_rls.get_all_overrides.return_value = {"user1@example.com": {}} mock_get_rls.return_value = mock_rls @@ -87,9 +84,11 @@ class TestGetRateLimitStats: "units_limit": 10000, "effective_limit": 9500, "upload_cost": 300, + "upload_count": 3, "estimated_uploads_remaining": 28, "seconds_until_reset": 43200, } + mock_quota.get_gcp_quota_usage.return_value = {"available": False} mock_get_qs.return_value = mock_quota # Setup queue service mock @@ -111,15 +110,26 @@ class TestGetRateLimitStats: assert response.status_code == 200 data = response.json() assert data["jobs_per_day_limit"] == 5 - assert data["youtube_uploads_per_day_limit"] == 10 + assert data["rate_limiting_enabled"] is True assert data["youtube_uploads_today"] == 3 - assert data["youtube_uploads_remaining"] == 7 assert data["disposable_domains_count"] == 100 assert data["total_overrides"] == 1 - # New quota fields + # Quota unit fields assert data["youtube_quota_units_consumed"] == 900 assert data["youtube_quota_units_remaining"] == 8600 assert data["youtube_quota_daily_limit"] == 10000 + assert data["youtube_quota_effective_limit"] == 9500 + assert data["youtube_quota_upload_cost"] == 300 + assert data["youtube_quota_estimated_uploads_remaining"] == 28 + # Queue fields + assert data["youtube_uploads_queued"] == 0 + assert data["youtube_uploads_failed"] == 0 + # GCP quota (unavailable in this test) + assert data["gcp_quota_available"] is False + # Removed fields should not be present + assert "youtube_uploads_per_day_limit" not in data + assert "beta_ip_per_day_limit" not in data + assert "youtube_uploads_remaining" not in data class TestGetUserRateLimitStatus: diff --git a/backend/workers/video_worker_orchestrator.py b/backend/workers/video_worker_orchestrator.py index 56be881e..a872de4a 100644 --- a/backend/workers/video_worker_orchestrator.py +++ b/backend/workers/video_worker_orchestrator.py @@ -612,16 +612,6 @@ class VideoWorkerOrchestrator: except Exception as e: self.job_log.warning(f"Failed to record YouTube quota usage: {e}") - # Also record in legacy rate limit service for backward compatibility - try: - from backend.services.rate_limit_service import get_rate_limit_service - rate_limit_service = get_rate_limit_service() - rate_limit_service.record_youtube_upload( - job_id=self.config.job_id, - user_email=user_email - ) - except Exception as e: - self.job_log.warning(f"Failed to record YouTube upload for legacy rate limiting: {e}") else: self.job_log.warning("YouTube upload did not return a URL") diff --git a/docs/API.md b/docs/API.md index 25a27fe7..602a70fa 100644 --- a/docs/API.md +++ b/docs/API.md @@ -774,47 +774,9 @@ POST /api/users/webhooks/stripe Handles `checkout.session.completed` events. -## Beta Tester Program (Deprecated) - -> **Note**: The beta program was removed from the homepage in PR #430. These endpoints are preserved for backward compatibility but are no longer actively promoted. New users receive 2 welcome credits instead. See [User Feedback for Credits](#user-feedback-for-credits) for the current feedback-for-credits mechanism. - -### Enroll as Beta Tester - -```http -POST /api/users/beta/enroll -Content-Type: application/json - -{ - "email": "user@example.com", - "promise_text": "I promise to provide feedback...", - "accept_corrections_work": true -} -``` - -Returns 1 free credit and session token. - -### Submit Beta Feedback - -```http -POST /api/users/beta/feedback -Authorization: Bearer SESSION_TOKEN -Content-Type: application/json - -{ - "overall_rating": 4, - "ease_of_use_rating": 5, - "lyrics_accuracy_rating": 4, - "correction_experience_rating": 3, - "what_went_well": "...", - "what_could_improve": "..." -} -``` - -Bonus credit for detailed feedback (50+ chars). - ### User Feedback for Credits -Available to all users (not just beta testers). Users who have completed 2+ jobs can submit feedback to earn 2 free credits. +Users who have completed 2+ jobs can submit feedback to earn 2 free credits. #### Check Eligibility @@ -903,8 +865,7 @@ Returns platform statistics: "complete": 400, "failed": 20 }, - "total_credits_issued_30d": 200, - "total_beta_testers": 30 + "total_credits_issued_30d": 200 } ``` @@ -997,24 +958,6 @@ POST /api/users/admin/users/{email}/disable Authorization: Bearer ADMIN_TOKEN ``` -### Beta Stats (Admin) - -```http -GET /api/users/admin/beta/stats -GET /api/users/admin/beta/stats?exclude_test=false -Authorization: Bearer ADMIN_TOKEN -``` - -Query parameters: -- `exclude_test` (bool, default: true) - Filter out test users from beta statistics - -### Beta Feedback List (Admin) - -```http -GET /api/users/admin/beta/feedback?limit=50 -Authorization: Bearer ADMIN_TOKEN -``` - ### Delete Job (Admin) ```http @@ -1568,8 +1511,7 @@ Called by Cloud Tasks 5 minutes after a job enters a blocking state (primarily ` ### User Limits - **Jobs per day**: 5 (configurable via `RATE_LIMIT_JOBS_PER_DAY`) -- **YouTube uploads**: Quota-aware, ~33 uploads/day within 10,000 units/day API quota (configurable via `YOUTUBE_QUOTA_DAILY_LIMIT`, `YOUTUBE_QUOTA_UPLOAD_COST`, `YOUTUBE_QUOTA_SAFETY_MARGIN`). Uploads exceeding quota are queued and processed hourly. -- **Beta enrollment per IP**: 1 per 24 hours (configurable via `RATE_LIMIT_BETA_IP_PER_DAY`) +- **YouTube uploads**: Quota-aware, ~33 uploads/day within 10,000 units/day API quota (configurable via `YOUTUBE_QUOTA_DAILY_LIMIT`, `YOUTUBE_QUOTA_UPLOAD_COST`, `YOUTUBE_QUOTA_SAFETY_MARGIN`). Uploads exceeding quota are queued and processed hourly. GCP Cloud Monitoring integration provides cross-referencing of actual API quota consumption (see Admin Rate Limits API). Rate limiting can be disabled via `ENABLE_RATE_LIMITING=false`. @@ -1598,7 +1540,21 @@ Admins can grant users bypass permissions or custom limits via the admin UI at ` ```http GET /api/admin/rate-limits/stats ``` -Returns current rate limit statistics (usage, blocklist counts, override counts). +Returns current rate limit statistics including YouTube quota usage, blocklist counts, override counts, and GCP Cloud Monitoring cross-reference data. + +Response fields include: +- `jobs_per_day_limit`, `rate_limiting_enabled` - Configuration +- `youtube_uploads_today` - Upload count from Firestore quota tracking (PT-based) +- `youtube_quota_units_consumed`, `youtube_quota_units_remaining`, `youtube_quota_daily_limit`, `youtube_quota_effective_limit`, `youtube_quota_upload_cost`, `youtube_quota_estimated_uploads_remaining`, `youtube_quota_seconds_until_reset` - YouTube quota unit tracking +- `youtube_uploads_queued`, `youtube_uploads_failed` - Upload queue status +- `disposable_domains_count`, `blocked_emails_count`, `blocked_ips_count` - Blocklist stats +- `total_overrides` - User override count +- `gcp_quota_available` - Whether GCP Cloud Monitoring data is available +- `gcp_quota_units_consumed` - Actual API quota units consumed per GCP (may differ from local tracking) +- `gcp_quota_last_datapoint` - Timestamp of the most recent GCP monitoring data point +- `gcp_quota_data_delay_minutes` - How stale the GCP data is (Cloud Monitoring has ~5 min delay) +- `quota_drift` - Difference between local tracking and GCP-reported consumption +- `quota_drift_alert` - Whether drift exceeds alert threshold ```http GET /api/admin/rate-limits/users/{email} diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 8bba66af..589030f6 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -169,6 +169,7 @@ LyricsTranscriber LyricsTranscriber | SendGrid | Email notifications | Optional | | Cloud Tasks | Delayed task scheduling (idle reminders, YouTube queue) | Optional | | Cloud Scheduler | Hourly YouTube upload queue processing | Optional | +| Cloud Monitoring | Cross-reference YouTube API quota consumption from GCP metrics | Optional | | karaoke-decide | Song catalog (MusicBrainz + Spotify) for autocomplete | Optional | | KaraokeNerds | Community karaoke version detection | Optional | @@ -186,7 +187,6 @@ karaoke-gen shares a GCP project (`nomadkaraoke`) with karaoke-decide, but uses | `sessions` | Magic link auth sessions | user_email, token, expires_at | | `magic_links` | Passwordless auth tokens | email, token, expires_at, used | | `user_feedback` | User feedback for credits (all users) | user_email, ratings, comments, created_at | -| `beta_feedback` | Beta program feedback (deprecated) | user_email, ratings, comments | | `youtube_quota` | Daily YouTube API quota tracking | date_pt, units_consumed, units_limit, operations[] | | `youtube_upload_queue` | Deferred YouTube uploads | job_id, status, user_email, queued_at, youtube_url | @@ -238,7 +238,8 @@ The Video Worker uses an orchestrator pattern to ensure all features work regard | `template_service.py` | GCS-backed email templates | | `job_notification_service.py` | Email orchestration (completion, reminders) | | `audio_transcoding_service.py` | Transcode FLAC → OGG Opus for review UI playback | -| `youtube_quota_service.py` | Track YouTube API quota units (Firestore, PT midnight reset) | +| `youtube_quota_service.py` | Track YouTube API quota units (Firestore, PT midnight reset) + GCP Cloud Monitoring cross-reference | +| `email_validation_service.py` | Email validation, disposable domain detection, blocklist management | | `youtube_upload_queue_service.py` | Deferred YouTube upload queue management | | `catalog_proxy_service.py` | Proxy to karaoke-decide catalog API (artist/track search) | | `karaokenerds_service.py` | Scrape karaokenerds.com for community karaoke versions | @@ -466,7 +467,7 @@ if tenant_config and not tenant_config.features.audio_search: ### Tenant Portal Frontend (B2B Deployment) -B2B tenants are served from a **separate Cloudflare Pages project** (`karaoke-gen-tenant`) built with `NEXT_PUBLIC_TENANT_PORTAL=true`. This build flag enables tree-shaking of consumer-only content (landing page, pricing, beta enrollment) at build time, keeping the bundle lean for white-label deployments. +B2B tenants are served from a **separate Cloudflare Pages project** (`karaoke-gen-tenant`) built with `NEXT_PUBLIC_TENANT_PORTAL=true`. This build flag enables tree-shaking of consumer-only content (landing page, pricing) at build time, keeping the bundle lean for white-label deployments. **Runtime tenant injection**: A Cloudflare Pages edge function intercepts HTML responses and injects tenant-specific configuration before the page reaches the browser: diff --git a/docs/PRODUCT-VISION.md b/docs/PRODUCT-VISION.md index 669500ca..97686300 100644 --- a/docs/PRODUCT-VISION.md +++ b/docs/PRODUCT-VISION.md @@ -123,10 +123,6 @@ This is significantly cheaper than: - Hiring a studio: $100+ per track - DIY tools + your time: Hours of frustration -### Beta Tester Program - -New users can join the beta program to get free credits in exchange for feedback, helping improve the system while building initial user base. - ### B2B / White-Label Commercial producers pay for API access or custom integration to generate tracks at scale with their own branding, instrumentals, and distribution. @@ -144,7 +140,6 @@ Commercial producers pay for API access or custom integration to generate tracks - Token-based authentication - Magic link authentication - Payment flow (Stripe) -- Beta tester program - YouTube upload integration - Dropbox/Google Drive distribution diff --git a/frontend/app/admin/beta/page.tsx b/frontend/app/admin/beta/page.tsx deleted file mode 100644 index 4601f628..00000000 --- a/frontend/app/admin/beta/page.tsx +++ /dev/null @@ -1,288 +0,0 @@ -"use client" - -import { useEffect, useState } from "react" -import { adminApi, AdminBetaStats, AdminBetaFeedback } from "@/lib/api" -import { useAdminSettings } from "@/lib/admin-settings" -import { StatsCard, StatsGrid } from "@/components/admin/stats-card" -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" -import { Badge } from "@/components/ui/badge" -import { - Table, - TableBody, - TableCell, - TableHead, - TableHeader, - TableRow, -} from "@/components/ui/table" -import { - Users, - MessageSquare, - Star, - RefreshCw, - Loader2, - ThumbsUp, - ThumbsDown, -} from "lucide-react" -import { Button } from "@/components/ui/button" -import { useToast } from "@/hooks/use-toast" - -export default function AdminBetaPage() { - const { toast } = useToast() - const { showTestData } = useAdminSettings() - const [stats, setStats] = useState- Manage beta testers and view feedback -
-Overall
-Ease of Use
-Lyrics Accuracy
-Correction Experience
-No ratings available yet
- )} -No feedback submissions yet
- ) : ( -What went well:
-{item.what_went_well}
-What could improve:
-{item.what_could_improve}
-Additional comments:
-{item.additional_comments}
-+ GCP quota data unavailable (metrics have ~1-2h delay) +
+ )} +