graphql-engine/server/tests-py/test_jwk.py
Samir Talwar 0ea8bd8df8 server/tests-py: Parallelize test_logging.py.
This adds the ability to capture logs to the HGE fixture, and uses this in test_logging.py to analyze the logs, instead of relying on a shell script redirecting the logs to a file.

We then inject the logs into the tests and parse the JSON. Because we're no longer reading a file, we need to do this in a separate thread, as we'll block on reading rather than the stream ending. (Once HGE stops, the stream will be closed.)

Some of the tests require a JWK server, so this has been extracted from test_jwk.py.

[NDAT-540]: https://hasurahq.atlassian.net/browse/NDAT-540?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8772
GitOrigin-RevId: 9413e714f1c42b8a0991d0d30c4358209fd30c0c
2023-04-17 13:09:34 +00:00

64 lines
3.2 KiB
Python

import pytest
import requests
from time import perf_counter, sleep
pytestmark = [
pytest.mark.admin_secret
]
def wait_until_request_count_reaches(num_requests: int, state_key: str, timeout_secs: int, jwk_server_url: str) -> float:
start_time = perf_counter()
requests.post(jwk_server_url + '/reset-state')
request_count = 0
time_elapsed = 0
while request_count < num_requests:
time_elapsed = perf_counter() - start_time
if time_elapsed > timeout_secs:
raise Exception(f'Waited {time_elapsed} seconds for {state_key} JWK requests to reach {num_requests}. Only received {request_count}.')
sleep(0.2)
state = requests.get(jwk_server_url + '/state').json()
request_count = state[state_key]
return time_elapsed
@pytest.mark.jwk_path('/jwk-cache-control?max-age=3')
def test_cache_control_header_max_age(jwk_server_url: str):
# The test uses max-age=3, so we are expecting one request
time_elapsed = wait_until_request_count_reaches(num_requests=1, state_key='cache-control', timeout_secs=6, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")
@pytest.mark.jwk_path('/jwk-cache-control?max-age=3&must-revalidate=true')
def test_cache_control_header_max_age_must_revalidate(jwk_server_url: str):
# The test uses max-age=3, so we are expecting one request
time_elapsed = wait_until_request_count_reaches(num_requests=1, state_key='cache-control', timeout_secs=6, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")
@pytest.mark.jwk_path('/jwk-cache-control?must-revalidate=true')
def test_cache_control_header_must_revalidate(jwk_server_url: str):
# HGE should refresh the JWK once a second, so we are expecting three requests in at least two seconds
time_elapsed = wait_until_request_count_reaches(num_requests=3, state_key='cache-control', timeout_secs=10, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")
assert(time_elapsed >= 2)
@pytest.mark.jwk_path('/jwk-cache-control?no-cache=true&public=true')
def test_cache_control_header_no_cache_public(jwk_server_url: str):
# HGE should refresh the JWK once a second, so we are expecting three requests in at least two seconds
time_elapsed = wait_until_request_count_reaches(num_requests=3, state_key='cache-control', timeout_secs=10, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")
assert(time_elapsed >= 2)
@pytest.mark.jwk_path('/jwk-cache-control?no-store=true&max-age=3')
def test_cache_control_header_no_store_max_age(jwk_server_url: str):
# HGE should refresh the JWK once a second, so we are expecting three requests in at least two seconds
time_elapsed = wait_until_request_count_reaches(num_requests=3, state_key='cache-control', timeout_secs=10, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")
assert(time_elapsed >= 2)
@pytest.mark.jwk_path('/jwk-expires?seconds=3')
def test_expires_header(jwk_server_url: str):
# The test uses a three second jwk expiry so we are expecting one request
time_elapsed = wait_until_request_count_reaches(num_requests=1, state_key='expires', timeout_secs=6, jwk_server_url=jwk_server_url)
print(f"time_elapsed: {time_elapsed}")