Fix API key error states and verify revocation lifecycle

This commit is contained in:
2026-09-06 10:37:14 +03:00
parent 21e80d0dd6
commit 372c0e76e0
7 changed files with 164 additions and 30 deletions
+36
View File
@@ -0,0 +1,36 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { apiKeyRequest } from '../src/services/apiKeys.js';
test('API key client distinguishes failure, malformed data, and a genuinely empty list', async () => {
const original = globalThis.fetch;
try {
globalThis.fetch = async () => new Response(JSON.stringify({success:true,anahtarlar:[],limit:0}));
assert.deepEqual((await apiKeyRequest()).anahtarlar, []);
globalThis.fetch = async () => new Response(JSON.stringify({success:false,error:'Depo erişilemiyor'}),{status:503});
await assert.rejects(apiKeyRequest(), /Depo erişilemiyor/);
globalThis.fetch = async () => new Response(JSON.stringify({success:true,anahtarlar:null}));
await assert.rejects(apiKeyRequest(), /eksik veya geçersiz/);
globalThis.fetch = async () => new Response('<html>502</html>');
await assert.rejects(apiKeyRequest(), /geçerli anahtar bilgisi/);
} finally { globalThis.fetch = original; }
});
test('API key client rejects late private responses after session change and does not retry uncertain creation', async () => {
const original = globalThis.fetch, storage = globalThis.localStorage;
let token = 'account-a', calls = 0;
globalThis.localStorage = {getItem:()=>token};
try {
globalThis.fetch = async () => {
token = 'account-b';
return new Response(JSON.stringify({success:true,anahtar:{id:'qa',anahtar:'ih_live_qa'}}));
};
await assert.rejects(apiKeyRequest('POST',{ad:'QA'}), /Oturum değişti/);
globalThis.fetch = async (_url, options) => {
calls++;
return new Promise((_resolve,reject)=>options.signal.addEventListener('abort',()=>reject(options.signal.reason),{once:true}));
};
await assert.rejects(apiKeyRequest('POST',{ad:'QA',timeoutMs:5}), /sunucuda gerçekleşmiş olabilir/);
assert.equal(calls,1);
} finally { globalThis.fetch=original; globalThis.localStorage=storage; }
});