This guide explains how to test the Contact Us API. Two test scripts are provided to help verify API functionality and email configuration.
Simulates website interaction with the API:
# Install test dependencies
pip install -e ".[test]"
# Run the test script (API must be running)
python tests/test_api.py
# With custom options
python tests/test_api.py --url http://localhost:8000 --name "John Doe" --email "john@example.com" --message "Test message" --verboseThe test script follows the complete flow:
- Gets current Unix timestamp
- Requests a token from
/api/token - Requests a signature from
/api/signature - Submits contact information to
/api/contact
The test script automatically reads default values from environment variables or .env file:
- URL: Derived from
CONTACT_API_SERVER_HOSTandCONTACT_API_SERVER_PORT - Origin: Uses the first entry from
CONTACT_API_CORS_ALLOWED_ORIGINS
You can override these with command-line arguments:
--url: Override the API URL (e.g.,--url http://localhost:8000)--origin: Override the origin header (e.g.,--origin https://www.example.com)--name,--email,--message: Override form data--verbose: Enable verbose output
Tests SMTP email configuration by sending a test email:
# Test email credentials (no API server needed)
python tests/test_email.py contact@example.comThe test script reads SMTP configuration from environment variables or .env file.
This script:
- Loads SMTP configuration from environment variables or
.envfile - Attempts to send a test email to the specified address
- Reports success or failure with helpful error messages
- Has a 15-second timeout to prevent hanging on slow/unresponsive SMTP servers
- Useful for verifying SMTP credentials before deploying
If the email test fails, common issues include:
- Incorrect SMTP host or port
- Wrong username or password
- SMTP server requires authentication
- Firewall blocking SMTP connection
- SMTP server requires TLS/SSL
- Network connectivity issues
If you need to open the firewall for API testing, use the appropriate command for your firewall system.
# Temporarily open port 8000
sudo firewall-cmd --zone=your_zone_name_here --add-port=8000/tcp
# Make it permanent
sudo firewall-cmd --zone=your_zone_name_here --add-port=8000/tcp --permanent# Allow port 8000
sudo ufw allow 8000/tcpNote: In production, restrict access to only the reverse proxy (e.g., nginx) rather than exposing the API port directly.