mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-16 01:44:03 +03:00
c2cb07f7e8
We use a helper service to start a webhook-based authentication service for some tests. This moves the initialization of the service out of _test-server.sh_ and into the Python test harness, as a fixture. In order to do this, I had to make a few changes. The main deviation is that we no longer run _all_ tests against an HGE with this authentication service, just a few (those in _test_webhook.py_). Because this reduced coverage, I have added some more tests there, which actually cover some areas not exacerbated elsewhere (mainly trying to use webhook credentials to talk to an admin-only endpoint). The webhook service can run both with and without TLS, and decide whether it's necessary to skip one of these based on the arguments passed and how HGE is started, according to the following logic: * If a TLS CA certificate is passed in, it will run with TLS, otherwise it will skip it. * If HGE was started externally and a TLS certificate is provided, it will skip running without TLS, as it will assume that HGE was configured to talk to a webhook over HTTPS. * Some tests should only be run with TLS; this is marked with a `tls_webhook_server` marker. * Some tests should only be run _without_ TLS; this is marked with a `no_tls_webhook_server` marker. The actual parameterization of the webhook service configuration is done through test subclasses, because normal pytest parameterization doesn't work with the `hge_fixture_env` hack that we use. Because `hge_fixture_env` is not a sanctioned way of conveying data between fixtures (and, unfortunately, there isn't a sanctioned way of doing this when the fixtures in question may not know about each other directly), parameterizing the `webhook_server` fixture doesn't actually parameterize `hge_server` properly. Subclassing forces this to work correctly. The certificate generation is moved to a Python fixture, so that we don't have to revoke the CA certificate for _test_webhook_insecure.py_; we can just generate a bogus certificate instead. The CA certificate is still generated in the _test-server.sh_ script, as it needs to be installed into the OS certificate store. Interestingly, the CA certificate installation wasn't actually working, because the certificates were written to the wrong location. This didn't cause any failures, as we weren't actually testing this behavior. This is now fixed with the other changes. PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6363 GitOrigin-RevId: 0f277d374daa64f657257ed2a4c2057c74b911db
54 lines
2.2 KiB
Python
54 lines
2.2 KiB
Python
import json
|
|
|
|
class TestConfigAPI:
|
|
def test_config_api_user_role_error(self, hge_ctx, hge_key):
|
|
headers = {'x-hasura-role': 'user'}
|
|
if hge_key is not None:
|
|
headers['x-hasura-admin-secret'] = hge_key
|
|
|
|
resp = hge_ctx.http.get(hge_ctx.hge_url + '/v1alpha1/config', headers=headers)
|
|
|
|
assert resp.status_code == 400, resp
|
|
|
|
def test_config_api(self, hge_ctx, hge_key):
|
|
jwt_conf = hge_ctx.hge_jwt_conf
|
|
if jwt_conf is not None:
|
|
jwt_conf_dict = json.loads(hge_ctx.hge_jwt_conf)
|
|
else:
|
|
jwt_conf_dict = None
|
|
|
|
headers = {'x-hasura-role': 'admin'}
|
|
if hge_key is not None:
|
|
headers['x-hasura-admin-secret'] = hge_key
|
|
|
|
resp = hge_ctx.http.get(hge_ctx.hge_url + '/v1alpha1/config', headers=headers)
|
|
|
|
assert resp.status_code == 200, resp
|
|
|
|
body = resp.json()
|
|
assert body['is_admin_secret_set'] == (hge_key is not None)
|
|
assert body['is_auth_hook_set'] == (hge_ctx.webhook is not None)
|
|
assert body['is_jwt_set'] == (jwt_conf is not None)
|
|
|
|
if jwt_conf_dict:
|
|
claims_format = "json"
|
|
if 'claims_namespace_path' in jwt_conf_dict:
|
|
assert body['jwt']['claims_namespace_path'] == jwt_conf_dict['claims_namespace_path']
|
|
assert body['jwt']['claims_format'] == claims_format
|
|
else:
|
|
claims_namespace = "https://hasura.io/jwt/claims"
|
|
if 'claims_namespace' in jwt_conf_dict:
|
|
claims_namespace = jwt_conf_dict['claims_namespace']
|
|
if 'claims_format' in jwt_conf_dict:
|
|
claims_format = jwt_conf_dict['claims_format']
|
|
assert body['jwt']['claims_namespace'] == claims_namespace
|
|
assert body['jwt']['claims_format'] == claims_format
|
|
else:
|
|
assert body['jwt'] == []
|
|
|
|
# test if the request fails without auth headers if admin secret is set
|
|
if hge_key is not None:
|
|
resp = hge_ctx.http.get(hge_ctx.hge_url + '/v1alpha1/config')
|
|
body = resp.json()
|
|
assert ((resp.status_code == 401) or (resp.status_code == 400))
|