Skip to content

daimouchayasser/RESTful-API-for-a-Mock-Bank

Repository files navigation

Mock Bank API

A RESTful API for a mock banking system built with FastAPI, featuring user authentication, account management, and money transfers.

Features

  • User Management: Registration and authentication with JWT tokens
  • Account Management: Create accounts, check balances, view statements
  • Money Transfers: Secure transfers between accounts
  • Authentication: JWT-based authentication for all protected endpoints
  • Database: SQLite in-memory database with SQLAlchemy ORM
  • Validation: Pydantic models for request/response validation
  • Documentation: Auto-generated OpenAPI/Swagger documentation
  • Testing: Comprehensive integration tests

API Endpoints

Authentication

  • POST /register - Register a new user
  • POST /login - Login and get JWT token

Account Management

  • GET /account/{account_id}/balance - Get account balance
  • GET /account/{account_id}/statement - Get account transaction statement

Transfers

  • POST /transfer - Transfer money between accounts

Health Check

  • GET /health - API health status

Installation

  1. Clone the repository

    git clone <repository-url>
    cd RESTful-API-for-a-Mock-Bank
  2. Install dependencies

    pip install -r requirements.txt
  3. Run the application

    python main.py

    The API will be available at http://localhost:8000

  4. Access the documentation

    • Swagger UI: http://localhost:8000/docs
    • ReDoc: http://localhost:8000/redoc

Usage Examples

1. Register a User

curl -X POST "http://localhost:8000/register" \
  -H "Content-Type: application/json" \
  -d '{
    "email": "user@example.com",
    "password": "password123",
    "full_name": "John Doe"
  }'

2. Login

curl -X POST "http://localhost:8000/login" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "email=user@example.com&password=password123"

3. Check Account Balance

curl -X GET "http://localhost:8000/account/1/balance" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

4. Transfer Money

curl -X POST "http://localhost:8000/transfer" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "from_account_id": 1,
    "to_account_id": 2,
    "amount": 100.0,
    "description": "Payment for services"
  }'

5. Get Account Statement

curl -X GET "http://localhost:8000/account/1/statement" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Running Tests

Run All Tests

python run_tests.py

Run Individual Test Suites

# Authentication tests
pytest test_auth.py -v

# Account management tests
pytest test_accounts.py -v

# Transfer tests
pytest test_transfer.py -v

Test Coverage

The test suite includes:

  • User registration and authentication
  • Account balance and statement retrieval
  • Money transfer functionality
  • Error handling and validation
  • Security and authorization tests

Database Schema

Users Table

  • id: Primary key
  • email: Unique email address
  • hashed_password: Bcrypt hashed password
  • full_name: User's full name
  • created_at: Account creation timestamp

Accounts Table

  • id: Primary key
  • account_number: Unique account number
  • balance: Current account balance
  • account_type: Type of account (default: "checking")
  • user_id: Foreign key to users table
  • created_at: Account creation timestamp

Transactions Table

  • id: Primary key
  • amount: Transaction amount (positive for deposits, negative for withdrawals)
  • transaction_type: Type of transaction
  • description: Transaction description
  • from_account_id: Source account (for transfers)
  • to_account_id: Destination account (for transfers)
  • account_id: Account this transaction belongs to
  • created_at: Transaction timestamp

Security Features

  • JWT Authentication: All protected endpoints require valid JWT tokens
  • Password Hashing: Passwords are hashed using bcrypt
  • Input Validation: All inputs are validated using Pydantic models
  • Authorization: Users can only access their own accounts
  • SQL Injection Protection: Using SQLAlchemy ORM prevents SQL injection

Error Handling

The API provides comprehensive error handling:

  • 400 Bad Request: Invalid input data
  • 401 Unauthorized: Invalid or missing authentication
  • 403 Forbidden: Access denied
  • 404 Not Found: Resource not found
  • 500 Internal Server Error: Server errors

Development

Project Structure

RESTful-API-for-a-Mock-Bank/
├── main.py              # Main FastAPI application
├── requirements.txt     # Python dependencies
├── test_auth.py        # Authentication tests
├── test_accounts.py    # Account management tests
├── test_transfer.py    # Transfer functionality tests
├── run_tests.py        # Test runner script
└── README.md           # This file

Adding New Features

  1. Add new models to main.py
  2. Create new endpoints with proper authentication
  3. Add corresponding tests
  4. Update this README

License

This project is for educational purposes only.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages