This guide shows multiple ways to test the FastAPI User API.
- Install dependencies:
pip install -r requirements.txt-
Make sure your
.envfile has theMONGODB_URIconfigured. -
Start the FastAPI server:
python main.pyor
uvicorn main:app --reloadThe API will be available at http://localhost:8000
FastAPI automatically generates interactive API documentation:
-
Swagger UI: Open
http://localhost:8000/docsin your browser- Interactive interface to test all endpoints
- Click "Try it out" on any endpoint
- Fill in the request body and click "Execute"
-
ReDoc: Open
http://localhost:8000/redocin your browser- Alternative documentation format
- More readable but less interactive
Run the provided test script:
# Install requests if not already installed
pip install requests
# Run the test script
python test_api.pyThis will test all endpoints and show you example requests/responses.
curl http://localhost:8000/healthcurl -X POST "http://localhost:8000/api/users/" \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123",
"role": "tenant",
"status": "active"
}'curl http://localhost:8000/api/users/curl "http://localhost:8000/api/users/?role=landlord"curl "http://localhost:8000/api/users/?status=active"# Replace USER_ID with actual ID from create response
curl http://localhost:8000/api/users/USER_IDcurl http://localhost:8000/api/users/email/john@example.comcurl -X PUT "http://localhost:8000/api/users/USER_ID" \
-H "Content-Type: application/json" \
-d '{
"name": "John Updated",
"status": "suspended"
}'curl -X POST "http://localhost:8000/api/users/USER_ID/status" \
-H "Content-Type: application/json" \
-d '{
"status": "suspended"
}'curl -X DELETE http://localhost:8000/api/users/USER_ID-
Import the API endpoints:
- Base URL:
http://localhost:8000 - Endpoints are under
/api/users/
- Base URL:
-
Example request for creating a user:
- Method:
POST - URL:
http://localhost:8000/api/users/ - Headers:
Content-Type: application/json - Body (JSON):
{ "name": "Test User", "email": "test@example.com", "password": "test123", "role": "tenant", "status": "active" }
- Method:
import requests
import json
BASE_URL = "http://localhost:8000"
# Create a user
response = requests.post(
f"{BASE_URL}/api/users/",
json={
"name": "Alice Tenant",
"email": "alice@example.com",
"password": "secure123",
"role": "tenant"
}
)
print(json.dumps(response.json(), indent=2))
# Get the user ID from response
user_id = response.json()["id"]
# List all users
response = requests.get(f"{BASE_URL}/api/users/")
print(json.dumps(response.json(), indent=2))
# Update the user
response = requests.put(
f"{BASE_URL}/api/users/{user_id}",
json={"name": "Alice Updated"}
)
print(json.dumps(response.json(), indent=2))- Create an admin user:
curl -X POST "http://localhost:8000/api/users/" \
-H "Content-Type: application/json" \
-d '{"name":"Admin","email":"admin@rentshield.com","password":"admin123","role":"admin"}'- Create a landlord:
curl -X POST "http://localhost:8000/api/users/" \
-H "Content-Type: application/json" \
-d '{"name":"Landlord Smith","email":"landlord@rentshield.com","password":"landlord123","role":"landlord"}'- Create a tenant:
curl -X POST "http://localhost:8000/api/users/" \
-H "Content-Type: application/json" \
-d '{"name":"Tenant Jones","email":"tenant@rentshield.com","password":"tenant123","role":"tenant"}'- List all landlords:
curl "http://localhost:8000/api/users/?role=landlord"- Suspend a tenant:
# First get the tenant ID, then:
curl -X POST "http://localhost:8000/api/users/TENANT_ID/status" \
-H "Content-Type: application/json" \
-d '{"status":"suspended"}'{
"id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"role": "tenant",
"status": "active",
"createdAt": "2026-02-12T20:00:00",
"updatedAt": "2026-02-12T20:00:00"
}{
"users": [
{
"id": "507f1f77bcf86cd799439011",
"name": "John Doe",
"email": "john@example.com",
"role": "tenant",
"status": "active"
}
],
"total": 1,
"skip": 0,
"limit": 100
}{
"detail": "Error message here"
}- Connection refused: Make sure the server is running (
python main.py) - MongoDB connection error: Check your
MONGODB_URIin.env - Invalid user ID: Make sure you're using the correct MongoDB ObjectId format
- Duplicate email: Each email must be unique
adminlandlordtenant
Common statuses:
activesuspendedinactive
(You can use any string value for status)