Files
back_end_oudelaa/oudelaa_dashboard/tests/api-proxy.test.cjs
boutmoun123 1b753a419a
API Gateway CI / Build, lint, and test (push) Successful in 13m33s
API Gateway CI / Build API Gateway image (push) Successful in 5m43s
feat: sync marketplace dashboard and staging CI
2026-11-02 17:28:58 +03:00

78 lines
4.3 KiB
JavaScript

const { NextRequest } = require('next/server');
describe('Dashboard proxy', () => {
const originalFetch = global.fetch;
let route;
beforeEach(() => {
jest.resetModules();
process.env.API_BASE_URL = 'http://primary.test/api/v1';
process.env.API_FALLBACK_URLS = 'http://fallback.test/api/v1';
delete process.env.NEXT_PUBLIC_API_BASE_URL;
process.env.API_PROXY_TIMEOUT_MS = 'not-a-number';
route = require('../app/api/proxy/[...path]/route');
global.fetch = jest.fn();
});
afterEach(() => { global.fetch = originalFetch; });
const call = (method, path, headers = {}, body) => route[method](
new NextRequest(`http://dashboard.test/api/proxy/${path.join('/')}`, {
method, headers, ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
}), { params: Promise.resolve({ path }) },
);
it('sets separate HttpOnly company cookies from a wrapped login without a scope header', async () => {
global.fetch.mockResolvedValue(Response.json({ data: { accessToken: 'company-access', refreshToken: 'company-refresh' }, meta: { statusCode: 201 } }));
const response = await call('POST', ['auth', 'login'], { 'content-type': 'application/json' }, { email: 'test@example.com', password: 'test' });
expect(response.cookies.get('oudelaa_company_access').value).toBe('company-access');
expect(response.cookies.get('oudelaa_company_refresh').value).toBe('company-refresh');
expect(response.cookies.get('oudelaa_sa_access')).toBeUndefined();
expect(response.headers.get('set-cookie')).toContain('HttpOnly');
expect(response.headers.get('cache-control')).toBe('no-store');
});
it('takes superadmin auth scope from the route and retains support for unwrapped tokens', async () => {
global.fetch.mockResolvedValue(Response.json({ accessToken: 'super-access', refreshToken: 'super-refresh' }));
const response = await call('POST', ['auth', 'superadmin', 'login'], { 'x-oudelaa-auth-scope': 'company' }, {});
expect(response.cookies.get('oudelaa_sa_access').value).toBe('super-access');
expect(response.cookies.get('oudelaa_company_access')).toBeUndefined();
});
it('injects the correct refresh cookie into refresh requests', async () => {
global.fetch.mockResolvedValue(Response.json({ accessToken: 'new-token' }));
await call('POST', ['auth', 'refresh'], { 'content-type': 'application/json', cookie: 'oudelaa_company_refresh=company-token; oudelaa_sa_refresh=super-token' }, {});
expect(JSON.parse(global.fetch.mock.calls[0][1].body)).toEqual({ refreshToken: 'company-token' });
});
it('does not retry a mutation on the fallback after an ambiguous timeout', async () => {
global.fetch.mockRejectedValue(new Error('Timeout after request was accepted'));
const response = await call('POST', ['marketplace', 'orders'], { 'content-type': 'application/json' }, { items: [] });
expect(response.status).toBe(502);
expect(global.fetch).toHaveBeenCalledTimes(1);
});
it('falls back for reads and keeps company and superadmin authorization separate', async () => {
global.fetch.mockRejectedValueOnce(new Error('offline')).mockResolvedValueOnce(Response.json({ items: [] }));
const response = await call('GET', ['orders', 'admin'], { 'x-oudelaa-auth-scope': 'company', cookie: 'oudelaa_company_access=company-token; oudelaa_sa_access=super-token' });
expect(response.status).toBe(200);
expect(global.fetch).toHaveBeenCalledTimes(2);
const [url, init] = global.fetch.mock.calls[1];
expect(url).toBe('http://fallback.test/api/v1/orders/admin');
expect(init.headers.get('authorization')).toBe('Bearer company-token');
expect(init.redirect).toBe('manual');
});
it('rejects path traversal before forwarding credentials', async () => {
const response = await call('GET', ['..', 'auth']);
expect(response.status).toBe(400);
expect(global.fetch).not.toHaveBeenCalled();
});
it('clears only company cookies on company logout', async () => {
global.fetch.mockResolvedValue(Response.json({ success: true }));
const response = await call('POST', ['auth', 'logout'], { 'content-type': 'application/json' }, {});
expect(response.cookies.get('oudelaa_company_access').value).toBe('');
expect(response.cookies.get('oudelaa_company_refresh').value).toBe('');
expect(response.cookies.get('oudelaa_sa_access')).toBeUndefined();
});
});