cmvr-wms/mobile-app/tests/production-network-security.test.mjs
lixiaolong 95effeeff2 feat(app): 优化移动端应用配置和功能实现
- 添加生产环境服务器地址配置支持内外网切换
- 实现采购列表只看我创建的功能开关
- 更新构建脚本中的默认生产环境API地址
- 优化采购单表格单元格编辑体验改用自动补全组件
- 调整应用更新检查逻辑增强生产环境兼容性
- 修复开发服务器切换时的一些验证问题
- 增强应用启动时的服务地址配置灵活性
2026-10-29 17:27:46 +08:00

30 lines
2.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {createRequire} from 'node:module';
import vm from 'node:vm';
const require=createRequire(import.meta.url);
const appConfig=require('../app.config.js');
const source=readFileSync(new URL('../plugins/with-internal-network.cjs',import.meta.url),'utf8');
async function generate(env,hosts){
let xml='',manifest;
const plugins={withAndroidManifest:(config,fn)=>{const result=fn({modResults:{manifest:{application:[{$:{}}]}}});manifest=result.modResults.manifest.application[0].$;return config},withDangerousMod:(config,args)=>({config,handler:args[1]})};
const module={exports:{}};
vm.runInNewContext(source,{module,exports:module.exports,require:name=>name==='expo/config-plugins'?plugins:name==='node:fs/promises'?{mkdir:async()=>{},writeFile:async(_path,data)=>{xml=data}}:name==='node:path'?require('node:path'):name==='../config/environment.cjs'?{environment:()=>env}:require(name)});
const result=module.exports({}, {hosts});await result.handler({modRequest:{platformProjectRoot:'dummy'}});return {manifest,xml};
}
test('production APK permits cleartext only for its fixed private endpoint',async()=>{
const original={...process.env};
try{
process.env.APP_ENV='prod';process.env.APP_API_URL='https://wms.svc.cmvr-iot.icu/prod-api';process.env.APP_UPDATE_ORIGIN='https://wms.svc.cmvr-iot.icu';
const config=appConfig(),plugin=config.plugins.find(x=>Array.isArray(x)&&x[0]==='./plugins/with-internal-network.cjs');
assert.deepEqual(plugin[1].hosts,['192.168.28.10']);
const {xml,manifest}=await generate({variant:'prod',apiUrl:process.env.APP_API_URL,updateOrigin:process.env.APP_UPDATE_ORIGIN},plugin[1].hosts);
assert.equal(manifest['android:usesCleartextTraffic'],'false');
assert.match(xml,/<base-config cleartextTrafficPermitted="false"\/>/);
assert.match(xml,/<domain includeSubdomains="false">192\.168\.28\.10<\/domain>/);
assert.equal((xml.match(/<domain /g)||[]).length,1);
await assert.rejects(generate({variant:'prod',apiUrl:process.env.APP_API_URL,updateOrigin:process.env.APP_UPDATE_ORIGIN},['evil.example']));
}finally{process.env=original;}
});