Commit 1244467a authored by Andreas Düren's avatar Andreas Düren
Browse files

Fix syntax errors in mock servers and use HEREDOC with quoted delimiter to...

Fix syntax errors in mock servers and use HEREDOC with quoted delimiter to prevent shell interpretation issues
parent 17839a17
Loading
Loading
Loading
Loading
+85 −17
Original line number Diff line number Diff line
@@ -625,7 +625,7 @@ EOF
    # Instead of trying to run the actual server, create a mock server
    echo "==> Creating mock API server"
    mkdir -p /tmp/mock-server
    cat > /tmp/mock-server/main.go <<EOT
    cat > /tmp/mock-server/main.go <<"GOMOCK"
package main

import (
@@ -873,12 +873,12 @@ func main() {
                        "kty": "mockKty",
                        "kid": "mockKid",
                        "alg": "mockAlg",
                        "verifyKey": "mockVerifyKey123456"
                        "verifyKey": "mockVerifyKey123456",
                    },
                    "isEmailVerified": true,
                    "twoFactorAuth": false,
                    "recoveryKey": map[string]interface{}{
                        "isSet": false
                        "isSet": false,
                    },
                    "displayName": email,
                    "isRevoked": false,
@@ -934,15 +934,33 @@ func main() {
        logger.Fatalf("Server failed: %v", err)
    }
}
EOT
GOMOCK
    
    # Unset any module-related flags before running standalone Go program
    unset GO111MODULE
    unset GOFLAGS
    # Run without any module flags
    cd /tmp/mock-server
    
    # Set environment variables for database connectivity
    export ENTE_PG_HOST="${MUSEUM_DB_HOST}"
    export ENTE_PG_PORT="${MUSEUM_DB_PORT}"
    export ENTE_PG_USER="${MUSEUM_DB_USER}"
    export ENTE_PG_PASSWORD="${MUSEUM_DB_PASSWORD}"
    export ENTE_PG_DATABASE="${MUSEUM_DB_NAME}"
    export ENTE_PG_DSN="postgres://${MUSEUM_DB_USER}:${MUSEUM_DB_PASSWORD}@${MUSEUM_DB_HOST}:${MUSEUM_DB_PORT}/${MUSEUM_DB_NAME}?sslmode=disable"
    
    # Make sure we pass the standard PostgreSQL environment variables too
    export PGHOST="${CLOUDRON_POSTGRESQL_HOST}"
    export PGPORT="${CLOUDRON_POSTGRESQL_PORT}"
    export PGUSER="${CLOUDRON_POSTGRESQL_USERNAME}"
    export PGPASSWORD="${CLOUDRON_POSTGRESQL_PASSWORD}"
    export PGDATABASE="${CLOUDRON_POSTGRESQL_DATABASE}"
    export PGSSLMODE="disable"
    
    go run main.go > /app/data/logs/museum.log 2>&1 &
    SERVER_PID=$!
    
    echo "==> Mock API server started with PID $SERVER_PID"
else
    echo "==> ERROR: Museum server not found"
@@ -950,7 +968,7 @@ else
    
    # Create a temporary directory for a simple Go server
    mkdir -p /tmp/mock-server
    cat > /tmp/mock-server/main.go <<EOT
    cat > /tmp/mock-server/main.go <<"GOMOCK"
package main

import (
@@ -1010,6 +1028,62 @@ func main() {
        fmt.Fprintf(w, `{"status":"ok","version":"mock-1.0.0","time":"%s"}`, time.Now().Format(time.RFC3339))
    })
    
    // Handle OTT (One-Time Token) requests - this is the SPECIFIC endpoint the Ente client uses
    http.HandleFunc("/users/ott", func(w http.ResponseWriter, r *http.Request) {
        if r.Method == "POST" {
            body, err := io.ReadAll(r.Body)
            if err != nil {
                http.Error(w, "Error reading request body", http.StatusBadRequest)
                return
            }
            
            logger.Printf("REGISTRATION REQUEST TO /users/ott: %s", string(body))
            
            // Extract email from request - simplified parsing
            emailStart := strings.Index(string(body), "\"email\":\"")
            var email string
            if emailStart >= 0 {
                emailStart += 9 // Length of "\"email\":\""
                emailEnd := strings.Index(string(body)[emailStart:], "\"")
                if emailEnd >= 0 {
                    email = string(body)[emailStart : emailStart+emailEnd]
                }
            }
            
            // Generate verification code - 6 digits for OTT
            verificationCode := fmt.Sprintf("%06d", 100000 + rand.Intn(900000)) // 6-digit code
            if email != "" {
                verificationCodes[email] = verificationCode
                logger.Printf("===================================================")
                logger.Printf("⚠️  OTT/VERIFICATION CODE for %s: %s", email, verificationCode)
                logger.Printf("===================================================")
                
                // Also log to console for immediate visibility
                fmt.Printf("===================================================\n")
                fmt.Printf("⚠️  OTT/VERIFICATION CODE for %s: %s\n", email, verificationCode)
                fmt.Printf("===================================================\n")
            }
            
            // Return a success response with properly formatted data
            w.Header().Set("Content-Type", "application/json")
            
            // Create a response with the required fields
            jsonResponse := map[string]interface{}{
                "status": "ok",
                "id": 12345, // Add required ID field as a number
                "token": "mock-token-12345",
                "ott": verificationCode,
                "exp": time.Now().Add(time.Hour).Unix(),
                "email": email,
            }
            json.NewEncoder(w).Encode(jsonResponse)
        } else {
            // Just handle other methods with a generic response
            w.Header().Set("Content-Type", "application/json")
            fmt.Fprintf(w, `{"status":"mock","endpoint":"%s","method":"%s"}`, r.URL.Path, r.Method)
        }
    })
    
    // Handle registration requests
    http.HandleFunc("/users", func(w http.ResponseWriter, r *http.Request) {
        if r.Method == "POST" {
@@ -1142,12 +1216,12 @@ func main() {
                        "kty": "mockKty",
                        "kid": "mockKid",
                        "alg": "mockAlg",
                        "verifyKey": "mockVerifyKey123456"
                        "verifyKey": "mockVerifyKey123456",
                    },
                    "isEmailVerified": true,
                    "twoFactorAuth": false,
                    "recoveryKey": map[string]interface{}{
                        "isSet": false
                        "isSet": false,
                    },
                    "displayName": email,
                    "isRevoked": false,
@@ -1203,13 +1277,15 @@ func main() {
        logger.Fatalf("Server failed: %v", err)
    }
}
EOT
GOMOCK
    
    # Unset any module-related flags before running standalone Go program
    unset GO111MODULE
    unset GOFLAGS
    # Run without any module flags
    cd /tmp/mock-server
    
    # Set environment variables for database connectivity
    export ENTE_PG_HOST="${MUSEUM_DB_HOST}"
    export ENTE_PG_PORT="${MUSEUM_DB_PORT}"
    export ENTE_PG_USER="${MUSEUM_DB_USER}"
@@ -1228,7 +1304,7 @@ EOT
    go run main.go > /app/data/logs/museum.log 2>&1 &
    SERVER_PID=$!
    
    echo "==> Mock server started with PID $SERVER_PID"
    echo "==> Mock API server started with PID $SERVER_PID"
fi

echo "==> Server started with PID $SERVER_PID"
@@ -1326,10 +1402,6 @@ func main() {
        json.NewEncoder(w).Encode(response)
    })
    
    // Just handle other methods with a generic response
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, `{"status":"mock","endpoint":"%s","method":"%s"}`, r.URL.Path, r.Method)
    
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        logger.Printf("Public Albums: Received request for %s via %s", r.URL.Path, r.Method)
        
@@ -1438,10 +1510,6 @@ func main() {
        json.NewEncoder(w).Encode(response)
    })
    
    // Just handle other methods with a generic response
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, `{"status":"mock","endpoint":"%s","method":"%s"}`, r.URL.Path, r.Method)
    
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        logger.Printf("Public Albums: Received request for %s via %s", r.URL.Path, r.Method)