Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 2.66 KB

File metadata and controls

87 lines (62 loc) · 2.66 KB

Testing

This guide explains how to test the Contact Us API. Two test scripts are provided to help verify API functionality and email configuration.

API Test Script

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" --verbose

The test script follows the complete flow:

  1. Gets current Unix timestamp
  2. Requests a token from /api/token
  3. Requests a signature from /api/signature
  4. Submits contact information to /api/contact

Configuration

The test script automatically reads default values from environment variables or .env file:

  • URL: Derived from CONTACT_API_SERVER_HOST and CONTACT_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

Email Credentials Test Script

Tests SMTP email configuration by sending a test email:

# Test email credentials (no API server needed)
python tests/test_email.py contact@example.com

The test script reads SMTP configuration from environment variables or .env file.

This script:

  • Loads SMTP configuration from environment variables or .env file
  • 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

Troubleshooting Email Tests

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

Firewall Configuration

If you need to open the firewall for API testing, use the appropriate command for your firewall system.

firewalld

# 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

ufw

# Allow port 8000
sudo ufw allow 8000/tcp

Note: In production, restrict access to only the reverse proxy (e.g., nginx) rather than exposing the API port directly.