This project is a FastAPI-based user management API that demonstrates secure user creation, validation, password hashing, and automated testing. It follows best practices for API design, testing, and CI/CD.
- User creation endpoint (
POST /users/) - Input validation using Pydantic
- Secure password hashing (no plain-text storage)
- Duplicate user prevention
- Integration and unit testing with pytest
- Continuous Integration using GitHub Actions
- FastAPI
- Python 3.12
- SQLAlchemy
- Pydantic
- Passlib (bcrypt)
- Pytest
- GitHub Actions (CI/CD)
app/
βββ models.py # Database models
βββ schemas.py # Pydantic schemas
βββ crud.py # Database operations
βββ auth.py # Password hashing logic
tests/
βββ test_auth.py
βββ test_schemas.py
βββ test_users_integration.py
.github/workflows/
βββ test.yml # CI pipeline
main.py # FastAPI app entry point
requirements.txt
python -m venv venv# Windows
venv\Scripts\activate
# Mac/Linux
source venv/bin/activatepip install -r requirements.txtuvicorn main:app --reloadhttp://127.0.0.1:8000/docs
pytestPOST /users/
{
"username": "user1",
"email": "user1@example.com",
"password": "password123"
}{
"id": 1,
"username": "user1",
"email": "user1@example.com"
}{
"detail": "Username or email already exists"
}This project uses GitHub Actions to automatically run tests on every push.
- Workflow file:
.github/workflows/test.yml - Ensures all tests pass before code is considered valid
This project is containerized using Docker for easy setup and deployment.
From the root of the project directory, run:
docker build -t YOUR_DOCKER_USERNAME/fastapi-auth-api .docker run -d -p 8000:8000 YOUR_DOCKER_USERNAME/fastapi-auth-apiThen open:
http://127.0.0.1:8000/docs
- Log in to Docker:
docker login- Push your image:
docker push sh8733/module10-fastapi-authhttps://hub.docker.com/repository/docker/sh8733/module10-fastapi-auth/general
- Ensure Docker Desktop is running before building the image
- The container runs the FastAPI app using Uvicorn
- Port
8000is exposed for API access - Update
YOUR_DOCKER_USERNAMEwith your actual Docker Hub username
- Passwords are securely hashed using bcrypt
- Sensitive data (passwords) are never returned in API responses
- The database file (
test.db) is ignored to prevent test conflicts
- FastAPI Docs: https://fastapi.tiangolo.com/
- Pytest Docs: https://docs.pytest.org/
- GitHub Actions: https://docs.github.com/actions