Skip to content

Commit

Permalink
Modify TestServiceAppLogging to simplify test case additions
Browse files Browse the repository at this point in the history
  • Loading branch information
achave11-ucsc committed Oct 18, 2024
1 parent fb3105e commit cd97e59
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions test/service/test_app_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import requests

from app_test_case import (
LocalAppTestCase,
)
from azul.chalice import (
log,
)
from azul.json import (
json_head,
)
from azul.logging import (
configure_test_logging,
)
Expand All @@ -21,14 +21,27 @@
from indexer import (
DCP1CannedBundleTestCase,
)
from service import (
WebServiceTestCase,
)


# noinspection PyPep8Naming
def setUpModule():
configure_test_logging()


class TestServiceAppLogging(DCP1CannedBundleTestCase, LocalAppTestCase):
class TestServiceAppLogging(DCP1CannedBundleTestCase, WebServiceTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
cls._setup_indices()

@classmethod
def tearDownClass(cls):
cls._teardown_indices()
super().tearDownClass()

@classmethod
def lambda_name(cls) -> str:
Expand All @@ -38,10 +51,10 @@ def test_request_logs(self):
for level in INFO, DEBUG:
for authenticated in False, True:
with self.subTest(level=level, authenticated=authenticated):
url = self.base_url.set(path='/health/basic')
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)
response = requests.get(str(url), headers=headers)
logs = [(r.levelno, r.getMessage()) for r in logs.records]
headers = {
'host': url.netloc,
Expand All @@ -54,8 +67,9 @@ def test_request_logs(self):
self.assertEqual(logs, [
(
INFO,
f"Received GET request for '/health/basic', "
f"with {json.dumps({'query': None, 'headers': headers})}."),
f"Received GET request for '/index/projects', "
f"with {json.dumps({'query': None, 'headers': headers})}."
),
(
INFO,
"Authenticated request as OAuth2(access_token='foo_token')"
Expand All @@ -77,6 +91,6 @@ def test_request_logs(self):
'"X-XSS-Protection": "1; mode=block", '
'"Cache-Control": "no-store"}. '
'See next line for the first 1024 characters of the body.\n'
'{"up": true}'
+ json_head(1024, response.json()),
)
])

0 comments on commit cd97e59

Please sign in to comment.