- Enhance `test-auth.sh` with SQL injection, XSS, and credential validation tests. - Extend `test-roles-permissions.sh` to include unauthorized access and duplicate role creation tests. - Improve `test-users.sh` with checks for invalid emails, duplicate users, and unauthorized actions. - Introduce `test-security.sh` for thorough security assessments including CSRF, SQL injection, XSS, rate limiting, and session management. - Add `.serena.gitignore` and `.serena/project.yml` for project configuration and file management.
158 lines
7.3 KiB
Bash
158 lines
7.3 KiB
Bash
#!/bin/bash
|
|
|
|
# ==============================================================================
|
|
# CMS Tests - Events and Testimonials Endpoints
|
|
# ==============================================================================
|
|
|
|
source "$(dirname "$0")/../common/test-common.sh"
|
|
|
|
test_events_endpoints() {
|
|
printf "\n${CYAN}=== Testing Events Endpoints ===${NC}\n"
|
|
|
|
# Public endpoints
|
|
test_api_endpoint "GET Events List" "GET" "/v1/cms/landing/events" 200 "" false
|
|
test_api_endpoint "GET Events (Paginated)" "GET" "/v1/cms/landing/events?page=1&limit=10" 200 "" false
|
|
test_api_endpoint "GET Events (Search)" "GET" "/v1/cms/landing/events?search=test" 200 "" false
|
|
test_api_endpoint "GET Events (Filter Online)" "GET" "/v1/cms/landing/events?filter=online" 200 "" false
|
|
|
|
# Security: Test SQL injection in search
|
|
test_api_endpoint "GET Events with SQL Injection (Should Be Safe)" "GET" "/v1/cms/landing/events?search=' OR '1'='1" 200 "" false
|
|
|
|
# Get event by ID - use correct endpoint /detail/{id}
|
|
local events_response=$(curl -s "$BASE_URL/v1/cms/landing/events")
|
|
local test_event_id=$(echo "$events_response" | jq -r '.data[0].id // empty')
|
|
|
|
if [ -n "$test_event_id" ]; then
|
|
test_api_endpoint "GET Event By ID" "GET" "/v1/cms/landing/events/detail/$test_event_id" 200 "" false
|
|
fi
|
|
|
|
# Security: Test that create endpoint requires authentication
|
|
local create_event_data=$(jq -n '{
|
|
name: "Unauthorized Event '$(date +%s)'",
|
|
description: "Should not be created",
|
|
start_date: "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'",
|
|
end_date: "'$(date -u -d '+2 hours' +%Y-%m-%dT%H:%M:%SZ)'",
|
|
detail_link: "https://example.com/event",
|
|
price: 0,
|
|
is_online: true
|
|
}')
|
|
test_api_endpoint "POST Create Event without Auth (Should Fail)" "POST" "/v1/cms/landing/events/create" 401 "$create_event_data" false
|
|
|
|
# Create event (protected) - use correct field name
|
|
create_event_data=$(jq -n '{
|
|
name: "Test Event '$(date +%s)'",
|
|
description: "Auto-generated test event",
|
|
start_date: "'$(date -u +%Y-%m-%dT%H:%M:%SZ)'",
|
|
end_date: "'$(date -u -d '+2 hours' +%Y-%m-%dT%H:%M:%SZ)'",
|
|
detail_link: "https://example.com/event",
|
|
price: 0,
|
|
is_online: true
|
|
}')
|
|
local create_event_response=$(test_api_endpoint "POST Create Event" "POST" "/v1/cms/landing/events/create" 201 "$create_event_data" true)
|
|
local created_event_id=$(echo "$create_event_response" | jq -r '.data.id // empty')
|
|
|
|
if [ -n "$created_event_id" ]; then
|
|
# Security: Test XSS in event name
|
|
local xss_event_data=$(jq -n --arg id "$created_event_id" '{
|
|
name: "<script>alert(\"XSS\")</script>",
|
|
description: "XSS test",
|
|
is_online: false
|
|
}')
|
|
test_api_endpoint "PATCH Update Event with XSS (Should Be Sanitized)" "PATCH" "/v1/cms/landing/events/update/$created_event_id" 200 "$xss_event_data" true
|
|
|
|
# Update event - use correct endpoint /update/{id} with PATCH
|
|
local update_event_data=$(jq -n '{
|
|
name: "Updated Test Event",
|
|
description: "Updated description",
|
|
is_online: false
|
|
}')
|
|
test_api_endpoint "PATCH Update Event" "PATCH" "/v1/cms/landing/events/update/$created_event_id" 200 "$update_event_data" true
|
|
|
|
# Security: Test unauthorized update
|
|
local saved_token="$AUTH_TOKEN"
|
|
AUTH_TOKEN=""
|
|
test_api_endpoint "PATCH Update Event without Auth (Should Fail)" "PATCH" "/v1/cms/landing/events/update/$created_event_id" 401 "$update_event_data" false
|
|
AUTH_TOKEN="$saved_token"
|
|
|
|
# Delete event - use correct endpoint /delete/{id}
|
|
test_api_endpoint "DELETE Event" "DELETE" "/v1/cms/landing/events/delete/$created_event_id" 200 "" true
|
|
|
|
# Security: Test unauthorized delete
|
|
AUTH_TOKEN=""
|
|
test_api_endpoint "DELETE Event without Auth (Should Fail)" "DELETE" "/v1/cms/landing/events/delete/$created_event_id" 401 "" false
|
|
AUTH_TOKEN="$saved_token"
|
|
fi
|
|
}
|
|
|
|
test_testimonials_endpoints() {
|
|
printf "\n${CYAN}=== Testing Testimonials Endpoints ===${NC}\n"
|
|
|
|
# Public endpoints
|
|
test_api_endpoint "GET Testimonials List" "GET" "/v1/cms/landing/testimonials" 200 "" false
|
|
test_api_endpoint "GET Testimonials (Paginated)" "GET" "/v1/cms/landing/testimonials?page=1&limit=10" 200 "" false
|
|
test_api_endpoint "GET Testimonials (Search)" "GET" "/v1/cms/landing/testimonials?search=test" 200 "" false
|
|
|
|
# Security: Test SQL injection in search
|
|
test_api_endpoint "GET Testimonials with SQL Injection (Should Be Safe)" "GET" "/v1/cms/landing/testimonials?search=' OR '1'='1" 200 "" false
|
|
|
|
# Get testimonial by ID - use correct endpoint /detail/{id}
|
|
local testimonials_response=$(curl -s "$BASE_URL/v1/cms/landing/testimonials")
|
|
local test_testimonial_id=$(echo "$testimonials_response" | jq -r '.data[0].id // empty')
|
|
|
|
if [ -n "$test_testimonial_id" ]; then
|
|
test_api_endpoint "GET Testimonial By ID" "GET" "/v1/cms/landing/testimonials/detail/$test_testimonial_id" 200 "" false
|
|
fi
|
|
|
|
# Security: Test that create endpoint requires authentication
|
|
local unauth_testimonial_data=$(jq -n '{
|
|
role: "Hacker",
|
|
content: "Unauthorized testimonial"
|
|
}')
|
|
test_api_endpoint "POST Create Testimonial without Auth (Should Fail)" "POST" "/v1/cms/landing/testimonials/create" 401 "$unauth_testimonial_data" false
|
|
|
|
# Create testimonial (protected)
|
|
local create_testimonial_data=$(jq -n '{
|
|
role: "Student",
|
|
content: "This is a test testimonial created at '$(date +%s)'"
|
|
}')
|
|
local create_testimonial_response=$(test_api_endpoint "POST Create Testimonial" "POST" "/v1/cms/landing/testimonials/create" 201 "$create_testimonial_data" true)
|
|
local created_testimonial_id=$(echo "$create_testimonial_response" | jq -r '.data.id // empty')
|
|
|
|
if [ -n "$created_testimonial_id" ]; then
|
|
# Security: Test XSS in testimonial content
|
|
local xss_testimonial_data=$(jq -n '{
|
|
role: "Student",
|
|
content: "<script>alert(\"XSS\")</script>"
|
|
}')
|
|
test_api_endpoint "PATCH Update Testimonial with XSS (Should Be Sanitized)" "PATCH" "/v1/cms/landing/testimonials/update/$created_testimonial_id" 200 "$xss_testimonial_data" true
|
|
|
|
# Update testimonial - use correct endpoint /update/{id} with PATCH
|
|
local update_testimonial_data=$(jq -n '{
|
|
role: "Alumni",
|
|
content: "Updated testimonial content"
|
|
}')
|
|
test_api_endpoint "PATCH Update Testimonial" "PATCH" "/v1/cms/landing/testimonials/update/$created_testimonial_id" 200 "$update_testimonial_data" true
|
|
|
|
# Security: Test unauthorized update
|
|
local saved_token="$AUTH_TOKEN"
|
|
AUTH_TOKEN=""
|
|
test_api_endpoint "PATCH Update Testimonial without Auth (Should Fail)" "PATCH" "/v1/cms/landing/testimonials/update/$created_testimonial_id" 401 "$update_testimonial_data" false
|
|
AUTH_TOKEN="$saved_token"
|
|
|
|
# Delete testimonial - use correct endpoint /delete/{id}
|
|
test_api_endpoint "DELETE Testimonial" "DELETE" "/v1/cms/landing/testimonials/delete/$created_testimonial_id" 200 "" true
|
|
|
|
# Security: Test that non-existent resource returns proper error
|
|
test_api_endpoint "DELETE Non-existent Testimonial (Should Fail)" "DELETE" "/v1/cms/landing/testimonials/delete/00000000-0000-0000-0000-000000000000" 404 "" true
|
|
fi
|
|
}
|
|
|
|
# Run if executed directly
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
get_auth_token
|
|
test_events_endpoints
|
|
test_testimonials_endpoints
|
|
print_test_summary
|
|
[ "$FAIL_COUNT" -eq 0 ] && exit 0 || exit 1
|
|
fi
|