From d24c8dcc4d7e6fc1f2a5edcc2dce368b341311fd Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Wed, 11 Oct 2023 16:54:06 +0200 Subject: [PATCH] Automatic test (#1) * Automatic test * Complete test * npm update --- .github/workflows/nodejs.yml | 2 +- index.js | 22 ++++++++++++++++++ package-lock.json | 18 +++++++++++---- package.json | 7 ++++-- test.sh | 45 ++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 index.js create mode 100755 test.sh diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index d37559d..b08142e 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: - node-version: [18.x, 20.x] + node-version: [16.x, 20.x] steps: - uses: actions/checkout@v4 diff --git a/index.js b/index.js new file mode 100644 index 0000000..e46c983 --- /dev/null +++ b/index.js @@ -0,0 +1,22 @@ +'use strict'; +/** + * Command-line interface for Node-RED nodes. + * + * Script to run our Node-RED nodes from terminal without Node-RED and using STDIN / STDOUT. + * + * @author Alexandre Alapetite + * @copyright Alexandra Institute for the SynchroniCity European project + * as a contribution to FIWARE + * @license MIT + * @date 2019-11-28 / 2022-02-02 + */ + +// Load fake/mocked Node-RED +const RED = require('node-red-contrib-mock-cli'); +const noderedNode = RED.load(require.main); + +if (noderedNode) { + RED.run(); +} else { + console.error('Error loading Node-RED node!'); +} diff --git a/package-lock.json b/package-lock.json index 7b2ba71..0d92a06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,8 @@ "eslint-plugin-html": "^7.1.0", "eslint-plugin-import": "^2.28.1", "eslint-plugin-n": "^16.1.0", - "eslint-plugin-promise": "^6.1.1" + "eslint-plugin-promise": "^6.1.1", + "node-red-contrib-mock-cli": "^1.3.0" }, "engines": { "node": ">=12" @@ -1031,9 +1032,9 @@ } }, "node_modules/eslint-plugin-n": { - "version": "16.1.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-n/-/eslint-plugin-n-16.1.0.tgz", - "integrity": "sha512-3wv/TooBst0N4ND+pnvffHuz9gNPmk/NkLwAxOt2JykTl/hcuECe6yhTtLJcZjIxtZwN+GX92ACp/QTLpHA3Hg==", + "version": "16.2.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-n/-/eslint-plugin-n-16.2.0.tgz", + "integrity": "sha512-AQER2jEyQOt1LG6JkGJCCIFotzmlcCZFur2wdKrp1JX2cNotC7Ae0BcD/4lLv3lUAArM9uNS8z/fsvXTd0L71g==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.4.0", @@ -2095,6 +2096,15 @@ } } }, + "node_modules/node-red-contrib-mock-cli": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/node-red-contrib-mock-cli/-/node-red-contrib-mock-cli-1.3.0.tgz", + "integrity": "sha512-R9tlpJ6NzmqzCy7hD2zISkCoZU7sRmjCevQAS4LCj3Akgd5wkIynVnLPygMi8KcxozMdBBHF/zOJ5s/zMdztiA==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/nopt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/nopt/-/nopt-5.0.0.tgz", diff --git a/package.json b/package.json index bb932e3..c155278 100644 --- a/package.json +++ b/package.json @@ -50,12 +50,15 @@ "eslint-plugin-html": "^7.1.0", "eslint-plugin-import": "^2.28.1", "eslint-plugin-n": "^16.1.0", - "eslint-plugin-promise": "^6.1.1" + "eslint-plugin-promise": "^6.1.1", + "node-red-contrib-mock-cli": "^1.3.0" }, "scripts": { + "start": "node ./index.js", "eslint": "eslint --ext .js,.html .", "eslint_fix": "eslint --fix --ext .js,.html .", "fix": "npm run eslint_fix", - "test": "npm run eslint" + "pretest": "npm run-script eslint", + "test": "./test.sh" } } diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..c15f690 --- /dev/null +++ b/test.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +test=$( + cat <<'EOF' | node ./index.js http-basic-auth --realm='"node-red"' --username='"test"' --password='"$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6"' +{"req":{"headers":{"authorization":"Basic dGVzdDp0ZXN0"}}} +EOF +) + +if [ "$test" = "" ]; then + echo 'ERROR 1' + exit 1 +fi + +test=$( + cat <<'EOF' | node ./index.js http-basic-auth --realm='"node-red"' --username='"test"' --password='"test"' +{"req":{"headers":{"authorization":"Basic dGVzdDp0ZXN0"}}} +EOF +) + +if [ "$test" = "" ]; then + echo 'ERROR 2' + exit 1 +fi + +test=$( + cat <<'EOF' | node ./index.js http-basic-auth --realm='"node-red"' --username='"wrong"' --password='"$2y$10$5TSZDldoJ7MxDZdtK/SG2O3cwORqLDhHabYlKX9OsM.W/Z/oLwKW6"' +{"req":{"headers":{"authorization":"Basic dGVzdDp0ZXN0"}}} +EOF +) + +if [ "$test" != "" ]; then + echo 'ERROR 3' + exit 1 +fi + +test=$( + cat <<'EOF' | node ./index.js http-basic-auth --realm='"node-red"' --username='"test"' --password='"wrong"' +{"req":{"headers":{"authorization":"Basic dGVzdDp0ZXN0"}}} +EOF +) + +if [ "$test" != "" ]; then + echo 'ERROR 4' + exit 1 +fi