From 4abdd24b44efdcc192038fd6876d3f10c02ceca6 Mon Sep 17 00:00:00 2001 From: Dino Chiesa Date: Wed, 28 Aug 2024 16:05:13 -0700 Subject: [PATCH] chore: add tests for PD004 plugin (#461) * chore: add tests for PD004 plugin --- lib/package/plugins/PD004-proxyNameAttr.js | 75 +++++++----- .../apiproxy/flightdata.xml | 12 ++ .../policies/AM-Clean-Response-Headers.xml | 20 ++++ .../AM-Inject-Proxy-Revision-Header.xml | 7 ++ .../apiproxy/policies/AM-PreparedQuery-1.xml | 6 + .../apiproxy/policies/AM-PreparedQuery-2.xml | 6 + .../apiproxy/policies/AM-PreparedQuery-3.xml | 6 + .../apiproxy/policies/AM-PreparedQuery-4.xml | 6 + .../apiproxy/policies/AM-Query.xml | 15 +++ .../apiproxy/policies/EV-PathParams-4.xml | 7 ++ .../apiproxy/policies/JS-Convert-Response.xml | 21 ++++ .../apiproxy/policies/RF-Unknown-Request.xml | 16 +++ .../apiproxy/proxies/endpoint1.xml | 111 ++++++++++++++++++ .../apiproxy/proxies/endpoint2.xml | 111 ++++++++++++++++++ .../apiproxy/targets/target-1.xml | 42 +++++++ test/specs/PD004-proxyendpoint-name.js | 89 ++++++++++++++ 16 files changed, 519 insertions(+), 31 deletions(-) create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/flightdata.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Query.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/EV-PathParams-4.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/JS-Convert-Response.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint1.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint2.xml create mode 100644 test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/targets/target-1.xml create mode 100644 test/specs/PD004-proxyendpoint-name.js diff --git a/lib/package/plugins/PD004-proxyNameAttr.js b/lib/package/plugins/PD004-proxyNameAttr.js index 1ad7509c..27c9d5d7 100644 --- a/lib/package/plugins/PD004-proxyNameAttr.js +++ b/lib/package/plugins/PD004-proxyNameAttr.js @@ -1,9 +1,12 @@ /* - Copyright 2019-2020 Google LLC + Copyright 2019-2020,2024 Google LLC + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + https://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -11,9 +14,9 @@ limitations under the License. */ -const path = require('path'), - ruleId = require("../myUtil.js").getRuleId(), - debug = require("debug")("apigeelint:" + ruleId); +const path = require("path"), + ruleId = require("../myUtil.js").getRuleId(), + debug = require("debug")("apigeelint:" + ruleId); const plugin = { ruleId, @@ -25,33 +28,43 @@ const plugin = { enabled: true }; -const onProxyEndpoint = function(endpoint, cb) { - let shortFilename = path.basename(endpoint.getFileName()), - basename = shortFilename.split(".xml")[0], - nameFromAttribute = endpoint.getName(), - flagged = false; - - try { - debug(`onProxyEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})`); - if (basename !== nameFromAttribute) { - let nameAttr = endpoint.select('//@name'); - endpoint.addMessage({ - plugin, - line: nameAttr[0].lineNumber, - column: nameAttr[0].columnNumber, - message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).` - }); - flagged = true; - } - if (typeof(cb) == 'function') { - cb(null, flagged); - } - } - catch (exc1) { - console.error('exception: ' + exc1); - debug(`onProxyEndpoint exc(${exc1})`); - } - }; +const onProxyEndpoint = function (endpoint, cb) { + const shortFilename = path.basename(endpoint.getFileName()), + basename = shortFilename.split(".xml")[0], + nameFromAttribute = endpoint.getName(); + let flagged = false; + + try { + debug( + `onProxyEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})` + ); + if (!nameFromAttribute) { + const rootElement = endpoint.getElement(); + endpoint.addMessage({ + plugin, + line: rootElement.lineNumber, + column: rootElement.columnNumber, + message: `ProxyEndpoint has no name attribute.` + }); + flagged = true; + } else if (basename !== nameFromAttribute) { + const nameAttr = endpoint.select("//@name"); + endpoint.addMessage({ + plugin, + line: nameAttr[0].lineNumber, + column: nameAttr[0].columnNumber, + message: `File basename (${basename}) does not match endpoint name (${nameFromAttribute}).` + }); + flagged = true; + } + if (typeof cb == "function") { + cb(null, flagged); + } + } catch (exc1) { + console.error("exception: " + exc1); + debug(`onProxyEndpoint exc(${exc1})`); + } +}; module.exports = { plugin, diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/flightdata.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/flightdata.xml new file mode 100644 index 00000000..c7125d72 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/flightdata.xml @@ -0,0 +1,12 @@ + + + 1491498008040 + + flightdata + 1491517153495 + + + + + false + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml new file mode 100644 index 00000000..f5a8223b --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml @@ -0,0 +1,20 @@ + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+ + + false + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml new file mode 100644 index 00000000..02ed60b3 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml @@ -0,0 +1,7 @@ + + + +
{apiproxy.name} r{apiproxy.revision}
+
+
+
diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml new file mode 100644 index 00000000..16579101 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml @@ -0,0 +1,6 @@ + + + bq_query + + + \ No newline at end of file diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml new file mode 100644 index 00000000..9a9dcd45 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml @@ -0,0 +1,6 @@ + + + bq_query + + + \ No newline at end of file diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml new file mode 100644 index 00000000..d87001f6 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml @@ -0,0 +1,6 @@ + + + bq_query + + + \ No newline at end of file diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml new file mode 100644 index 00000000..4e2ef9aa --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml @@ -0,0 +1,6 @@ + + + bq_query + + + \ No newline at end of file diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Query.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Query.xml new file mode 100644 index 00000000..f297a84e --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/AM-Query.xml @@ -0,0 +1,15 @@ + + request + + target.copy.pathsuffix + false + + + +
application/json
+
+ {"query": "{bq_query}"} + + POST +
+
diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/EV-PathParams-4.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/EV-PathParams-4.xml new file mode 100644 index 00000000..391d31f6 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/EV-PathParams-4.xml @@ -0,0 +1,7 @@ + + request + + /airports/{param1}/counts/{param2} + + true + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/JS-Convert-Response.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/JS-Convert-Response.xml new file mode 100644 index 00000000..0e2ee29c --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/JS-Convert-Response.xml @@ -0,0 +1,21 @@ + + 0) { + var fieldnames = c.schema.fields.map(function(field){ + return field.name; + }); + var rows = c.rows.map(function(row) { + var values = row.f.map(function(f){ return f.v; }); + var result = {}; + fieldnames.forEach(function(name, ix){ result[name] = values[ix]; }); + return result; + }); + c.rows = rows; +} +delete c.kind; +delete c.jobReference; +context.setVariable('response.content', JSON.stringify(c,null,2)+'\n'); +]]> + + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml new file mode 100644 index 00000000..9f5f7fe1 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml @@ -0,0 +1,16 @@ + + true + + + { + "error" : { + "code" : 404.01, + "message" : "that request was unknown; try a different request." + } +} + + 404 + Not Found + + + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint1.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint1.xml new file mode 100644 index 00000000..0b3e2054 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint1.xml @@ -0,0 +1,111 @@ + + + + /flightdata + + secure + + + + + + AM-Inject-Proxy-Revision-Header + + true + + + + + + + + + + + + + JS-Convert-Response + + + AM-Inject-Proxy-Revision-Header + + + + + + + + + + + + + + + + AM-PreparedQuery-1 + + + + + proxy.pathsuffix MatchesPath "/airlines32" and request.verb = "GET" + + + + + + + AM-PreparedQuery-2 + + + + + proxy.pathsuffix MatchesPath "/airlines100" and request.verb = "GET" + + + + + + + AM-PreparedQuery-3 + + + + + proxy.pathsuffix MatchesPath "/airlines500" and request.verb = "GET" + + + + + + + EV-PathParams-4 + + + + AM-PreparedQuery-4 + + + + + proxy.pathsuffix MatchesPath "/airports/*/counts/*" and request.verb = "GET" + + + + + + + RF-Unknown-Request + + + + + + + + + + target-1 + + + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint2.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint2.xml new file mode 100644 index 00000000..3e285232 --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/proxies/endpoint2.xml @@ -0,0 +1,111 @@ + + + + /flightdata + + secure + + + + + + AM-Inject-Proxy-Revision-Header + + true + + + + + + + + + + + + + JS-Convert-Response + + + AM-Inject-Proxy-Revision-Header + + + + + + + + + + + + + + + + AM-PreparedQuery-1 + + + + + proxy.pathsuffix MatchesPath "/airlines32" and request.verb = "GET" + + + + + + + AM-PreparedQuery-2 + + + + + proxy.pathsuffix MatchesPath "/airlines100" and request.verb = "GET" + + + + + + + AM-PreparedQuery-3 + + + + + proxy.pathsuffix MatchesPath "/airlines500" and request.verb = "GET" + + + + + + + EV-PathParams-4 + + + + AM-PreparedQuery-4 + + + + + proxy.pathsuffix MatchesPath "/airports/*/counts/*" and request.verb = "GET" + + + + + + + RF-Unknown-Request + + + + + + + + + + target-1 + + + diff --git a/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/targets/target-1.xml b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/targets/target-1.xml new file mode 100644 index 00000000..46e1b00f --- /dev/null +++ b/test/fixtures/resources/PD004-ProxyEndpoint-name/apiproxy/targets/target-1.xml @@ -0,0 +1,42 @@ + + + + + AM-Query + + + + + AM-Clean-Response-Headers + + + + + + + + + + + + + + + + + + + https://www.googleapis.com/auth/cloud-platform + + + + + + true + false + + + + https://bigquery.googleapis.com/bigquery/v2/projects/infinite-chain-292422/queries + + diff --git a/test/specs/PD004-proxyendpoint-name.js b/test/specs/PD004-proxyendpoint-name.js new file mode 100644 index 00000000..d711ce91 --- /dev/null +++ b/test/specs/PD004-proxyendpoint-name.js @@ -0,0 +1,89 @@ +/* + Copyright 2019-2022,2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/* global describe, it */ + +const ruleId = "PD004", + assert = require("assert"), + path = require("path"), + util = require("util"), + debug = require("debug")(`apigeelint:${ruleId}`), + bl = require("../../lib/package/bundleLinter.js"); + +describe(`${ruleId} - proxyEndpoint name attr`, () => { + it("should generate the expected errors", () => { + const configuration = { + debug: true, + source: { + type: "filesystem", + path: path.resolve( + __dirname, + "../fixtures/resources/PD004-ProxyEndpoint-name/apiproxy" + ), + bundleType: "apiproxy" + }, + profile: "apigeex", + excluded: {}, + setExitCode: false, + output: () => {} // suppress output + }; + + bl.lint(configuration, (bundle) => { + const items = bundle.getReport(); + assert.ok(items); + assert.ok(items.length); + const actualIssues = items.filter( + (item) => + item.messages && + item.messages.length && + item.messages.find((m) => m.ruleId == ruleId) + ); + + assert.equal(actualIssues.length, 2); + debug(util.format(actualIssues)); + + const ep1Items = actualIssues.filter((e) => + e.filePath.endsWith("endpoint1.xml") + ); + assert.ok(ep1Items); + assert.equal(ep1Items.length, 1); + debug(util.format(ep1Items[0].messages)); + let pd004Messages = ep1Items[0].messages.filter( + (m) => m.ruleId == ruleId + ); + assert.equal(pd004Messages.length, 1); + assert.ok(pd004Messages[0].message); + assert.equal( + pd004Messages[0].message, + "File basename (endpoint1) does not match endpoint name (default)." + ); + + const ep2Items = actualIssues.filter((e) => + e.filePath.endsWith("endpoint2.xml") + ); + assert.ok(ep2Items); + assert.equal(ep2Items.length, 1); + debug(util.format(ep2Items[0].messages)); + pd004Messages = ep2Items[0].messages.filter((m) => m.ruleId == ruleId); + assert.equal(pd004Messages.length, 1); + assert.ok(pd004Messages[0].message); + assert.equal( + pd004Messages[0].message, + "ProxyEndpoint has no name attribute." + ); + }); + }); +});