This guide explains how to test the User Backend API endpoints using Postman or any other API testing tool.
- Start the server:
npm start
- Import the Postman collection:
- Open Postman
- Import
Contact_Management_API.postman_collection.json
Follow this sequence to test all endpoints:
- Endpoint:
POST /api/users/register
- Body:
{
"username": "testuser",
"email": "test@example.com",
"password": "test123"
}
- Expected: Returns user ID and email
- Endpoint:
POST /api/users/login
- Body:
{
"email": "test@example.com",
"password": "test123"
}
- Expected: Returns access token
- Note: Save the token for subsequent requests
- Endpoint:
GET /api/users/current
- Headers:
Authorization: Bearer <your_token>
- Expected: Returns user details
- Endpoint:
POST /api/contacts
- Headers:
Authorization: Bearer <your_token>
- Body:
{
"name": "John Doe",
"email": "john@example.com",
"mobile": "1234567890"
}
- Expected: Returns created contact
- Endpoint:
GET /api/contacts
- Headers:
Authorization: Bearer <your_token>
- Expected: Returns array of contacts
- Endpoint:
GET /api/contacts/:id
- Headers:
Authorization: Bearer <your_token>
- Expected: Returns contact details
- Endpoint:
PUT /api/contacts/:id
- Headers:
Authorization: Bearer <your_token>
- Body:
{
"name": "Jane Doe",
"email": "jane@example.com",
"mobile": "0987654321"
}
- Expected: Returns updated contact
- Endpoint:
DELETE /api/contacts/:id
- Headers:
Authorization: Bearer <your_token>
- Expected: Returns success message
- Endpoint:
DELETE /api/users/delete-user
- Headers:
Authorization: Bearer <your_token>
- Expected: Returns success message
Run the automated test suite:
npm test
This will run all tests and show the results in the console.
-
401 Unauthorized
- Check if your token is valid
- Make sure to include the token in the Authorization header
- Token format should be:
Bearer <your_token>
-
404 Not Found
- Verify the endpoint URL
- Check if the resource (user/contact) exists
-
400 Bad Request
- Check if all required fields are provided
- Verify the data format
-
403 Forbidden
- You're trying to access/modify a resource that doesn't belong to you
- Keep the access token handy - you'll need it for most requests
- Test endpoints in the order listed above
- Save example responses for reference
- Check error responses for helpful messages