Add integrated tests and patch dependencies
This commit is contained in:
1 parent
145f13fee5
commit
3fc74ae189
6 files changed
+389
-631
No files matched your search
@@ -56,7 +56,7 @@ docker-compose up -d
|
|||||||
|
|
||||||
#### 环境要求
|
#### 环境要求
|
||||||
|
|
||||||
- Node.js 18+
|
- Node.js 20+
|
||||||
- PostgreSQL 13+
|
- PostgreSQL 13+
|
||||||
|
|
||||||
#### 步骤
|
#### 步骤
|
||||||
|
|||||||
Generated
+284
-625
File diff suppressed because it is too large.
Load diff
+12
-4
@@ -3,18 +3,26 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Forumlify - 简洁优雅的现代社区系统",
|
"description": "Forumlify - 简洁优雅的现代社区系统",
|
||||||
"main": "server.js",
|
"main": "server.js",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=20"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server.js",
|
"start": "node server.js",
|
||||||
"dev": "node server.js"
|
"dev": "node server.js",
|
||||||
|
"check": "node scripts/check-syntax.js",
|
||||||
|
"test": "node --test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bcrypt": "^5.1.1",
|
"bcryptjs": "3.0.3",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"express-rate-limit": "8.1.0",
|
"express-rate-limit": "8.6.2",
|
||||||
"helmet": "8.1.0",
|
"helmet": "8.1.0",
|
||||||
"jsonwebtoken": "^9.0.2",
|
"jsonwebtoken": "^9.0.2",
|
||||||
"multer": "^1.4.5-lts.1",
|
"multer": "2.2.0",
|
||||||
"pg": "^8.11.3"
|
"pg": "^8.11.3"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"supertest": "7.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
const { execFileSync } = require('node:child_process');
|
||||||
|
const path = require('node:path');
|
||||||
|
|
||||||
|
const files = execFileSync('git', ['ls-files', '*.js'], { encoding: 'utf8' })
|
||||||
|
.split(/\r?\n/)
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
execFileSync(process.execPath, ['--check', path.resolve(file)], { stdio: 'inherit' });
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Syntax check passed for ${files.length} JavaScript files.`);
|
||||||
@@ -7,7 +7,7 @@ const express = require('express');
|
|||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const helmet = require('helmet');
|
const helmet = require('helmet');
|
||||||
const { rateLimit } = require('express-rate-limit');
|
const { rateLimit } = require('express-rate-limit');
|
||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcryptjs');
|
||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const { Pool } = require('pg');
|
const { Pool } = require('pg');
|
||||||
const multer = require('multer');
|
const multer = require('multer');
|
||||||
@@ -1526,6 +1526,7 @@ app.use((err, req, res, next) => {
|
|||||||
// 启动服务器
|
// 启动服务器
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
app.listen(PORT, '0.0.0.0', () => {
|
app.listen(PORT, '0.0.0.0', () => {
|
||||||
console.log('========================================');
|
console.log('========================================');
|
||||||
console.log(' 🌊 Forumlify 已启动');
|
console.log(' 🌊 Forumlify 已启动');
|
||||||
@@ -1533,3 +1534,6 @@ app.listen(PORT, '0.0.0.0', () => {
|
|||||||
console.log(' 📡 API: http://localhost:' + PORT + '/api');
|
console.log(' 📡 API: http://localhost:' + PORT + '/api');
|
||||||
console.log('========================================');
|
console.log('========================================');
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { app, pool };
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
const test = require('node:test');
|
||||||
|
const assert = require('node:assert/strict');
|
||||||
|
const request = require('supertest');
|
||||||
|
const bcrypt = require('bcryptjs');
|
||||||
|
const multerPackage = require('multer/package.json');
|
||||||
|
const projectPackage = require('../package.json');
|
||||||
|
const { app, pool } = require('../server');
|
||||||
|
|
||||||
|
test.after(async () => {
|
||||||
|
await pool.end();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('serves the frontend with hardened response headers', async () => {
|
||||||
|
const response = await request(app).get('/').expect(200);
|
||||||
|
assert.match(response.headers['content-type'], /^text\/html/);
|
||||||
|
assert.equal(response.headers['x-content-type-options'], 'nosniff');
|
||||||
|
assert.equal(response.headers['x-frame-options'], 'SAMEORIGIN');
|
||||||
|
assert.equal(response.headers['x-powered-by'], undefined);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('does not serve backend source and deployment files', async () => {
|
||||||
|
for (const path of ['/server.js', '/schema.sql', '/package.json', '/docker-compose.yml']) {
|
||||||
|
await request(app).get(path).expect(404);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unknown API route returns JSON 404 instead of hanging', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.get('/api/definitely-unknown')
|
||||||
|
.timeout({ response: 1000, deadline: 2000 })
|
||||||
|
.expect(404);
|
||||||
|
assert.deepEqual(response.body, { error: '接口不存在' });
|
||||||
|
assert.ok(response.headers.ratelimit);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('request body limit returns a structured 413 response', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/api/auth/login')
|
||||||
|
.send({ email: 'user@example.test', password: 'x'.repeat(110000) })
|
||||||
|
.expect(413);
|
||||||
|
assert.equal(response.body.error, '请求内容过大');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('registration and login validate required fields before database access', async () => {
|
||||||
|
let response = await request(app)
|
||||||
|
.post('/api/auth/register')
|
||||||
|
.send({ email: '', password: '', username: '' })
|
||||||
|
.expect(400);
|
||||||
|
assert.equal(response.body.error, '请填写完整信息');
|
||||||
|
|
||||||
|
response = await request(app)
|
||||||
|
.post('/api/auth/login')
|
||||||
|
.send({ email: '', password: '' })
|
||||||
|
.expect(400);
|
||||||
|
assert.equal(response.body.error, '请填写邮箱和密码');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('protected routes reject unauthenticated requests', async () => {
|
||||||
|
const response = await request(app)
|
||||||
|
.post('/api/posts')
|
||||||
|
.send({ title: 'test', content: 'test' })
|
||||||
|
.expect(401);
|
||||||
|
assert.equal(response.body.error, '请先登录');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('bcryptjs hashes passwords without native install scripts', async () => {
|
||||||
|
const hash = await bcrypt.hash('correct horse battery staple', 4);
|
||||||
|
assert.equal(await bcrypt.compare('correct horse battery staple', hash), true);
|
||||||
|
assert.equal(await bcrypt.compare('wrong password', hash), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('security-sensitive dependencies are on patched release lines', () => {
|
||||||
|
assert.match(multerPackage.version, /^2\.2\./);
|
||||||
|
assert.equal(projectPackage.dependencies['express-rate-limit'], '8.6.2');
|
||||||
|
});
|
||||||
Reference in new issue
Block a user