Skip to content

Commit

Permalink
[1/2] Fix: Log request body (#6404)
Browse files Browse the repository at this point in the history
Add test for new request body logging
  • Loading branch information
achave11-ucsc committed Oct 29, 2024
1 parent 0fcd47c commit 1f819ed
Showing 1 changed file with 161 additions and 130 deletions.
291 changes: 161 additions & 130 deletions test/service/test_app_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
DEBUG,
INFO,
)
from unittest.mock import (
PropertyMock,
patch,
)

import requests

from azul import (
Config,
)
from azul.chalice import (
log,
)
Expand Down Expand Up @@ -48,141 +55,165 @@ def lambda_name(cls) -> str:
return 'service'

def test_request_logs(self):
for level in INFO, DEBUG:
for azul_debug in (0, 1, 2):
level = [INFO, DEBUG, DEBUG][azul_debug]
for authenticated in False, True:
with self.subTest(level=level, authenticated=authenticated):
for request_body in False, True:
url = self.base_url.set(path='/index/projects')
headers = {'authorization': 'Bearer foo_token'} if authenticated else {}
with self.assertLogs(logger=log, level=level) as logs:
requests.get(str(url), headers=headers)
logs = [(r.levelno, r.getMessage()) for r in logs.records]
headers = {
'host': url.netloc,
'user-agent': 'python-requests/2.32.2',
'accept-encoding': 'gzip, deflate, br',
'accept': '*/*',
'connection': 'keep-alive',
**headers,
}
self.assertEqual(logs, [
(
INFO,
f"Received GET request for '/index/projects', "
f"with {json.dumps({'query': None, 'headers': headers})}."
),
(
INFO,
"Authenticated request as OAuth2(access_token='foo_token')"
if authenticated else
'Did not authenticate request.'
),
(
level,
'Returning 200 response. To log headers and body, set AZUL_DEBUG to 1.'
if level == INFO else
'Returning 200 response with headers {"Access-Control-Allow-Origin": '
'"*", "Access-Control-Allow-Headers": '
'"Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key", '
f'"Content-Security-Policy": "default-src {sq("self")}", '
'"Referrer-Policy": "strict-origin-when-cross-origin", '
'"Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload", '
'"X-Content-Type-Options": "nosniff", '
'"X-Frame-Options": "DENY", '
'"X-XSS-Protection": "1; mode=block", '
'"Cache-Control": "no-store"}. '
'See next line for the first 1024 characters of the body.\n'
+ json_head(1024, {
'pagination': {
'count': 1,
'total': 1,
'size': 10,
'next': None,
'previous': None,
'pages': 1,
'sort': 'projectTitle',
'order': 'asc'
},
'termFacets': {
'organ': {
'terms': [{
'term': 'pancreas',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'sampleEntityType': {
'terms': [{
'term': 'specimens',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'dataUseRestriction': {
'terms': [{
'term': None,
'count': 1
}],
if request_body:
request_body = json.dumps({'filters': json.dumps({'organ': {'is': ['foo']}})})
headers = {
'content-length': str(len(request_body)),
'content-type': 'application/json',
**headers,
}
with self.subTest(level=level,
authenticated=authenticated,
request_body=request_body):
with self.assertLogs(logger=log, level=level) as logs:
debug = PropertyMock(return_value=azul_debug)
with patch.object(Config, 'debug', new=debug):
json_body = json.loads(request_body) if request_body else None
response = requests.get(str(url), headers=headers, json=json_body)
logs = [(r.levelno, r.getMessage()) for r in logs.records]
headers = {
'host': url.netloc,
'user-agent': 'python-requests/2.32.2',
'accept-encoding': 'gzip, deflate, br',
'accept': '*/*',
'connection': 'keep-alive',
**headers,
}
self.assertEqual(logs, [
(
INFO,
f"Received GET request for '/index/projects', "
f"with {json.dumps({'query': None, 'headers': headers})}"
),
(
INFO,
'… without request body'
) if not request_body else
(
INFO,
f'… with request body {request_body!r} '
f'({str(len(request_body)) if azul_debug == 2 else "first 1024"} characters)'
),
(
INFO,
"Authenticated request as OAuth2(access_token='foo_token')"
if authenticated else
'Did not authenticate request.'
),
(
level,
'Returning 200 response. To log headers and body, set AZUL_DEBUG to 1.'
if level == INFO else
'Returning 200 response with headers {"Access-Control-Allow-Origin": '
'"*", "Access-Control-Allow-Headers": '
'"Authorization,Content-Type,X-Amz-Date,X-Amz-Security-Token,X-Api-Key", '
f'"Content-Security-Policy": "default-src {sq("self")}", '
'"Referrer-Policy": "strict-origin-when-cross-origin", '
'"Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload", '
'"X-Content-Type-Options": "nosniff", '
'"X-Frame-Options": "DENY", '
'"X-XSS-Protection": "1; mode=block", '
'"Cache-Control": "no-store"}. '
'See next line for the first 1024 characters of the body.\n'
+ json_head(1024, {
'pagination': {
'count': 1,
'total': 1,
'type': 'terms'
'size': 10,
'next': None,
'previous': None,
'pages': 1,
'sort': 'projectTitle',
'order': 'asc'
},
'project': {
'terms': [{
'term': 'Single of human pancreas',
'count': 1,
'projectId': ['e8642221-4c2c-4fd7-b926-a68bce363c88']
}],
'total': 1,
'type': 'terms'
},
'sampleDisease': {
'terms': [{
'term': 'normal',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'nucleicAcidSource': {
'terms': [{
'term': 'single cell',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'assayType': {
'terms': [{
'term': None,
'count': 1
}],
'total': 0,
'type': 'terms'
},
'instrumentManufacturerModel': {
'terms': [{
'term': 'Illumina NextSeq 500',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'institution': {
'terms': [
{
'term': 'Farmers Trucks',
'termFacets': {
'organ': {
'terms': [{
'term': 'pancreas',
'count': 1
},
{
'term': 'University',
}],
'total': 1,
'type': 'terms'
},
'sampleEntityType': {
'terms': [{
'term': 'specimens',
'count': 1
}
],
'total': 1,
'type': 'terms'
}],
'total': 1,
'type': 'terms'
},
'dataUseRestriction': {
'terms': [{
'term': None,
'count': 1
}],
'total': 1,
'type': 'terms'
},
'project': {
'terms': [{
'term': 'Single of human pancreas',
'count': 1,
'projectId': ['e8642221-4c2c-4fd7-b926-a68bce363c88']
}],
'total': 1,
'type': 'terms'
},
'sampleDisease': {
'terms': [{
'term': 'normal',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'nucleicAcidSource': {
'terms': [{
'term': 'single cell',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'assayType': {
'terms': [{
'term': None,
'count': 1
}],
'total': 0,
'type': 'terms'
},
'instrumentManufacturerModel': {
'terms': [{
'term': 'Illumina NextSeq 500',
'count': 1
}],
'total': 1,
'type': 'terms'
},
'institution': {
'terms': [
{
'term': 'Farmers Trucks',
'count': 1
},
{
'term': 'University',
'count': 1
}
],
'total': 1,
'type': 'terms'
}
}
}
})
)
])
})
)
])
self.assertEqual(200, response.status_code)

0 comments on commit 1f819ed

Please sign in to comment.