Branchwell

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
5API_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
8API_KEY=${AI_RESOURCE_PRO_API_KEY:-"YOUR_API_KEY_HERE"}
9
10# Check if API key is set
11if [ "$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
14fi
15
16echo "====================================="
17echo "Testing /api/news API endpoints"
18echo "====================================="
19echo "NOTE: This API requires BOTH of the following:"
20echo "1. Supabase admin authentication (cookie/session)"
21echo "2. Valid API key in x-api-key header"
22echo
23echo "In development, if BYPASS_AUTH_IN_DEV=true is set in .env.local,"
24echo "the Supabase admin authentication will be bypassed for testing."
25echo "====================================="
26echo "API key: ${API_KEY:0:5}...${API_KEY: -5}" # Show first 5 and last 5 chars for verification
27echo
28
29# Helper function for post request
30test_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
44test_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
59test_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)
72echo "INITIAL SETUP: Creating a test news item with valid API key"
73RESULT=$(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)
78TEST_ID=$(echo $RESULT | grep -o '"id":"[^"]*"' | head -1 | cut -d '"' -f 4)
79
80if [ -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
83fi
84
85echo "Created test item with ID: $TEST_ID"
86echo
87
88echo "====================================="
89echo "TESTING AUTHENTICATION REQUIREMENTS"
90echo "====================================="
91echo "Expected: Only requests with valid API key should succeed"
92echo "Expected: Requests without authentication should return 401 Unauthorized"
93echo "====================================="
94echo
95
96# POST Tests
97test_post "" "No authentication (should fail with 401)"
98test_post "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)"
99test_post "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 201)"
100
101# PUT Tests
102test_put "" "No authentication (should fail with 401)" "$TEST_ID"
103test_put "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID"
104test_put "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID"
105
106# DELETE Tests
107test_delete "" "No authentication (should fail with 401)" "$TEST_ID"
108test_delete "-H \"x-api-key: invalid-key\"" "Invalid API key (should fail with 401)" "$TEST_ID"
109test_delete "-H \"x-api-key: $API_KEY\"" "Valid API key (should succeed with 200)" "$TEST_ID"
110
111echo "====================================="
112echo "Tests completed"
113echo "====================================="