A simple Flask REST API for managing stock trades - perfect for learning functional testing!
⚠️ Disclaimer: This is an example application for educational purposes only. It is not production-ready. Never hardcode credentials or API keys in real applications - use environment variables or a secrets manager instead.
📝 Exercise: Ready to write your own tests? Check out the Functional Test Exercise!
- Python 3.7 or higher
- pip (Python package installer)
See the Installation Guide for detailed instructions on installing Python and pip.
pip install -r requirements.txtpython app.pyThe API runs at http://127.0.0.1:5000
Import stock-trading-api.postman_collection.json into Postman to explore the API endpoints.
The collection includes two variables pre-configured:
base_url- set tohttp://127.0.0.1:5000api_key- set totest-key-123
- Download and install Postman
- Open Postman
- Click Import in the top left corner
- Select File and choose
stock-trading-api.postman_collection.json - Click Import to confirm
The collection will appear in your Collections sidebar, ready to use.
For more details see the official Postman import guide.
Most endpoints require an API key. Include it in the X-API-Key header.
Valid API Keys:
test-key-123demo-key-456
GET /stocksGET /stocks/{symbol}GET /trades
X-API-Key: test-key-123GET /trades/{id}
X-API-Key: test-key-123POST /trades
X-API-Key: test-key-123
Content-Type: application/json
{
"symbol": "AAPL",
"quantity": 10,
"action": "buy"
}PUT /trades/{id}
X-API-Key: test-key-123
Content-Type: application/json
{
"quantity": 15
}DELETE /trades/{id}
X-API-Key: test-key-123GET /portfolio
X-API-Key: test-key-123GET /portfolio/value
X-API-Key: test-key-123GET /account/balance
X-API-Key: test-key-123POST /account/deposit
X-API-Key: test-key-123
Content-Type: application/json
{
"amount": 1000.00
}POST /account/withdraw
X-API-Key: test-key-123
Content-Type: application/json
{
"amount": 500.00
}# Get all available stocks (no auth)
curl http://127.0.0.1:5000/stocks
# Get specific stock (no auth)
curl http://127.0.0.1:5000/stocks/AAPL
# Deposit funds (with auth)
curl -X POST http://127.0.0.1:5000/account/deposit \
-H "Content-Type: application/json" \
-H "X-API-Key: test-key-123" \
-d '{"amount":10000}'
# Execute a buy trade (with auth)
curl -X POST http://127.0.0.1:5000/trades \
-H "Content-Type: application/json" \
-H "X-API-Key: test-key-123" \
-d '{"symbol":"AAPL","quantity":10,"action":"buy"}'
# Get all trades (with auth)
curl -H "X-API-Key: test-key-123" http://127.0.0.1:5000/trades
# Update a trade (with auth)
curl -X PUT http://127.0.0.1:5000/trades/1 \
-H "Content-Type: application/json" \
-H "X-API-Key: test-key-123" \
-d '{"quantity":15}'
# Get portfolio (with auth)
curl -H "X-API-Key: test-key-123" http://127.0.0.1:5000/portfolio
# Get portfolio value (with auth)
curl -H "X-API-Key: test-key-123" http://127.0.0.1:5000/portfolio/value
# Get account balance (with auth)
curl -H "X-API-Key: test-key-123" http://127.0.0.1:5000/account/balance
# Cancel a trade (with auth)
curl -X DELETE -H "X-API-Key: test-key-123" http://127.0.0.1:5000/trades/1# Install test dependencies
pip install -r requirements.txt
# Run unit tests (no server needed)
pytest tests/unit/ -v
# Run functional tests (requires running server)
# Start the API server (in one terminal)
python app.py
# Run tests (in another terminal)
pytest tests/functional/ -v
# Run all tests
pytest tests/ -v
# Run tests by marker
pytest -m unit -v
pytest -m functional -v
pytest -m positive -v
pytest -m negative -v