Skip to content

Commit

Permalink
add test and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 10, 2023
1 parent 865719c commit 446564c
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/definitions/3.0.0/securityRequirements.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const assert = require('assert');
const path = require('path');
const Ajv = require("ajv");
const schemas = [
'userPassword.json',
'apiKey.json',
'X509.json',
'symmetricEncryption.json',
'asymmetricEncryption.json',
'HTTPSecurityScheme.json',
'oauth2Flows.json',
'openIdConnect.json',
'specificationExtension.json',
'oauth2Scopes.json',
'NonBearerHTTPSecurityScheme.json',
'BearerHTTPSecurityScheme.json',
'APIKeyHTTPSecurityScheme.json',
'oauth2Flow.json',
'SaslPlainSecurityScheme.json',
'SaslScramSecurityScheme.json',
'SaslGssapiSecurityScheme.json',
]

describe("Should be able to validate securityRequirements", function () {
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: '$id',
logger: false,
validateFormats: false,
strict: false
});

schemas.map((pathToDoc) => {
return path.resolve(__dirname, `../../../definitions/3.0.0/${pathToDoc}`)}
).forEach((pathToDoc) => {
const document = require(pathToDoc);
ajv.addSchema(document);
});
const schema = require(path.resolve(__dirname, `../../../definitions/3.0.0/SecurityScheme.json`));
const validate = ajv.compile(schema);
it('openIdConnect should be valid', () => {
const valid = validate({
type: 'openIdConnect',
openIdConnectUrl: 'openIdConnectUrl',
scopes: [
'some:scope:1',
'some:scope:2'
]
});
assert(valid === true, 'Should accurately validate openIdConnect');
});
});
104 changes: 104 additions & 0 deletions test/definitions/3.0.0/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
const assert = require('assert');
const path = require('path');
const Ajv = require("ajv");
const schemas = [
'userPassword.json',
'apiKey.json',
'X509.json',
'symmetricEncryption.json',
'asymmetricEncryption.json',
'HTTPSecurityScheme.json',
'oauth2Flows.json',
'openIdConnect.json',
'specificationExtension.json',
'oauth2Scopes.json',
'NonBearerHTTPSecurityScheme.json',
'BearerHTTPSecurityScheme.json',
'APIKeyHTTPSecurityScheme.json',
'oauth2Flow.json',
'SaslPlainSecurityScheme.json',
'SaslScramSecurityScheme.json',
'SaslGssapiSecurityScheme.json',
'serverVariables.json',
'securityRequirements.json',
'tag.json',
'externalDocs.json',
'serverBindingsObject.json',
'Reference.json',
'ReferenceObject.json',
'serverVariable.json',
'SecurityScheme.json'
];

describe("Should be able to validate server object", function () {
const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: '$id',
logger: false,
validateFormats: false,
strict: false
});

schemas.forEach((pathToDoc) => {
let document;
if(pathToDoc === 'serverBindingsObject.json') {
// Lets ignore binding for this test
document = {"$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://asyncapi.com/definitions/3.0.0/serverBindingsObject.json"};
} else {
const fullPath = path.resolve(__dirname, `../../../definitions/3.0.0/${pathToDoc}`);
document = require(fullPath);
}
ajv.addSchema(document);
});
const schema = require(path.resolve(__dirname, `../../../definitions/3.0.0/server.json`));
const validate = ajv.compile(schema)
it('should validate multiple security schemas', () => {
const valid = validate({
"host": "api.streetlights.smartylighting.com:{port}",
"protocol": "mqtt",
"description": "Test broker",
"variables": {
"port": {
"description": "Secure connection (TLS) is available through port 8883.",
"default": "1883",
"enum": [
"1883",
"8883"
]
}
},
"security": [
{
"type": "apiKey",
"in": "user",
"description": "Provide your API key as the user and leave the password empty."
},
{
"type": "oauth2",
"flows": {
"implicit": {
"authorizationUrl": "https://example.com/api/oauth/dialog",
"availableScopes": {
"write:pets": "modify pets in your account",
"read:pets": "read your pets"
}
}
},
"scopes": [
"write:pets"
]
},
{
"type": "openIdConnect",
"openIdConnectUrl": "openIdConnectUrl",
"scopes": [
"some:scope:1",
"some:scope:2"
]
}
]
});
assert(valid === true, 'Should accurately validate server object');
});
});

0 comments on commit 446564c

Please sign in to comment.