- POST /api/web/auth/wechat/login:code 换 openid,已绑定直接签发令牌, 未绑定自动建平台账号(首登标记 firstTime) - bind / bindings / unbind:登录态下绑定、查询、解绑小程序账号 - wechat_mp_accounts 表 + 迁移;未配置 WX_MP_APPID/SECRET 时本地与单测走 mock - 新增 cors 中间件(小程序 H5 预览直连后端;鉴权只靠 header 令牌,不依赖 cookie) - 补充 wechat-mp 接口单测 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
157 lines
5.6 KiB
JavaScript
157 lines
5.6 KiB
JavaScript
'use strict';
|
||
|
||
const { app, mock, assert } = require('egg-mock/bootstrap');
|
||
const createFakeRedis = require('../../utils/fake-redis');
|
||
|
||
const instance = (data) => Object.assign(
|
||
{
|
||
toJSON: () => data,
|
||
update: async (fields) => Object.assign(data, fields),
|
||
},
|
||
data,
|
||
);
|
||
|
||
describe('test/app/controller/wechat-mp.test.js', () => {
|
||
const agreement = {
|
||
id: 9,
|
||
code: 'user_agreement',
|
||
name: '用户协议',
|
||
required: true,
|
||
version: { id: 11, version: '1.0.0', title: '用户协议', content: '正文' },
|
||
};
|
||
const accepted = [{ code: 'user_agreement', versionId: 11 }];
|
||
|
||
it('未同意协议 → 428(与账号密码登录一致)', async () => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
app.mockService('agreement', 'requiredVersions', async () => [ agreement ]);
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/web/auth/wechat/login')
|
||
.send({ code: 'mock:openid-abc' })
|
||
.expect(428);
|
||
|
||
assert(res.body.status === 'agreement_required', JSON.stringify(res.body));
|
||
assert(res.body.data.agreements[0].code === 'user_agreement', JSON.stringify(res.body));
|
||
});
|
||
|
||
it('首次登录:按 openid 建账号 + 绑定,返回 firstTime=true', async () => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
|
||
let account = null;
|
||
mock(app, 'model', {
|
||
WechatMpAccount: {
|
||
async findOne() {
|
||
return account ? instance(account.data) : null;
|
||
},
|
||
async create(data) {
|
||
account = { data: Object.assign({ id: 5, loginCount: 0, nickname: '', avatar: '' }, data) };
|
||
return instance(account.data);
|
||
},
|
||
},
|
||
});
|
||
|
||
let registered = null;
|
||
app.mockService('auth', 'register', async (payload) => {
|
||
registered = payload;
|
||
return { id: 42, username: payload.username, nickname: payload.nickname };
|
||
});
|
||
app.mockService('user', 'findById', async () => ({ id: 42, username: registered.username, status: 1, nickname: '微信用户' }));
|
||
app.mockService('user', 'touchLogin', async () => null);
|
||
app.mockService('agreement', 'requiredVersions', async () => [ agreement ]);
|
||
app.mockService('agreement', 'record', async () => [{ code: 'user_agreement', versionId: 11, version: '1.0.0' }]);
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/web/auth/wechat/login')
|
||
.send({ code: 'mock:openid-abc', agreements: accepted })
|
||
.expect(200);
|
||
|
||
assert(res.body.ok === true, JSON.stringify(res.body));
|
||
assert(res.body.data.firstTime === true, JSON.stringify(res.body.data));
|
||
assert(res.body.data.wechat.userId === 42, JSON.stringify(res.body.data.wechat));
|
||
assert(registered && registered.username.startsWith('wx_'), JSON.stringify(registered));
|
||
assert(res.body.data.agreements[0].versionId === 11, JSON.stringify(res.body.data));
|
||
});
|
||
|
||
it('已绑定账号再登录:复用账号,不重复建用户', async () => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
|
||
const account = {
|
||
id: 5,
|
||
userId: 7,
|
||
status: 1,
|
||
loginCount: 3,
|
||
nickname: '',
|
||
avatar: '',
|
||
data: { id: 5, userId: 7, status: 1, loginCount: 3, nickname: '', avatar: '' },
|
||
};
|
||
mock(app, 'model', {
|
||
WechatMpAccount: {
|
||
async findOne() {
|
||
return instance(account.data);
|
||
},
|
||
},
|
||
});
|
||
app.mockService('user', 'findById', async () => ({ id: 7, username: 'demo', status: 1 }));
|
||
app.mockService('user', 'touchLogin', async () => null);
|
||
app.mockService('agreement', 'requiredVersions', async () => [ agreement ]);
|
||
app.mockService('agreement', 'record', async () => []);
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/web/auth/wechat/login')
|
||
.send({ code: 'mock:openid-abc', agreements: accepted })
|
||
.expect(200);
|
||
|
||
assert(res.body.data.firstTime === false, JSON.stringify(res.body.data));
|
||
assert(res.body.data.user.id === 7, JSON.stringify(res.body.data.user));
|
||
assert(res.body.data.wechat.loginCount === 4, JSON.stringify(res.body.data.wechat));
|
||
});
|
||
|
||
it('缺 code → 422;账号被禁用 → 403', async () => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
app.mockService('agreement', 'requiredVersions', async () => [ agreement ]);
|
||
|
||
await app.httpRequest().post('/api/web/auth/wechat/login').send({ agreements: accepted }).expect(422);
|
||
|
||
mock(app, 'model', {
|
||
WechatMpAccount: {
|
||
async findOne() {
|
||
return instance({ id: 5, userId: 7, status: 1, loginCount: 0, nickname: '', avatar: '' });
|
||
},
|
||
},
|
||
});
|
||
app.mockService('user', 'findById', async () => ({ id: 7, username: 'demo', status: 0 }));
|
||
|
||
const res = await app.httpRequest()
|
||
.post('/api/web/auth/wechat/login')
|
||
.send({ code: 'mock:openid-abc', agreements: accepted })
|
||
.expect(403);
|
||
assert(res.body.message.includes('禁用'), JSON.stringify(res.body));
|
||
});
|
||
|
||
it('绑定:openid 已属于别人 → 409', async () => {
|
||
mock(app, 'redis', createFakeRedis().client);
|
||
|
||
mock(app, 'model', {
|
||
// apiAuth 会用 service.user.findById → User.findByPk
|
||
User: {
|
||
async findByPk() {
|
||
return instance({ id: 7, username: 'demo', status: 1 });
|
||
},
|
||
},
|
||
WechatMpAccount: {
|
||
async findOne() {
|
||
return instance({ id: 5, userId: 99, status: 1 });
|
||
},
|
||
},
|
||
});
|
||
|
||
const token = app.createAnonymousContext().service.jwt.signUser({ id: 7, username: 'demo' });
|
||
const res = await app.httpRequest()
|
||
.post('/api/web/auth/wechat/bind')
|
||
.set('Authorization', `Bearer ${token}`)
|
||
.send({ code: 'mock:openid-other' })
|
||
.expect(409);
|
||
assert(res.body.message.includes('已绑定其他账号'), JSON.stringify(res.body));
|
||
});
|
||
});
|