import test from 'node:test'; import assert from 'node:assert/strict'; import {profilePayload,profileError,passwordError,feedbackPayload,feedbackStatuses,imagePath} from '../src/core/account.ts'; import {ApiClient,base64} from '../src/core/network.ts'; test('profile update allowlist preserves all four fields, never submits identity or role changes',()=>{ const form=profilePayload({nickName:' 同事 ',email:' a@cmvr.cn ',phonenumber:'13812345678',sex:'1',userId:2,roles:['admin'],deptId:1}); assert.deepEqual(form,{nickName:'同事',email:'a@cmvr.cn',phonenumber:'13812345678',sex:'1'}); assert.equal(profileError(form),'');assert.match(profileError({...form,email:'abc'}),/邮箱/);assert.match(profileError({...form,phonenumber:'123'}),/手机/); assert.equal(profileError({...form,email:'',phonenumber:''}),'');assert.equal(profilePayload({nickName:'甲'}).sex,'2'); }); test('password validation follows current server account policy and confirms without changing credentials',()=>{ assert.match(passwordError('','123456','123456','0'),/当前密码/); assert.match(passwordError('oldpass','123','123','0'),/6–20/); assert.match(passwordError('oldpass','123456','654321','0'),/不一致/); assert.match(passwordError('123456','123456','123456','0'),/相同/); assert.match(passwordError('oldpass','123456','123456','3'),/字母和数字/); assert.equal(passwordError('oldpass','New123','New123','3'),''); assert.equal(passwordError('oldpass','New123!','New123!','4'),''); assert.match(passwordError('oldpass','bad<123','bad<123','0'),/请勿/); }); test('feedback uses backend enums and image ownership identifiers, no client identity/admin fields',()=>{ const id='a'.repeat(32);assert.deepEqual(feedbackPayload('BUG',' 标题 ',' 详细情况 ',[id]),{type:'BUG',title:'标题',description:'详细情况',images:[id],pagePath:'/mobile/feedback',pageTitle:'移动端问题反馈'}); assert.throws(()=>feedbackPayload('REQUIREMENT','标题','描述',[])); assert.throws(()=>feedbackPayload('BUG',' ','描述',[]));assert.throws(()=>feedbackPayload('BUG','标题','描述',[id,id])); assert.throws(()=>feedbackPayload('FEATURE','标题','描述',['../../other.png'])); assert.equal(feedbackStatuses.RESOLVED,'已解决'); }); test('avatar never takes credentials off-origin through a user supplied URL',()=>{ assert.equal(imagePath('/profile/avatar/2026/a.jpg'),'/profile/avatar/2026/a.jpg'); for(const path of ['https://evil.test/a.png','//evil.test/profile/a','/profile/../secret','/profile/%2e%2e/file','/profile/a?token=123'])assert.equal(imagePath(path),null); }); test('binary image content is authenticated, validated and encoded without leaking a bearer in URLs',async()=>{ let request;const api=new ApiClient('http://192.168.1.1',()=>{},async(url,options)=>{request={url,options};return new Response(new Uint8Array([1,2,3,4]),{headers:{'Content-Type':'image/png'}});});api.authenticate('test-only'); const uri=await api.request('/system/feedback/images/abc/content',{responseType:'image'}); assert.equal(uri,'data:image/png;base64,AQIDBA==');assert.equal(request.options.headers.Authorization,'Bearer test-only');assert.doesNotMatch(request.url,/test-only/);assert.equal(request.options.redirect,'error'); for(let size=0;size<100;size++){const bytes=Uint8Array.from({length:size},(_,i)=>i*7%256);assert.equal(base64(bytes),Buffer.from(bytes).toString('base64'));} }); test('stale images and forbidden image reads are not displayed as successful content',async()=>{ let finish;const api=new ApiClient('http://192.168.1.1',()=>{},()=>new Promise(r=>finish=r));api.authenticate('a');const old=api.request('/profile/a.png',{responseType:'image'});api.authenticate('b');finish(new Response(new Uint8Array([1]),{headers:{'Content-Type':'image/png'}}));await assert.rejects(old,e=>e.code===409); const denied=new ApiClient('http://192.168.1.1',()=>{},async()=>new Response(JSON.stringify({code:403}),{headers:{'Content-Type':'application/json'}}));await assert.rejects(denied.request('/system/feedback/images/a/content',{responseType:'image'}),e=>e.code===403); }); test('oversized protected image is rejected before decoding',async()=>{ const api=new ApiClient('http://192.168.1.1',()=>{},async()=>new Response(new Uint8Array([1]),{headers:{'Content-Type':'image/png','Content-Length':String(7*1024*1024)}}));await assert.rejects(api.request('/profile/a.png',{responseType:'image'}),e=>e.code===413); });