2015-12-12 00:50:26 +03:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-23 20:18:42 +03:00
|
|
|
import { SpecManager } from '../../lib/utils/spec-manager';
|
2017-03-09 20:56:50 +03:00
|
|
|
import * as xExtendedDefs from './x-extended-defs.json';
|
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
describe('Utils', () => {
|
|
|
|
describe('Schema manager', () => {
|
2017-02-26 00:38:19 +03:00
|
|
|
let specMgr: SpecManager;
|
2015-12-10 00:46:27 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
beforeEach(() => {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr = new SpecManager();
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
2015-12-10 00:46:27 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('load should return a promise', ()=> {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').should.be.instanceof(Promise);
|
2015-12-14 16:54:11 +03:00
|
|
|
});
|
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('load should resolve promise for valid url', (done)=> {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(() => {
|
2015-12-10 00:46:27 +03:00
|
|
|
done();
|
2015-12-12 18:28:46 +03:00
|
|
|
}, () => {
|
|
|
|
throw new Error('Error handler should not be called');
|
2015-12-10 00:46:27 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
describe('Schema manager basic functionality', ()=> {
|
2016-11-24 16:29:29 +03:00
|
|
|
beforeEach(function (done) {
|
|
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(done, done.fail);
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
2015-12-10 22:36:11 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should contain non-empty schema', ()=> {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr.schema.should.be.an.Object();
|
|
|
|
specMgr.schema.should.be.not.empty();
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
2015-12-10 22:36:11 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should correctly init api url', ()=> {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
|
2015-12-12 00:50:26 +03:00
|
|
|
});
|
|
|
|
|
2016-07-26 14:51:59 +03:00
|
|
|
it('should correctly init api url if both http and https', ()=> {
|
|
|
|
specMgr._schema.schemes.push('https');
|
|
|
|
specMgr.init();
|
|
|
|
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
|
|
|
|
});
|
|
|
|
|
2016-08-01 03:26:10 +03:00
|
|
|
it('should substitute api scheme when spec schemes are undefined', () => {
|
|
|
|
specMgr._schema.schemes = undefined;
|
2017-02-26 00:38:19 +03:00
|
|
|
specMgr._specUrl = 'https://petstore.swagger.io/v2';
|
2016-08-01 03:26:10 +03:00
|
|
|
specMgr.init();
|
|
|
|
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
|
|
|
|
});
|
|
|
|
|
2016-08-01 02:46:34 +03:00
|
|
|
it('should substitute api host when spec host is undefined', () => {
|
|
|
|
specMgr._schema.host = undefined;
|
2017-02-26 00:38:19 +03:00
|
|
|
specMgr._specUrl = 'http://petstore.swagger.io/v2';
|
2016-08-01 02:46:34 +03:00
|
|
|
specMgr.init();
|
2016-11-24 16:29:29 +03:00
|
|
|
specMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
|
2016-08-01 02:46:34 +03:00
|
|
|
});
|
|
|
|
|
2017-02-14 10:49:58 +03:00
|
|
|
it('should use empty basePath when basePath is not present', () => {
|
|
|
|
specMgr._schema.basePath = undefined;
|
2017-02-26 00:38:19 +03:00
|
|
|
specMgr._specUrl = 'https://petstore.swagger.io';
|
2017-02-14 10:49:58 +03:00
|
|
|
specMgr.init();
|
|
|
|
specMgr.basePath.should.be.equal('');
|
|
|
|
});
|
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
describe('byPointer method', () => {
|
|
|
|
it('should return correct schema part', ()=> {
|
2016-10-31 21:15:56 +03:00
|
|
|
let part = specMgr.byPointer('/tags/0');
|
|
|
|
part.should.be.deepEqual(specMgr.schema.tags[0]);
|
|
|
|
part.should.be.equal(specMgr.schema.tags[0]);
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return null for incorrect pointer', ()=> {
|
2016-06-23 17:36:38 +03:00
|
|
|
let part = specMgr.byPointer('/incorrect/pointer');
|
2015-12-19 20:36:28 +03:00
|
|
|
expect(part).toBeNull();
|
|
|
|
});
|
2015-12-10 22:36:11 +03:00
|
|
|
});
|
2015-12-10 00:46:27 +03:00
|
|
|
});
|
2015-12-12 18:28:46 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
describe('getTagsMap method', () => {
|
2016-11-24 16:29:29 +03:00
|
|
|
beforeEach(function () {
|
2016-06-23 17:36:38 +03:00
|
|
|
specMgr._schema = {
|
2015-12-19 20:36:28 +03:00
|
|
|
tags: [
|
|
|
|
{name: 'tag1', description: 'info1'},
|
|
|
|
{name: 'tag2', description: 'info2', 'x-traitTag': true}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
});
|
2015-12-12 00:50:26 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should return correct tags map', () => {
|
2016-06-23 17:36:38 +03:00
|
|
|
let tagsMap = specMgr.getTagsMap();
|
2015-12-19 20:36:28 +03:00
|
|
|
let expectedResult = {
|
|
|
|
tag1: {description: 'info1', 'x-traitTag': false},
|
|
|
|
tag2: {description: 'info2', 'x-traitTag': true}
|
|
|
|
};
|
|
|
|
tagsMap.should.be.deepEqual(expectedResult);
|
|
|
|
});
|
2015-12-14 16:54:11 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should return empty array for non-specified tags', () => {
|
2016-06-23 17:36:38 +03:00
|
|
|
delete specMgr._schema.tags;
|
|
|
|
let tagsMap = specMgr.getTagsMap();
|
2016-06-13 20:54:24 +03:00
|
|
|
tagsMap.should.be.empty();
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
2015-12-14 16:54:11 +03:00
|
|
|
});
|
2015-12-12 00:50:26 +03:00
|
|
|
|
2017-03-30 15:17:08 +03:00
|
|
|
describe('getOperationParams method', () => {
|
2016-11-24 16:29:29 +03:00
|
|
|
beforeEach((done:any) => {
|
2017-03-30 15:17:08 +03:00
|
|
|
specMgr.load('/tests/schemas/schema-mgr-operationParams.json').then(done, done.fail);
|
2015-12-12 00:50:26 +03:00
|
|
|
});
|
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should propagate path parameters', () => {
|
2017-03-30 15:17:08 +03:00
|
|
|
let params = specMgr.getOperationParams('/paths/test1/get');
|
2015-12-19 20:36:28 +03:00
|
|
|
params.length.should.be.equal(2);
|
2017-03-30 15:17:08 +03:00
|
|
|
params[0].name.should.be.equal('operationParam');
|
2015-12-19 20:36:28 +03:00
|
|
|
params[1].name.should.be.equal('pathParam');
|
|
|
|
});
|
2015-12-12 18:28:46 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should inject correct pointers', () => {
|
2017-03-30 15:17:08 +03:00
|
|
|
let params = specMgr.getOperationParams('/paths/test1/get');
|
2015-12-19 20:36:28 +03:00
|
|
|
params[0]._pointer.should.be.equal('/paths/test1/get/parameters/0');
|
|
|
|
params[1]._pointer.should.be.equal('/paths/test1/parameters/0');
|
|
|
|
});
|
2015-12-12 18:28:46 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should accept pointer directly to parameters', () => {
|
2017-03-30 15:17:08 +03:00
|
|
|
let params = specMgr.getOperationParams('/paths/test1/get/parameters');
|
2015-12-19 20:36:28 +03:00
|
|
|
expect(params).not.toBeNull();
|
|
|
|
params.length.should.be.equal(2);
|
|
|
|
});
|
2015-12-14 16:54:11 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should resolve path params from Parameters Definitions Object', () => {
|
2017-03-30 15:17:08 +03:00
|
|
|
let params = specMgr.getOperationParams('/paths/test2/get');
|
2015-12-19 20:36:28 +03:00
|
|
|
params.length.should.be.equal(2);
|
2017-03-30 15:17:08 +03:00
|
|
|
params[0].name.should.be.equal('operationParam');
|
2015-12-19 20:36:28 +03:00
|
|
|
params[1].name.should.be.equal('extParam');
|
|
|
|
params[1]._pointer.should.be.equal('#/parameters/extparam');
|
|
|
|
});
|
2015-12-12 18:28:46 +03:00
|
|
|
|
2017-03-30 15:17:08 +03:00
|
|
|
it('should resolve operation params from Parameters Definitions Object', () => {
|
|
|
|
let params = specMgr.getOperationParams('/paths/test3/get');
|
2015-12-19 20:36:28 +03:00
|
|
|
params.length.should.be.equal(1);
|
|
|
|
params[0].name.should.be.equal('extParam');
|
|
|
|
params[0]._pointer.should.be.equal('#/parameters/extparam');
|
|
|
|
});
|
2015-12-14 16:54:11 +03:00
|
|
|
|
2015-12-19 20:36:28 +03:00
|
|
|
it('should throw for parameters other than array', () => {
|
2017-03-30 15:17:08 +03:00
|
|
|
let func = () => specMgr.getOperationParams('/paths/test4/get');
|
2015-12-19 20:36:28 +03:00
|
|
|
expect(func).toThrow();
|
|
|
|
});
|
2015-12-12 00:50:26 +03:00
|
|
|
});
|
2015-12-12 18:28:46 +03:00
|
|
|
|
2016-01-09 17:52:24 +03:00
|
|
|
describe('findDerivedDefinitions method', () => {
|
2016-11-24 16:29:29 +03:00
|
|
|
beforeEach((done) => {
|
|
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(done, done.fail);
|
2016-01-09 17:52:24 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should find derived definitions for Pet', () => {
|
2016-06-23 17:36:38 +03:00
|
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Pet');
|
2016-01-09 17:52:24 +03:00
|
|
|
deriveDefs.should.be.instanceof(Array);
|
2016-02-01 15:47:21 +03:00
|
|
|
deriveDefs.should.not.be.empty();
|
2016-06-13 20:54:24 +03:00
|
|
|
deriveDefs.should.be.deepEqual([
|
2016-08-30 20:05:21 +03:00
|
|
|
{name: 'Cat', $ref: '#/definitions/Cat'},
|
|
|
|
{name: 'Dog', $ref: '#/definitions/Dog'}
|
2016-06-13 20:54:24 +03:00
|
|
|
]);
|
2016-01-09 17:52:24 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return emtpy array for definitions that dont have discriminator', () => {
|
2016-06-23 17:36:38 +03:00
|
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Order');
|
2016-01-09 17:52:24 +03:00
|
|
|
deriveDefs.should.be.instanceof(Array);
|
2016-06-13 20:54:24 +03:00
|
|
|
deriveDefs.should.be.empty();
|
2016-01-09 17:52:24 +03:00
|
|
|
});
|
2017-03-09 20:56:50 +03:00
|
|
|
|
|
|
|
it('should correctly work with x-extendedDiscriminator', () => {
|
|
|
|
specMgr._schema = {
|
|
|
|
definitions: xExtendedDefs
|
|
|
|
};
|
|
|
|
|
|
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Payment');
|
|
|
|
deriveDefs.should.be.instanceof(Array);
|
|
|
|
deriveDefs.should.be.deepEqual([
|
|
|
|
{
|
|
|
|
name: 'cash',
|
|
|
|
$ref: '#/definitions/CashPayment'
|
|
|
|
}, {
|
|
|
|
name: 'paypal',
|
|
|
|
$ref: '#/definitions/PayPalPayment'
|
|
|
|
}])
|
|
|
|
});
|
2016-01-09 17:52:24 +03:00
|
|
|
});
|
2015-12-19 20:36:28 +03:00
|
|
|
});
|
2015-12-12 00:50:26 +03:00
|
|
|
});
|