Skip to content

Commit

Permalink
Add resolvers tests to improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwheele committed Jun 2, 2020
1 parent c5f24b5 commit 8fc1d6c
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/operation.handler.spec.ts
@@ -0,0 +1,68 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import { OpenApiValidator } from '../src';
import * as resolvers from '../src/resolvers';

describe('operation handler', () => {
it('should not install handlers when nothing provided', async () => {
const apiSpec = path.join(__dirname, 'resources/eov-operations.yaml');
const app = express();

const oam = new OpenApiValidator({
apiSpec
})

oam.installSync(app);

expect(oam)
.to.have.property('options')
.to.deep.include({ operationHandlers: false });

expect(app._router.stack.length).to.equal(6)
})

it('should use the default handler when string provided', async () => {
const apiSpec = path.join(__dirname, 'resources/eov-operations.yaml');
const app = express();

const oam = new OpenApiValidator({
apiSpec,
operationHandlers: path.join(__dirname, 'resources')
})

oam.installSync(app);

expect(oam)
.to.have.property('options')
.to.deep.include({ operationHandlers: {
basePath: path.join(__dirname, 'resources'),
resolver: resolvers.defaultResolver
}});

expect(app._router.stack.length).to.be.greaterThan(6)
});

it('can use a custom operation resolver', async () => {
const apiSpec = path.join(__dirname, 'resources/eov-operations.modulepath.yaml');
const app = express();

const handler = {
basePath: path.join(__dirname, 'resources/routes'),
resolver: resolvers.modulePathResolver
};

const oam = new OpenApiValidator({
apiSpec,
operationHandlers: handler
})

oam.installSync(app);

expect(oam)
.to.have.property('options')
.to.deep.include({ operationHandlers: handler});

expect(app._router.stack.length).to.be.greaterThan(6)
});
});
230 changes: 230 additions & 0 deletions test/resources/eov-operations.modulepath.yaml
@@ -0,0 +1,230 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
description: A sample API
termsOfService: http://swagger.io/terms/
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
- url: /v1
paths:
/ping:
get:
description: |
ping then pong!
# use operationId, (x-eov-operation-id is optional. it can be used in place of operationId or to override it)
operationId: ping.ping
responses:
'200':
description: OK
content:
text/plain:
schema:
type: string
example: pong
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets:
get:
description: |
Returns all pets
operationId: pets.list
parameters:
- name: type
in: query
description: maximum number of results to return
required: true
schema:
type: string
enum:
- dog
- cat
- name: tags
in: query
description: tags to filter by
required: false
style: form
schema:
type: array
items:
type: string
- name: limit
in: query
description: maximum number of results to return
required: true
schema:
type: integer
format: int32
minimum: 1
maximum: 20
responses:
'200':
description: pet response
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

post:
description: Creates a new pet in the store.
operationId: pets.create
security:
- ApiKeyAuth: []
requestBody:
description: Pet to add to the store
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/pets/{id}:
get:
description: Returns a user based on a single ID, if the user does not have access to the pet
operationId: pets.pet
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
responses:
'200':
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
delete:
description: deletes a single pet based on the ID supplied
operationId: pets.delete
parameters:
- name: id
in: path
description: ID of pet to delete
required: true
schema:
type: integer
format: int64
responses:
'204':
description: pet deleted
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/pets/{id}/photos:
post:
description: upload a photo of the pet
operationId: pets.petPhotos
parameters:
- name: id
in: path
description: ID of pet to fetch
required: true
schema:
type: integer
format: int64
requestBody:
content:
multipart/form-data:
schema:
# $ref: '#/components/schemas/NewPhoto'
type: object
required:
- file
properties:
file:
description: The photo
type: string
format: binary
required: true
responses:
201:
description: Created
content:
application/json:
schema:
type: object
properties:
success:
type: boolean

components:
schemas:
Pet:
required:
- id
- name
- type
properties:
id:
readOnly: true
type: number
name:
type: string
tag:
type: string
type:
$ref: '#/components/schemas/PetType'

PetType:
type: string
enum:
- dog
- cat

Error:
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string

securitySchemes:
ApiKeyAuth:
type: apiKey
in: header
name: X-API-Key

0 comments on commit 8fc1d6c

Please sign in to comment.