This project provides a simple API for managing maintenance tasks, with distinct roles for technicians and managers. It's built with Node.js, Express, and uses MySQL as its database.
- src/: Source code for the API, including app.js (main application), routes.js (API endpoints), database.js (database connection), auth.js (authentication middleware), and notification.js (mock notification service).
- tests/: Unit and integration tests for the API.
- .env.test: Environment variables specifically for testing.
- .env: Environment variables for development/production.
To get this project up and running, follow these steps.
The project uses .env files to manage environment variables.
- .env: For the main application. Create this file in the backend/ directory.
- .env.test: For running unit tests. Create this file in the backend/ directory.
- Here are the variables you need for each:
.env (for main application)
PORT=3000
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password
DB_NAME=maintenance_db
JWT_SECRET=supersecretjwtkey
.env.test (for unit tests)
PORT=3001
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=password_test
DB_NAME=maintenance_test_db
JWT_SECRET=testsupersecretjwtkey
- DBPASSWORD: This should be the password for your MySQL _root user (or the user you configure for the database). For testing, DB_PASSWORD_TEST should be the password for your test database user.
- JWTSECRET: This is a secret key used to sign and verify JSON Web Tokens (JWTs) for authentication. _Use a strong, unique secret for production environments. For testing, a simpler key is acceptable.
- Hashing Strategy: User passwords are hashed using bcrypt before being stored in the database. When a user attempts to log in, their provided password is also hashed and compared against the stored hash. This ensures that raw passwords are never stored, enhancing security.
Using Docker is the easiest way to get the MySQL database running without manual setup.
Navigate to the root of your project (the directory containing docker-compose.yml) and run:
Bash
docker-compose up -d
This command will:
Build and start a MySQL container (mysql-db). Map port 3306 (host) to 3306 (container). Initialize two databases: maintenance_db(for the main application) and maintenance_test_db (for tests). Set the root password for MySQL.
Navigate to the backend/ directory:
Bash
cd backend
Then, install the Node.js dependencies:
Bash
npm install
To start the API server for development or production, run from the backend/ directory:
Bash
npm start
The server will run on the PORT specified in your .env file (default: 3000). You should see a message like "Server running on port 3000" in your console.
To execute the unit tests, which use the .env.test configuration and mock the database, run from the backend/ directory:
Bash
npm test
This command uses jest --detectOpenHandles --forceExit to run your tests. The dotenv part is handled directly within the api.test.js file for clarity in this simplified version.
How Credentials and Environment Variables are Used in Tests:
The backend/tests/api.test.js file directly loads the test environment variables at the very beginning of the file:
JavaScript
// backend/tests/api.test.js
require("dotenv").config({ path: "../.env.test" }); // Loads variables from .env.test
// ... rest of the test file ...
This ensures that process.env.JWT_SECRET and other variables correctly point to your test configuration when the tests are executed.
The tests use mock user data for authentication and authorization. These users and their roles are defined in backend/tests/mockData.js.
-
Manager User:
- Username: manager1
- Password: password123 (this is the plain text password, which gets hashed for comparison)
-
Technician User:
- Username: technician1
- Password: password123 (this is the plain text password, which gets hashed for comparison)
These credentials are used to generate JWT tokens for authenticated requests in the tests.