From 5416db2e16bb1e4e9af6fa352e1f2ee139f7abae Mon Sep 17 00:00:00 2001 From: Artem Derevnjuk Date: Thu, 27 Jan 2022 21:36:57 +0300 Subject: [PATCH] test(oas): add tests to cover these cases --- packages/oas/tests/DefaultConverter.spec.ts | 39 +++++- .../fixtures/wrong-schema-in-body.oas.yaml | 111 ++++++++++++++++++ .../fixtures/wrong-schema-in-header.oas.yaml | 111 ++++++++++++++++++ .../fixtures/wrong-schema-in-path.oas.yaml | 111 ++++++++++++++++++ .../fixtures/wrong-schema-in-query.oas.yaml | 108 +++++++++++++++++ 5 files changed, 478 insertions(+), 2 deletions(-) create mode 100644 packages/oas/tests/fixtures/wrong-schema-in-body.oas.yaml create mode 100644 packages/oas/tests/fixtures/wrong-schema-in-header.oas.yaml create mode 100644 packages/oas/tests/fixtures/wrong-schema-in-path.oas.yaml create mode 100644 packages/oas/tests/fixtures/wrong-schema-in-query.oas.yaml diff --git a/packages/oas/tests/DefaultConverter.spec.ts b/packages/oas/tests/DefaultConverter.spec.ts index 728538fc..38e3eb2f 100644 --- a/packages/oas/tests/DefaultConverter.spec.ts +++ b/packages/oas/tests/DefaultConverter.spec.ts @@ -1,6 +1,5 @@ import githubSwagger from './fixtures/github.swagger.json'; -import { oas2har } from '../src'; -import { ConvertError } from '../src/errors'; +import { ConvertError, oas2har } from '../src'; import yaml from 'js-yaml'; import { OpenAPIV2, OpenAPIV3, Request } from '@har-sdk/core'; import { use } from 'chai'; @@ -134,5 +133,41 @@ describe('DefaultConverter', () => { .rejectedWith(ConvertError) .and.eventually.have.property('jsonPointer', '/host'); }); + + [ + { + input: 'wrong-schema-in-body.oas.yaml', + expected: + '/paths//store/order/{orderId}/put/requestBody/content/application/json/schema' + }, + { + input: 'wrong-schema-in-header.oas.yaml', + expected: '/paths//store/order/{orderId}/put/parameters/2/schema' + }, + { + input: 'wrong-schema-in-path.oas.yaml', + expected: '/paths//store/order/{orderId}/put/parameters/0/schema' + }, + { + input: 'wrong-schema-in-query.oas.yaml', + expected: '/paths//store/order/{orderId}/put/parameters/1/schema' + } + ].forEach(({ input, expected }) => + it(`should throw an convert error on auto sampling issues in ${input.replace( + /^wrong-schema-in-(.+).oas.yaml$/, + '$1' + )}`, async () => { + const content: string = await promisify(readFile)( + resolve(`./tests/fixtures/${input}`), + 'utf8' + ); + + const result = oas2har(yaml.load(content) as OpenAPIV2.Document); + + return result.should.be + .rejectedWith(ConvertError) + .and.eventually.have.property('jsonPointer', expected); + }) + ); }); }); diff --git a/packages/oas/tests/fixtures/wrong-schema-in-body.oas.yaml b/packages/oas/tests/fixtures/wrong-schema-in-body.oas.yaml new file mode 100644 index 00000000..4a840d75 --- /dev/null +++ b/packages/oas/tests/fixtures/wrong-schema-in-body.oas.yaml @@ -0,0 +1,111 @@ +openapi: 3.0.1 +info: + title: Swagger Petstore + description: + 'This is a sample server Petstore server. You can find out more about Swagger + at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For + this sample, you can use the api key `special-key` to test the authorization filters.' + termsOfService: http://swagger.io/terms/ + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io +servers: + - url: https://{instance}.swagger.io:{port}/{basePath} + variables: + instance: + default: petstore + port: + enum: + - '8443' + - '443' + default: '8443' + basePath: + default: v2 +paths: + /store/order/{orderId}: + put: + parameters: + - name: orderId + in: path + required: true + schema: + maximum: 10.0 + minimum: 1.0 + type: integer + format: int64 + - name: exclude + in: query + required: true + schema: + type: array + items: + type: string + enum: ['petId', 'quantity', 'shipDate', 'status', 'complete'] + minItems: 1 + maxItems: 5 + uniqueItems: true + - name: api_key + in: header + schema: + type: string + requestBody: + content: + application/json: + schema: + type: 'number' + minimum: 5 + maximum: 3 + application/xml: + schema: + $ref: '#/components/schemas/Order' + responses: + 200: + description: successful operation + content: + application/xml: + schema: + $ref: '#/components/schemas/Order' + application/json: + schema: + $ref: '#/components/schemas/Order' + 400: + description: Invalid ID supplied + content: {} + 404: + description: Order not found + content: {} +components: + schemas: + Order: + type: object + properties: + id: + type: integer + format: int64 + petId: + type: integer + format: int64 + quantity: + type: integer + format: int32 + shipDate: + type: string + format: date-time + status: + type: string + description: Order Status + enum: + - placed + - approved + - delivered + complete: + type: boolean + default: false + xml: + name: Order diff --git a/packages/oas/tests/fixtures/wrong-schema-in-header.oas.yaml b/packages/oas/tests/fixtures/wrong-schema-in-header.oas.yaml new file mode 100644 index 00000000..fad01564 --- /dev/null +++ b/packages/oas/tests/fixtures/wrong-schema-in-header.oas.yaml @@ -0,0 +1,111 @@ +openapi: 3.0.1 +info: + title: Swagger Petstore + description: + 'This is a sample server Petstore server. You can find out more about Swagger + at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For + this sample, you can use the api key `special-key` to test the authorization filters.' + termsOfService: http://swagger.io/terms/ + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io +servers: + - url: https://{instance}.swagger.io:{port}/{basePath} + variables: + instance: + default: petstore + port: + enum: + - '8443' + - '443' + default: '8443' + basePath: + default: v2 +paths: + /store/order/{orderId}: + put: + parameters: + - name: orderId + in: path + required: true + schema: + maximum: 10.0 + minimum: 1.0 + type: integer + format: int64 + - name: exclude + in: query + required: true + schema: + type: array + items: + type: string + enum: ['petId', 'quantity', 'shipDate', 'status', 'complete'] + minItems: 1 + maxItems: 5 + uniqueItems: true + - name: x-date + in: header + schema: + type: 'string' + format: 'date' + minLength: 11 + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Order' + application/xml: + schema: + $ref: '#/components/schemas/Order' + responses: + 200: + description: successful operation + content: + application/xml: + schema: + $ref: '#/components/schemas/Order' + application/json: + schema: + $ref: '#/components/schemas/Order' + 400: + description: Invalid ID supplied + content: {} + 404: + description: Order not found + content: {} +components: + schemas: + Order: + type: object + properties: + id: + type: integer + format: int64 + petId: + type: integer + format: int64 + quantity: + type: integer + format: int32 + shipDate: + type: string + format: date-time + status: + type: string + description: Order Status + enum: + - placed + - approved + - delivered + complete: + type: boolean + default: false + xml: + name: Order diff --git a/packages/oas/tests/fixtures/wrong-schema-in-path.oas.yaml b/packages/oas/tests/fixtures/wrong-schema-in-path.oas.yaml new file mode 100644 index 00000000..0b10219b --- /dev/null +++ b/packages/oas/tests/fixtures/wrong-schema-in-path.oas.yaml @@ -0,0 +1,111 @@ +openapi: 3.0.1 +info: + title: Swagger Petstore + description: + 'This is a sample server Petstore server. You can find out more about Swagger + at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For + this sample, you can use the api key `special-key` to test the authorization filters.' + termsOfService: http://swagger.io/terms/ + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io +servers: + - url: https://{instance}.swagger.io:{port}/{basePath} + variables: + instance: + default: petstore + port: + enum: + - '8443' + - '443' + default: '8443' + basePath: + default: v2 +paths: + /store/order/{orderId}: + put: + parameters: + - name: orderId + in: path + required: true + schema: + minimum: 42, + exclusiveMinimum: true + maximum: 43 + exclusiveMaximum: true + type: integer + format: int64 + - name: exclude + in: query + required: true + schema: + type: array + items: + type: string + enum: ['petId', 'quantity', 'shipDate', 'status', 'complete'] + minItems: 1 + maxItems: 5 + uniqueItems: true + - name: api_key + in: header + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Order' + application/xml: + schema: + $ref: '#/components/schemas/Order' + responses: + 200: + description: successful operation + content: + application/xml: + schema: + $ref: '#/components/schemas/Order' + application/json: + schema: + $ref: '#/components/schemas/Order' + 400: + description: Invalid ID supplied + content: {} + 404: + description: Order not found + content: {} +components: + schemas: + Order: + type: object + properties: + id: + type: integer + format: int64 + petId: + type: integer + format: int64 + quantity: + type: integer + format: int32 + shipDate: + type: string + format: date-time + status: + type: string + description: Order Status + enum: + - placed + - approved + - delivered + complete: + type: boolean + default: false + xml: + name: Order diff --git a/packages/oas/tests/fixtures/wrong-schema-in-query.oas.yaml b/packages/oas/tests/fixtures/wrong-schema-in-query.oas.yaml new file mode 100644 index 00000000..d7b3ea1d --- /dev/null +++ b/packages/oas/tests/fixtures/wrong-schema-in-query.oas.yaml @@ -0,0 +1,108 @@ +openapi: 3.0.1 +info: + title: Swagger Petstore + description: + 'This is a sample server Petstore server. You can find out more about Swagger + at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For + this sample, you can use the api key `special-key` to test the authorization filters.' + termsOfService: http://swagger.io/terms/ + contact: + email: apiteam@swagger.io + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +externalDocs: + description: Find out more about Swagger + url: http://swagger.io +servers: + - url: https://{instance}.swagger.io:{port}/{basePath} + variables: + instance: + default: petstore + port: + enum: + - '8443' + - '443' + default: '8443' + basePath: + default: v2 +paths: + /store/order/{orderId}: + put: + parameters: + - name: orderId + in: path + required: true + schema: + maximum: 10.0 + minimum: 1.0 + type: integer + format: int64 + - name: exclude + in: query + required: true + schema: + type: array + items: + type: string + format: 'pattern' + pattern: '\\d{10,20}' + maxLength: 5 + - name: api_key + in: header + schema: + type: string + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Order' + application/xml: + schema: + $ref: '#/components/schemas/Order' + responses: + 200: + description: successful operation + content: + application/xml: + schema: + $ref: '#/components/schemas/Order' + application/json: + schema: + $ref: '#/components/schemas/Order' + 400: + description: Invalid ID supplied + content: {} + 404: + description: Order not found + content: {} +components: + schemas: + Order: + type: object + properties: + id: + type: integer + format: int64 + petId: + type: integer + format: int64 + quantity: + type: integer + format: int32 + shipDate: + type: string + format: date-time + status: + type: string + description: Order Status + enum: + - placed + - approved + - delivered + complete: + type: boolean + default: false + xml: + name: Order