Files
2026-10-09 14:57:52 +03:00

46 lines
1.8 KiB
Python

import asyncio
import json
import unittest
from unittest.mock import patch
from src.main import app
class ErrorTests(unittest.TestCase):
def test_unexpected_error_has_request_id(self):
async def request():
sent = []
received = False
async def receive():
nonlocal received
if not received:
received = True
return {'type': 'http.request', 'body': b'', 'more_body': False}
await asyncio.Event().wait()
async def send(message):
sent.append(message)
scope = {
'type': 'http', 'asgi': {'version': '3.0'}, 'http_version': '1.1',
'method': 'GET', 'scheme': 'http', 'path': '/api/v1/users/me',
'raw_path': b'/api/v1/users/me', 'query_string': b'',
'headers': [(b'x-user-id', b'test'), (b'x-request-id', b'failure-test')],
'server': ('localhost', 8080), 'client': ('localhost', 1234),
}
with patch('src.dependencies.store.require_user', side_effect=RuntimeError('private detail')):
try:
await app(scope, receive, send)
except RuntimeError:
pass
return sent
sent = asyncio.run(request())
start = next(item for item in sent if item['type'] == 'http.response.start')
body = b''.join(item.get('body', b'') for item in sent if item['type'] == 'http.response.body')
self.assertEqual(start['status'], 500)
self.assertEqual(dict(start['headers'])[b'x-request-id'], b'failure-test')
self.assertEqual(json.loads(body)['error']['request_id'], 'failure-test')
self.assertNotIn(b'private detail', body)