Skip to content

Add unit tests for authentication middleware and error handling - #63

Draft
prasadhonrao with Copilot wants to merge 3 commits into
mainfrom
copilot/write-unit-tests-user-service
Draft

Add unit tests for authentication middleware and error handling#63
prasadhonrao with Copilot wants to merge 3 commits into
mainfrom
copilot/write-unit-tests-user-service

Conversation

Copilot AI commented Feb 16, 2026

Copy link
Copy Markdown

Added comprehensive unit test coverage for authentication/authorization middleware and core error handling utilities.

Test Coverage Added

Authentication Middleware (tests/unit/middlewares/auth.middleware.test.js)

  • JWT validation (Bearer token and cookie-based)
  • Token expiration and invalid token handling
  • Service-to-service authentication via X-Service-Token header
  • Role-based access control (requireAdmin, requireRoles)
  • User deactivation enforcement
// Example: Service token authentication test
it('should authenticate valid service token', async () => {
  mockReq.headers['x-service-token'] = 'auth-service-token';
  await requireServiceToken(mockReq, mockRes, mockNext);
  
  expect(mockReq.callingService).toBe('auth-service');
  expect(mockNext).toHaveBeenCalledWith();
});

Error Response Class (tests/unit/core/errors.test.js)

  • ErrorResponse construction with status codes and error codes
  • HTTP error scenarios (400, 401, 403, 404, 409, 500)
  • Error throwability and message preservation

Results

  • 39 new tests across 2 files
  • 249 total tests passing
  • 100% coverage on errors.js
  • 70.78% coverage on auth.middleware.js

Implementation Notes

  • ES6 modules with Jest
  • Winston logger mocked to prevent console warnings
  • All external dependencies (JWT, MongoDB) properly mocked
  • Tests isolated with no external dependencies

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 16, 2026 11:24
Co-authored-by: prasadhonrao <1454174+prasadhonrao@users.noreply.github.com>
Co-authored-by: prasadhonrao <1454174+prasadhonrao@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for user service Add unit tests for authentication middleware and error handling Feb 16, 2026
Copilot AI requested a review from prasadhonrao February 16, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants