Skip to content

Latest commit

 

History

History
265 lines (205 loc) · 7.08 KB

File metadata and controls

265 lines (205 loc) · 7.08 KB

B2C E-commerce API Testing Guide

🚀 Quick Start

The Spring Boot application is running on http://localhost:8080 with H2 in-memory database.

📋 Testing Methods

1. Using the Test Script (Recommended)

Run the comprehensive test script:

./api-test.sh

This script will test all major endpoints automatically.

2. Using Postman

  1. Import the B2C-Ecommerce-API.postman_collection.json file into Postman
  2. The collection includes all endpoints with proper authentication
  3. Run the requests in sequence (Authentication → Categories → Products → etc.)

3. Using cURL (Manual Testing)

Here are some key cURL commands to test the API:

Authentication

# Register a customer
curl -X POST http://localhost:8080/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "email": "customer@example.com",
    "password": "password123",
    "role": "CUSTOMER",
    "firstName": "John",
    "lastName": "Doe",
    "phone": "+1234567890"
  }'

# Login as customer
curl -X POST http://localhost:8080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "customer@example.com",
    "password": "password123"
  }'

Categories

# Create a category (requires vendor token)
curl -X POST http://localhost:8080/api/categories \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_VENDOR_TOKEN" \
  -d '{
    "name": "Electronics",
    "description": "Electronic devices and accessories",
    "imageUrl": "https://example.com/electronics.jpg"
  }'

# Get all categories
curl -X GET http://localhost:8080/api/categories

Products

# Create a product (requires vendor token)
curl -X POST http://localhost:8080/api/products \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_VENDOR_TOKEN" \
  -d '{
    "name": "Smartphone",
    "description": "Latest smartphone model",
    "price": 599.99,
    "stockQuantity": 50,
    "categoryId": 1,
    "images": "[\"https://example.com/phone1.jpg\", \"https://example.com/phone2.jpg\"]"
  }'

# Get all products
curl -X GET http://localhost:8080/api/products

Cart Operations

# Add item to cart (requires customer token)
curl -X POST http://localhost:8080/api/cart/items \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_CUSTOMER_TOKEN" \
  -d '{
    "productId": 1,
    "quantity": 2
  }'

# Get cart
curl -X GET http://localhost:8080/api/cart \
  -H "Authorization: Bearer YOUR_CUSTOMER_TOKEN"

Orders

# Create an order (requires customer token)
curl -X POST http://localhost:8080/api/orders \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_CUSTOMER_TOKEN" \
  -d '{
    "shippingAddressId": 1
  }'

# Get customer orders
curl -X GET http://localhost:8080/api/orders \
  -H "Authorization: Bearer YOUR_CUSTOMER_TOKEN"

4. Using H2 Console

Access the H2 database console to view data:

  • URL: http://localhost:8080/h2-console
  • JDBC URL: jdbc:h2:mem:testdb
  • Username: sa
  • Password: (leave empty)

🔐 Authentication Flow

  1. Register a user (customer or vendor)
  2. Login to get a JWT token
  3. Use the token in the Authorization: Bearer <token> header for protected endpoints

📊 Expected Response Format

Success Response

{
  "success": true,
  "message": "Operation successful",
  "data": { ... }
}

Error Response

{
  "success": false,
  "message": "Error description",
  "timestamp": "2024-01-01T00:00:00",
  "path": "/api/endpoint"
}

🧪 Testing Scenarios

1. Complete Customer Journey

  1. Register as customer
  2. Login to get token
  3. Browse products
  4. Add items to cart
  5. Create shipping address
  6. Place order
  7. Add review

2. Complete Vendor Journey

  1. Register as vendor
  2. Login to get token
  3. Create categories
  4. Add products
  5. View orders
  6. Update order status

3. Admin Operations

  1. View all users
  2. Verify vendors
  3. Manage system

🔧 Troubleshooting

Common Issues

  1. 401 Unauthorized: Check if you're using a valid JWT token
  2. 403 Forbidden: Check if you have the correct role for the endpoint
  3. 404 Not Found: Check if the endpoint URL is correct
  4. 500 Internal Server Error: Check server logs for details

Debug Mode

To enable debug logging, add to application.yml:

logging:
  level:
    com.example.b2c: DEBUG
    org.springframework.security: DEBUG

📝 API Endpoints Summary

Method Endpoint Description Auth Required
POST /api/auth/register Register user No
POST /api/auth/login Login user No
GET /api/auth/profile Get user profile Yes
GET /api/categories Get all categories No
POST /api/categories Create category Vendor
GET /api/products Get all products No
POST /api/products Create product Vendor
GET /api/cart Get cart Customer
POST /api/cart/items Add to cart Customer
POST /api/orders Create order Customer
GET /api/orders Get orders Customer
POST /api/reviews Create review Customer
POST /api/price-quotes/upload Upload quote Customer

🎯 Next Steps

  1. Test all endpoints using the provided methods
  2. Check the H2 console to verify data persistence
  3. Integrate with your React frontend
  4. Set up MySQL database for production
  5. Configure proper JWT secrets and database credentials

New API Endpoints

Product Endpoints

  • GET /api/products: Search and filter products.
    • Params: keyword (string), categoryId (long), page (int), size (int), sort (string)
    • Access: Public
  • GET /api/products/{id}: Get a single product by its ID.
    • Access: Public
  • POST /api/products: Create a new product.
    • Access: Vendor, Admin
    • Body: ProductRequest DTO. If the user is an admin, vendorId must be provided.
  • PUT /api/products/{id}: Update an existing product.
    • Access: Vendor (own products), Admin
  • DELETE /api/products/{id}: Delete a product.
    • Access: Vendor (own products), Admin

Order Endpoints

  • POST /api/orders/checkout: Create an order from the user's cart.
    • Access: Customer
    • Body: OrderRequest DTO containing shippingAddressId.
  • GET /api/orders/my-orders: Get the currently authenticated customer's order history.
    • Access: Customer
  • GET /api/orders: Get all orders in the system (paginated).
    • Access: Admin

Report Endpoints

  • GET /api/reports/vendor/sales: Generate a sales report for the currently authenticated vendor.
    • Access: Vendor
    • Response: A map containing totalRevenue and totalOrders.