- 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 密码,绝不入库)。
185 lines
4.9 KiB
JavaScript
185 lines
4.9 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(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
describe('test/app/controller/wechat.test.js', () => {
|
||
beforeEach(() => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
});
|
||
|
||
it('POST /api/wechat/bind 首次绑定创建记录(201)', async () => {
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async findOne() {
|
||
return null;
|
||
},
|
||
async create(data) {
|
||
assert(data.userId === 1);
|
||
assert(data.appid === 'wx123');
|
||
assert(data.openid === 'o_abc');
|
||
assert(data.lastAuthAt instanceof Date, '应记录最近授权时间');
|
||
return instance(Object.assign({ id: 9 }, data));
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http
|
||
.post('/api/wechat/bind')
|
||
.send({ userId: 1, appid: 'wx123', openid: 'o_abc', nickname: '微信昵称' })
|
||
.expect(201);
|
||
|
||
assert(res.body.data.id === 9);
|
||
assert(res.body.data.nickname === '微信昵称');
|
||
});
|
||
|
||
it('POST /api/wechat/bind 已存在时更新(含换绑用户)', async () => {
|
||
let updated = null;
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async findOne() {
|
||
return instance({
|
||
id: 9,
|
||
userId: 1,
|
||
appid: 'wx123',
|
||
openid: 'o_abc',
|
||
update: async (fields) => {
|
||
updated = fields;
|
||
},
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
await http
|
||
.post('/api/wechat/bind')
|
||
.send({ userId: 2, appid: 'wx123', openid: 'o_abc', sessionKey: 'sk-1' })
|
||
.expect(201);
|
||
|
||
assert(updated.userId === 2, '应支持把授权记录换绑到另一个用户');
|
||
assert(updated.sessionKey === 'sk-1');
|
||
});
|
||
|
||
it('POST /api/wechat/bind 缺少必填参数返回 422', async () => {
|
||
await http.post('/api/wechat/bind').send({ appid: 'wx123' }).expect(422);
|
||
});
|
||
|
||
it('GET /api/wechat/:appid/:openid 返回授权与关联用户', async () => {
|
||
mock(app, 'model', {
|
||
User: { name: 'User' },
|
||
WechatOauth: {
|
||
async findOne() {
|
||
return instance({
|
||
id: 9,
|
||
appid: 'wx123',
|
||
openid: 'o_abc',
|
||
user: { id: 1, nickname: '张三', deleted_at: null },
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http.get('/api/wechat/wx123/o_abc').expect(200);
|
||
assert(res.body.data.user.nickname === '张三');
|
||
assert(res.body.data.user.deleted_at === undefined, '不应透出软删除字段');
|
||
});
|
||
|
||
it('GET /api/wechat/:appid/:openid 不存在返回 404', async () => {
|
||
mock(app, 'model', {
|
||
User: {},
|
||
WechatOauth: {
|
||
async findOne() {
|
||
return null;
|
||
},
|
||
},
|
||
});
|
||
|
||
await http.get('/api/wechat/wx123/o_none').expect(404);
|
||
});
|
||
|
||
it('GET /api/users/:id/wechats 返回该用户的授权列表', async () => {
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async findAll(options) {
|
||
assert(options.where.userId === 1);
|
||
return [ instance({ id: 9, appid: 'wx123', openid: 'o_abc' }) ];
|
||
},
|
||
},
|
||
});
|
||
|
||
const res = await http.get('/api/users/1/wechats').expect(200);
|
||
assert(res.body.data.total === 1);
|
||
});
|
||
|
||
it('PUT /api/wechat/:id/tokens 更新令牌', async () => {
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async findByPk(id) {
|
||
assert(id === 9);
|
||
return instance({
|
||
id: 9,
|
||
update: async fields => {
|
||
assert(fields.accessToken === 'at-1');
|
||
assert(fields.tokenExpiresAt instanceof Date);
|
||
},
|
||
});
|
||
},
|
||
},
|
||
});
|
||
|
||
await http
|
||
.put('/api/wechat/9/tokens')
|
||
.send({ accessToken: 'at-1', expiresIn: 7200 })
|
||
.expect(200);
|
||
});
|
||
|
||
it('PUT /api/wechat/:id/tokens 记录不存在返回 404', async () => {
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async findByPk() {
|
||
return null;
|
||
},
|
||
},
|
||
});
|
||
|
||
await http.put('/api/wechat/9/tokens').send({ accessToken: 'at-1' }).expect(404);
|
||
});
|
||
|
||
it('DELETE /api/wechat/:id 解绑成功 / 不存在返回 404', async () => {
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async destroy() {
|
||
return 1;
|
||
},
|
||
},
|
||
});
|
||
await http.delete('/api/wechat/9').expect(200);
|
||
|
||
mock(app, 'model', {
|
||
WechatOauth: {
|
||
async destroy() {
|
||
return 0;
|
||
},
|
||
},
|
||
});
|
||
await http.delete('/api/wechat/9').expect(404);
|
||
});
|
||
});
|