feat: 收官补齐全部未实现功能
- 账号体系:修改密码(/account + PATCH /api/auth/password)、自助删除账号 (DELETE /api/auth/me)、订阅退订(token + /unsubscribe 页) - 前台/SEO:RSS 订阅 /feed.xml、作品详情分享、canonical URL、 文章 Article JSON-LD、品牌化错误页(i18n) - 后台增强:图片管理 /admin/uploads、操作审计日志 /admin/audit(含清空)、 联系消息已读标记、评论列表分页 - 工程:vitest 测试(密码哈希/内容校验器/种子完整性,12 例)+ pnpm test - DB 迁移:contacts.read、subscribers.token、audit_logs(老库 ALTER 兼容) - 密码逻辑抽为 server/utils/password.ts 供测试
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { hashPassword, verifyPassword } from '../server/utils/password'
|
||||
|
||||
describe('password hashing (scrypt)', () => {
|
||||
it('hashes and verifies the correct password', async () => {
|
||||
const hash = await hashPassword('secret123')
|
||||
expect(hash).toContain(':')
|
||||
expect(await verifyPassword('secret123', hash)).toBe(true)
|
||||
})
|
||||
|
||||
it('rejects a wrong password', async () => {
|
||||
const hash = await hashPassword('secret123')
|
||||
expect(await verifyPassword('wrong-pass', hash)).toBe(false)
|
||||
})
|
||||
|
||||
it('rejects a malformed stored hash', async () => {
|
||||
expect(await verifyPassword('secret123', 'no-separator')).toBe(false)
|
||||
expect(await verifyPassword('secret123', '')).toBe(false)
|
||||
})
|
||||
|
||||
it('produces a unique salt per call', async () => {
|
||||
const a = await hashPassword('secret123')
|
||||
const b = await hashPassword('secret123')
|
||||
expect(a).not.toBe(b)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,45 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { articlesSeed, worksSeed } from '../server/utils/seed'
|
||||
|
||||
describe('seed data integrity', () => {
|
||||
it('has 6 articles and 6 works', () => {
|
||||
expect(articlesSeed).toHaveLength(6)
|
||||
expect(worksSeed).toHaveLength(6)
|
||||
})
|
||||
|
||||
it('slugs are unique', () => {
|
||||
const articleSlugs = articlesSeed.map((a) => a.slug)
|
||||
expect(new Set(articleSlugs).size).toBe(articleSlugs.length)
|
||||
const workSlugs = worksSeed.map((w) => w.slug)
|
||||
expect(new Set(workSlugs).size).toBe(workSlugs.length)
|
||||
})
|
||||
|
||||
it('bilingual fields are complete for articles', () => {
|
||||
for (const a of articlesSeed) {
|
||||
expect(a.slug).toBeTruthy()
|
||||
expect(a.titleZh).toBeTruthy()
|
||||
expect(a.titleEn).toBeTruthy()
|
||||
expect(a.excerptZh).toBeTruthy()
|
||||
expect(a.excerptEn).toBeTruthy()
|
||||
expect(a.contentZh.length).toBeGreaterThan(0)
|
||||
expect(a.contentEn.length).toBeGreaterThan(0)
|
||||
expect(a.image).toBeTruthy()
|
||||
expect(a.published).toBe(1)
|
||||
}
|
||||
})
|
||||
|
||||
it('bilingual fields are complete for works', () => {
|
||||
for (const w of worksSeed) {
|
||||
expect(w.slug).toBeTruthy()
|
||||
expect(w.titleZh).toBeTruthy()
|
||||
expect(w.titleEn).toBeTruthy()
|
||||
expect(w.descriptionZh).toBeTruthy()
|
||||
expect(w.descriptionEn).toBeTruthy()
|
||||
expect(w.overviewZh).toBeTruthy()
|
||||
expect(w.overviewEn).toBeTruthy()
|
||||
expect(w.gallery.length).toBeGreaterThan(0)
|
||||
expect(w.tags.length).toBeGreaterThan(0)
|
||||
expect(w.published).toBe(1)
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,55 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
isValidSlug,
|
||||
isValidArticleCategory,
|
||||
isValidWorkCategory,
|
||||
normalizeBlocks,
|
||||
normalizeTags,
|
||||
normalizeGallery,
|
||||
} from '../server/utils/validateContent'
|
||||
|
||||
describe('content validation', () => {
|
||||
it('validates slugs', () => {
|
||||
expect(isValidSlug('design-trends-2026')).toBe(true)
|
||||
expect(isValidSlug('a')).toBe(false)
|
||||
expect(isValidSlug('')).toBe(false)
|
||||
expect(isValidSlug('with space')).toBe(false)
|
||||
expect(isValidSlug('UPPER')).toBe(false)
|
||||
expect(isValidSlug('x'.repeat(65))).toBe(false)
|
||||
})
|
||||
|
||||
it('validates article/work categories', () => {
|
||||
expect(isValidArticleCategory('insights')).toBe(true)
|
||||
expect(isValidArticleCategory('projects')).toBe(true)
|
||||
expect(isValidArticleCategory('bogus')).toBe(false)
|
||||
expect(isValidWorkCategory('webDesign')).toBe(true)
|
||||
expect(isValidWorkCategory('development')).toBe(true)
|
||||
expect(isValidWorkCategory('insights')).toBe(false)
|
||||
})
|
||||
|
||||
it('normalizes content blocks (filters invalid)', () => {
|
||||
const input = [
|
||||
{ type: 'paragraph', text: 'hi' },
|
||||
{ type: 'heading', text: 'title' },
|
||||
{ type: 'quote', text: 'quote' },
|
||||
{ type: 'image', text: 'https://x/a.png' },
|
||||
{ type: 'bogus', text: 'x' },
|
||||
{ text: 'no-type' },
|
||||
null,
|
||||
'string',
|
||||
]
|
||||
expect(normalizeBlocks(input as never[])).toEqual([
|
||||
{ type: 'paragraph', text: 'hi' },
|
||||
{ type: 'heading', text: 'title' },
|
||||
{ type: 'quote', text: 'quote' },
|
||||
{ type: 'image', text: 'https://x/a.png' },
|
||||
])
|
||||
expect(normalizeBlocks(undefined)).toEqual([])
|
||||
})
|
||||
|
||||
it('normalizes tags and gallery', () => {
|
||||
expect(normalizeTags([' a ', 'b', '', 5])).toEqual(['a', 'b'])
|
||||
expect(normalizeTags('not-array' as never)).toEqual([])
|
||||
expect(normalizeGallery(['/x.png', ' ', '/y.png'])).toEqual(['/x.png', '/y.png'])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user