#!/bin/bash # ============================================================================== # SCRIPT DE PRUEBA: Teacher Portal Endpoints # ============================================================================== # Descripción: Valida que los endpoints críticos del portal teacher funcionen # Autor: Backend-Agent # Fecha: 2025-11-24 # ============================================================================== set -e # Configuración API_URL="${API_URL:-http://localhost:3006}" API_BASE="${API_BASE:-/api/v1}" TEACHER_EMAIL="${TEACHER_EMAIL:-teacher@gamilit.com}" TEACHER_PASSWORD="${TEACHER_PASSWORD:-Test1234}" # Colores para output GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # ============================================================================== # FUNCIONES AUXILIARES # ============================================================================== print_header() { echo "" echo "====================================================" echo "$1" echo "====================================================" echo "" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_info() { echo "ℹ️ $1" } # ============================================================================== # INICIO DE PRUEBAS # ============================================================================== print_header "Testing Teacher Portal Endpoints" print_info "API URL: $API_URL$API_BASE" print_info "Teacher: $TEACHER_EMAIL" echo "" # ============================================================================== # STEP 1: LOGIN # ============================================================================== print_header "Step 1: Login as Teacher" LOGIN_RESPONSE=$(curl -s -X POST "$API_URL$API_BASE/auth/login" \ -H "Content-Type: application/json" \ -d "{\"email\": \"$TEACHER_EMAIL\", \"password\": \"$TEACHER_PASSWORD\"}") # Extraer token TOKEN=$(echo "$LOGIN_RESPONSE" | sed -n 's/.*"accessToken":"\([^"]*\)".*/\1/p') if [ -z "$TOKEN" ]; then print_error "Failed to get authentication token" print_info "Response: $LOGIN_RESPONSE" exit 1 fi print_success "Login successful" print_info "Token: ${TOKEN:0:30}..." echo "" # ============================================================================== # STEP 2: GET CLASSROOMS # ============================================================================== print_header "Step 2: Testing GET /teacher/classrooms" CLASSROOMS_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ -X GET "$API_URL$API_BASE/teacher/classrooms" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json") HTTP_STATUS=$(echo "$CLASSROOMS_RESPONSE" | grep "HTTP_STATUS" | sed 's/HTTP_STATUS://') CLASSROOMS_BODY=$(echo "$CLASSROOMS_RESPONSE" | sed '/HTTP_STATUS/d') if [ "$HTTP_STATUS" != "200" ]; then print_error "Expected status 200, got $HTTP_STATUS" print_info "Response: $CLASSROOMS_BODY" exit 1 fi # Validar que es JSON válido CLASSROOM_COUNT=$(echo "$CLASSROOMS_BODY" | sed -n 's/.*"total":\([0-9]*\).*/\1/p') if [ -z "$CLASSROOM_COUNT" ]; then print_error "Invalid JSON response or missing 'total' field" exit 1 fi print_success "GET /teacher/classrooms → Status 200" print_info "Classrooms found: $CLASSROOM_COUNT" # Mostrar response (primeros 300 caracteres) print_info "Response preview:" echo "$CLASSROOMS_BODY" | head -c 300 echo "..." echo "" # ============================================================================== # STEP 3: GET CLASSROOM STUDENTS # ============================================================================== # Extraer primer classroom ID CLASSROOM_ID=$(echo "$CLASSROOMS_BODY" | sed -n 's/.*"id":"\([^"]*\)".*/\1/p' | head -1) if [ -n "$CLASSROOM_ID" ]; then print_header "Step 3: Testing GET /teacher/classrooms/:id/students" print_info "Using classroom ID: $CLASSROOM_ID" STUDENTS_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ -X GET "$API_URL$API_BASE/teacher/classrooms/$CLASSROOM_ID/students" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json") HTTP_STATUS=$(echo "$STUDENTS_RESPONSE" | grep "HTTP_STATUS" | sed 's/HTTP_STATUS://') STUDENTS_BODY=$(echo "$STUDENTS_RESPONSE" | sed '/HTTP_STATUS/d') if [ "$HTTP_STATUS" != "200" ]; then print_error "Expected status 200, got $HTTP_STATUS" print_info "Response: $STUDENTS_BODY" exit 1 fi # Validar que es JSON válido STUDENT_COUNT=$(echo "$STUDENTS_BODY" | sed -n 's/.*"total":\([0-9]*\).*/\1/p') if [ -z "$STUDENT_COUNT" ]; then print_error "Invalid JSON response or missing 'total' field" exit 1 fi print_success "GET /teacher/classrooms/:id/students → Status 200" print_info "Students found: $STUDENT_COUNT" # Mostrar response (primeros 300 caracteres) print_info "Response preview:" echo "$STUDENTS_BODY" | head -c 300 echo "..." echo "" else print_warning "Step 3: SKIPPED - No classroom found to test students endpoint" echo "" fi # ============================================================================== # STEP 4: TEST QUERY PARAMETERS # ============================================================================== print_header "Step 4: Testing Query Parameters" print_info "Testing pagination (page=1, limit=2)..." PAGINATION_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ -X GET "$API_URL$API_BASE/teacher/classrooms?page=1&limit=2" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json") HTTP_STATUS=$(echo "$PAGINATION_RESPONSE" | grep "HTTP_STATUS" | sed 's/HTTP_STATUS://') if [ "$HTTP_STATUS" = "200" ]; then print_success "Pagination works correctly" else print_error "Pagination test failed with status $HTTP_STATUS" fi print_info "Testing search filter (search=test)..." SEARCH_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ -X GET "$API_URL$API_BASE/teacher/classrooms?search=test" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json") HTTP_STATUS=$(echo "$SEARCH_RESPONSE" | grep "HTTP_STATUS" | sed 's/HTTP_STATUS://') if [ "$HTTP_STATUS" = "200" ]; then print_success "Search filter works correctly" else print_error "Search test failed with status $HTTP_STATUS" fi echo "" # ============================================================================== # STEP 5: TEST AUTHORIZATION # ============================================================================== print_header "Step 5: Testing Authorization" print_info "Testing without authentication token..." UNAUTH_RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ -X GET "$API_URL$API_BASE/teacher/classrooms" \ -H "Content-Type: application/json") HTTP_STATUS=$(echo "$UNAUTH_RESPONSE" | grep "HTTP_STATUS" | sed 's/HTTP_STATUS://') if [ "$HTTP_STATUS" = "401" ]; then print_success "Correctly returns 401 without token" else print_error "Expected 401, got $HTTP_STATUS" fi echo "" # ============================================================================== # RESUMEN FINAL # ============================================================================== print_header "Test Summary" print_success "All critical endpoints are working correctly:" echo " ✅ GET /teacher/classrooms (with pagination and filters)" echo " ✅ GET /teacher/classrooms/:id/students" echo " ✅ Authentication guards working" echo " ✅ Authorization validation working" echo "" print_info "Teacher Portal is ready for use!" echo "" exit 0