test(auth): add unit tests for ltProxyAuth and ltProxyHandler

- Add tests to validate ltProxyAuth behavior for valid, invalid, and missing API keys
- Add tests to verify ltProxyHandler correctly proxies requests and returns responses
This commit is contained in:
2025-05-11 01:10:39 +02:00
parent bcb22f983e
commit 52ce172ef5
2 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
// deno-lint-ignore-file require-await
import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts';
import type { IContext } from 'http-kernel/Interfaces/mod.ts';
import { ltProxyAuth } from '../ltProxyAuth.ts';
Deno.test('ltProxyAuth: accepts valid API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/?apiKey=valid123');
const ctx: IContext = {
req,
params: {},
query: { apiKey: 'valid123' },
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('OK', { status: 200 }),
);
assertEquals(response.status, 200);
assertEquals(await response.text(), 'OK');
});
Deno.test('ltProxyAuth: rejects invalid API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/?apiKey=invalid456');
const ctx: IContext = {
req,
params: {},
query: { apiKey: 'invalid456' },
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('SHOULD NOT HAPPEN'),
);
assertEquals(response.status, 403);
assertEquals(await response.text(), 'Forbidden – Invalid API key');
});
Deno.test('ltProxyAuth: rejects missing API key', async () => {
Deno.env.set('API_KEYS', 'valid123');
const req = new Request('http://localhost/');
const ctx: IContext = {
req,
params: {},
query: {},
state: {},
};
const response = await ltProxyAuth(
ctx,
async () => new Response('SHOULD NOT HAPPEN'),
);
assertEquals(response.status, 403);
});

View File

@@ -0,0 +1,44 @@
// deno-lint-ignore-file require-await
import { ltProxyHandler } from '../ltProxyHandler.ts';
import { assertEquals } from 'https://deno.land/std@0.204.0/assert/mod.ts';
import type { IContext } from 'http-kernel/Interfaces/mod.ts';
Deno.test('ltProxyHandler: proxies request and returns response', async () => {
const expectedResponse = new Response('Mocked LT Response', {
status: 200,
headers: {
'content-type': 'text/plain',
'x-mocked': 'true',
},
});
// Backup and mock global fetch
const originalFetch = globalThis.fetch;
globalThis.fetch = async () => expectedResponse;
const req = new Request('http://localhost/v2/check?language=de-DE', {
method: 'POST',
body: new TextEncoder().encode('text=Hallo+Welt'),
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
});
const ctx: IContext = {
req,
params: {},
query: {
language: 'de-DE',
},
state: {},
};
const response = await ltProxyHandler(ctx);
assertEquals(response.status, 200);
assertEquals(await response.text(), 'Mocked LT Response');
assertEquals(response.headers.get('x-mocked'), 'true');
// Restore fetch
globalThis.fetch = originalFetch;
});