tests/
├── unit/ # Unit tests for individual components
│ ├── user.model.test.js # User model validation & database operations
│ ├── notes.model.test.js # Notes model validation & database operations
│ ├── authMiddleware.test.js # Authentication middleware tests
│ ├── validation.test.js # Input validation schema tests
│ └── database.test.js # Database connection tests
├── integration/ # Integration tests for API endpoints
│ ├── auth.routes.test.js # Authentication routes testing
│ └── notes.routes.test.js # Notes CRUD routes testing
└── utils/ # Test utilities and helpers
├── setup.js # Jest setup with MongoDB Memory Server
└── helpers.js # Test helper functions
Additional files:
├── jest.config.js # Jest configuration
├── run-tests.js # Custom test runner script
└── test-api.js # API health check script
-
Validation Tests (11 tests):
- ✅ Valid user creation
- ✅ Required field validation (username, fullName, email, password)
- ✅ Email format validation
- ✅ Username length constraints (3-30 characters)
- ✅ Password minimum length (6 characters)
- ✅ Field trimming and email lowercase conversion
-
Database Operations (3 tests):
- ✅ User saving with timestamps
- ✅ Unique username constraint
- ✅ Unique email constraint
-
Validation Tests (10 tests):
- ✅ Valid note creation
- ✅ Required field validation (user_id, title, description)
- ✅ Title length constraints (5-100 characters)
- ✅ Description length constraints (5-500 characters)
- ✅ Field trimming
- ✅ ObjectId validation for user_id
-
Database Operations (3 tests):
- ✅ Note saving with user reference
- ✅ User population in queries
- ✅ Multiple notes per user support
- ✅ Valid token authentication
- ✅ Missing token handling
- ✅ Invalid token handling
- ✅ Expired token handling
- ✅ Wrong signature handling
- ✅ Malformed token handling
- ✅ Empty token handling
- ✅ Title validation rules
- ✅ Description validation rules
- ✅ Error message accuracy
- ✅ Schema export verification
- ✅ Successful connection
- ✅ Missing URI handling
- ✅ Connection error handling
- ✅ Mongoose configuration
POST /api/user/register:
- ✅ Successful user registration
- ✅ Missing field validation
- ✅ Duplicate username handling
- ✅ Duplicate email handling
- ✅ Database error handling
POST /api/user/login:
- ✅ Username/password login
- ✅ Email/password login
- ✅ Missing credentials handling
- ✅ Invalid user handling
- ✅ Wrong password handling
- ✅ Secure cookie configuration
GET /api/user/logout:
- ✅ Successful logout
- ✅ Cookie clearing
GET /api/user/refetch:
- ✅ Valid token user fetch
- ✅ Missing token handling
- ✅ Invalid token handling
- ✅ Expired token handling
- ✅ Deleted user handling
GET /api/v1/notes:
- ✅ Fetch all user notes
- ✅ Empty notes array
- ✅ User isolation (only own notes)
- ✅ Unauthorized access protection
GET /api/v1/notes/:id:
- ✅ Fetch specific note
- ✅ Non-existent note handling
- ✅ Other user's note protection
- ✅ Unauthorized access protection
POST /api/v1/notes:
- ✅ Create new note
- ✅ Missing title validation
- ✅ Missing description validation
- ✅ Validation error handling
- ✅ Unauthorized access protection
PUT /api/v1/notes/:id:
- ✅ Update existing note
- ✅ Non-existent note handling
- ✅ Other user's note protection
- ✅ Unauthorized access protection
DELETE /api/v1/notes/:id:
- ✅ Delete existing note
- ✅ Non-existent note handling
- ✅ Other user's note protection
- ✅ Unauthorized access protection
- Environment: Node.js
- Test Timeout: 30 seconds
- Database: MongoDB Memory Server (in-memory)
- Workers: 1 (serial execution to avoid conflicts)
- Coverage: Enabled for
src/directory
NODE_ENV=test
JWT_SECRET=test_secret_for_testing
JWT_EXPIRES_IN=1hnpm testnpm run test:unitnpm run test:integrationnpm run test:coveragenpm run test:watchnode run-tests.jsgenerateTestUser()- Creates test user datagenerateTestNote()- Creates test note datacreateTestUser()- Saves test user to databasecreateTestNote()- Saves test note to databasegenerateAuthToken()- Creates JWT tokens for testingloginUser()- Simulates user login flowtestMissingField()- Tests missing field validationtestUnauthorized()- Tests unauthorized access
- Uses MongoDB Memory Server for isolation
- Automatic cleanup after each test
- No external database dependencies
- Fast test execution
The test suite provides comprehensive coverage of:
- ✅ Models: Data validation and database operations
- ✅ Controllers: Business logic and error handling
- ✅ Middleware: Authentication and authorization
- ✅ Routes: API endpoint functionality
- ✅ Validation: Input sanitization and validation
- ✅ Database: Connection and error handling
- ✅ Security: JWT tokens, password hashing, user isolation
- ✅ JWT token validation
- ✅ Token expiration handling
- ✅ Invalid token protection
- ✅ Missing token protection
- ✅ User session management
- ✅ User data isolation
- ✅ Password hashing verification
- ✅ Input validation and sanitization
- ✅ SQL injection prevention (via Mongoose)
- ✅ Cross-user data access prevention
- ✅ Missing required fields
- ✅ Invalid data formats
- ✅ Length constraints
- ✅ Unique constraint violations
- ✅ 400 Bad Request (validation errors)
- ✅ 401 Unauthorized (authentication errors)
- ✅ 404 Not Found (resource not found)
- ✅ 409 Conflict (duplicate resources)
- ✅ 500 Internal Server Error (server errors)
- ✅ Connection failures
- ✅ Query errors
- ✅ Constraint violations
- ✅ Timeout handling
- Tests run in parallel where possible
- In-memory database for speed
- Minimal test data creation
- Efficient cleanup procedures
- Isolated test environments
- Test Isolation: Each test is independent
- Data Cleanup: Automatic cleanup after each test
- Realistic Data: Tests use realistic test data
- Error Scenarios: Comprehensive error testing
- Security Focus: Authentication and authorization testing
- Documentation: Well-documented test purposes
- Maintainability: Modular test structure with helpers
The test suite is designed to be run:
- ✅ Before every commit
- ✅ In CI/CD pipelines
- ✅ During development (watch mode)
- ✅ Before deployments
- ✅ For regression testing
This comprehensive test suite ensures your CRUD Notes API is robust, secure, and reliable! 🎉