jwt-auth.test.js 1.36 KB
/**
 * Unit Tests — middleware/jwt-auth.js
 */
import { describe, it, expect } from 'vitest';

const jwtAuth = require('../../middleware/jwt-auth');

describe('jwt-auth', () => {
  describe('exports', () => {
    it('should export authentication middleware functions', () => {
      expect(typeof jwtAuth.requireAuth).toBe('function');
      expect(typeof jwtAuth.requireAdmin).toBe('function');
      expect(typeof jwtAuth.optionalAuth).toBe('function');
      expect(typeof jwtAuth.extractUser).toBe('function');
    });

    it('should export cookie helpers', () => {
      expect(typeof jwtAuth.signAndSetCookie).toBe('function');
      expect(typeof jwtAuth.clearCookie).toBe('function');
    });

    it('should export token extraction', () => {
      expect(typeof jwtAuth.extractUserFromToken).toBe('function');
    });

    it('should export constants', () => {
      expect(typeof jwtAuth.JWT_SECRET).toBe('string');
      expect(typeof jwtAuth.COOKIE_NAME).toBe('string');
    });
  });

  describe('extractUserFromToken', () => {
    it('should return null for invalid token', () => {
      const user = jwtAuth.extractUserFromToken('invalid-token');
      expect(user).toBeNull();
    });

    it('should return null for empty token', () => {
      expect(jwtAuth.extractUserFromToken('')).toBeNull();
      expect(jwtAuth.extractUserFromToken(null)).toBeNull();
    });
  });
});