Repository: BRIKEV/express-jsdoc-swagger (js)

Issue:
[Enhancement] Security configuration in path method
We need to add the feature of the security method for each endpoint. We've already added the configuration and schemas in this PR #18 so we have to add for the endpoint config.

The API for the user can be:

```js
/**
 * POST /api/v1/song
 * @summary Create new song
 * @security BasicAuth
 */
```

*Note*: BasicAuth is the name we provide in the security options like in this example.

```js
const options = {
  info: {
    version: '1.0.0',
    title: 'Albums store',
    license: {
      name: 'MIT',
    },
  },
  security: {
    BasicAuth: {
      type: 'http',
      scheme: 'basic',
    },
  },
  file: './basic-auth.js',
  baseDir: __dirname,
};
```

Expected son should be like this one in each endpoint:

```js
"security": [
  {
     "BasicAuth": []
  }
]
```

## Tasks

- [ ] Include it as an option in this [function](https://github.com/BRIKEV/express-jsdoc-swagger/blob/master/transforms/paths/index.js#L15).


Pull request:
Security configuration
### What kind of change does this PR introduce? (check at least one)
 - [ ] Bugfix
 - [x] Feature
 - [x] Test
 - [x] Docs
 - [ ] Refactor
 - [ ] Build-related changes
 - [ ] Other, please describe:

### Description:
- This PR fixes #34 

Patch:
diff --git a/examples/responses/full-example.js b/examples/responses/full-example.js
index 114504d..5df7d7d 100644
--- a/examples/responses/full-example.js
+++ b/examples/responses/full-example.js
@@ -13,6 +13,12 @@ const options = {
   },
   file: './full-example.js',
   baseDir: __dirname,
+  security: {
+    BasicAuth: {
+      type: 'http',
+      scheme: 'basic',
+    },
+  },
 };
 
 const app = express();
@@ -32,6 +38,7 @@ expressJSDocSwagger(app)(options);
  * GET /api/v2/album
  * @summary This is the summary or description of the endpoint
  * @tags album
+ * @security BasicAuth
  * @return {object} 200 - success response - application/json
  * @return {object} 400 - Bad request response
  */
diff --git a/transforms/paths/index.js b/transforms/paths/index.js
index 5bb6d9a..bdf6191 100644
--- a/transforms/paths/index.js
+++ b/transforms/paths/index.js
@@ -13,6 +13,10 @@ const formatTags = (tags = []) => tags.map(({ description }) => {
   return name;
 });
 
+const formatSecurity = (securityValues = []) => securityValues.map(({ description }) => ({
+  [description]: [],
+}));
+
 const bodyParams = ({ name }) => name.includes('request.body');
 
 const pathValues = tags => {
@@ -27,6 +31,8 @@ const pathValues = tags => {
   const parameters = parametersGenerator(paramValues);
   /* Tags info */
   const tagsValues = getTagsInfo(tags, 'tags');
+  /* Security info */
+  const securityValues = getTagsInfo(tags, 'security');
   /* Request body info */
   const bodyValues = paramValues.filter(bodyParams);
   return {
@@ -36,6 +42,7 @@ const pathValues = tags => {
     parameters,
     tagsValues,
     bodyValues,
+    securityValues,
   };
 };
 
@@ -48,7 +55,7 @@ const parsePath = (path, state) => {
   if (!validHTTPMethod(lowerCaseMethod)) return {};
   const { tags } = path;
   const {
-    summary, bodyValues, isDeprecated, responses, parameters, tagsValues,
+    summary, bodyValues, isDeprecated, responses, parameters, tagsValues, securityValues,
   } = pathValues(tags);
   const hasBodyValues = bodyValues.length > 0;
   const requestBody = requestBodyGenerator(bodyValues);
@@ -59,6 +66,7 @@ const parsePath = (path, state) => {
       [lowerCaseMethod]: {
         deprecated: isDeprecated,
         summary: summary && summary.description ? summary.description : '',
+        security: formatSecurity(securityValues),
         responses,
         parameters,
         tags: formatTags(tagsValues),


Test patch:
diff --git a/test/e2e/parameters/parameters.test.js b/test/e2e/parameters/parameters.test.js
index 26ff8ac..6cdc23e 100644
--- a/test/e2e/parameters/parameters.test.js
+++ b/test/e2e/parameters/parameters.test.js
@@ -66,6 +66,7 @@ test('should parse components jsdoc from jsdoc-example', async () => {
               },
             },
           ],
+          security: [],
           tags: [],
         },
       },
@@ -112,6 +113,7 @@ test('should parse components jsdoc from jsdoc-example', async () => {
               },
             },
           ],
+          security: [],
           tags: [],
         },
       },
diff --git a/test/transforms/paths/index.test.js b/test/transforms/paths/index.test.js
index 358c995..1ce590b 100644
--- a/test/transforms/paths/index.test.js
+++ b/test/transforms/paths/index.test.js
@@ -38,6 +38,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -76,6 +77,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -121,6 +123,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -141,6 +144,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -186,6 +190,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -204,6 +209,7 @@ describe('setPaths method', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
diff --git a/test/transforms/paths/parameters.test.js b/test/transforms/paths/parameters.test.js
index c2763a7..5d3a4cc 100644
--- a/test/transforms/paths/parameters.test.js
+++ b/test/transforms/paths/parameters.test.js
@@ -17,6 +17,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [{
               allowEmptyValue: false,
               deprecated: false,
@@ -52,6 +53,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
           },
         },
@@ -77,6 +79,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [{
               allowEmptyValue: false,
               deprecated: true,
@@ -116,6 +119,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [{
               allowEmptyValue: false,
               deprecated: true,
@@ -164,6 +168,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [{
               allowEmptyValue: false,
               deprecated: false,
@@ -199,6 +204,7 @@ describe('params tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [{
               allowEmptyValue: false,
               deprecated: true,
diff --git a/test/transforms/paths/requestBody.test.js b/test/transforms/paths/requestBody.test.js
index 1487086..040a55d 100644
--- a/test/transforms/paths/requestBody.test.js
+++ b/test/transforms/paths/requestBody.test.js
@@ -16,6 +16,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
           },
         },
@@ -41,6 +42,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
@@ -78,6 +80,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: '',
@@ -117,6 +120,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
@@ -157,6 +161,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
@@ -196,6 +201,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
@@ -232,6 +238,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
@@ -272,6 +279,7 @@ describe('request body tests', () => {
             summary: '',
             responses: {},
             tags: [],
+            security: [],
             parameters: [],
             requestBody: {
               description: 'name body description',
diff --git a/test/transforms/paths/responses.test.js b/test/transforms/paths/responses.test.js
index 9341653..6f78c59 100644
--- a/test/transforms/paths/responses.test.js
+++ b/test/transforms/paths/responses.test.js
@@ -19,6 +19,7 @@ describe('response tests', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -68,6 +69,7 @@ describe('response tests', () => {
             responses: {},
             parameters: [],
             tags: [],
+            security: [],
           },
         },
       },
@@ -95,6 +97,7 @@ describe('response tests', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -135,6 +138,7 @@ describe('response tests', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -172,6 +176,7 @@ describe('response tests', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
@@ -214,6 +219,7 @@ describe('response tests', () => {
             summary: 'This is the summary or description of the endpoint',
             parameters: [],
             tags: [],
+            security: [],
             responses: {
               200: {
                 description: 'success response',
diff --git a/test/transforms/paths/security.test.js b/test/transforms/paths/security.test.js
new file mode 100644
index 0000000..0baf055
--- /dev/null
+++ b/test/transforms/paths/security.test.js
@@ -0,0 +1,35 @@
+const jsdocInfo = require('../../../consumers/jsdocInfo');
+const setPaths = require('../../../transforms/paths');
+
+describe('Paths - security', () => {
+  it('Should parse jsdoc security params into security array with each security type', () => {
+    const jsodInput = [`
+      /**
+       * POST /api/v1/song
+       * @summary Create new song
+       * @security BasicAuth
+       */
+    `];
+    const expected = {
+      paths: {
+        '/api/v1/song': {
+          post: {
+            deprecated: false,
+            summary: 'Create new song',
+            security: [
+              {
+                BasicAuth: [],
+              },
+            ],
+            tags: [],
+            responses: {},
+            parameters: [],
+          },
+        },
+      },
+    };
+    const parsedJSDocs = jsdocInfo()(jsodInput);
+    const result = setPaths({}, parsedJSDocs);
+    expect(result).toEqual(expected);
+  });
+});
diff --git a/test/transforms/paths/tags.test.js b/test/transforms/paths/tags.test.js
index 0856440..aa5c23a 100644
--- a/test/transforms/paths/tags.test.js
+++ b/test/transforms/paths/tags.test.js
@@ -15,6 +15,7 @@ describe('Paths - tags', () => {
           get: {
             deprecated: false,
             summary: '',
+            security: [],
             tags: [
               'album',
             ],
@@ -43,6 +44,7 @@ describe('Paths - tags', () => {
           get: {
             deprecated: false,
             summary: '',
+            security: [],
             tags: [
               'album',
               'Years',
@@ -72,6 +74,7 @@ describe('Paths - tags', () => {
           get: {
             deprecated: false,
             summary: '',
+            security: [],
             tags: [
               'album',
               'Years',