#!/bin/bash # Get API key from .env.local or environment variable # Extract API key from .env.local if it exists API_KEY=$(grep -E "^AI_RESOURCE_PRO_API_KEY=" .env.local | sed 's/^NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY=//' | tr -d '\n' | tr -d '"') # Fall back to environment variable if not found in .env.local API_KEY=${AI_RESOURCE_PRO_API_KEY:-"YOUR_API_KEY_HERE"} # Check if API key is set if [ "$API_KEY" = "YOUR_API_KEY_HERE" ]; then echo "ERROR: API key not set. Please set the NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY environment variable or add it to .env.local" exit 1 fi echo "=====================================" echo "Testing /api/news API endpoints" echo "=====================================" echo "NOTE: This API requires BOTH of the following:" echo "1. Supabase admin authentication (cookie/session)" echo "2. Valid API key in x-api-key header" echo echo "In development, if BYPASS_AUTH_IN_DEV=true is set in .env.local," echo "the Supabase admin authentication will be bypassed for testing." echo "=====================================" echo "API key: ${API_KEY:0:5}...${API_KEY: -5}" # Show first 5 and last 5 chars for verification echo # Helper function for post request test_post() { local auth_header="$1" local auth_label="$2" echo "===== POST TEST: $auth_label =====" eval "curl -v -X POST \ -H \"Content-Type: application/json\" \ $auth_header \ -d '{\"title\": \"Test News\", \"content\": \"Test Content\", \"url\": \"http://example.com\", \"source\": \"Test\", \"category\": \"Test\", \"article_date\": \"2023-01-01\"}' \ http://localhost:3000/api/news 2>&1 | grep -E \"HTTP/(1.1|2)\"" echo -e "\n" } # Helper function for put request test_put() { local auth_header="$1" local auth_label="$2" local test_id="$3" echo "===== PUT TEST: $auth_label =====" eval "curl -v -X PUT \ -H \"Content-Type: application/json\" \ $auth_header \ -d '{\"id\": \"$test_id\", \"title\": \"Updated Test News\", \"content\": \"Updated Test Content\", \"url\": \"http://example.com\", \"source\": \"Test\", \"category\": \"Test\", \"article_date\": \"2023-01-01\"}' \ http://localhost:3000/api/news 2>&1 | grep -E \"HTTP/(1.1|2)\"" echo -e "\n" } # Helper function for delete request test_delete() { local auth_header="$1" local auth_label="$2" local test_id="$3" echo "===== DELETE TEST: $auth_label =====" eval "curl -v -X DELETE \ $auth_header \ \"http://localhost:3000/api/news?id=$test_id\" 2>&1 | grep -E \"HTTP/(1.1|2)\"" echo -e "\n" } # TEST SETUP: Create a news item with valid API key (for later tests) echo "INITIAL SETUP: Creating a test news item with valid API key" RESULT=$(curl -s -X POST \ -H "Content-Type: application/json" \ -H "x-api-key: $API_KEY" \ -d '{"title": "Test News For API Tests", "content": "This is a test news item that will be used for API testing.", "url": "http://example.com/test", "source": "API Test", "category": "Test", "article_date": "2023-01-01"}' \ http://localhost:3000/api/news) TEST_ID=$(echo $RESULT | grep -o '"id":"[^"]*"' | head -1 | cut -d '"' -f 4) if [ -z "$TEST_ID" ]; then echo "ERROR: Failed to create test item. Check if the API is running and API key is valid." exit 1 fi echo "Created test item with ID: $TEST_ID" echo echo "=====================================" echo "TESTING AUTHENTICATION REQUIREMENTS" echo "=====================================" echo "Expected: Only requests with valid API key should succeed" echo "Expected: Requests without authentication should return 401 Unauthorized" echo "=====================================" echo # POST Tests test_post "" "No authentication (should fail with 401)" test_post "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" test_post "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 201)" # PUT Tests test_put "" "No authentication (should fail with 401)" "$TEST_ID" test_put "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID" test_put "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID" # DELETE Tests test_delete "" "No authentication (should fail with 401)" "$TEST_ID" test_delete "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID" test_delete "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID" echo "=====================================" echo "Tests completed" echo "====================================="