diff --git a/server/tests-py/context.py b/server/tests-py/context.py index 60ad026125066..225ef9e643f20 100644 --- a/server/tests-py/context.py +++ b/server/tests-py/context.py @@ -3,8 +3,6 @@ from http import HTTPStatus from urllib.parse import urlparse from ruamel.yaml.comments import CommentedMap as OrderedDict # to avoid '!!omap' in yaml -# from collections import OrderedDict -# import socketserver import threading import http.server import json @@ -12,7 +10,6 @@ import socket import subprocess import time -import uuid import string import random import os @@ -292,7 +289,8 @@ def anyq(self, u, q, h): ) # NOTE: make sure we preserve key ordering so we can test the ordering # properties in the graphql spec properly - return resp.status_code, resp.json(object_pairs_hook=OrderedDict) + # Returning response headers to get the request id from response + return resp.status_code, resp.json(object_pairs_hook=OrderedDict), resp.headers def sql(self, q): conn = self.engine.connect() diff --git a/server/tests-py/test_subscriptions.py b/server/tests-py/test_subscriptions.py index e84d1eb2724e4..6d61b6ee77dff 100644 --- a/server/tests-py/test_subscriptions.py +++ b/server/tests-py/test_subscriptions.py @@ -275,7 +275,7 @@ def get_parameterized_sql(self, hge_ctx, query, variables): headers['X-Hasura-Admin-Secret'] = admin_secret request = { 'query': { 'query': query, 'variables': variables }, 'user': {} } - status_code, response = hge_ctx.anyq('/v1/graphql/explain', request, headers) + status_code, response, _ = hge_ctx.anyq('/v1/graphql/explain', request, headers) assert status_code == 200, (request, status_code, response) sql = response['sql'] diff --git a/server/tests-py/test_v1_queries.py b/server/tests-py/test_v1_queries.py index 73808f440915d..81fdeff6c7f92 100644 --- a/server/tests-py/test_v1_queries.py +++ b/server/tests-py/test_v1_queries.py @@ -509,13 +509,13 @@ def test_export_replace(self, hge_ctx): headers = {} if hge_ctx.hge_key is not None: headers['X-Hasura-Admin-Secret'] = hge_ctx.hge_key - export_code, export_resp = hge_ctx.anyq(url, export_query, headers) + export_code, export_resp, _ = hge_ctx.anyq(url, export_query, headers) assert export_code == 200, export_resp replace_query = { 'type': 'replace_metadata', 'args': export_resp } - replace_code, replace_resp = hge_ctx.anyq(url, replace_query, headers) + replace_code, replace_resp, _ = hge_ctx.anyq(url, replace_query, headers) assert replace_code == 200, replace_resp diff --git a/server/tests-py/validate.py b/server/tests-py/validate.py index 66b2366bd6aa9..4f0ce053c345f 100644 --- a/server/tests-py/validate.py +++ b/server/tests-py/validate.py @@ -79,26 +79,28 @@ def test_forbidden_when_admin_secret_reqd(hge_ctx, conf): headers = conf['headers'] # Test without admin secret - code, resp = hge_ctx.anyq(conf['url'], conf['query'], headers) + code, resp, resp_hdrs = hge_ctx.anyq(conf['url'], conf['query'], headers) #assert code in [401,404], "\n" + yaml.dump({ assert code in status, "\n" + yaml.dump({ "expected": "Should be access denied as admin secret is not provided", "actual": { "code": code, "response": resp - } + }, + 'request id': resp_hdrs.get('x-request-id') }) # Test with random admin secret headers['X-Hasura-Admin-Secret'] = base64.b64encode(os.urandom(30)) - code, resp = hge_ctx.anyq(conf['url'], conf['query'], headers) + code, resp, resp_hdrs = hge_ctx.anyq(conf['url'], conf['query'], headers) #assert code in [401,404], "\n" + yaml.dump({ assert code in status, "\n" + yaml.dump({ "expected": "Should be access denied as an incorrect admin secret is provided", "actual": { "code": code, "response": resp - } + }, + 'request id': resp_hdrs.get('x-request-id') }) @@ -112,14 +114,15 @@ def test_forbidden_webhook(hge_ctx, conf): status = [401, 404] h = {'Authorization': 'Bearer ' + base64.b64encode(base64.b64encode(os.urandom(30))).decode('utf-8')} - code, resp = hge_ctx.anyq(conf['url'], conf['query'], h) + code, resp, resp_hdrs = hge_ctx.anyq(conf['url'], conf['query'], h) #assert code in [401,404], "\n" + yaml.dump({ assert code in status, "\n" + yaml.dump({ "expected": "Should be access denied as it is denied from webhook", "actual": { "code": code, "response": resp - } + }, + 'request id': resp_hdrs.get('x-request-id') }) @@ -232,12 +235,12 @@ def validate_gql_ws_q(hge_ctx, endpoint, query, headers, exp_http_response, retr def validate_http_anyq(hge_ctx, url, query, headers, exp_code, exp_response): - code, resp = hge_ctx.anyq(url, query, headers) + code, resp, resp_hdrs = hge_ctx.anyq(url, query, headers) print(headers) assert code == exp_code, resp print('http resp: ', resp) if exp_response: - return assert_graphql_resp_expected(resp, exp_response, query) + return assert_graphql_resp_expected(resp, exp_response, query, resp_hdrs) else: return resp, True @@ -247,7 +250,7 @@ def validate_http_anyq(hge_ctx, url, query, headers, exp_code, exp_response): # # Returns 'resp' and a bool indicating whether the test passed or not (this # will always be True unless we are `--accepting`) -def assert_graphql_resp_expected(resp_orig, exp_response_orig, query): +def assert_graphql_resp_expected(resp_orig, exp_response_orig, query, resp_hdrs={}): # Prepare actual and respected responses so comparison takes into # consideration only the ordering that we care about: resp = collapse_order_not_selset(resp_orig, query) @@ -260,16 +263,19 @@ def assert_graphql_resp_expected(resp_orig, exp_response_orig, query): yml = yaml.YAML() # https://yaml.readthedocs.io/en/latest/example.html#output-of-dump-as-a-string : dump_str = StringIO() - yml.dump({ + test_output = { # Keep strict received order when displaying errors: 'response': resp_orig, 'expected': exp_response_orig, - 'diff': - (lambda diff: + 'diff': + (lambda diff: "(results differ only in their order of keys)" if diff == {} else diff) (stringify_keys(jsondiff.diff(exp_response, resp))) - }, stream=dump_str) - assert matched, dump_str.getvalue() + } + if 'x-request-id' in resp_hdrs: + test_output['request id'] = resp_hdrs['x-request-id'] + yml.dump(test_output, stream=dump_str) + assert matched, '\n' + dump_str.getvalue() return resp, matched # matched always True unless --accept # This really sucks; newer ruamel made __eq__ ignore ordering: