Skip to content

Commit

Permalink
test(definitions): apiKey security schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Pakisan committed May 21, 2024
1 parent 7c8ad0a commit 029003e
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/definitions/3.0.0/security/apiKey/apiKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const Ajv = require('ajv');
const assert = require('assert');
const addFormats = require('ajv-formats');
const fs = require('fs');

const ajv = new Ajv({
jsonPointers: true,
allErrors: true,
schemaId: '$id',
logger: false,
validateFormats: true,
strict: false,
});
addFormats(ajv);

const jsonSchema = require('../../../../../definitions/3.0.0/apiKey.json');
const validator = ajv
.addMetaSchema(require('../../../../../definitions/3.0.0/schema.json'))
.addSchema(require('../../../../../definitions/3.0.0/specificationExtension.json'))
.compile(jsonSchema);

describe('API Key', () => {
it('empty', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/empty.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'API Key with empty body is not valid');
assert(validator.errors[0].message === 'must have required property \'type\'');
assert(validator.errors[1].message === 'must have required property \'in\'');
assert(validator.errors.length === 2);
});

it('without required properties', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/without required properties.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'API Key without required properties is not valid');
assert(validator.errors[0].message === 'must have required property \'type\'');
assert(validator.errors[1].message === 'must have required property \'in\'');
assert(validator.errors.length === 2);
});

it('only required properties', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/only required properties.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === true, 'API Key is valid with only required properties');
});

it('extended', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/extended.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === true, 'API Key can be extended');
});

it('wrongly extended', () => {
const model = JSON.parse(fs.readFileSync(`${__dirname}/wrongly extended.json`, 'utf-8'));
const validationResult = validator(model);

assert(validationResult === false, 'API Key is not valid when was wrongly extended');
assert(validator.errors[0].message === 'must NOT have additional properties');
assert(validator.errors[0].params.additionalProperty === 'ext-number');
assert(validator.errors.length === 1);
});
});
1 change: 1 addition & 0 deletions test/definitions/3.0.0/security/apiKey/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
10 changes: 10 additions & 0 deletions test/definitions/3.0.0/security/apiKey/extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "apiKey",
"description": "apiKey",
"in": "user",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "apiKey",
"in": "user"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "apiKey",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
}
}
11 changes: 11 additions & 0 deletions test/definitions/3.0.0/security/apiKey/wrongly extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "apiKey",
"description": "apiKey",
"in": "user",
"x-number": 0,
"x-string": "",
"x-object": {
"property": {}
},
"ext-number": 1
}
3 changes: 3 additions & 0 deletions test/definitions/3.0.0/security/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe('Security Schemes', () => {
require('./apiKey/apiKey.js');
});
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('AsyncAPI: 3.0.0', () => {
require('./definitions/3.0.0/license/license.js');
require('./definitions/3.0.0/reference/reference.js');
require('./definitions/3.0.0/reference object/reference-object.js');

require('./definitions/3.0.0/security/index.js');
});

describe('AsyncAPI', () => {
Expand Down

0 comments on commit 029003e

Please sign in to comment.