FastAPI RBAC - User Management Microservice¶
A comprehensive Role-Based Access Control (RBAC) system with FastAPI backend and React frontend, designed to handle Authentication and Authorization for other services.
π Quick Start¶
New to the project?¶
Start here: docs/getting-started/GETTING_STARTED.md
Want to run immediately?¶
git clone <repository-url>
cd fastapi_rbac
docker-compose up -d
# Access at:
# Frontend: http://localhost:80
# Backend API: http://localhost:8000/docs
π Documentation¶
All documentation has been organized for easy navigation:
- π Getting Started - New developer onboarding
- π» Development - Development setup and workflows
- π Deployment - Production deployment guides
- π§ Troubleshooting - Problem-solving guides
- π Reference - Technical reference materials
π οΈ Scripts & Utilities¶
Utility scripts are organized by purpose in the scripts/
directory:
- Development:
scripts/dev/
- Local development helpers - Docker:
scripts/docker/
- Container operations & production builds - Deployment:
scripts/deployment/
- Release and deployment - Database:
scripts/database/
- Database management
β¨ Key Features¶
π Enterprise Security¶
- π JWT Authentication: Secure token-based auth with refresh tokens
- π‘οΈ CSRF Protection: Complete protection against Cross-Site Request Forgery
- π§½ Input Sanitization: XSS prevention with comprehensive HTML cleaning
- β‘ Rate Limiting: DoS attack prevention on all critical endpoints
- π Security Headers: Strict Content Security Policy and browser protections
π₯ Access Control & Management¶
- π₯ Role-Based Access Control: Flexible permission system with role hierarchies
- π― User Management: Complete CRUD operations with admin oversight
- π’ Organization Management: Role groups and permission groups for enterprise use
π§ͺ Quality & Testing¶
- π§ͺ Comprehensive Testing: 90+ backend tests + 354 frontend tests across 16 files
- All async DB queries in backend and tests use SQLModelβs
.exec()
idiom withAsyncSession
(not.execute()
). - See
backend/test/README.md
for full test/factory/fixture/optimization details. - β‘ Frontend Testing: Complete coverage with Vitest, React Testing Library
- π API Testing: Comprehensive service layer testing with mocking
π Infrastructure & Integration¶
- π§ Email Integration: Notifications and password reset functionality
- β‘ Background Tasks: Celery integration for async operations
- π³ Docker Ready: Full containerization with production configs
- π API Documentation: Auto-generated OpenAPI/Swagger docs
ποΈ Project Structure¶
fastapi_rbac/
βββ π backend/ # FastAPI application
β βββ app/ # Main application code
β βββ alembic/ # Database migrations
β βββ tests/ # Backend tests
βββ π react-frontend/ # React TypeScript app
β βββ src/ # Frontend source code
β βββ public/ # Static assets
βββ π docs/ # π Organized documentation
β βββ getting-started/ # New developer guides
β βββ development/ # Development workflows
β βββ deployment/ # Production guides
β βββ troubleshooting/ # Problem solving
β βββ reference/ # Technical references
βββ π scripts/ # π οΈ Utility scripts
β βββ dev/ # Development helpers
β βββ docker/ # Container operations
β βββ deployment/ # Release scripts
β βββ database/ # DB management
βββ π docker-compose.dev.yml # Development environment
βββ π docker-compose.test.yml # Testing environment
βββ π docker-compose.prod-test.yml # Production testing environment
π― Development Workflow¶
1. First Time Setup¶
# Follow the comprehensive setup guide
# This covers IDE setup, dependencies, and configuration
.\docs\development\DEVELOPER_SETUP.md
2. Daily Development¶
# Start development environment
docker-compose up -d
# Run backend tests
.\scripts\dev\run-tests.ps1
# Run frontend tests
cd react-frontend
npm test
# Access services:
# - Frontend: http://localhost:80
# - Backend: http://localhost:8000
# - API Docs: http://localhost:8000/docs
3. Before Deployment¶
# Validate production setup
.\scripts\docker\validate-config.ps1 -Validate
# Test production configuration locally
.\scripts\docker\test-production.ps1
π§ Common Operations¶
Task | Command | Documentation |
---|---|---|
Start development | docker-compose up -d |
Getting Started |
Run backend tests | .\scripts\dev\run-tests.ps1 |
Testing Guide |
Run frontend tests | cd react-frontend && npm test |
Frontend Testing |
Security validation | python backend/test/test_csrf_implementation.py |
Security Features |
Deploy to production | .\scripts\deployment\push-to-dockerhub.ps1 |
Deployment |
Troubleshoot CORS | .\scripts\docker\diagnose-cors.ps1 |
CORS Guide |
Database migration | .\scripts\database\migrate-db.ps1 |
DB Reference |
π§ͺ Integration Testing Environments¶
This project supports both Docker Compose-based and local integration testing for the backend.
-
Docker Compose-based testing (recommended for CI and team consistency):
-
Uses
backend/.env.test
for environment variables. - Database and Redis hostnames:
DATABASE_HOST=fastapi_rbac_db_test
REDIS_HOST=fastapi_rbac_redis_test
REDIS_URL=redis://fastapi_rbac_redis_test:6379/0
-
Run with:
-
Local testing (if you want to run tests outside Docker):
- Uses
backend/.env.test.local
for environment variables. - Database and Redis hostnames:
DATABASE_HOST=localhost
REDIS_HOST=localhost
REDIS_URL=redis://localhost:6379/0
- Make sure Postgres and Redis are running locally.
- Run tests with:
See comments in backend/.env.example
for more details.
Running Tests with test_runner.py¶
All backend test running is now managed through a single script: backend/test_runner.py
.
- Run all tests:
- Run unit tests only:
- Run integration tests only:
- Run a specific test file:
- Run the comprehensive demo suite:
- Other options: See
python backend/test_runner.py --help
for more.
Note: All previous test scripts (
run_tests.py
,run_comprehensive_tests.py
,test_all_units.py
,run_final_tests.py
) have been removed. Use onlytest_runner.py
for all test operations.
- For full details on test/factory/fixture usage, see
backend/test/README.md
.
π Need Help?¶
- π Check documentation: Start with Getting Started
- π§ Browse troubleshooting: See troubleshooting guides
- π¬ Ask the team: Create an issue with detailed information
- π Read the code: Well-documented with inline comments
π€ Contributing¶
- Read the Development Setup
- Follow the Coding Standards
- Write tests for new features
- Update documentation as needed
Ready to get started? π Click here to begin!
Run the backend service¶
uvicorn app.main:app --port 8001 --reload
### Frontend
```bash
# Navigate to frontend directory
cd react-frontend
# Install dependencies
npm install
# Run the development server
npm run dev
Docker-based Development¶
Run the entire stack:
Run only specific components:
# Just the backend
cd backend
docker-compose up -d
# Just the frontend
cd react-frontend
docker-compose up -d
Documentation¶
- Backend Documentation
- Frontend Documentation
- API Documentation (when backend is running)
License¶
This project is licensed under the terms of the MIT license.