Skip to content

Commit

Permalink
feat(handler): added handler for must contain domain and context and …
Browse files Browse the repository at this point in the history
…it tests
  • Loading branch information
robertLichtnow committed Jan 21, 2020
1 parent aca3f6e commit 15b8c6e
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 2 deletions.
59 changes: 59 additions & 0 deletions src/__tests__/data/with-domain-and-context/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com/domain/context'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/city:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
60 changes: 60 additions & 0 deletions src/__tests__/data/with-domain-context-and-version/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com/domain/context/v1'
- url: 'https://api.geodatasource.com/v1/domain/context'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/city:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
59 changes: 59 additions & 0 deletions src/__tests__/data/without-domain-and-context/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/city:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
59 changes: 59 additions & 0 deletions src/__tests__/data/without-domain-and-with-context/swagger.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openapi: 3.0.1
servers:
- url: 'https://api.geodatasource.com/context'
info:
contact:
x-twitter: _geodatasource
description: 'GeoDataSourceâ„¢ Web Service is a REST API enable user to lookup for a city by using latitude and longitude coordinate. It will return the result in either JSON or XML containing the information of country, region, city, latitude and longitude. Visit https://www.geodatasource.com/web-service for further information.'
title: GeoDataSource Location Search
version: '1.0'
x-apisguru-categories:
- location
x-logo:
url: 'https://api.apis.guru/v2/cache/logo/https_twitter.com__geodatasource_profile_image.png'
x-origin:
- converter:
url: 'https://github.com/lucybot/api-spec-converter'
version: 2.7.31
format: openapi
url: 'https://app.swaggerhub.com/apiproxy/schema/file/geodatasource/geodatasource-location-search/1.0/swagger.yaml'
version: '3.0'
x-preferred: true
x-providerName: geodatasource.com
paths:
/city:
get:
description: Get City name by using latitude and longitude
parameters:
- in: query
name: key
required: true
schema:
type: string
- in: query
name: lng
required: true
schema:
type: number
- in: query
name: lat
required: true
schema:
type: number
- in: query
name: format
schema:
enum:
- json
- xml
type: string
responses:
'200':
content:
application/json; charset=utf-8:
examples:
'0':
value: '{"country":"","region":"","city":"","latitude":"","longitude":""}'
schema:
type: string
description: Get response from longitude latitude lookup
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import path from 'path';
import { parse } from '../../../index';
import { mustContainDomainAndContext } from '../../../rules/handlers';
import { RuleFault } from '../../../rules/rule-fault';

describe('mustContainDomainAndContext function', () => {
const apiFileWithoutServer = path.join(__dirname, '..', '..', 'data', 'without-server-url', 'swagger.yml');
const apiFileWithDomainAndcontext = path.join(__dirname, '..', '..', 'data', 'with-domain-and-context', 'swagger.yml');
const apiFileWithDomaincontextAndVersion = path.join(__dirname, '..', '..', 'data', 'with-domain-context-and-version', 'swagger.yml');
const apiFileWithoutDomainAndContext = path.join(__dirname, '..', '..', 'data', 'without-domain-and-context', 'swagger.yml');
const apiFileWithoutDomainAndWithContext = path.join(__dirname, '..', '..', 'data', 'without-domain-and-with-context', 'swagger.yml');

it('should return when no server is present', async () => {
const api = await parse(apiFileWithoutServer);

const faults: RuleFault[] = [];

mustContainDomainAndContext(api, faults);

expect(faults.length).toBe(0);
});

it('should have no faults when domain and context are present', async () => {
const api = await parse(apiFileWithDomainAndcontext);

const faults: RuleFault[] = [];

mustContainDomainAndContext(api, faults);

expect(faults.length).toBe(0);
});

it('should have no faults when domain, context and version are present', async () => {
const api = await parse(apiFileWithDomaincontextAndVersion);

const faults: RuleFault[] = [];

mustContainDomainAndContext(api, faults);

expect(faults.length).toBe(0);
});

it('should have one fault when domain and context are not present', async () => {
const api = await parse(apiFileWithoutDomainAndContext);

const faults: RuleFault[] = [];

mustContainDomainAndContext(api, faults);

expect(faults.length).toBe(1);
});

it('should have one fault when domain is not present and context is', async () => {
const api = await parse(apiFileWithoutDomainAndWithContext);

const faults: RuleFault[] = [];

mustContainDomainAndContext(api, faults);

expect(faults.length).toBe(1);
});
});
4 changes: 3 additions & 1 deletion src/rules/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { mustContainServerURL } from './must-contain-server-url';
import { mustContainPort } from './must-contain-port';
import { noSingularResource } from './no-singular-resource';
import { mustContainVersion } from './must-contain-version';
import { mustContainDomainAndContext } from './must-contain-domain-and-context';

export {
mustContainServerURL,
mustContainPort,
noSingularResource,
mustContainVersion
mustContainVersion,
mustContainDomainAndContext
};
47 changes: 47 additions & 0 deletions src/rules/handlers/must-contain-domain-and-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { OpenAPI } from "openapi-types";
import Url from "url-parse";
import { RuleFault, Severity, RuleFaultContent } from "../rule-fault";
import { produceRuleFaultValueForUrl, pushFault } from './util';

const faults = {
mustContainDomainAndContext: 'Missing domain and/or context on url property'
};

const produceMustContainDomainAndContext = (urlString: string): RuleFault => {
return {
value: produceRuleFaultValueForUrl(urlString),
errors: [
{
severity: Severity.warning,
message: faults.mustContainDomainAndContext
} as RuleFaultContent
]
};
};

const isMissingDomainAndContext = (urlString: string): boolean => {
const url = new Url(urlString);

/**
* Matches /domain/context but excludes the version on the test which matches `/v\w+/`
*/
const matcher = /\/(?!(v\d+)\b)\w+\/(?!(v\d+)\b)\w+/i;
return !matcher.test(url.pathname);
};

export const mustContainDomainAndContext = (api: OpenAPI.Document, ruleFaults: RuleFault[]) => {
const apiParsed: any = api;

/**
* Without any url, there is no way to evaluate port
*/
if (!apiParsed.servers) {
return;
}

apiParsed.servers.forEach((server: any) => {
if (isMissingDomainAndContext(server.url)) {
pushFault(produceMustContainDomainAndContext(server.url), ruleFaults);
}
});
};
1 change: 0 additions & 1 deletion src/rules/handlers/must-contain-port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ export const mustContainPort = (api: OpenAPI.Document, ruleFaults: RuleFault[])
pushFault(produceMustContainPort(server.url), ruleFaults);
}
});

};

0 comments on commit 15b8c6e

Please sign in to comment.