diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..5c7d968 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,71 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '24 1 * * 1' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'javascript' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # â„šī¸ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # âœī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.gitignore b/.gitignore index 6360e5e..9dcefc4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ *~ .DS_Store npm_debug.log +.idea diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ba3c005..0000000 --- a/.travis.yml +++ /dev/null @@ -1,6 +0,0 @@ -language: node_js -node_js: - - lts/* -script: - - npm run lint - - npm test diff --git a/README.md b/README.md index 0e24c99..2f41ee6 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,6 @@ The purpose of this openapitest framework is to simplify authoring, organizing, executing, and reporting result of API tests using API specification. It runs on node.js and distributed via npm. Add this module to your node.js project as a development dependency and start writing API or Integration tests using YAML language. - -[![Build Status](https://travis-ci.org/testbetter/openapitest.svg?branch=master)](https://travis-ci.org/testbetter/openapitest) - ### Installation ---- ```npm install --save-dev openapitest``` @@ -67,6 +64,9 @@ Options: -g, --globalConfig [path] Global Test data config folder relative/ absolute path. e.g: /global-config -u, --url [url] Server URL. e.g: http://localhost:9000 -r, --report Will generate the html report or not. Default 0; e.g: 1 or 0 + -p, --proxy [proxy] The Proxy URL, e.g: http://127.0.0.1:8080 + -a, --tag [tag] Comma seperated tags to run the test, leave empty to run all + -m, --reportName [name] The name of mochawesome report file -h, --help output usage information ``` diff --git a/bin/openapitest.js b/bin/openapitest.js index 44973c8..492408c 100755 --- a/bin/openapitest.js +++ b/bin/openapitest.js @@ -34,7 +34,10 @@ program 'Will generate the html report or not. e.g: 1 or 0', parseInt ) + .option('-a, --tag [tag]', 'Comma seperated tags to run the test, leave empty to run all') .option('-u, --url [url]', 'Server URL. e.g: http://localhost:9000') + .option('-p, --proxy [proxy]', 'Proxy URL. e.g: http://127.0.0.1:8080') + .option('-m, --reportName [name]', 'The name of mochawesome report file') program.parse(process.argv) @@ -72,18 +75,41 @@ if (program.dataConfig) { process.env.COMMON_DATA_CONFIG = program.dataConfig } +if (program.proxy) { + process.env.PROXYURL = program.proxy; +} + process.env.API_SERVER_URL = program.url +if (program.tag) { + process.env.TAGS = program.tag +} + +let reportName = "openapitest-report"; +if (program.reportName) { + reportName = program.reportName; +} let options = {} if(program.report) { - options.reporter = 'mochawesome' + options.reporter = 'mocha-multi-reporters' options.reporterOptions = { - reportDir: 'reports', - reportFilename: 'test-int-report', - overwrite: true, - charts: true, - code: false, - quiet: true + reporterEnabled: "mocha-junit-reporter, mochawesome", + mochaJunitReporterReporterOptions: { + mochaFile: "reports/junit/results-[hash].xml", + includePending: true, + jenkinsMode: true + }, + mochawesomeReporterOptions: { + reportDir: "reports/mochawesome", + reportFilename: reportName, + html: true, + json: true, + overwrite: true, + charts: true, + code: false, + quiet: true, + inline: true + } } } @@ -106,4 +132,4 @@ function checkExists(file, desc) { console.log(`${desc}: ${file} does not exist.`.bold.red) process.exit(-1) } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 6f97df7..e1f5e35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "openapitest", - "version": "3.0.0", + "version": "3.4.5", "description": "", "main": "src/index.js", "scripts": { @@ -16,15 +16,14 @@ "openapitest": "./bin/openapitest.js" }, "repository": { - "type" : "git", - "url" : "git+https://github.com/testbetter/openapitest.git" + "type": "git", + "url": "git+https://github.com/testbetter/openapitest.git" }, "keywords": [ "operapi", "swagger", "integration test" ], - "author": "Test Better", "author": { "name": "Test Better", "email": "testbetter100@gmail.com" @@ -34,27 +33,30 @@ }, "license": "MIT", "dependencies": { - "chai": "^4.2.0", + "chai": "^4.3.4", "colors": "^1.4.0", "commander": "^2.20.1", "expect.js": "^0.3.1", - "faker": "^4.1.0", + "faker": "^5.5.3", "js-yaml": "^3.13.1", - "jsonpath": "^1.0.0", + "jsonpath": "^1.1.1", "klaw-sync": "^6.0.0", - "lodash": "^4.17.14", - "mocha": "^5.2.0", - "mochawesome": "^3.1.2", - "object-path": "^0.11.3", - "object-values": "^1.0.0", - "shelljs": "^0.8.3", - "superagent": "^4.1.0", + "lodash": "^4.17.21", + "mocha": "^8.4.0", + "mocha-junit-reporter": "^2.0.2", + "mocha-multi-reporters": "^1.5.1", + "mochawesome": "^6.2.2", + "object-path": "^0.11.5", + "object-values": "^2.0.0", + "shelljs": "^0.8.4", + "superagent": "^6.1.0", + "superagent-proxy": "^2.1.0", "tryer": "^1.0.1" }, "devDependencies": { - "eslint": "^5.16.0", - "eslint-config-airbnb-base": "^13.2.0", - "eslint-plugin-import": "^2.18.2", - "sinon": "^7.5.0" + "eslint": "^7.26.0", + "eslint-config-airbnb-base": "^14.2.1", + "eslint-plugin-import": "^2.22.1", + "sinon": "^10.0.0" } } diff --git a/src/mocha.js b/src/mocha.js index b6a4579..35fe363 100644 --- a/src/mocha.js +++ b/src/mocha.js @@ -8,6 +8,11 @@ const { loadYamlFile } = require('./util.js'); const apiPort = new ApiPort(); apiPort.init(); +let tagsArray = []; +if (process.env.TAGS) { + tagsArray = process.env.TAGS.split(','); +} + if (apiPort.get('API_TESTS_PATH')) { const paths = klawSync(apiPort.get('API_TESTS_PATH'), { nodir: true }); if (paths) { @@ -18,6 +23,34 @@ if (apiPort.get('API_TESTS_PATH')) { apiPort.currentFile = filePath; const config = loadYamlFile(filePath); describe(filePath, () => { + let tagsInTest = []; + if (tagsInTest) { + if (!Array.isArray(config.apiCalls.tag)) { + tagsInTest = [config.apiCalls.tag]; + } else { + tagsInTest = config.apiCalls.tag; + } + } + if (tagsArray.length > 0) { + let tagMatched = false; + for (const tagInArgs of tagsArray) { + for (const tagInTest of tagsInTest) { + if (`${tagInArgs}` === `${tagInTest}`) { + tagMatched = true; + break; + } + } + if (tagMatched) { + break; + } + } + + if (!tagMatched) { + console.error(`The tags ${config.apiCalls.tag || 'empty'} in file: ${filePath} does not match the expected tags: ${tagsArray || 'empty'}`); + return false; + } + } + const describeMethod = config.only ? describe.only : describe; describeMethod( (config.apiCalls || {}).name || 'unnamed', diff --git a/src/superClient.js b/src/superClient.js index b9180dc..e18fdcf 100644 --- a/src/superClient.js +++ b/src/superClient.js @@ -1,6 +1,9 @@ /* eslint-disable comma-dangle */ const _ = require('lodash'); const request = require('superagent'); +require('superagent-proxy')(request); + +const proxy = process.env.PROXYURL || null; module.exports = async function superClient( apiPort, @@ -27,8 +30,19 @@ module.exports = async function superClient( } else if (basicAuth) { suObj = suObj.auth(basicAuth.username || '', basicAuth.password || ''); } - return suObj - .set(req.header || '') - .send(data) - .sortQuery(); + + suObj.set(req.header || ''); + + if (proxy) { + suObj.proxy(proxy); + } + + // Only set data if data has some value, so that content-length is zero + if (data && data !== 'null' && Object.keys(data).length > 0) { + suObj.send(data); + } + + suObj.sortQuery(); + + return suObj; };