13 lines
2.6 KiB
TypeScript
13 lines
2.6 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import {test} from "node:test"
|
|
import {getEvoBgpSettings,putEvoBgpSettings,testEvoBgpConnection} from "./evobgp-settings"
|
|
const settings={baseUrl:"https://evo.example.test",enabled:false,secretConfigured:false},base="http://fixture.test"
|
|
const json=(value:unknown)=>new Response(JSON.stringify(value))
|
|
test("Evo settings require known boolean values and no returned secret",async()=>{const original=globalThis.fetch;try{globalThis.fetch=async()=>json(settings);assert.deepEqual(await getEvoBgpSettings(base),settings);for(const bad of [{}, {...settings,enabled:undefined},{...settings,secretConfigured:0},{...settings,apiKey:"fixture-only"},{...settings,baseUrl:"https://evo.test/v1"}]){globalThis.fetch=async()=>json(bad);await assert.rejects(getEvoBgpSettings(base),/не подтверждены/)}}finally{globalThis.fetch=original}})
|
|
test("Evo PUT normalizes explicit fields and requires matching receipt/secret flag without echoing key",async()=>{const original=globalThis.fetch;try{let calls=0;globalThis.fetch=async(_url,init)=>{calls++;assert.deepEqual(JSON.parse(String(init?.body)),{baseUrl:settings.baseUrl,enabled:false,apiKey:"fixture-only"});return json({...settings,ok:true,secretConfigured:true,savedFields:["baseUrl","enabled","apiKey"]})};await putEvoBgpSettings(base,{baseUrl:"evo.example.test/",enabled:false,apiKey:"Bearer fixture-only"});assert.equal(calls,1)
|
|
for(const bad of [{...settings},{...settings,ok:false,savedFields:["enabled"]},{...settings,ok:true,enabled:true,savedFields:["enabled"]},{...settings,ok:true,savedFields:["enabled","enabled"]}]){globalThis.fetch=async()=>json(bad);await assert.rejects(putEvoBgpSettings(base,{enabled:false}),/не подтверждено/)}
|
|
for(const patch of [{},{baseUrl:"https://evo.test/v1"},{baseUrl:"ftp://evo.test"},{apiKey:"fixture\nonly"}]){calls=0;globalThis.fetch=async()=>{calls++;return json({})};await assert.rejects(putEvoBgpSettings(base,patch));assert.equal(calls,0)}
|
|
globalThis.fetch=async()=>json({...settings,ok:true,savedFields:["apiKey"]});assert.equal((await putEvoBgpSettings(base,{apiKey:null})).secretConfigured,false)
|
|
}finally{globalThis.fetch=original}})
|
|
test("Evo test requires literal confirmation and never retries unknown POST",async()=>{const original=globalThis.fetch;try{globalThis.fetch=async()=>json({ok:true});await testEvoBgpConnection(base);for(const bad of [{},{ok:false},{ok:"true"}]){let calls=0;globalThis.fetch=async()=>{calls++;return json(bad)};await assert.rejects(testEvoBgpConnection(base),/не подтверждено/);assert.equal(calls,1)}}finally{globalThis.fetch=original}})
|