liufumingren dc4894cb6d 1
2026-05-25 17:08:56 +08:00

351 lines
10 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { strict: assert } = require('node:assert');
const path = require('node:path');
const { statSync } = require('node:fs');
const { app, mm } = require('egg-mock/bootstrap');
const HomeController = require('../../../app/controller/home');
const PoloApiService = require('../../../app/service/systems/polo_api');
describe('test/app/controller/home.test.js', () => {
it('should assert', async () => {
const pkg = require('../../../package.json');
assert(app.config.keys.startsWith(pkg.name));
});
it('should typings exists', async () => {
const typings = path.join(__dirname, '../../../typings');
assert(statSync(typings));
});
it('should GET /', async () => {
return app.httpRequest()
.get('/')
.expect('pong')
.expect(200);
});
it('should GET /testPoloApi return poloApi.test result directly', async () => {
const expectedResult = {
ok: true,
data: {
message: 'real result passthrough',
},
};
let callCount = 0;
try {
mm(PoloApiService.prototype, 'test', async () => {
callCount += 1;
return expectedResult;
});
const res = await app.httpRequest()
.get('/testPoloApi')
.expect('Content-Type', /json/)
.expect(200);
assert.deepEqual(res.body, expectedResult);
assert.equal(callCount, 1);
} finally {
mm.restore();
}
});
it('should extract qimen warehouseCode from controller helper', async () => {
const controller = Object.create(HomeController.prototype);
assert.equal(
controller.getQimenWarehouseCode({
request: {
deliveryOrder: {
warehouseCode: '100000',
},
},
}),
'100000'
);
assert.equal(
controller.getQimenWarehouseCode({
request: {
warehouseCode: 'warehouseCode',
deliveryOrder: {
warehouseCode: '100000',
},
},
}),
'100000'
);
assert.equal(
controller.getQimenWarehouseCode({
request: {
warehouseCode: 'warehouseCode',
deliveryOrder: {
warehouseCode: 'warehouseCode',
},
},
}),
''
);
assert.equal(
controller.getQimenWarehouseCode({
request: {
deliveryOrder: {
warehouseCode: '',
},
},
}),
''
);
});
it('should ignore unrelated string nodes when extracting qimen warehouseCode', async () => {
const controller = Object.create(HomeController.prototype);
assert.equal(
controller.getQimenWarehouseCode({
request: {
deliveryOrder: {
warehouseCode: 'warehouseCode',
orderStatus: '100000',
note: '真实请求里不应被这些字符串节点误命中',
},
traceId: '100000',
},
}),
''
);
});
it('buildCainiaoOauthShopUpdatePayload 应正确映射菜鸟授权字段中的淘宝协议字段', async () => {
const controller = Object.create(HomeController.prototype);
const payload = controller.buildCainiaoOauthShopUpdatePayload({
taobao_user_id: 'tb-user-1',
taobao_user_nick: '淘宝店铺A',
access_token: 'tb-access-token',
refresh_token: 'tb-refresh-token',
expire_time: 1767225600000,
refresh_token_valid_time: 1769817600000,
});
assert.equal(payload.owner_id, 'tb-user-1');
assert.equal(payload.shop_name, '淘宝店铺A');
assert.equal(payload.access_token, 'tb-access-token');
assert.equal(payload.refresh_token, 'tb-refresh-token');
assert.equal(payload.token_expires_at.getTime(), 1767225600000);
assert.equal(payload.auth_expire_at.getTime(), 1767225600000);
assert.equal(payload.refresh_token_expires_at.getTime(), 1769817600000);
assert.equal(payload.scope, null);
assert.equal(payload.status, 1);
});
it('buildCainiaoOauthShopUpdatePayload 在 taobao_user_nick 缺失时不应输出 shop_name 字段', async () => {
const controller = Object.create(HomeController.prototype);
const payload = controller.buildCainiaoOauthShopUpdatePayload({
taobao_user_id: 'tb-user-2',
access_token: 'tb-access-token-2',
refresh_token: 'tb-refresh-token-2',
expire_time: 1767225600000,
refresh_token_valid_time: 1769817600000,
});
assert.equal(payload.owner_id, 'tb-user-2');
assert.equal(payload.access_token, 'tb-access-token-2');
assert.equal(payload.refresh_token, 'tb-refresh-token-2');
assert.equal(payload.token_expires_at.getTime(), 1767225600000);
assert.equal(payload.refresh_token_expires_at.getTime(), 1769817600000);
assert.equal(payload.auth_expire_at.getTime(), 1767225600000);
assert.equal(Object.prototype.hasOwnProperty.call(payload, 'shop_name'), false);
});
it('buildCainiaoOauthShopUpdatePayload 应解码 URL 编码的 taobao_user_nick 到 shop_name', async () => {
const controller = Object.create(HomeController.prototype);
const payload = controller.buildCainiaoOauthShopUpdatePayload({
taobao_user_id: 'tb-user-3',
taobao_user_nick: '15325687760%E9%BE%99%E6%B8%8A%E9%80%B8',
access_token: 'tb-access-token-3',
refresh_token: 'tb-refresh-token-3',
expire_time: 1767225600000,
refresh_token_valid_time: 1769817600000,
});
assert.equal(payload.shop_name, '15325687760龙渊逸');
});
it('platformOauthCallback 在 CAINIAO 授权失败时不应进入写库流程', async () => {
const controller = Object.create(HomeController.prototype);
let findOneCallCount = 0;
let updateCainiaoOauthShopCallCount = 0;
let renderArgs = null;
controller.app = {
config: {
platformOauthReservationCodePrefix: '',
},
Sequelize: {
Op: {
or: Symbol('or'),
ne: Symbol('ne'),
},
},
};
controller.cainiao = async code => {
assert.equal(code, 'oauth-code-tb-error');
return {
success: false,
message: '菜鸟授权失败:invalid_client',
result: {
data: {
error: 'invalid_client',
},
},
};
};
controller.updateCainiaoOauthShop = async () => {
updateCainiaoOauthShopCallCount += 1;
throw new Error('CAINIAO 授权失败场景不应调用 updateCainiaoOauthShop');
};
controller.ctx = {
query: {
code: 'oauth-code-tb-error',
state: 'CAINIAO_auth-random-tb-error',
},
model: {
Shop: {
async findOne() {
findOneCallCount += 1;
throw new Error('CAINIAO 授权失败场景不应查询店铺');
},
},
},
async render(view, data) {
renderArgs = { view, data };
},
status: null,
};
await controller.platformOauthCallback();
assert.equal(findOneCallCount, 0);
assert.equal(updateCainiaoOauthShopCallCount, 0);
assert.equal(controller.ctx.status, 400);
assert.equal(renderArgs.view, 'platform_oauth_callback.html');
assert.equal(renderArgs.data.platformCode, 'CAINIAO');
assert.equal(renderArgs.data.resultLevel, 'error');
assert.equal(renderArgs.data.resultText, '菜鸟授权失败:invalid_client');
});
it('platformOauthCallback 在 CAINIAO 场景找到店铺时应写回关键字段', async () => {
const controller = Object.create(HomeController.prototype);
const orOperator = Symbol('or');
const neOperator = Symbol('ne');
const findOneCalls = [];
const saveCalls = [];
let renderArgs = null;
const pendingShop = {
id: 1001,
warehouse_id: 2001,
platform_code: 'CAINIAO',
shop_name: '待授权店铺',
status: 0,
set(payload) {
Object.assign(this, payload);
},
async save(options) {
saveCalls.push(options);
return this;
},
async destroy() {
throw new Error('当前场景不应删除原店铺');
},
};
controller.app = {
config: {
platformOauthReservationCodePrefix: '',
},
Sequelize: {
Op: {
or: orOperator,
ne: neOperator,
},
},
};
controller.cainiao = async code => {
assert.equal(code, 'oauth-code-3');
return {
success: true,
result: {
data: {
taobao_user_id: 'tb-user-3',
taobao_user_nick: '淘宝店铺C',
access_token: 'tb-access-token-3',
refresh_token: 'tb-refresh-token-3',
expire_time: 1767225600000,
refresh_token_valid_time: 1769817600000,
},
},
};
};
controller.ctx = {
query: {
code: 'oauth-code-3',
state: 'CAINIAO_auth-random-3',
},
model: {
transaction: async callback => await callback({ id: 'tx-1' }),
Shop: {
async findOne(options) {
findOneCalls.push(options);
if (options.where && options.where.auth_random_code === 'auth-random-3') {
return pendingShop;
}
if (options.where && options.where[orOperator]) {
return null;
}
throw new Error('未预期的 Shop 查询');
},
},
},
async render(view, data) {
renderArgs = { view, data };
},
status: null,
};
await controller.platformOauthCallback();
assert.equal(findOneCalls.length, 2);
assert.equal(pendingShop.owner_id, 'tb-user-3');
assert.equal(pendingShop.shop_name, '淘宝店铺C');
assert.equal(pendingShop.access_token, 'tb-access-token-3');
assert.equal(pendingShop.refresh_token, 'tb-refresh-token-3');
assert.equal(pendingShop.token_expires_at.getTime(), 1767225600000);
assert.equal(pendingShop.auth_expire_at.getTime(), 1767225600000);
assert.equal(pendingShop.refresh_token_expires_at.getTime(), 1769817600000);
assert.equal(pendingShop.status, 1);
assert.equal(saveCalls.length, 1);
assert.deepEqual(saveCalls[0].fields, [
'owner_id',
'shop_name',
'access_token',
'refresh_token',
'token_expires_at',
'refresh_token_expires_at',
'auth_expire_at',
'scope',
'status',
]);
assert.equal(controller.ctx.status, 200);
assert.equal(renderArgs.view, 'platform_oauth_callback.html');
assert.equal(renderArgs.data.platformCode, 'CAINIAO');
assert.equal(renderArgs.data.resultLevel, 'warning');
});
});