diff --git a/lib/package/plugins/TD003-targetNameAttr.js b/lib/package/plugins/TD003-targetNameAttr.js index f12f0331..73624bc6 100644 --- a/lib/package/plugins/TD003-targetNameAttr.js +++ b/lib/package/plugins/TD003-targetNameAttr.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. @@ -18,41 +21,51 @@ const plugin = { name: "TargetEndpoint Name", message: "Check TargetEndpoint name attribute against file name.", fatal: false, - severity: 2, // 1 = warn, 2 = error + severity: 2, // 1 = warn, 2 = error nodeType: "Endpoint", enabled: true }; -const path = require('path'), - debug = require("debug")("apigeelint:" + ruleId); - -const onTargetEndpoint = function(endpoint, cb) { - let shortFilename = path.basename(endpoint.getFileName()), - basename = shortFilename.split(".xml")[0], - nameFromAttribute = endpoint.getName(), - flagged = false; - - try { - debug(`onTargetEndpoint 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(`onTargetEndpoint exc(${exc1})`); - } - }; +const path = require("path"), + debug = require("debug")("apigeelint:" + ruleId); + +const onTargetEndpoint = function (endpoint, cb) { + const shortFilename = path.basename(endpoint.getFileName()), + basename = shortFilename.split(".xml")[0], + nameFromAttribute = endpoint.getName(); + let flagged = false; + + try { + debug( + `onTargetEndpoint shortfile(${shortFilename}) nameFromAttr(${nameFromAttribute})` + ); + if (!nameFromAttribute) { + const rootElement = endpoint.getElement(); + endpoint.addMessage({ + plugin, + line: rootElement.lineNumber, + column: rootElement.columnNumber, + message: `TargetEndpoint 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(`onTargetEndpoint exc(${exc1})`); + } +}; module.exports = { plugin, diff --git a/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/flightdata.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/flightdata.xml new file mode 100644 index 00000000..c7125d72 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/flightdata.xml @@ -0,0 +1,12 @@ + + + 1491498008040 + + flightdata + 1491517153495 + + + + + false + diff --git a/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml new file mode 100644 index 00000000..f5a8223b --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Clean-Response-Headers.xml @@ -0,0 +1,20 @@ + + + +
+
+
+
+
+
+
+
+
+
+
+
+
+ + + false + diff --git a/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml new file mode 100644 index 00000000..02ed60b3 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Inject-Proxy-Revision-Header.xml @@ -0,0 +1,7 @@ + + + +
{apiproxy.name} r{apiproxy.revision}
+
+
+
diff --git a/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-1.xml new file mode 100644 index 00000000..16579101 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-2.xml new file mode 100644 index 00000000..9a9dcd45 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-3.xml new file mode 100644 index 00000000..d87001f6 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-PreparedQuery-4.xml new file mode 100644 index 00000000..4e2ef9aa --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/AM-Query.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/AM-Query.xml new file mode 100644 index 00000000..f297a84e --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/EV-PathParams-4.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/EV-PathParams-4.xml new file mode 100644 index 00000000..391d31f6 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/EV-PathParams-4.xml @@ -0,0 +1,7 @@ + + request + + /airports/{param1}/counts/{param2} + + true + diff --git a/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/JS-Convert-Response.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/JS-Convert-Response.xml new file mode 100644 index 00000000..0e2ee29c --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/policies/RF-Unknown-Request.xml new file mode 100644 index 00000000..9f5f7fe1 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/TD003-TargetEndpoint-name/apiproxy/proxies/endpoint1.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/proxies/endpoint1.xml new file mode 100644 index 00000000..49ab42ee --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/proxies/endpoint1.xml @@ -0,0 +1,122 @@ + + + + /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/TD003-TargetEndpoint-name/apiproxy/targets/target-1.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-1.xml new file mode 100644 index 00000000..46e1b00f --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-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/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-2.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-2.xml new file mode 100644 index 00000000..d272f1da --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-2.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/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-3.xml b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-3.xml new file mode 100644 index 00000000..6f986f89 --- /dev/null +++ b/test/fixtures/resources/TD003-TargetEndpoint-name/apiproxy/targets/target-3.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/TD003-targetendpoint-name.js b/test/specs/TD003-targetendpoint-name.js new file mode 100644 index 00000000..9ba5f69f --- /dev/null +++ b/test/specs/TD003-targetendpoint-name.js @@ -0,0 +1,94 @@ +/* + 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 = "TD003", + assert = require("assert"), + path = require("path"), + util = require("util"), + debug = require("debug")(`apigeelint:${ruleId}`), + bl = require("../../lib/package/bundleLinter.js"); + +describe(`${ruleId} - targetEndpoint name attr`, () => { + it("should generate the expected errors", () => { + const configuration = { + debug: true, + source: { + type: "filesystem", + path: path.resolve( + __dirname, + "../fixtures/resources/TD003-TargetEndpoint-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 t1Items = actualIssues.filter((e) => + e.filePath.endsWith("target-1.xml") + ); + assert.ok(t1Items); + assert.equal(t1Items.length, 0); + + const t2Items = actualIssues.filter((e) => + e.filePath.endsWith("target-2.xml") + ); + assert.ok(t2Items); + assert.equal(t2Items.length, 1); + + debug(util.format(t2Items[0].messages)); + let td003Messages = t2Items[0].messages.filter((m) => m.ruleId == ruleId); + assert.equal(td003Messages.length, 1); + assert.ok(td003Messages[0].message); + assert.equal( + td003Messages[0].message, + "File basename (target-2) does not match endpoint name (wrongname)." + ); + + const t3Items = actualIssues.filter((e) => + e.filePath.endsWith("target-3.xml") + ); + assert.ok(t3Items); + assert.equal(t3Items.length, 1); + + td003Messages = t3Items[0].messages.filter((m) => m.ruleId == ruleId); + assert.equal(td003Messages.length, 1); + assert.ok(td003Messages[0].message); + assert.equal( + td003Messages[0].message, + "TargetEndpoint has no name attribute." + ); + }); + }); +});