diff --git a/__tests__/fixtures/local-link.json b/__tests__/fixtures/local-link.json new file mode 100644 index 00000000..0c1007e9 --- /dev/null +++ b/__tests__/fixtures/local-link.json @@ -0,0 +1,369 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Link Example", + "version": "1.0.0" + }, + "servers": [ + { + "url": "http://local-link.com" + } + ], + "paths": { + "/2.0/users/{username}": { + "get": { + "operationId": "getUserByName", + "security": [ + { + "cookieAuth": [] + }, + { + "bearerAuth": [] + } + ], + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "The User", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/user" + } + } + }, + "links": { + "userRepositories": { + "$ref": "#/components/links/UserRepositories" + } + } + } + } + } + }, + "/2.0/repositories/{username}": { + "get": { + "operationId": "getRepositoriesByOwner", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "repositories owned by the supplied user", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/repository" + } + } + } + }, + "links": { + "userRepository": { + "$ref": "#/components/links/UserRepository" + } + } + } + } + } + }, + "/2.0/repositories/{username}/{slug}": { + "get": { + "operationId": "getRepository", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "slug", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "The repository", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/repository" + } + } + }, + "links": { + "repositoryPullRequests": { + "$ref": "#/components/links/RepositoryPullRequests" + } + } + } + } + } + }, + "/2.0/repositories/{username}/{slug}/pullrequests": { + "get": { + "operationId": "getPullRequestsByRepository", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "slug", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "state", + "in": "query", + "schema": { + "type": "string", + "enum": [ + "open", + "merged", + "declined" + ] + } + }, + { + "$ref": "#/components/parameters/host" + } + ], + "responses": { + "200": { + "description": "an array of pull request objects", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "$ref": "#/components/schemas/pullrequest" + } + } + } + } + } + } + } + }, + "/2.0/repositories/{username}/{slug}/pullrequests/{pid}": { + "get": { + "operationId": "getPullRequestsById", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "slug", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "pid", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "a pull request object", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/pullrequest" + } + } + }, + "links": { + "pullRequestMerge": { + "$ref": "#/components/links/PullRequestMerge" + } + } + } + } + } + }, + "/2.0/repositories/{username}/{slug}/pullrequests/{pid}/merge": { + "post": { + "operationId": "mergePullRequest", + "parameters": [ + { + "name": "username", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "slug", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "pid", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "204": { + "description": "the PR was successfully merged" + } + } + } + } + }, + "components": { + "links": { + "UserRepositories": { + "operationId": "getRepositoriesByOwner", + "parameters": { + "username": "$response.body#/username" + } + }, + "UserRepository": { + "operationId": "getRepository", + "parameters": { + "username": "$response.body#/owner/username", + "slug": "$response.body#/slug" + } + }, + "RepositoryPullRequests": { + "operationId": "getPullRequestsByRepository", + "parameters": { + "username": "$response.body#/owner/username", + "slug": "$response.body#/slug" + } + }, + "PullRequestMerge": { + "operationId": "mergePullRequest", + "parameters": { + "username": "$response.body#/author/username", + "slug": "$response.body#/repository/slug", + "pid": "$response.body#/id" + } + } + }, + "schemas": { + "user": { + "type": "object", + "properties": { + "username": { + "type": "string" + }, + "uuid": { + "type": "string" + } + } + }, + "repository": { + "type": "object", + "properties": { + "slug": { + "type": "string" + }, + "owner": { + "$ref": "#/components/schemas/user" + } + } + }, + "pullrequest": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "title": { + "type": "string" + }, + "repository": { + "$ref": "#/components/schemas/repository" + }, + "author": { + "$ref": "#/components/schemas/user" + } + } + } + }, + "securitySchemes": { + "apiKey": { + "name": "X-API-KEY", + "type": "apiKey", + "in": "header" + }, + "basicAuth": { + "type": "http", + "scheme": "basic" + }, + "bearerAuth": { + "type": "http", + "scheme": "bearer", + "bearerFormat": "JWT" + }, + "cookieAuth": { + "type": "apiKey", + "in": "cookie", + "name": "cookieSessionId" + } + }, + "parameters": { + "host": { + "in": "header", + "name": "hostname", + "schema": { + "type": "string" + } + } + } + } +} diff --git a/__tests__/fixtures/multiple-securities.json b/__tests__/fixtures/multiple-securities.json index 634de191..1a556413 100644 --- a/__tests__/fixtures/multiple-securities.json +++ b/__tests__/fixtures/multiple-securities.json @@ -237,7 +237,7 @@ }, "apiKeyScheme": { "type": "apiKey", - "name": "apiKey", + "name": "testKey", "in": "header" }, "unknownAuthType": { diff --git a/__tests__/fixtures/petstore.json b/__tests__/fixtures/petstore.json index eef09161..39d0b3be 100644 --- a/__tests__/fixtures/petstore.json +++ b/__tests__/fixtures/petstore.json @@ -303,7 +303,7 @@ "operationId": "deletePet", "parameters": [ { - "name": "api_key", + "name": "testKey", "in": "header", "required": false, "schema": { @@ -984,7 +984,7 @@ }, "api_key": { "type": "apiKey", - "name": "api_key", + "name": "testKey", "in": "header" } }, diff --git a/__tests__/oas.test.js b/__tests__/oas.test.js index 87fc5546..5a712f94 100644 --- a/__tests__/oas.test.js +++ b/__tests__/oas.test.js @@ -2,6 +2,7 @@ const Oas = require('../src/oas'); const { Operation } = require('../src/oas'); const petstore = require('./fixtures/petstore.json'); const multipleSecurities = require('./fixtures/multiple-securities.json'); +const referenceSpec = require('./fixtures/local-link.json'); describe('class.Oas', () => { describe('operation()', () => { @@ -150,6 +151,24 @@ describe('class.Oas', () => { }, }); }); + + it('should return normally if path is formatted poorly', () => { + const oas = new Oas(petstore); + const uri = `http://petstore.swagger.io/v2/pet/1/`; + const method = 'DELETE'; + + const res = oas.findOperation(uri, method); + expect(res).toMatchObject({ + url: { + origin: 'http://petstore.swagger.io/v2', + path: '/pet/:petId', + slugs: { + ':petId': '1', + }, + method: 'DELETE', + }, + }); + }); }); }); @@ -341,7 +360,7 @@ describe('class.operation', () => { const operation = new Operation(oas, logOperation.url.path, logOperation.url.method, logOperation.operation); expect(operation.getHeaders()).toMatchObject({ - request: ['api_key'], + request: ['testKey'], response: [], }); }); @@ -355,7 +374,34 @@ describe('class.operation', () => { const operation = new Operation(oas, logOperation.url.path, logOperation.url.method, logOperation.operation); expect(operation.getHeaders()).toMatchObject({ - request: ['apiKey'], + request: ['testKey'], + response: [], + }); + }); + + it('should return a Cookie header if security is located in cookie scheme', () => { + const oas = new Oas(referenceSpec); + const uri = 'http://local-link.com/2.0/users/johnSmith'; + const method = 'GET'; + + const logOperation = oas.findOperation(uri, method); + const operation = new Operation(oas, logOperation.url.path, logOperation.url.method, logOperation.operation); + console.log(operation.getHeaders()); + expect(operation.getHeaders()).toMatchObject({ + request: ['Cookie', 'Authorization'], + response: [], + }); + }); + + it('should target parameter refs and return names if applicable', () => { + const oas = new Oas(referenceSpec); + const uri = 'http://local-link.com/2.0/repositories/janeDoe/oas/pullrequests'; + const method = 'GET'; + + const logOperation = oas.findOperation(uri, method); + const operation = new Operation(oas, logOperation.url.path, logOperation.url.method, logOperation.operation); + expect(operation.getHeaders()).toMatchObject({ + request: ['hostname'], response: [], }); });