- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权; 已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF), 开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。 单测 91 个通过(不需要本机数据库)。 - frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 / 首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。 - extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。 - 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、 .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
207 lines
6.1 KiB
JavaScript
207 lines
6.1 KiB
JavaScript
'use strict';
|
|
|
|
const { app, mock, assert } = require('egg-mock/bootstrap');
|
|
const createFakeRedis = require('../../utils/fake-redis');
|
|
const { HEADER: INTERNAL } = require('../../utils/internal');
|
|
|
|
/** 系统配置属「内部接口」:请求都带上运维令牌 */
|
|
const http = {
|
|
get: url => app.httpRequest().get(url).set(INTERNAL),
|
|
post: url => app.httpRequest().post(url).set(INTERNAL),
|
|
put: url => app.httpRequest().put(url).set(INTERNAL),
|
|
delete: url => app.httpRequest().delete(url).set(INTERNAL),
|
|
};
|
|
|
|
const instance = (data) => Object.assign({}, data, {
|
|
toJSON: () => data,
|
|
update: async (fields) => Object.assign(data, fields),
|
|
});
|
|
|
|
describe('test/app/controller/config.test.js', () => {
|
|
let fakeRedis;
|
|
|
|
beforeEach(() => {
|
|
fakeRedis = createFakeRedis();
|
|
mock(app, 'redis', fakeRedis.client);
|
|
});
|
|
|
|
it('GET /api/configs/:group/:key 读配置并写缓存', async () => {
|
|
let queries = 0;
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne() {
|
|
queries++;
|
|
return instance({ groupName: 'wechat', configKey: 'appid', configValue: 'wx123', valueType: 'string' });
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await http.get('/api/configs/wechat/appid').expect(200);
|
|
assert(res.body.data.value === 'wx123');
|
|
assert(queries === 1);
|
|
assert(fakeRedis.store.get('packs:config:wechat:appid'), '应写入缓存');
|
|
|
|
// 第二次读命中缓存
|
|
await http.get('/api/configs/wechat/appid').expect(200);
|
|
assert(queries === 1);
|
|
});
|
|
|
|
it('value_type 为 json / number / boolean 时自动转换类型', async () => {
|
|
const rows = {
|
|
json: { configValue: '{"a":1}', valueType: 'json' },
|
|
number: { configValue: '42', valueType: 'number' },
|
|
boolean: { configValue: 'true', valueType: 'boolean' },
|
|
};
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne(options) {
|
|
const key = options.where.configKey;
|
|
return instance(Object.assign({ groupName: 'g', configKey: key }, rows[key]));
|
|
},
|
|
},
|
|
});
|
|
|
|
const json = await http.get('/api/configs/g/json').expect(200);
|
|
assert.deepStrictEqual(json.body.data.value, { a: 1 });
|
|
|
|
const number = await http.get('/api/configs/g/number').expect(200);
|
|
assert.strictEqual(number.body.data.value, 42);
|
|
|
|
const boolean = await http.get('/api/configs/g/boolean').expect(200);
|
|
assert.strictEqual(boolean.body.data.value, true);
|
|
});
|
|
|
|
it('GET /api/configs/:group/:key 不存在(或已禁用)返回 404', async () => {
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne() {
|
|
return null;
|
|
},
|
|
},
|
|
});
|
|
|
|
await http.get('/api/configs/wechat/none').expect(404);
|
|
});
|
|
|
|
it('GET /api/configs/:group 返回分组内启用的配置', async () => {
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findAll() {
|
|
return [
|
|
instance({ configKey: 'appid', configValue: 'wx123', valueType: 'string' }),
|
|
instance({ configKey: 'secret', configValue: 's1', valueType: 'string' }),
|
|
];
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await http.get('/api/configs/wechat').expect(200);
|
|
assert.deepStrictEqual(res.body.data, { appid: 'wx123', secret: 's1' });
|
|
});
|
|
|
|
it('PUT /api/configs 新建配置', async () => {
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne() {
|
|
return null;
|
|
},
|
|
async create(data) {
|
|
assert(data.groupName === 'wechat');
|
|
assert(data.configKey === 'secret');
|
|
assert(data.configValue === 's1');
|
|
return instance(Object.assign({ id: 3 }, data));
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await http
|
|
.put('/api/configs')
|
|
.send({ groupName: 'wechat', configKey: 'secret', configValue: 's1' })
|
|
.expect(200);
|
|
|
|
assert(res.body.data.id === 3);
|
|
});
|
|
|
|
it('PUT /api/configs 更新已有配置并清掉缓存', async () => {
|
|
// 先读一次建立缓存
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne() {
|
|
return instance({ id: 3, groupName: 'wechat', configKey: 'appid', configValue: 'old', valueType: 'string' });
|
|
},
|
|
},
|
|
});
|
|
await http.get('/api/configs/wechat/appid').expect(200);
|
|
assert(fakeRedis.store.has('packs:config:wechat:appid'));
|
|
|
|
// 再更新
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findOne() {
|
|
return instance({
|
|
id: 3,
|
|
groupName: 'wechat',
|
|
configKey: 'appid',
|
|
configValue: 'old',
|
|
valueType: 'string',
|
|
});
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await http
|
|
.put('/api/configs')
|
|
.send({ groupName: 'wechat', configKey: 'appid', configValue: 'new' })
|
|
.expect(200);
|
|
|
|
assert(res.body.data.configValue === 'new');
|
|
assert(!fakeRedis.store.has('packs:config:wechat:appid'), '写入后应清除缓存');
|
|
});
|
|
|
|
it('PUT /api/configs 缺少 configKey 返回 422', async () => {
|
|
await http.put('/api/configs').send({ groupName: 'wechat' }).expect(422);
|
|
});
|
|
|
|
it('GET /api/configs 列表(管理用)', async () => {
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async findAll(options) {
|
|
assert(options.where.groupName === 'wechat');
|
|
return [ instance({ groupName: 'wechat', configKey: 'appid', configValue: 'wx123' }) ];
|
|
},
|
|
},
|
|
});
|
|
|
|
const res = await http.get('/api/configs?group=wechat').expect(200);
|
|
assert(res.body.data.total === 1);
|
|
});
|
|
|
|
it('DELETE /api/configs/:group/:key 软删除并清缓存', async () => {
|
|
fakeRedis.store.set('packs:config:wechat:appid', '{}');
|
|
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async destroy(options) {
|
|
assert.deepStrictEqual(options.where, { groupName: 'wechat', configKey: 'appid' });
|
|
return 1;
|
|
},
|
|
},
|
|
});
|
|
|
|
await http.delete('/api/configs/wechat/appid').expect(200);
|
|
assert(!fakeRedis.store.has('packs:config:wechat:appid'));
|
|
});
|
|
|
|
it('DELETE /api/configs/:group/:key 配置不存在返回 404', async () => {
|
|
mock(app, 'model', {
|
|
SysConfig: {
|
|
async destroy() {
|
|
return 0;
|
|
},
|
|
},
|
|
});
|
|
|
|
await http.delete('/api/configs/wechat/none').expect(404);
|
|
});
|
|
});
|