AI Resource Pro / website / test-api.sh
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Get API key from .env.local or environment variable |
| 4 | # Extract API key from .env.local if it exists |
| 5 | 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 '"') |
| 6 | |
| 7 | # Fall back to environment variable if not found in .env.local |
| 8 | API_KEY=${AI_RESOURCE_PRO_API_KEY:-"YOUR_API_KEY_HERE"} |
| 9 | |
| 10 | # Check if API key is set |
| 11 | if [ "$API_KEY" = "YOUR_API_KEY_HERE" ]; then |
| 12 | echo "ERROR: API key not set. Please set the NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY environment variable or add it to .env.local" |
| 13 | exit 1 |
| 14 | fi |
| 15 | |
| 16 | echo "=====================================" |
| 17 | echo "Testing /api/news API endpoints" |
| 18 | echo "=====================================" |
| 19 | echo "NOTE: This API requires BOTH of the following:" |
| 20 | echo "1. Supabase admin authentication (cookie/session)" |
| 21 | echo "2. Valid API key in x-api-key header" |
| 22 | echo |
| 23 | echo "In development, if BYPASS_AUTH_IN_DEV=true is set in .env.local," |
| 24 | echo "the Supabase admin authentication will be bypassed for testing." |
| 25 | echo "=====================================" |
| 26 | echo "API key: ${API_KEY:0:5}...${API_KEY: -5}" # Show first 5 and last 5 chars for verification |
| 27 | echo |
| 28 | |
| 29 | # Helper function for post request |
| 30 | test_post() { |
| 31 | local auth_header="$1" |
| 32 | local auth_label="$2" |
| 33 | |
| 34 | echo "===== POST TEST: $auth_label =====" |
| 35 | eval "curl -v -X POST \ |
| 36 | -H \"Content-Type: application/json\" \ |
| 37 | $auth_header \ |
| 38 | -d '{\"title\": \"Test News\", \"content\": \"Test Content\", \"url\": \"http://example.com\", \"source\": \"Test\", \"category\": \"Test\", \"article_date\": \"2023-01-01\"}' \ |
| 39 | http://localhost:3000/api/news 2>&1 | grep -E \"HTTP/(1.1|2)\"" |
| 40 | echo -e "\n" |
| 41 | } |
| 42 | |
| 43 | # Helper function for put request |
| 44 | test_put() { |
| 45 | local auth_header="$1" |
| 46 | local auth_label="$2" |
| 47 | local test_id="$3" |
| 48 | |
| 49 | echo "===== PUT TEST: $auth_label =====" |
| 50 | eval "curl -v -X PUT \ |
| 51 | -H \"Content-Type: application/json\" \ |
| 52 | $auth_header \ |
| 53 | -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\"}' \ |
| 54 | http://localhost:3000/api/news 2>&1 | grep -E \"HTTP/(1.1|2)\"" |
| 55 | echo -e "\n" |
| 56 | } |
| 57 | |
| 58 | # Helper function for delete request |
| 59 | test_delete() { |
| 60 | local auth_header="$1" |
| 61 | local auth_label="$2" |
| 62 | local test_id="$3" |
| 63 | |
| 64 | echo "===== DELETE TEST: $auth_label =====" |
| 65 | eval "curl -v -X DELETE \ |
| 66 | $auth_header \ |
| 67 | \"http://localhost:3000/api/news?id=$test_id\" 2>&1 | grep -E \"HTTP/(1.1|2)\"" |
| 68 | echo -e "\n" |
| 69 | } |
| 70 | |
| 71 | # TEST SETUP: Create a news item with valid API key (for later tests) |
| 72 | echo "INITIAL SETUP: Creating a test news item with valid API key" |
| 73 | RESULT=$(curl -s -X POST \ |
| 74 | -H "Content-Type: application/json" \ |
| 75 | -H "x-api-key: $API_KEY" \ |
| 76 | -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"}' \ |
| 77 | http://localhost:3000/api/news) |
| 78 | TEST_ID=$(echo $RESULT | grep -o '"id":"[^"]*"' | head -1 | cut -d '"' -f 4) |
| 79 | |
| 80 | if [ -z "$TEST_ID" ]; then |
| 81 | echo "ERROR: Failed to create test item. Check if the API is running and API key is valid." |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | echo "Created test item with ID: $TEST_ID" |
| 86 | echo |
| 87 | |
| 88 | echo "=====================================" |
| 89 | echo "TESTING AUTHENTICATION REQUIREMENTS" |
| 90 | echo "=====================================" |
| 91 | echo "Expected: Only requests with valid API key should succeed" |
| 92 | echo "Expected: Requests without authentication should return 401 Unauthorized" |
| 93 | echo "=====================================" |
| 94 | echo |
| 95 | |
| 96 | # POST Tests |
| 97 | test_post "" "No authentication (should fail with 401)" |
| 98 | test_post "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" |
| 99 | test_post "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 201)" |
| 100 | |
| 101 | # PUT Tests |
| 102 | test_put "" "No authentication (should fail with 401)" "$TEST_ID" |
| 103 | test_put "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID" |
| 104 | test_put "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID" |
| 105 | |
| 106 | # DELETE Tests |
| 107 | test_delete "" "No authentication (should fail with 401)" "$TEST_ID" |
| 108 | test_delete "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID" |
| 109 | test_delete "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID" |
| 110 | |
| 111 | echo "=====================================" |
| 112 | echo "Tests completed" |
| 113 | echo "=====================================" |