'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); }); });