from fastapi.testclient import TestClient import gateway.app as gateway_module class DummyResponse: def __init__( self, status_code: int, headers: dict[str, str], json_payload: dict | None = None, text: str = '', content: bytes | None = None, ): self.status_code = status_code self.headers = headers self._json_payload = json_payload or {} self.text = text self.content = content if content is not None else text.encode("utf-8") def json(self) -> dict: return self._json_payload class DummyAsyncClient: last_request: dict = {} def __init__(self, timeout: int): self.timeout = timeout async def __aenter__(self): return self async def __aexit__(self, exc_type, exc, tb): return None async def request(self, method, url, params=None, content=None, headers=None): DummyAsyncClient.last_request = { 'method': method, 'url': url, 'params': params, 'content': content, 'headers': headers or {}, } return DummyResponse(200, {'content-type': 'application/json'}, {'ok': True}) def test_gateway_forwards_authorization_header(monkeypatch): monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.get( '/proxy/auth/auth/me', headers={'Authorization': 'Bearer test-token', 'X-User': 'admin', 'X-Role': 'admin'}, ) assert response.status_code == 200 assert DummyAsyncClient.last_request['headers']['Authorization'] == 'Bearer test-token' assert DummyAsyncClient.last_request['headers']['X-User'] == 'admin' def test_gateway_forwards_telegram_secret_header(monkeypatch): monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.post( '/proxy/telegram/integrations/telegram/bot/webhook', headers={'X-Telegram-Bot-Api-Secret-Token': 'secret-token'}, json={'update_id': 1}, ) assert response.status_code == 200 assert DummyAsyncClient.last_request['headers']['X-Telegram-Bot-Api-Secret-Token'] == 'secret-token' def test_gateway_forwards_whatsapp_secret_header(monkeypatch): monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.post( '/proxy/whatsapp/integrations/whatsapp/provider/webhook', headers={'X-WhatsApp-Webhook-Secret': 'wa-secret'}, json={'entry': []}, ) assert response.status_code == 200 assert DummyAsyncClient.last_request['headers']['X-WhatsApp-Webhook-Secret'] == 'wa-secret' assert DummyAsyncClient.last_request['content'] == b'{"entry":[]}' def test_gateway_forwards_meta_signature_header_and_raw_body(monkeypatch): monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.post( '/proxy/whatsapp/integrations/whatsapp/provider/webhook', headers={ 'X-Hub-Signature-256': 'sha256=test-signature', 'Content-Type': 'application/json', }, content=b'{"entry":[{"changes":[]}]}' , ) assert response.status_code == 200 assert DummyAsyncClient.last_request['headers']['X-Hub-Signature-256'] == 'sha256=test-signature' assert DummyAsyncClient.last_request['headers']['Content-Type'] == 'application/json' assert DummyAsyncClient.last_request['content'] == b'{"entry":[{"changes":[]}]}' def test_gateway_preserves_html_response(monkeypatch): async def request(self, method, url, params=None, content=None, headers=None): # noqa: ANN001, ANN201 return DummyResponse(200, {'content-type': 'text/html'}, text='bridge') monkeypatch.setattr(DummyAsyncClient, 'request', request) monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.get('/proxy/auth/auth/oidc/callback') assert response.status_code == 200 assert 'bridge' in response.text def test_gateway_preserves_plain_text_response(monkeypatch): async def request(self, method, url, params=None, content=None, headers=None): # noqa: ANN001, ANN201 return DummyResponse(200, {'content-type': 'text/plain; charset=utf-8'}, text='123456') monkeypatch.setattr(DummyAsyncClient, 'request', request) monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.get('/proxy/whatsapp/integrations/whatsapp/provider/webhook') assert response.status_code == 200 assert response.text == '123456' assert response.headers['content-type'].startswith('text/plain') def test_gateway_forwards_delete_requests(monkeypatch): monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.delete( '/proxy/routing/queues/que_delete_me', headers={'X-User': 'admin', 'X-Role': 'admin'}, ) assert response.status_code == 200 assert DummyAsyncClient.last_request['method'] == 'DELETE' assert DummyAsyncClient.last_request['url'].endswith('/queues/que_delete_me') def test_gateway_preserves_binary_audio_response(monkeypatch): async def request(self, method, url, params=None, content=None, headers=None): # noqa: ANN001, ANN201 return DummyResponse( 200, { 'content-type': 'audio/wav', 'content-disposition': 'attachment; filename="demo-call.wav"', }, content=b'RIFFdemo', ) monkeypatch.setattr(DummyAsyncClient, 'request', request) monkeypatch.setattr(gateway_module.httpx, 'AsyncClient', DummyAsyncClient) client = TestClient(gateway_module.app) response = client.get('/proxy/recording/recordings/rec_demo/content') assert response.status_code == 200 assert response.content == b'RIFFdemo' assert response.headers['content-type'].startswith('audio/wav') assert 'demo-call.wav' in response.headers['content-disposition']