Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trebler committed Aug 19, 2019
1 parent 9396f1c commit 00ab580
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/path.level.parameters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');

describe(packageJson.name, () => {
let app = null;
let basePath = null;

before(async () => {
// Set up the express app
const apiSpec = path.join(
'test',
'resources',
'path.level.parameters.yaml',
);
app = await createApp({ apiSpec }, 3005);
basePath = app.basePath;

// Define new coercion routes
app.use(
`${basePath}`,
express
.Router()
.get(`/path_level_parameters`, (_req, res) => res.send())
);
});

after(() => {
app.server.close();
});

it('should return 400 if pathLevel query parameter is not provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?operationLevel=123`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(1);
const message = r.body.errors[0].message;
expect(message).to.equal('should have required property \'pathLevel\'');
}));

it('should return 400 if operationLevel query parameter is not provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?pathLevel=123`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(1);
const message = r.body.errors[0].message;
expect(message).to.equal('should have required property \'operationLevel\'');
}));

it('should return 400 if neither operationLevel, nor pathLevel query parameters are provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(2);
const messages = r.body.errors.map(err => err.message);
expect(messages).to.have.members([
'should have required property \'pathLevel\'',
'should have required property \'operationLevel\''
]);
}));

it('should return 200 if both pathLevel and operationLevel query parameter are provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?operationLevel=123&pathLevel=123`)
.send()
.expect(200));
});
36 changes: 36 additions & 0 deletions test/resources/path.level.parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: '3.0.2'
info:
version: 1.0.0
title: Path Level Parameters
description: Path Level Parameters Test

servers:
- url: /v1/

paths:
/path_level_parameters:
parameters:
- $ref: '#components/parameters/pathLevelParameter'
get:
parameters:
- $ref: '#components/parameters/operationLevelParameter'
responses:
'200':
description: OK
'400':
description: Bad Request

components:
parameters:
pathLevelParameter:
name: pathLevel
in: query
required: true
schema:
type: string
operationLevelParameter:
name: operationLevel
in: query
required: true
schema:
type: string

0 comments on commit 00ab580

Please sign in to comment.