# Farm Produce Backend - API Testing Flow Guide ## Prerequisites 1. Ensure your backend server is running on `http://localhost:5000` 2. MongoDB is connected and running 3. Create an `uploads` folder in your project root 4. Set up your `.env` file with all required variables ## Testing Flow Overview ### Phase 1: Initial Setup & Authentication **Purpose**: Set up users and authenticate them to get tokens 1. **Health Check** - Test: `GET /api/health` - Purpose: Verify server is running - Expected: Status 200 with server info 2. **Register Admin** - Test: `POST /api/auth/register` - Purpose: Create admin user - Expected: Admin user created, token returned - **Important**: Save the token for admin operations 3. **Register Farmer** - Test: `POST /api/auth/register` - Purpose: Create farmer user (unverified by default) - Expected: Farmer user created, token returned, `isVerified: false` - **Important**: Save both token and farmer ID 4. **Register Customer** - Test: `POST /api/auth/register` - Purpose: Create customer user (auto-verified) - Expected: Customer user created, token returned, `isVerified: true` ### Phase 2: Admin Operations **Purpose**: Admin verifies farmers and manages the platform 5. **Login Admin** (if needed) - Test: `POST /api/auth/login` - Purpose: Get fresh admin token 6. **Get Admin Profile** - Test: `GET /api/auth/profile` - Headers: `Authorization: Bearer ` - Purpose: Verify admin authentication 7. **Get Dashboard Stats** - Test: `GET /api/admin/dashboard-stats` - Headers: `Authorization: Bearer ` - Purpose: Check platform statistics 8. **Get Pending Farmers** - Test: `GET /api/admin/pending-farmers` - Headers: `Authorization: Bearer ` - Purpose: See farmers waiting for verification - Expected: Should show the farmer registered in step 3 9. **Approve Farmer** - Test: `PUT /api/admin/verify-farmer/{farmerId}` - Headers: `Authorization: Bearer ` - Body: `{"action": "approve"}` - Purpose: Verify the farmer account - **Important**: Use the farmer ID from step 3 10. **Get All Users** - Test: `GET /api/admin/users` - Headers: `Authorization: Bearer ` - Purpose: View all registered users ### Phase 3: Farmer Operations **Purpose**: Farmer adds products and manages orders 11. **Login Farmer** (if needed) - Test: `POST /api/auth/login` - Purpose: Get fresh farmer token 12. **Get Farmer Dashboard Stats** - Test: `GET /api/farmer/dashboard-stats` - Headers: `Authorization: Bearer ` - Purpose: Check farmer's statistics 13. **Add Product (Text Only)** - Test: `POST /api/farmer/products` - Headers: `Authorization: Bearer ` - Body: JSON with product details - Purpose: Add a product without images - **Important**: Save the product ID from response 14. **Add Product with Images** (Optional) - Test: `POST /api/farmer/products` - Headers: `Authorization: Bearer ` - Body: Form-data with product details and image files - Purpose: Add a product with images - **Note**: You'll need actual image files for this test 15. **Get My Products** - Test: `GET /api/farmer/products` - Headers: `Authorization: Bearer ` - Purpose: View farmer's products 16. **Update Product** - Test: `PUT /api/farmer/products/{productId}` - Headers: `Authorization: Bearer ` - Body: Updated product fields - Purpose: Modify existing product ### Phase 4: Customer Operations **Purpose**: Customer browses products and places orders 17. **Get All Products (Public)** - Test: `GET /api/customer/products` - No authentication required - Query parameters: `?page=1&limit=10&category=vegetable&search=tomato` - Purpose: Browse available products 18. **Get Product Details (Public)** - Test: `GET /api/customer/products/{productId}` - No authentication required - Purpose: View detailed product information 19. **Login Customer** (if needed) - Test: `POST /api/auth/login` - Purpose: Get fresh customer token 20. **Place Order** - Test: `POST /api/customer/orders` - Headers: `Authorization: Bearer ` - Body: Order details with product IDs and quantities - Purpose: Create new order - **Important**: Save the order ID from response 21. **Get My Orders** - Test: `GET /api/customer/orders` - Headers: `Authorization: Bearer ` - Purpose: View customer's orders 22. **Get Order Details** - Test: `GET /api/customer/orders/{orderId}` - Headers: `Authorization: Bearer ` - Purpose: View specific order details ### Phase 5: Order Management **Purpose**: Test order lifecycle and status updates 23. **Farmer: Get My Orders** - Test: `GET /api/farmer/orders` - Headers: `Authorization: Bearer ` - Purpose: View orders received by farmer 24. **Farmer: Update Order Status** - Test: `PUT /api/farmer/orders/{orderId}/status` - Headers: `Authorization: Bearer ` - Body: `{"status": "processing", "note": "Order being prepared"}` - Purpose: Update order status 25. **Customer: Cancel Order** (Optional) - Test: `PUT /api/customer/orders/{orderId}/cancel` - Headers: `Authorization: Bearer ` - Purpose: Cancel an order (only if status is 'placed') ## Important Testing Notes ### Authentication Flow - **Admin**: Can access all admin routes - **Farmer**: Must be verified by admin before accessing farmer routes - **Customer**: Auto-verified, can access customer routes immediately ### Token Management - All protected routes require `Authorization: Bearer ` header - Tokens are automatically saved in Postman variables when you run the registration/login requests ### Error Scenarios to Test 1. **Unauthorized Access**: Try accessing protected routes without tokens 2. **Invalid Credentials**: Login with wrong email/password 3. **Role-based Access**: Try accessing admin routes with farmer token 4. **Unverified Farmer**: Try adding products before farmer verification 5. **Invalid Product ID**: Use non-existent product ID in requests 6. **Insufficient Stock**: Try ordering more quantity than available 7. **Order Status Validation**: Try canceling an order that's already dispatched ### Data Dependencies - **Products**: Require verified farmers - **Orders**: