Skip to content

sh873-sam/module10-fastapi-auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FastAPI User Authentication API (Module 10)

πŸ“Œ Overview

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.


πŸš€ Features

  • 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

πŸ› οΈ Tech Stack

  • FastAPI
  • Python 3.12
  • SQLAlchemy
  • Pydantic
  • Passlib (bcrypt)
  • Pytest
  • GitHub Actions (CI/CD)

πŸ“‚ Project Structure

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

▢️ Running the Application

1. Create virtual environment

python -m venv venv

2. Activate environment

# Windows
venv\Scripts\activate

# Mac/Linux
source venv/bin/activate

3. Install dependencies

pip install -r requirements.txt

4. Run the server

uvicorn main:app --reload

5. Open in browser

http://127.0.0.1:8000/docs

πŸ§ͺ Running Tests

pytest

πŸ” API Endpoint

Create User

POST /users/

Request Body

{
  "username": "user1",
  "email": "user1@example.com",
  "password": "password123"
}

Response

{
  "id": 1,
  "username": "user1",
  "email": "user1@example.com"
}

⚠️ Error Handling

Duplicate User

{
  "detail": "Username or email already exists"
}

πŸ”„ CI/CD

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

🐳 Docker Setup & Deployment

This project is containerized using Docker for easy setup and deployment.


πŸ“¦ Build Docker Image

From the root of the project directory, run:

docker build -t YOUR_DOCKER_USERNAME/fastapi-auth-api .

▢️ Run Docker Container

docker run -d -p 8000:8000 YOUR_DOCKER_USERNAME/fastapi-auth-api

Then open:

http://127.0.0.1:8000/docs

πŸ“€ Push to Docker Hub

  1. Log in to Docker:
docker login
  1. Push your image:
docker push sh8733/module10-fastapi-auth

πŸ”— Docker Hub Repository

https://hub.docker.com/repository/docker/sh8733/module10-fastapi-auth/general


πŸ“Œ Notes

  • Ensure Docker Desktop is running before building the image
  • The container runs the FastAPI app using Uvicorn
  • Port 8000 is exposed for API access
  • Update YOUR_DOCKER_USERNAME with your actual Docker Hub username

πŸ“Œ Notes

  • 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

πŸ”— Useful Links


About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors