22
33Enrollment Service is the course enrollment microservice in the University Student Management System (SE4010 Cloud Computing Assignment).
44
5+ ## Table of Contents
6+ - [ Features] ( #features )
7+ - [ Service Purpose] ( #service-purpose )
8+ - [ Architecture] ( #architecture )
9+ - [ Requirements] ( #requirements )
10+ - [ Quick Start] ( #quick-start )
11+ - [ Configuration] ( #configuration )
12+ - [ API Endpoints] ( #api-endpoints )
13+ - [ Security Notes (Secret Manager + Snyk + JWT)] ( #security-notes-secret-manager--snyk--jwt )
14+ - [ Inter-Service Communication Flow] ( #inter-service-communication-flow )
15+ - [ Testing] ( #testing )
16+ - [ Docker] ( #docker )
17+ - [ CI/CD and Deployment Notes] ( #cicd-and-deployment-notes )
18+ - [ Project Structure] ( #project-structure )
19+ - [ Troubleshooting] ( #troubleshooting )
20+ - [ Repository Artifacts Checklist] ( #repository-artifacts-checklist )
21+
22+ ## Features
23+ - JWT-protected write operations (` POST ` , ` PATCH ` , ` DELETE ` ) with fail-closed auth behavior.
24+ - Enrollment lifecycle management (create, list, check, update status, cancel).
25+ - Duplicate active enrollment prevention.
26+ - Inter-service validation with Student, Course, and Grade services.
27+ - MongoDB persistence via Mongoose.
28+ - Swagger/OpenAPI docs at ` /api-docs ` .
29+ - Security middleware stack: ` helmet ` , ` cors ` , rate limiter, and request logging.
30+ - Automated tests with Jest + Supertest.
31+ - Docker-ready deployment.
32+
533## Service Purpose
634- Enroll students into courses.
735- Track enrollment records and status.
836- Provide enrollment lookup endpoints for other services.
937- Integrate with Student Service and Course Service for validation.
10- - Integrate with Grade Service to initialize grade records (when Grade Service is available).
38+ - Integrate with Grade Service to initialize grade records.
39+
40+ ## Architecture
41+ ``` text
42+ Client / API Gateway
43+ |
44+ v
45+ Enrollment Service (Express, Port 5003 local / 8080 Cloud Run)
46+ - Auth middleware (JWT validation via Student Service)
47+ - Enrollment routes/controllers
48+ - Rate limiting + Helmet + CORS
49+ - Swagger docs (/api-docs)
50+ |
51+ +--> MongoDB (enrollmentdb)
52+ +--> Student Service (auth/validate + student checks)
53+ +--> Course Service (course/capacity checks)
54+ +--> Grade Service (initial grade creation)
55+ ```
1156
12- ## API Endpoints
13- Base URL (Cloud Run): ` https://enrollment-service-763150334229.us-central1.run.app `
57+ ## Requirements
58+ - Node.js 18+ (recommended current LTS)
59+ - npm 9+
60+ - MongoDB (local or Atlas)
61+ - Docker (optional, for containerized run)
1462
15- - ` POST /enroll ` - Enroll a student in a course
16- - ` GET /enrollments ` - Get all enrollments
17- - ` GET /enrollments/student/{studentId} ` - Get enrollments by student
18- - ` GET /enrollments/course/{courseId} ` - Get course roster
19- - ` GET /enrollments/check ` - Check enrollment status
20- - ` PATCH /enrollments/{id}/status ` - Update enrollment status
21- - ` DELETE /enroll/{id} ` - Cancel enrollment
22- - ` GET /api-docs ` - Swagger/OpenAPI UI
63+ ## Quick Start
64+ ### 1. Install dependencies
65+ ``` bash
66+ npm install
67+ ```
68+
69+ ### 2. Configure environment
70+ ``` bash
71+ cp .env.example .env
72+ ```
73+ Update ` .env ` values to your environment.
74+
75+ ### 3. Run the service
76+ ``` bash
77+ npm run dev
78+ ```
2379
24- ## Environment Variables
80+ ### 4. Verify service
81+ - Health: ` http://localhost:5003/ `
82+ - API docs: ` http://localhost:5003/api-docs `
83+
84+ ## Configuration
85+ ### Environment Variables
2586Required runtime variables:
2687
27- - ` MONGO_URI ` - MongoDB connection string (in Cloud Run, configured via Secret Manager)
28- - ` STUDENT_SERVICE_URL ` - Base URL of Student Service
29- - ` COURSE_SERVICE_URL ` - Base URL of Course Service
30- - ` GRADE_SERVICE_URL ` - Base URL of Grade Service
31- - ` ALLOW_AUTH_BYPASS ` - Optional local/dev switch (` true ` only for controlled testing)
32- - ` ALLOW_MOCK_SERVICES ` - Optional local/dev switch (` true ` only for controlled testing)
33- - ` PORT ` - Service port (Cloud Run uses ` 8080 ` )
88+ | Variable | Required | Description |
89+ | ---| ---| ---|
90+ | ` MONGO_URI ` | Yes | MongoDB connection string (Cloud Run maps from Secret Manager) |
91+ | ` STUDENT_SERVICE_URL ` | Yes | Base URL of Student Service |
92+ | ` COURSE_SERVICE_URL ` | Yes | Base URL of Course Service |
93+ | ` GRADE_SERVICE_URL ` | Yes | Base URL of Grade Service |
94+ | ` ALLOW_AUTH_BYPASS ` | No | Local/dev auth bypass switch (` true ` only for controlled testing) |
95+ | ` ALLOW_MOCK_SERVICES ` | No | Local/dev mock switch (` true ` only for controlled testing) |
96+ | ` PORT ` | No | Service port (Cloud Run uses ` 8080 ` , local default in ` .env.example ` is ` 5003 ` ) |
3497
3598### Local ` .env ` example
3699``` env
@@ -43,16 +106,64 @@ ALLOW_AUTH_BYPASS=false
43106ALLOW_MOCK_SERVICES=false
44107```
45108
46- ## Run Locally
109+ ## API Endpoints
110+ Base URL (production): configured via deployment/environment
111+
112+ Base URL (local): ` http://localhost:5003 `
113+
114+ - ` GET / ` - Health check
115+ - ` POST /enroll ` - Enroll a student in a course (protected)
116+ - ` GET /enrollments ` - Get all enrollments (protected)
117+ - ` GET /enrollments/student/{studentId} ` - Get enrollments by student
118+ - ` GET /enrollments/course/{courseId} ` - Get course roster
119+ - ` GET /enrollments/check ` - Check enrollment status
120+ - ` PATCH /enrollments/{id}/status ` - Update enrollment status (protected)
121+ - ` DELETE /enroll/{id} ` - Cancel enrollment (protected)
122+ - ` GET /api-docs ` - Swagger/OpenAPI UI
123+
124+ ## Security Notes (Secret Manager + Snyk + JWT)
125+ - ` MONGO_URI ` is not deployed as plain text; it is mapped from Google Secret Manager.
126+ - CI includes Snyk security scanning before build/deploy.
127+ - API Gateway and service endpoints support JWT-based protected routes for authorized operations.
128+ - Enrollment auth is fail-closed by default: failed token validation returns ` 401 ` (no implicit production bypass).
129+ - Inter-service validation is fail-closed by default: unreachable dependencies return ` 503 ` (no implicit production mock success).
130+ - Input validation and duplicate enrollment checks are enforced in business logic.
131+ - Global rate limit is enabled with ` express-rate-limit ` (100 requests per 15-minute window per IP).
132+
133+ ## Inter-Service Communication Flow
134+ Enrollment Service communicates with other microservices:
135+
136+ - Student Service:
137+ - Validate student identity/existence before enrollment.
138+ - Course Service:
139+ - Validate course and capacity before enrollment.
140+ - Grade Service:
141+ - Create/initialize grade record after successful enrollment.
142+
143+ Typical ` POST /enroll ` flow:
144+ 1 . Receive enrollment request (` student_id ` , ` course_id ` ).
145+ 2 . Validate auth token via Student Service (` /auth/validate ` ) for protected routes.
146+ 3 . Call Student Service validation endpoint.
147+ 4 . Call Course Service validation/capacity endpoint.
148+ 5 . Save enrollment record in Enrollment DB.
149+ 6 . Call Grade Service to create initial grade record (if configured/available).
150+
151+ ## Testing
152+ Run tests:
153+ ``` bash
154+ npm test
155+ ```
156+
157+ Run tests with coverage:
47158``` bash
48- npm install
49- cp .env.example .env
50- # update .env values
51- npm run dev
159+ npm run test:coverage
52160```
53161
54- Local docs:
55- - ` http://localhost:5003/api-docs `
162+ Current test suite covers:
163+ - Health and docs endpoints.
164+ - Auth middleware behavior.
165+ - Enrollment create/list/check/update/cancel routes.
166+ - Basic security headers (Helmet) and CORS behavior.
56167
57168## Docker
58169Build and run:
@@ -71,7 +182,7 @@ GitHub Actions workflow file:
71182Pipeline stages:
721831 . Test
731842 . Snyk Security Scan (fails on high/critical)
74- 3 . Build and Push Docker image to Docker Hub
185+ 3 . Build and push Docker image to Docker Hub
751864 . Deploy to Google Cloud Run
76187
77188Cloud deployment:
@@ -80,30 +191,45 @@ Cloud deployment:
80191- Image: ` docker.io/nuwanifonseka/enrollment-service:latest `
81192- Secret mapping in deploy step: ` MONGO_URI=MONGO_URI:latest `
82193
83- ## Security Notes (Secret Manager + Snyk + JWT)
84- - ` MONGO_URI ` is not deployed as plain text; it is mapped from Google Secret Manager.
85- - CI includes Snyk security scanning before build/deploy.
86- - API Gateway and service endpoints support JWT-based protected routes for authorized operations.
87- - Enrollment auth is fail-closed by default: failed token validation returns ` 401 ` (no implicit production bypass).
88- - Inter-service validation is fail-closed by default: unreachable dependencies return ` 503 ` (no implicit production mock success).
89- - Input validation and duplicate enrollment checks are enforced in business logic.
194+ ## Project Structure
195+ ``` text
196+ enrollment-service/
197+ |-- src/
198+ | |-- controllers/
199+ | | `-- enrollmentController.js
200+ | |-- middleware/
201+ | | `-- auth.js
202+ | |-- models/
203+ | | `-- Enrollment.js
204+ | |-- routes/
205+ | | `-- enrollmentRoutes.js
206+ | |-- services/
207+ | | `-- externalServices.js
208+ | |-- server.js
209+ | `-- swagger.yaml
210+ |-- tests/
211+ | `-- enrollment.test.js
212+ |-- .github/workflows/main.yml
213+ |-- Dockerfile
214+ |-- package.json
215+ `-- README.md
216+ ```
90217
91- ## Inter-Service Communication Flow
92- Enrollment Service communicates with other microservices:
218+ ## Troubleshooting
219+ ### "MONGO_URI not defined in environment"
220+ Set ` MONGO_URI ` in ` .env ` or environment before starting the service.
93221
94- - Student Service:
95- - Validate student identity/existence before enrollment.
96- - Course Service:
97- - Validate course and capacity before enrollment.
98- - Grade Service:
99- - Create/initialize grade record after successful enrollment.
222+ ### Token validation returns ` 401 `
223+ Check:
224+ 1 . Authorization header is present as ` Bearer <token> ` .
225+ 2 . ` STUDENT_SERVICE_URL ` is reachable.
226+ 3 . ` ALLOW_AUTH_BYPASS ` is ` false ` in production.
100227
101- Typical ` POST /enroll ` flow:
102- 1 . Receive enrollment request (` student_id ` , ` course_id ` ).
103- 2 . Call Student Service validation endpoint.
104- 3 . Call Course Service validation/capacity endpoint.
105- 4 . Save enrollment record in Enrollment DB.
106- 5 . Call Grade Service to create initial grade record (if configured/available).
228+ ### Dependency services unavailable (` 503 ` )
229+ Check Student/Course/Grade service URLs and network connectivity. In local controlled testing, use mock/bypass flags only when intentional.
230+
231+ ### Port conflict locally
232+ Change ` PORT ` in ` .env ` and restart the app.
107233
108234## Repository Artifacts Checklist
109235This repository includes:
0 commit comments