diff --git a/.circleci/config.yml b/.circleci/config.yml index 955c890..f1a0d04 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,14 +33,11 @@ jobs: - run: name: Javascript Linter - command: - set -e - npm run lint + command: npm run lint - run: name: All Unit Tests with Code Coverage - command: - npm run test:coverage + command: npm run test:coverage - run: name: Push any lockfile changes diff --git a/.eslintrc.js b/.eslintrc.js index 2b75ebd..8e0a722 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,13 +1,12 @@ module.exports = { extends: ['standard', 'prettier', 'prettier/standard'], - plugins: ['prettier', 'standard', 'mocha'], + plugins: ['prettier', 'standard', 'import'], parserOptions: { sourceType: 'module' }, env: { es6: true, - node: true, - mocha: true + node: true }, rules: { 'prettier/prettier': ['error', { singleQuote: true, semi: false }], diff --git a/README.md b/README.md index 96b8b32..542419e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # swagger-routes-express -Connect your Express route controllers to restful paths using your Swagger/OpenAPI definition file +Connect your Express route controllers to restful paths using your Swagger 2 or OpenAPI 3 definition file [![Greenkeeper badge](https://badges.greenkeeper.io/davesag/swagger-routes-express.svg)](https://greenkeeper.io/) @@ -12,6 +12,8 @@ Connect your Express route controllers to restful paths using your Swagger/OpenA | `develop` | [![CircleCI](https://circleci.com/gh/davesag/swagger-routes-express/tree/develop.svg?style=svg)](https://circleci.com/gh/davesag/swagger-routes-express/tree/develop) | [![codecov](https://codecov.io/gh/davesag/swagger-routes-express/branch/develop/graph/badge.svg)](https://codecov.io/gh/davesag/swagger-routes-express) | Work in progress | | `master` | [![CircleCI](https://circleci.com/gh/davesag/swagger-routes-express/tree/master.svg?style=svg)](https://circleci.com/gh/davesag/swagger-routes-express/tree/master) | [![codecov](https://codecov.io/gh/davesag/swagger-routes-express/branch/master/graph/badge.svg)](https://codecov.io/gh/davesag/swagger-routes-express) | Latest stable release | +[![NPM](https://nodei.co/npm/swagger-routes-express.png)](https://nodei.co/npm/swagger-routes-express/) + ## Prerequisites This library assumes: @@ -23,7 +25,7 @@ This library assumes: Add `swagger-routes-express` as a `dependency`: -``` +```sh npm i swagger-routes-express ``` @@ -33,7 +35,7 @@ npm i swagger-routes-express Assume the following API route controllers, defined in `./api/index.js` as follows: -``` +```js const { name, version, description } = require('../../package.json') const versions = (req, res) => { @@ -61,75 +63,75 @@ module.exports = { ping, versions } Given a Swagger (v2) YAML file `my-api.yml` along the lines of: -``` -swagger: "2.0" +```yml +swagger: '2.0' info: description: Something about the API - version: "1.0.0" - title: "Test API" -basePath: "/api/v1" + version: '1.0.0' + title: 'Test API' +basePath: '/api/v1' schemes: - - "https" - - "http" + - 'https' + - 'http' paths: /: get: tags: - - "root" - summary: "Get API Version Information" - description: "Returns a list of the available API versions" - operationId: "versions" + - 'root' + summary: 'Get API Version Information' + description: 'Returns a list of the available API versions' + operationId: 'versions' produces: - - "application/json" + - 'application/json' responses: 200: - description: "success" + description: 'success' schema: - $ref: "#/definitions/ArrayOfVersions" + $ref: '#/definitions/ArrayOfVersions' /ping: get: tags: - - "root" - summary: "Get Server Information" - description: "Returns information about the server" - operationId: "ping" + - 'root' + summary: 'Get Server Information' + description: 'Returns information about the server' + operationId: 'ping' produces: - - "application/json" + - 'application/json' responses: 200: - description: "success" + description: 'success' schema: - $ref: "#/definitions/ServerInfo" + $ref: '#/definitions/ServerInfo' definitions: # see https://swagger.io/docs/specification/data-models/data-types APIVersion: - type: "object" + type: 'object' properties: version: - type: "integer" - format: "int64" + type: 'integer' + format: 'int64' path: - type: "string" + type: 'string' ServerInfo: - type: "object" + type: 'object' properties: name: - type: "string" + type: 'string' description: - type: "string" + type: 'string' version: - type: "string" + type: 'string' uptime: - type: "number" + type: 'number' ArrayOfVersions: - type: "array" + type: 'array' items: - $ref: "#/definitions/APIVersion" + $ref: '#/definitions/APIVersion' ``` -### Or as an OpenAPI Version 3 example +### OpenAPI Version 3 example -``` +```yml openapi: 3.0.0 info: description: Something about the API @@ -197,7 +199,7 @@ components: You could set up your server as follows: -``` +```js const express = require('express') const SwaggerParser = require('swagger-parser') const swaggerRoutes = require('swagger-routes-express') @@ -220,20 +222,63 @@ const makeApp = async () => { With the result that requests to `GET /` will invoke the `versions` controller and a request to `/ping` will invoke the `ping` controller. -### Adding security handlers +### Adding security middleware handlers + +You can pass in a range of options, so if your swagger document defines security scopes you can pass in via a `security` option: + +For example if your path has a `security` block like + +```yml +paths: + /private + get: + summary: some private route + security: + - access: ['read', 'write'] + /admin + get: + summary: some admin route + security: + - access: ['admin'] +``` + +Supply a `security` option as follows + +```js +const options = { + security: { + 'read-write': readWriteAuthMiddlewareFunction, + admin: adminAuthMiddlewareFunction + } +} +``` -You can pass in a range of options, so if your swagger document defines security scopes you can pass in the following `scopes` option: +If your paths supply a `security` block but its `scopes` array is empty you can just use its name instead in the `security` option. +```yml +paths: + /private + get: + summary: some private route + security: + - apiKey: [] ``` + +supply a `security` option like + +```js const options = { - scopes: { - 'my-scope': correspondingMiddlewareFunction, - 'my-other-scope': otherAuthMiddleware, - 'admin': adminAuthMiddleware + security: { + apiKey: myAuthMiddlewareFunction } } ``` +#### Notes + +- Only the **first** security option is used. +- The previous version of `swagger-routes-express` used a `scopes` option but this didn't make sense for security without scopes. To preserve backwards compatibility the `scopes` option is still permitted but you'll get a deprecation warning. + #### What's an Auth Middleware function? An Auth Middleware Function is simply an [Express Middleware function](https://expressjs.com/en/guide/using-middleware.html) that checks to see if the user making the request is allowed to do so. @@ -242,7 +287,7 @@ How this actually works in your server's case is going to be completely applicat Your Auth Middleware then just needs to check that the user / roles you've stored corresponds with what you'd like to allow that user to do. -``` +```js async function correspondingMiddlewareFunction(req, res, next) { // previously you have added a userId to req (say from an 'Authorization: Bearer token' header) // how you check that the token is valid is up to your app's logic @@ -263,7 +308,7 @@ OpenAPI V3 allows you to define a global `security` definition as well as path s You can supply an `onCreateRoute` handler function with the options with signature -``` +```js const onCreateRoute = (method, descriptor) => { const [path, ...handlers] = descriptor console.log('created route', method, path, handlers) @@ -274,8 +319,8 @@ The method will be one of `get`, `put`, `post`, `delete`, etc. The descriptor is an array of -``` -[ +```js +;[ path, // a string. Swagger param formats will have been converted to express route formats. security, // a middleware function (if needed) controller // a route controller function @@ -316,14 +361,14 @@ The spec allows you to include template variables in the `servers`' `url` field. If you don't pass in any options the defaults are: -``` +```js { apiSeparator: '_', notFound: : require('./routes/notFound'), notImplemented: require('./routes/notImplemented'), onCreateRoute: undefined, rootTag: 'root', // unused in OpenAPI v3 docs - scopes: {}, + security: {}, variables: {}, // unused in Swagger V2 docs INVALID_VERSION: require('./errors').INVALID_VERSION } @@ -342,7 +387,7 @@ If you don't pass in any options the defaults are: ### Lint it -``` +```sh npm run lint ``` diff --git a/package-lock.json b/package-lock.json index 7450fb1..ddbe78c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "swagger-routes-express", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -255,6 +255,16 @@ "integrity": "sha1-z+nYwmYoudxa7MYqn12PHzUsEZU=", "dev": true }, + "array-includes": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/array-includes/-/array-includes-3.0.3.tgz", + "integrity": "sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0=", + "dev": true, + "requires": { + "define-properties": "^1.1.2", + "es-abstract": "^1.7.0" + } + }, "array-union": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/array-union/-/array-union-1.0.2.tgz", @@ -1126,11 +1136,12 @@ } }, "eslint-plugin-import": { - "version": "2.16.0", - "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.16.0.tgz", - "integrity": "sha512-z6oqWlf1x5GkHIFgrSvtmudnqM6Q60KM4KvpWi5ubonMjycLjndvd5+8VAZIsTlHC03djdgJuyKG6XO577px6A==", + "version": "2.17.0", + "resolved": "https://registry.npmjs.org/eslint-plugin-import/-/eslint-plugin-import-2.17.0.tgz", + "integrity": "sha512-JCsOtNwPYUoeZPlSr8t0+uCU5OVlHh+dIBn8Rw7FiOPjCECG+QzDIKDqshbyJE6CYoj9wpcstEl8vUY7rXkqVA==", "dev": true, "requires": { + "array-includes": "^3.0.3", "contains-path": "^0.1.0", "debug": "^2.6.9", "doctrine": "1.5.0", @@ -1140,7 +1151,7 @@ "lodash": "^4.17.11", "minimatch": "^3.0.4", "read-pkg-up": "^2.0.0", - "resolve": "^1.9.0" + "resolve": "^1.10.0" }, "dependencies": { "debug": { @@ -1161,15 +1172,6 @@ "esutils": "^2.0.2", "isarray": "^1.0.0" } - }, - "resolve": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.10.0.tgz", - "integrity": "sha512-3sUr9aq5OfSg2S9pNtPA9hL1FVEAjvfOC4leW0SNf/mpnaakz2a9femSd6LqAww2RaFctwyf1lCqnTHuF1rxDg==", - "dev": true, - "requires": { - "path-parse": "^1.0.6" - } } } }, @@ -2718,9 +2720,9 @@ } }, "mocha": { - "version": "6.1.2", - "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.1.2.tgz", - "integrity": "sha512-BgD2/RozoSC3uQK5R0isDcxjqaWw2n5HWdk8njYUyZf2NC79ErO5FtYVX52+rfqGoEgMfJf4fuG0IWh2TMzFoA==", + "version": "6.1.3", + "resolved": "https://registry.npmjs.org/mocha/-/mocha-6.1.3.tgz", + "integrity": "sha512-QdE/w//EPHrqgT5PNRUjRVHy6IJAzAf1R8n2O8W8K2RZ+NbPfOD5cBDp+PGa2Gptep37C/TdBiaNwakppEzEbg==", "dev": true, "requires": { "ansi-colors": "3.2.3", @@ -2737,7 +2739,7 @@ "minimatch": "3.0.4", "mkdirp": "0.5.1", "ms": "2.1.1", - "node-environment-flags": "1.0.4", + "node-environment-flags": "1.0.5", "object.assign": "4.1.0", "strip-json-comments": "2.0.1", "supports-color": "6.0.0", @@ -2904,12 +2906,21 @@ } }, "node-environment-flags": { - "version": "1.0.4", - "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.4.tgz", - "integrity": "sha512-M9rwCnWVLW7PX+NUWe3ejEdiLYinRpsEre9hMkU/6NS4h+EEulYaDH1gCEZ2gyXsmw+RXYDaV2JkkTNcsPDJ0Q==", + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/node-environment-flags/-/node-environment-flags-1.0.5.tgz", + "integrity": "sha512-VNYPRfGfmZLx0Ye20jWzHUjyTW/c+6Wq+iLhDzUI4XmhrDd9l/FozXV3F2xOaXjvp0co0+v1YSR3CMP6g+VvLQ==", "dev": true, "requires": { - "object.getownpropertydescriptors": "^2.0.3" + "object.getownpropertydescriptors": "^2.0.3", + "semver": "^5.7.0" + }, + "dependencies": { + "semver": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", + "dev": true + } } }, "normalize-package-data": { @@ -4328,9 +4339,9 @@ "dev": true }, "prettier": { - "version": "1.16.4", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.16.4.tgz", - "integrity": "sha512-ZzWuos7TI5CKUeQAtFd6Zhm2s6EpAD/ZLApIhsF9pRvRtM1RFo61dM/4MSRUA0SuLugA/zgrZD8m0BaY46Og7g==", + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.17.0.tgz", + "integrity": "sha512-sXe5lSt2WQlCbydGETgfm1YBShgOX4HxQkFPvbxkcwgDvGDeqVau8h+12+lmSVlP3rHPz0oavfddSZg/q+Szjw==", "dev": true }, "prettier-linter-helpers": { diff --git a/package.json b/package.json index b7fbe0f..3944ad9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "swagger-routes-express", - "version": "2.0.2", - "description": "Connect your Express route controllers to restful paths using your Swagger definition file", + "version": "2.0.3", + "description": "Connect your Express route controllers to restful paths using your Swagger 2 or OpenAPI 3 definition file", "main": "src/index.js", "engines": { "node": ">= 10.15.1" @@ -25,8 +25,8 @@ "eslint-check": "eslint --print-config .eslintrc.js | eslint-config-prettier-check", "lint": "eslint .", "prettier": "prettier --write '**/*.{js,json,md}'", - "test": "find ./test/unit -name '*spec.js' | NODE_PATH=. NODE_ENV=test xargs mocha --require ./test/unit/test_helper.js", - "test:coverage": "find ./test/unit -name '*spec.js' | NODE_PATH=. NODE_ENV=test xargs nyc mocha --require ./test/unit/test_helper.js" + "test": "find ./test/unit -name '*.test.js' | NODE_PATH=. NODE_ENV=test xargs mocha --require ./test/unit/testHelper.js", + "test:coverage": "find ./test/unit -name '*.test.js' | NODE_PATH=. NODE_ENV=test xargs nyc mocha --require ./test/unit/testHelper.js" }, "keywords": [ "express", @@ -42,7 +42,7 @@ "eslint": "^5.16.0", "eslint-config-prettier": "^4.1.0", "eslint-config-standard": "^12.0.0", - "eslint-plugin-import": "^2.16.0", + "eslint-plugin-import": "^2.17.0", "eslint-plugin-mocha": "^5.3.0", "eslint-plugin-node": "^8.0.1", "eslint-plugin-prettier": "^3.0.1", @@ -51,10 +51,10 @@ "faker": "^4.1.0", "husky": "^1.3.1", "lint-staged": "^8.1.5", - "mocha": "^6.1.2", + "mocha": "^6.1.3", "mock-req-res": "^1.0.5", "nyc": "^13.3.0", - "prettier": "^1.16.4", + "prettier": "^1.17.0", "proxyquire": "^2.1.0", "sinon": "^7.3.1", "sinon-chai": "^3.3.0" diff --git a/src/connector.js b/src/connector.js index bee1efc..07fb618 100644 --- a/src/connector.js +++ b/src/connector.js @@ -16,17 +16,14 @@ const connectController = require('./connectors/connectController') * notFound = src/routes/notFound * notImplemented = src/routes/notImplemented * onCreateRoute - * rootTag = 'root' - * scopes = {} + * rootTag = 'root' // ignored if using OpenAPI v3 + * security = {} * variables = {} * INVALID_VERSION = errors.INVALID_VERSION * } */ const connector = (api, apiDoc, options = {}) => { - const opts = ({ - INVALID_VERSION = ERRORS.INVALID_VERSION, - onCreateRoute - } = options) + const { INVALID_VERSION = ERRORS.INVALID_VERSION, onCreateRoute } = options const version = extractVersion(apiDoc) if (!version) throw new Error(INVALID_VERSION) diff --git a/src/connectors/connectSecurity.js b/src/connectors/connectSecurity.js index fa63060..7a070db 100644 --- a/src/connectors/connectSecurity.js +++ b/src/connectors/connectSecurity.js @@ -1,6 +1,15 @@ -connectSecurity = (security, options) => { - const { scopes = {} } = options - if (security) return scopes[security] +const connectSecurity = (key, options) => { + const { scopes = {}, security = {} } = options + if (key) { + if (scopes[key]) { + process.emitWarning( + 'The `scopes` option has been deprecated. Please use `securites` instead.', + 'DeprecationWarning' + ) + return scopes[key] + } + return security[key] + } } module.exports = connectSecurity diff --git a/src/extract/v2/extractPaths.js b/src/extract/v2/extractPaths.js index 0d3e17b..3d9abf0 100644 --- a/src/extract/v2/extractPaths.js +++ b/src/extract/v2/extractPaths.js @@ -1,5 +1,5 @@ const { METHODS } = require('../../constants') -const normaliseSecurity = require('../../normalise/normaliseSecurity') +const normaliseSecurity = require('../../normalise/v2/normaliseSecurity') const normaliseOperationId = require('../../normalise/normaliseOperationId') const normaliseRoute = require('../../normalise/normaliseRoute') diff --git a/src/extract/v3/extractPaths.js b/src/extract/v3/extractPaths.js index 39bcc01..3b4c28d 100644 --- a/src/extract/v3/extractPaths.js +++ b/src/extract/v3/extractPaths.js @@ -1,5 +1,5 @@ const { METHODS } = require('../../constants') -const normaliseSecurity = require('../../normalise/normaliseSecurity') +const normaliseSecurity = require('../../normalise/v3/normaliseSecurity') const normaliseOperationId = require('../../normalise/normaliseOperationId') const normaliseRoute = require('../../normalise/normaliseRoute') const basePath = require('./basePath') diff --git a/src/normalise/normaliseSecurity.js b/src/normalise/v2/normaliseSecurity.js similarity index 100% rename from src/normalise/normaliseSecurity.js rename to src/normalise/v2/normaliseSecurity.js diff --git a/src/normalise/v3/normaliseSecurity.js b/src/normalise/v3/normaliseSecurity.js new file mode 100644 index 0000000..dffe8fd --- /dev/null +++ b/src/normalise/v3/normaliseSecurity.js @@ -0,0 +1,13 @@ +// ref https://swagger.io/docs/specification/authentication/ +const normaliseV2Security = require('../v2/normaliseSecurity') + +const normaliseSecurity = security => { + if (!security) return + const [first] = security + const [key] = Object.keys(first) + const value = first[key] + if (value.length === 0) return key + return normaliseV2Security(security) +} + +module.exports = normaliseSecurity diff --git a/test/.eslintrc.js b/test/.eslintrc.js index 4e33b83..fccc22c 100644 --- a/test/.eslintrc.js +++ b/test/.eslintrc.js @@ -1,6 +1,10 @@ module.exports = { + plugins: ['mocha'], rules: { 'padded-blocks': 0, 'no-unused-vars': 1 + }, + env: { + mocha: true } } diff --git a/test/unit/connector.spec.js b/test/unit/connector.test.js similarity index 93% rename from test/unit/connector.spec.js rename to test/unit/connector.test.js index bcbd990..1dff46a 100644 --- a/test/unit/connector.spec.js +++ b/test/unit/connector.test.js @@ -12,7 +12,7 @@ describe('src/connector', () => { versions: () => {} } - const fakeScopes = { + const fakeSecurity = { 'admin,identity.basic,identity.email': () => {} } @@ -58,10 +58,10 @@ describe('src/connector', () => { }) context('with options', () => { - context('with scopes', () => { + context('with a security option', () => { before(() => { const connect = connector(mockApi, doc, { - scopes: fakeScopes + security: fakeSecurity }) connect(mockApp) }) @@ -76,7 +76,7 @@ describe('src/connector', () => { context('with onCreateRoute callback', () => { before(() => { const connect = connector(mockApi, doc, { - scopes: fakeScopes, + security: fakeSecurity, onCreateRoute }) connect(mockApp) diff --git a/test/unit/connectors/connectController.spec.js b/test/unit/connectors/connectController.test.js similarity index 100% rename from test/unit/connectors/connectController.spec.js rename to test/unit/connectors/connectController.test.js diff --git a/test/unit/connectors/connectSecurity.spec.js b/test/unit/connectors/connectSecurity.test.js similarity index 54% rename from test/unit/connectors/connectSecurity.spec.js rename to test/unit/connectors/connectSecurity.test.js index 904ca95..6239969 100644 --- a/test/unit/connectors/connectSecurity.spec.js +++ b/test/unit/connectors/connectSecurity.test.js @@ -8,16 +8,28 @@ describe('src/connectors/connectSecurity', () => { expect(connectSecurity(undefined, {})).to.be.undefined }) }) + context('given out of scope security', () => { it('returns undefined', () => { expect(connectSecurity('test', {})).to.be.undefined }) }) + context('given in-scope security', () => { - const scopes = { test: () => {} } + context('in scopes', () => { + const scopes = { test: () => {} } + + it('returns the correct middleware', () => { + expect(connectSecurity('test', { scopes })).to.equal(scopes.test) + }) + }) + + context('in security', () => { + const security = { test: () => {} } - it('returns the correct middleware', () => { - expect(connectSecurity('test', { scopes })).to.equal(scopes.test) + it('returns the correct middleware', () => { + expect(connectSecurity('test', { security })).to.equal(security.test) + }) }) }) }) diff --git a/test/unit/extract/extractVersion.spec.js b/test/unit/extract/extractVersion.test.js similarity index 100% rename from test/unit/extract/extractVersion.spec.js rename to test/unit/extract/extractVersion.test.js diff --git a/test/unit/extract/v2/extractPaths.spec.js b/test/unit/extract/v2/extractPaths.test.js similarity index 100% rename from test/unit/extract/v2/extractPaths.spec.js rename to test/unit/extract/v2/extractPaths.test.js diff --git a/test/unit/extract/v3/basePath.spec.js b/test/unit/extract/v3/basePath.test.js similarity index 100% rename from test/unit/extract/v3/basePath.spec.js rename to test/unit/extract/v3/basePath.test.js diff --git a/test/unit/extract/v3/extractPaths.spec.js b/test/unit/extract/v3/extractPaths.test.js similarity index 100% rename from test/unit/extract/v3/extractPaths.spec.js rename to test/unit/extract/v3/extractPaths.test.js diff --git a/test/unit/normalise/normaliseOperationId.spec.js b/test/unit/normalise/normaliseOperationId.test.js similarity index 100% rename from test/unit/normalise/normaliseOperationId.spec.js rename to test/unit/normalise/normaliseOperationId.test.js diff --git a/test/unit/normalise/normaliseRoute.spec.js b/test/unit/normalise/normaliseRoute.test.js similarity index 100% rename from test/unit/normalise/normaliseRoute.spec.js rename to test/unit/normalise/normaliseRoute.test.js diff --git a/test/unit/normalise/normaliseSecurity.spec.js b/test/unit/normalise/v2/normaliseSecurity.test.js similarity index 62% rename from test/unit/normalise/normaliseSecurity.spec.js rename to test/unit/normalise/v2/normaliseSecurity.test.js index 5686d1a..7f1aae5 100644 --- a/test/unit/normalise/normaliseSecurity.spec.js +++ b/test/unit/normalise/v2/normaliseSecurity.test.js @@ -1,17 +1,17 @@ const { expect } = require('chai') -const normaliseSecurity = require('src/normalise/normaliseSecurity') +const normaliseSecurity = require('src/normalise/v2/normaliseSecurity') -describe('src/normalise/normaliseSecurity', () => { +describe('src/normalise/v2/normaliseSecurity', () => { context('given a swagger security block', () => { const security = [ { example: ['identity.basic', 'identity.email', 'admin'] } ] - expected = 'admin,identity.basic,identity.email' + const expected = 'admin,identity.basic,identity.email' - it('normalises the security block', () => { + it('normalises the security block correctly', () => { expect(normaliseSecurity(security)).to.equal(expected) }) }) diff --git a/test/unit/normalise/v3/normaliseSecurity.test.js b/test/unit/normalise/v3/normaliseSecurity.test.js new file mode 100644 index 0000000..46861a0 --- /dev/null +++ b/test/unit/normalise/v3/normaliseSecurity.test.js @@ -0,0 +1,39 @@ +const { expect } = require('chai') + +const normaliseSecurity = require('src/normalise/v3/normaliseSecurity') + +describe('src/normalise/v3/normaliseSecurity', () => { + const doTest = (security, expected) => { + it('normalises the security block correctly', () => { + expect(normaliseSecurity(security)).to.equal(expected) + }) + } + + context('given a swagger security block', () => { + context('there are no scopes', () => { + const security = [ + { + example: [] + } + ] + + doTest(security, 'example') + }) + + context('there are scopes', () => { + const security = [ + { + example: ['identity.basic', 'identity.email', 'admin'] + } + ] + + doTest(security, 'admin,identity.basic,identity.email') + }) + }) + + context('given nothing', () => { + it('returns undefined', () => { + expect(normaliseSecurity()).to.be.undefined + }) + }) +}) diff --git a/test/unit/routes/notFound.spec.js b/test/unit/routes/notFound.test.js similarity index 100% rename from test/unit/routes/notFound.spec.js rename to test/unit/routes/notFound.test.js diff --git a/test/unit/routes/notImplemented.spec.js b/test/unit/routes/notImplemented.test.js similarity index 100% rename from test/unit/routes/notImplemented.spec.js rename to test/unit/routes/notImplemented.test.js diff --git a/test/unit/test_helper.js b/test/unit/testHelper.js similarity index 100% rename from test/unit/test_helper.js rename to test/unit/testHelper.js diff --git a/test/unit/utils/substituteVariables.spec.js b/test/unit/utils/substituteVariables.test.js similarity index 100% rename from test/unit/utils/substituteVariables.spec.js rename to test/unit/utils/substituteVariables.test.js