|
| 1 | +"""Regression tests for x-correlator documentation rules S-039 and S-040.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import os |
| 7 | +import subprocess |
| 8 | +import tempfile |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | + |
| 12 | +_REPO_ROOT = Path(__file__).resolve().parent.parent.parent |
| 13 | +_RULESET = _REPO_ROOT / "linting" / "config" / ".spectral-r4.yaml" |
| 14 | +_NODE_MODULES = _REPO_ROOT / "validation" / "node_modules" |
| 15 | +_REQUEST_RULE = "camara-x-correlator-request-parameter" |
| 16 | +_RESPONSE_RULE = "camara-x-correlator-response-header" |
| 17 | + |
| 18 | + |
| 19 | +def _run_spectral(files: dict[str, str], entrypoint: str = "api.yaml") -> list[dict]: |
| 20 | + with tempfile.TemporaryDirectory() as directory: |
| 21 | + root = Path(directory) |
| 22 | + for name, content in files.items(): |
| 23 | + target = root / name |
| 24 | + target.parent.mkdir(parents=True, exist_ok=True) |
| 25 | + target.write_text(content, encoding="utf-8") |
| 26 | + |
| 27 | + env = { |
| 28 | + "PATH": os.environ.get("PATH", ""), |
| 29 | + "NODE_PATH": str(_NODE_MODULES), |
| 30 | + "HOME": os.environ.get("HOME", ""), |
| 31 | + } |
| 32 | + result = subprocess.run( |
| 33 | + [ |
| 34 | + "node", |
| 35 | + str(_NODE_MODULES / ".bin" / "spectral"), |
| 36 | + "lint", |
| 37 | + str(root / entrypoint), |
| 38 | + "-r", |
| 39 | + str(_RULESET), |
| 40 | + "--format", |
| 41 | + "json", |
| 42 | + ], |
| 43 | + capture_output=True, |
| 44 | + text=True, |
| 45 | + env=env, |
| 46 | + timeout=30, |
| 47 | + ) |
| 48 | + assert result.stdout.strip(), result.stderr |
| 49 | + return json.loads(result.stdout) |
| 50 | + |
| 51 | + |
| 52 | +def _for_rule(findings: list[dict], rule: str) -> list[dict]: |
| 53 | + return [finding for finding in findings if finding["code"] == rule] |
| 54 | + |
| 55 | + |
| 56 | +_SPEC = """\ |
| 57 | +openapi: 3.0.3 |
| 58 | +info: |
| 59 | + title: Correlator Test |
| 60 | + version: wip |
| 61 | +paths: |
| 62 | + /widgets: |
| 63 | + get: |
| 64 | + operationId: getWidgets |
| 65 | + parameters: |
| 66 | + - name: x-correlator |
| 67 | + in: header |
| 68 | + schema: |
| 69 | + type: string |
| 70 | + responses: |
| 71 | + "200": |
| 72 | + description: OK |
| 73 | + headers: |
| 74 | + x-correlator: |
| 75 | + schema: |
| 76 | + type: string |
| 77 | +""" |
| 78 | + |
| 79 | + |
| 80 | +def test_documented_request_and_response_pass(): |
| 81 | + findings = _run_spectral({"api.yaml": _SPEC}) |
| 82 | + assert _for_rule(findings, _REQUEST_RULE) == [] |
| 83 | + assert _for_rule(findings, _RESPONSE_RULE) == [] |
| 84 | + |
| 85 | + |
| 86 | +def test_path_level_request_parameter_applies_to_operation(): |
| 87 | + spec = _SPEC.replace( |
| 88 | + " get:\n operationId: getWidgets\n parameters:\n", |
| 89 | + " parameters:\n - name: x-correlator\n in: header\n schema:\n type: string\n get:\n operationId: getWidgets\n parameters:\n", |
| 90 | + ).replace( |
| 91 | + " - name: x-correlator\n in: header\n schema:\n type: string\n", |
| 92 | + " - name: limit\n in: query\n schema:\n type: integer\n", |
| 93 | + ) |
| 94 | + findings = _run_spectral({"api.yaml": spec}) |
| 95 | + assert _for_rule(findings, _REQUEST_RULE) == [] |
| 96 | + |
| 97 | + |
| 98 | +def test_missing_request_parameter_reports_operation_path(): |
| 99 | + spec = _SPEC.replace(" - name: x-correlator", " - name: traceparent") |
| 100 | + findings = _for_rule(_run_spectral({"api.yaml": spec}), _REQUEST_RULE) |
| 101 | + assert len(findings) == 1 |
| 102 | + assert findings[0]["path"] == ["paths", "/widgets", "get", "parameters"] |
| 103 | + |
| 104 | + |
| 105 | +def test_each_response_without_header_is_reported(): |
| 106 | + spec = _SPEC.replace( |
| 107 | + " headers:\n x-correlator:\n schema:\n type: string\n", |
| 108 | + "", |
| 109 | + ) + """\ |
| 110 | + "400": |
| 111 | + description: Bad request |
| 112 | + headers: {} |
| 113 | +""" |
| 114 | + findings = _for_rule(_run_spectral({"api.yaml": spec}), _RESPONSE_RULE) |
| 115 | + assert len(findings) == 2 |
| 116 | + assert any("200" in finding["path"] for finding in findings) |
| 117 | + assert any("400" in finding["path"] for finding in findings) |
| 118 | + |
| 119 | + |
| 120 | +def test_component_references_are_resolved(): |
| 121 | + spec = """\ |
| 122 | +openapi: 3.0.3 |
| 123 | +info: |
| 124 | + title: Correlator Test |
| 125 | + version: wip |
| 126 | +paths: |
| 127 | + /widgets: |
| 128 | + get: |
| 129 | + operationId: getWidgets |
| 130 | + parameters: |
| 131 | + - $ref: "#/components/parameters/XCorrelator" |
| 132 | + responses: |
| 133 | + "200": |
| 134 | + $ref: "#/components/responses/CorrelatedResponse" |
| 135 | +components: |
| 136 | + parameters: |
| 137 | + XCorrelator: |
| 138 | + name: x-correlator |
| 139 | + in: header |
| 140 | + schema: |
| 141 | + type: string |
| 142 | + responses: |
| 143 | + CorrelatedResponse: |
| 144 | + description: OK |
| 145 | + headers: |
| 146 | + x-correlator: |
| 147 | + schema: |
| 148 | + type: string |
| 149 | +""" |
| 150 | + findings = _run_spectral({"api.yaml": spec}) |
| 151 | + assert _for_rule(findings, _REQUEST_RULE) == [] |
| 152 | + assert _for_rule(findings, _RESPONSE_RULE) == [] |
| 153 | + |
| 154 | + |
| 155 | +def test_external_references_are_resolved(): |
| 156 | + spec = """\ |
| 157 | +openapi: 3.0.3 |
| 158 | +info: |
| 159 | + title: Correlator Test |
| 160 | + version: wip |
| 161 | +paths: |
| 162 | + /widgets: |
| 163 | + get: |
| 164 | + operationId: getWidgets |
| 165 | + parameters: |
| 166 | + - $ref: "./common.yaml#/components/parameters/XCorrelator" |
| 167 | + responses: |
| 168 | + "200": |
| 169 | + $ref: "./common.yaml#/components/responses/CorrelatedResponse" |
| 170 | +""" |
| 171 | + common = """\ |
| 172 | +openapi: 3.0.3 |
| 173 | +info: |
| 174 | + title: Common |
| 175 | + version: wip |
| 176 | +paths: {} |
| 177 | +components: |
| 178 | + parameters: |
| 179 | + XCorrelator: |
| 180 | + name: x-correlator |
| 181 | + in: header |
| 182 | + schema: |
| 183 | + type: string |
| 184 | + responses: |
| 185 | + CorrelatedResponse: |
| 186 | + description: OK |
| 187 | + headers: |
| 188 | + x-correlator: |
| 189 | + schema: |
| 190 | + type: string |
| 191 | +""" |
| 192 | + findings = _run_spectral({"api.yaml": spec, "common.yaml": common}) |
| 193 | + assert _for_rule(findings, _REQUEST_RULE) == [] |
| 194 | + assert _for_rule(findings, _RESPONSE_RULE) == [] |
| 195 | + |
| 196 | + |
| 197 | +def test_callback_operation_is_in_scope(): |
| 198 | + spec = _SPEC.replace( |
| 199 | + " responses:\n", |
| 200 | + " callbacks:\n" |
| 201 | + " onEvent:\n" |
| 202 | + " '{$request.body#/sink}':\n" |
| 203 | + " post:\n" |
| 204 | + " operationId: onEvent\n" |
| 205 | + " responses:\n" |
| 206 | + " '204':\n" |
| 207 | + " description: Accepted\n" |
| 208 | + " responses:\n", |
| 209 | + 1, |
| 210 | + ) |
| 211 | + findings = _run_spectral({"api.yaml": spec}) |
| 212 | + request_findings = _for_rule(findings, _REQUEST_RULE) |
| 213 | + response_findings = _for_rule(findings, _RESPONSE_RULE) |
| 214 | + assert len(request_findings) == 1 |
| 215 | + assert "callbacks" in request_findings[0]["path"] |
| 216 | + assert len(response_findings) == 1 |
| 217 | + assert "callbacks" in response_findings[0]["path"] |
0 commit comments