- Fix list-to-detail navigation on product/service/solution/case pages - Fix soft 404 on service detail by removing (marketing)/loading.tsx and using force-dynamic - Fix contact form submission feedback and news placeholder image handling - Unify SSR/client authentication state in auth.ts - Add "新闻动态" to main navigation - Fix Playwright storageState path and Firefox footer link flakiness - Add E2E coverage for nav dropdown, cases filter and auth token parsing - Update visual regression baselines (desktop/tablet/mobile, chromium/webkit/firefox) - Update README, lessons-learned and add REGRESSION_REPORT_2026-07-27.md - Ignore .lighthouseci/ and heading-hierarchy-report.json
148 lines
4.7 KiB
TypeScript
148 lines
4.7 KiB
TypeScript
import { describe, it, expect, jest, beforeEach } from '@jest/globals';
|
|
|
|
process.env.JWT_SECRET = 'test-jwt-secret';
|
|
process.env.JWT_REFRESH_SECRET = 'test-refresh-secret';
|
|
|
|
jest.mock('jsonwebtoken', () => ({
|
|
verify: jest.fn(),
|
|
sign: jest.fn(),
|
|
}));
|
|
|
|
jest.unmock('./auth');
|
|
|
|
import jwt from 'jsonwebtoken';
|
|
import { NextRequest } from 'next/server';
|
|
import { authenticateRequest, getTokenFromRequest, getTokenFromCookie } from './auth';
|
|
import type { JwtPayload } from './auth';
|
|
|
|
const mockJwtVerify = jwt.verify as jest.MockedFunction<typeof jwt.verify>;
|
|
|
|
type NextRequestInit = NonNullable<ConstructorParameters<typeof NextRequest>[1]>;
|
|
|
|
function createRequest(init?: NextRequestInit): NextRequest {
|
|
const request = new NextRequest('http://localhost/api/admin/models', init);
|
|
const cookieHeader = init?.headers && 'cookie' in init.headers ? init.headers.cookie : undefined;
|
|
const cookieMap = new Map<string, string>();
|
|
if (typeof cookieHeader === 'string') {
|
|
cookieHeader.split(';').forEach((pair) => {
|
|
const [name, value] = pair.trim().split('=');
|
|
if (name && value !== undefined) cookieMap.set(name, value);
|
|
});
|
|
}
|
|
Object.defineProperty(request, 'cookies', {
|
|
value: {
|
|
get: (name: string) => {
|
|
const value = cookieMap.get(name);
|
|
return value !== undefined ? { name, value } : undefined;
|
|
},
|
|
},
|
|
configurable: true,
|
|
});
|
|
return request;
|
|
}
|
|
|
|
describe('getTokenFromRequest', () => {
|
|
it('returns token from Authorization header', () => {
|
|
const request = createRequest({
|
|
headers: { Authorization: 'Bearer valid-token' },
|
|
});
|
|
expect(getTokenFromRequest(request)).toBe('valid-token');
|
|
});
|
|
|
|
it('returns null when Authorization header is missing', () => {
|
|
const request = createRequest();
|
|
expect(getTokenFromRequest(request)).toBeNull();
|
|
});
|
|
|
|
it('returns null when Authorization header is not Bearer', () => {
|
|
const request = createRequest({
|
|
headers: { Authorization: 'Basic valid-token' },
|
|
});
|
|
expect(getTokenFromRequest(request)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('getTokenFromCookie', () => {
|
|
it('returns token from novalon_token cookie', () => {
|
|
const request = createRequest({
|
|
headers: { cookie: 'novalon_token=cookie-token; other=value' },
|
|
});
|
|
expect(getTokenFromCookie(request)).toBe('cookie-token');
|
|
});
|
|
|
|
it('returns null when novalon_token cookie is missing', () => {
|
|
const request = createRequest({
|
|
headers: { cookie: 'other=value' },
|
|
});
|
|
expect(getTokenFromCookie(request)).toBeNull();
|
|
});
|
|
|
|
it('returns null when no cookies are present', () => {
|
|
const request = createRequest();
|
|
expect(getTokenFromCookie(request)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('authenticateRequest', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('authenticates from Authorization header', () => {
|
|
const payload: JwtPayload = { userId: 'user-1', username: 'admin', role: 'super_admin' };
|
|
mockJwtVerify.mockReturnValue(payload as unknown as void);
|
|
|
|
const request = createRequest({
|
|
headers: { Authorization: 'Bearer header-token' },
|
|
});
|
|
|
|
expect(authenticateRequest(request)).toEqual(payload);
|
|
expect(mockJwtVerify).toHaveBeenCalledWith('header-token', 'test-jwt-secret');
|
|
});
|
|
|
|
it('falls back to cookie when Authorization header is missing', () => {
|
|
const payload: JwtPayload = { userId: 'user-2', username: 'editor', role: 'editor' };
|
|
mockJwtVerify.mockReturnValue(payload as unknown as void);
|
|
|
|
const request = createRequest({
|
|
headers: { cookie: 'novalon_token=cookie-token' },
|
|
});
|
|
|
|
expect(authenticateRequest(request)).toEqual(payload);
|
|
expect(mockJwtVerify).toHaveBeenCalledWith('cookie-token', 'test-jwt-secret');
|
|
});
|
|
|
|
it('prefers Authorization header over cookie when both are present', () => {
|
|
const payload: JwtPayload = { userId: 'user-3', username: 'admin', role: 'super_admin' };
|
|
mockJwtVerify.mockReturnValue(payload as unknown as void);
|
|
|
|
const request = createRequest({
|
|
headers: {
|
|
Authorization: 'Bearer header-token',
|
|
cookie: 'novalon_token=cookie-token',
|
|
},
|
|
});
|
|
|
|
authenticateRequest(request);
|
|
expect(mockJwtVerify).toHaveBeenCalledWith('header-token', 'test-jwt-secret');
|
|
});
|
|
|
|
it('returns null when no token is present', () => {
|
|
const request = createRequest();
|
|
expect(authenticateRequest(request)).toBeNull();
|
|
expect(mockJwtVerify).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('returns null when token verification fails', () => {
|
|
mockJwtVerify.mockImplementation(() => {
|
|
throw new Error('invalid token');
|
|
});
|
|
|
|
const request = createRequest({
|
|
headers: { Authorization: 'Bearer invalid-token' },
|
|
});
|
|
|
|
expect(authenticateRequest(request)).toBeNull();
|
|
});
|
|
});
|